1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Collection library for PHP. |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/php-collection |
6
|
|
|
* @package php-collection |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\php\collection; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Collection Trait. |
15
|
|
|
* Array inside of object. |
16
|
|
|
*/ |
17
|
|
|
trait CollectionTrait |
18
|
|
|
{ |
19
|
|
|
use BaseTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Returns property of item by name. |
23
|
|
|
* @param mixed $name |
24
|
|
|
* @return mixed |
25
|
|
|
*/ |
26
|
|
|
public function get($name) |
27
|
|
|
{ |
28
|
|
|
return $this->getItem($name); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Sets an item. Silently resets if already exists. |
33
|
|
|
* @param int|string $name |
34
|
|
|
* @param mixed $value the element value |
35
|
|
|
* @param string|array $where where to put, see [[setItem()]] |
36
|
|
|
* @see setItem() |
37
|
|
|
*/ |
38
|
4 |
|
public function set($name, $value, $where = '') |
39
|
|
|
{ |
40
|
4 |
|
$this->setItem($name, $value, $where); |
41
|
4 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Adds an item. Does not touch if already exists. |
45
|
|
|
* @param int|string $name item name |
46
|
|
|
* @param array $value item value |
47
|
|
|
* @param string|array $where where to put, see [[setItem()]] |
48
|
|
|
* @return $this for chaining |
49
|
|
|
* @see setItem() |
50
|
|
|
*/ |
51
|
4 |
|
public function add($name, $value = null, $where = '') |
52
|
|
|
{ |
53
|
4 |
|
if (!$this->has($name)) { |
54
|
3 |
|
$this->set($name, $value, $where); |
55
|
|
|
} |
56
|
|
|
|
57
|
4 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Check collection has the item. |
62
|
|
|
* @param string $name item name |
63
|
|
|
* @return bool whether item exist |
64
|
|
|
*/ |
65
|
4 |
|
public function has($name) |
66
|
|
|
{ |
67
|
4 |
|
return $this->hasItem($name); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Delete an item. |
72
|
|
|
* You can't create function `unset`. |
73
|
|
|
* @param $name |
74
|
|
|
*/ |
75
|
2 |
|
public function delete($name) |
76
|
|
|
{ |
77
|
2 |
|
$this->unsetItem($name); |
78
|
2 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* This method is overridden to support accessing items like properties. |
82
|
|
|
* @param string $name item or property name |
83
|
|
|
* @return mixed item of found or the named property value |
84
|
|
|
*/ |
85
|
1 |
|
public function __get($name) |
86
|
|
|
{ |
87
|
1 |
|
return $this->getItem($name); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* This method is overridden to support accessing items like properties. |
92
|
|
|
* @param string $name item or property name |
93
|
|
|
* @param string $value value to be set |
94
|
|
|
* @return mixed item of found or the named property value |
95
|
|
|
*/ |
96
|
1 |
|
public function __set($name, $value) |
97
|
|
|
{ |
98
|
1 |
|
$this->set($name, $value); |
99
|
1 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Checks if a property value is null. |
103
|
|
|
* This method overrides the parent implementation by checking if the named item is loaded. |
104
|
|
|
* @param string $name the property name or the event name |
105
|
|
|
* @return bool whether the property value is null |
106
|
|
|
*/ |
107
|
1 |
|
public function __isset($name) |
108
|
|
|
{ |
109
|
1 |
|
return ($name && parent::__isset($name)) || $this->issetItem($name); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Checks if a property value is null. |
114
|
|
|
* This method overrides the parent implementation by checking if the named item is loaded. |
115
|
|
|
* @param string $name the property name or the event name |
116
|
|
|
* @return bool whether the property value is null |
117
|
|
|
*/ |
118
|
|
|
public function __unset($name) |
119
|
|
|
{ |
120
|
|
|
$this->unsetItem($name); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|