Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
10 / 10
CRAP
100.00% covered (success)
100.00%
1 / 1
Key
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
10 / 10
15
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 enableAdd
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 enableRemove
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 enableRename
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 isAddKey
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isRemoveKey
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isRenameKey
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 callConflict
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * @license MIT
5 * @author hazuki3417<hazuki3417@gmail.com>
6 * @copyright 2022 hazuki3417 all rights reserved.
7 */
8
9namespace Selen\Schema\Exchange\Define;
10
11use LogicException;
12
13class Key
14{
15    /** @var bool keyの追加処理フラグ */
16    private $haveCalledAdd = false;
17
18    /** @var bool keyの削除処理フラグ */
19    private $haveCalledRemove = false;
20
21    /** @var bool keyのリネーム処理フラグ */
22    private $haveCalledRename = false;
23
24    /** @var string|int|null key名 */
25    private $name;
26
27    /**
28     * インスタンスを生成します
29     *
30     * @param string|int|null $name key名を指定します。index arrayの場合はnullを渡します。
31     *
32     * @return Key
33     */
34    public function __construct(string|int|null $name)
35    {
36        $this->name = $name;
37    }
38
39    /**
40     * keyの追加処理を有効にします
41     *
42     * @return Key
43     *
44     * @throws LogicException メソッドの呼び出し順が不正なときに発生します
45     */
46    public function enableAdd()
47    {
48        if ($this->callConflict()) {
49            $mes = 'Invalid method call. cannot call enableRemove or enableRename method after enableAdd.';
50            throw new LogicException($mes);
51        }
52
53        $this->haveCalledAdd = true;
54        return $this;
55    }
56
57    /**
58     * keyの削除処理を有効にします
59     *
60     * @return Key
61     *
62     * @throws LogicException メソッドの呼び出し順が不正なときに発生します
63     */
64    public function enableRemove()
65    {
66        if ($this->callConflict()) {
67            $mes = 'Invalid method call. cannot call enableAdd or enableRename method after enableRemove.';
68            throw new LogicException($mes);
69        }
70
71        $this->haveCalledRemove = true;
72        return $this;
73    }
74
75    /**
76     * keyのリネーム処理を有効にします
77     *
78     * @return Key
79     *
80     * @throws LogicException メソッドの呼び出し順が不正なときに発生します
81     */
82    public function enableRename()
83    {
84        if ($this->callConflict()) {
85            $mes = 'Invalid method call. cannot call enableAdd or enableRemove method after enableRename.';
86            throw new LogicException($mes);
87        }
88
89        $this->haveCalledRename = true;
90        return $this;
91    }
92
93    /**
94     * keyの追加処理を実行するかどうか返します。
95     *
96     * @return bool 処理する場合はtrueを、それ以外の場合はfalseを返します
97     */
98    public function isAddKey()
99    {
100        return $this->haveCalledAdd;
101    }
102
103    /**
104     * keyの削除処理を実行するかどうか返します
105     *
106     * @return bool 処理する場合はtrueを、それ以外の場合はfalseを返します
107     */
108    public function isRemoveKey()
109    {
110        return $this->haveCalledRemove;
111    }
112
113    /**
114     * keyのリネーム処理を実行するかどうか返します
115     *
116     * @return bool 処理する場合はtrueを、それ以外の場合はfalseを返します
117     */
118    public function isRenameKey()
119    {
120        return $this->haveCalledRename;
121    }
122
123    /**
124     * key名を取得します
125     *
126     * @return string|int|null key名を返します
127     */
128    public function getName()
129    {
130        return $this->name;
131    }
132
133    /**
134     * key名を設定します
135     */
136    public function setName(string|int|null $value): void
137    {
138        $this->name = $value;
139    }
140
141    /**
142     * keyの追加・削除・リネームの呼び出しが競合しているか確認します
143     *
144     * @return bool 競合する場合はtrueを、それ以外の場合はfalseを返します
145     */
146    private function callConflict()
147    {
148        return
149            $this->haveCalledAdd
150            || $this->haveCalledRemove
151            || $this->haveCalledRename;
152    }
153}