1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Collection library for Yii2. |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/yii2-collection |
6
|
|
|
* @package yii2-collection |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\yii2\collection; |
12
|
|
|
|
13
|
|
|
use hiqdev\php\collection\BaseTrait; |
14
|
|
|
use yii\base\ArrayableTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* ObjectTrait. |
18
|
|
|
* Intended to be used for yii\base\BaseObject descendants. |
19
|
|
|
* Uses canSet/GetPropperty and magic functions to provide compatible getter/setter mechanisms. |
20
|
|
|
*/ |
21
|
|
|
trait ObjectTrait |
22
|
|
|
{ |
23
|
|
|
use ArrayableTrait; |
24
|
|
|
use BaseTrait { |
25
|
|
|
BaseTrait::fields insteadof ArrayableTrait; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Returns property of item by name. |
30
|
|
|
* @param mixed $name |
31
|
|
|
* @return mixed |
32
|
|
|
*/ |
33
|
20 |
|
public function get($name) |
34
|
|
|
{ |
35
|
20 |
|
return $this->__get($name); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Sets an item. Silently resets if already exists. |
40
|
|
|
* @param int|string $name |
41
|
|
|
* @param mixed $value the element value |
42
|
|
|
* @param string|array $where where to put, see [[setItem()]] |
43
|
|
|
* @see setItem() |
44
|
|
|
*/ |
45
|
80 |
|
public function set($name, $value, $where = '') |
46
|
|
|
{ |
47
|
80 |
|
if (($name && $this->canSetProperty($name)) || strpos($name, 'on ') === 0 || strpos($name, 'as ') === 0) { |
|
|
|
|
48
|
80 |
|
parent::__set($name, $value); |
|
|
|
|
49
|
80 |
|
} else { |
50
|
42 |
|
$this->setItem($name, $value, $where); |
51
|
|
|
} |
52
|
80 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Adds an item. Does not touch if already exists. |
56
|
|
|
* @param int|string $name item name |
57
|
|
|
* @param array $value item value |
58
|
|
|
* @param string|array $where where to put, see [[setItem()]] |
59
|
|
|
* @return $this for chaining |
60
|
|
|
* @see setItem() |
61
|
|
|
*/ |
62
|
20 |
|
public function add($name, $value = null, $where = '') |
63
|
|
|
{ |
64
|
20 |
|
if (!$this->has($name)) { |
65
|
15 |
|
$this->set($name, $value, $where); |
66
|
15 |
|
} |
67
|
|
|
|
68
|
20 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Check collection has the item. |
73
|
|
|
* @param string $name item name |
74
|
|
|
* @return bool whether item exist |
75
|
|
|
*/ |
76
|
49 |
|
public function has($name) |
77
|
|
|
{ |
78
|
49 |
|
return ($name && $this->hasProperty($name)) || $this->hasItem($name); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Delete an item. |
83
|
|
|
* @param $name |
84
|
|
|
*/ |
85
|
12 |
|
public function delete($name) |
86
|
|
|
{ |
87
|
12 |
|
$this->__unset($name); |
88
|
12 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* This method is overridden to support accessing items like properties. |
92
|
|
|
* @param string $name item or property name |
93
|
|
|
* @return mixed item of found or the named property value |
94
|
|
|
*/ |
95
|
28 |
|
public function __get($name) |
96
|
|
|
{ |
97
|
28 |
|
if ($name && $this->canGetProperty($name)) { |
|
|
|
|
98
|
1 |
|
return parent::__get($name); |
99
|
|
|
} else { |
100
|
28 |
|
return $this->getItem($name); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* This method is overridden to support accessing items like properties. |
106
|
|
|
* @param string $name item or property name |
107
|
|
|
* @param string $value value to be set |
108
|
|
|
* @return mixed item of found or the named property value |
109
|
|
|
*/ |
110
|
80 |
|
public function __set($name, $value) |
111
|
|
|
{ |
112
|
80 |
|
$this->set($name, $value); |
113
|
80 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Checks if a property value is null. |
117
|
|
|
* This method overrides the parent implementation by checking if the named item is loaded. |
118
|
|
|
* @param string $name the property name or the event name |
119
|
|
|
* @return bool whether the property value is null |
120
|
|
|
*/ |
121
|
5 |
|
public function __isset($name) |
122
|
|
|
{ |
123
|
5 |
|
return ($name && parent::__isset($name)) || $this->issetItem($name); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Checks if a property value is null. |
128
|
|
|
* This method overrides the parent implementation by checking if the named item is loaded. |
129
|
|
|
* @param string $name the property name or the event name |
130
|
|
|
* @return bool whether the property value is null |
131
|
|
|
*/ |
132
|
12 |
|
public function __unset($name) |
133
|
|
|
{ |
134
|
12 |
|
if ($name && $this->canSetProperty($name)) { |
|
|
|
|
135
|
|
|
parent::__unset($name); |
136
|
|
|
} else { |
137
|
12 |
|
$this->unsetItem($name); |
138
|
|
|
} |
139
|
12 |
|
} |
140
|
|
|
} |
141
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.