Completed
Push — master ( 43695e...e3b460 )
by dotzero
27:31
created

Fields   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 126
rs 10
ccs 28
cts 28
cp 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __set() 0 4 1
A __get() 0 4 1
A add() 0 4 1
A get() 0 4 1
A offsetExists() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 6 2
A getIterator() 0 4 1
A count() 0 4 1
A offsetGet() 0 8 2
1
<?php
2
3
namespace AmoCRM\Helpers;
4
5
/**
6
 * Class Fields
7
 *
8
 * Хелпер для хранения идентификаторов полей amoCRM API
9
 *
10
 * @package AmoCRM\Helpers
11
 * @version 0.1.0
12
 * @author dotzero <[email protected]>
13
 * @link http://www.dotzero.ru/
14
 * @link https://github.com/dotzero/amocrm-php
15
 *
16
 * For the full copyright and license information, please view the LICENSE
17
 * file that was distributed with this source code.
18
 */
19
class Fields implements \IteratorAggregate, \ArrayAccess, \Countable
20
{
21
    /**
22
     * @var array Массив ключей и идентификаторов полей
23
     */
24
    private $fields = [];
25
26
    /**
27
     * Магический сеттер для поля
28
     *
29
     * @param mixed $name Название поля
30
     * @param mixed $value Значение поля
31
     */
32 1
    public function __set($name, $value)
33
    {
34 1
        $this->offsetSet($name, $value);
35 1
    }
36
37
    /**
38
     * Магический геттер для поля
39
     *
40
     * @param mixed $name Название поля
41
     * @return mixed Значение поля
42
     */
43 1
    public function __get($name)
44
    {
45 1
        return $this->offsetGet($name);
46
    }
47
48
    /**
49
     * Сеттер для поля
50
     *
51
     * @param mixed $key Название поля
52
     * @param mixed $value Значение поля
53
     */
54 6
    public function add($key, $value = null)
55
    {
56 6
        $this->offsetSet($key, $value);
57 6
    }
58
59
    /**
60
     * Геттер для поля
61
     *
62
     * @param mixed $key Название поля
63
     * @return mixed Значение поля
64
     */
65 1
    public function get($key)
66
    {
67 1
        return $this->offsetGet($key);
68
    }
69
70
    /**
71
     * Определяет, существует ли заданное смещение (ключ)
72
     *
73
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
74
     * @param mixed $offset Смещение (ключ) для проверки
75
     * @return boolean Возвращает true или false
76
     */
77 2
    public function offsetExists($offset)
78
    {
79 2
        return isset($this->fields[$offset]);
80
    }
81
82
    /**
83
     * Возвращает заданное смещение (ключ)
84
     *
85
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
86
     * @param mixed $offset Смещение (ключ) для возврата
87
     * @return mixed Значение смещения (ключа)
88
     */
89 1
    public function offsetGet($offset)
90
    {
91 1
        if (isset($this->fields[$offset])) {
92 1
            return $this->fields[$offset];
93
        }
94
95 1
        return null;
96
    }
97
98
    /**
99
     * Устанавливает заданное смещение (ключ)
100
     *
101
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
102
     * @param mixed $offset Смещение (ключ), которому будет присваиваться значение
103
     * @param mixed $value Значение для присвоения
104
     */
105 6
    public function offsetSet($offset, $value)
106
    {
107 6
        $this->fields[$offset] = $value;
108 6
    }
109
110
    /**
111
     * Удаляет смещение
112
     *
113
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
114
     * @param mixed $offset Смещение для удаления
115
     */
116 1
    public function offsetUnset($offset)
117
    {
118 1
        if (isset($this->fields[$offset])) {
119 1
            unset($this->fields[$offset]);
120 1
        }
121 1
    }
122
123
    /**
124
     * Возвращает внешний итератор
125
     *
126
     * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
127
     * @return Traversable Экземпляр объекта, использующего Iterator или Traversable
128
     */
129 1
    public function getIterator()
130
    {
131 1
        return new \ArrayIterator($this->fields);
132
    }
133
134
    /**
135
     * Количество элементов объекта
136
     *
137
     * @link http://php.net/manual/en/countable.count.php
138
     * @return int Количество элементов объекта
139
     */
140 2
    public function count()
141
    {
142 2
        return count($this->fields);
143
    }
144
}
145