|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
// Copyright (c) Lellys Informática. All rights reserved. See License.txt in the project root for license information. |
|
4
|
|
|
namespace Collections; |
|
5
|
|
|
|
|
6
|
|
|
use Collections\Immutable\ImmArrayList; |
|
7
|
|
|
use Collections\Immutable\ImmDictionary; |
|
8
|
|
|
use Collections\Immutable\ImmSet; |
|
9
|
|
|
use Rx\Observable\ArrayObservable; |
|
10
|
|
|
use Rx\Observable\BaseObservable; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Provides the abstract base class for a strongly typed collection. |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class AbstractConstCollectionArray extends AbstractCollection implements |
|
|
|
|
|
|
16
|
|
|
ConstIndexAccessInterface, |
|
17
|
|
|
CollectionConvertableInterface, |
|
18
|
|
|
\Serializable, |
|
19
|
|
|
\JsonSerializable |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
use SortTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $container = []; |
|
28
|
|
|
|
|
29
|
60 |
|
public function __construct($array = null) |
|
30
|
|
|
{ |
|
31
|
60 |
|
if ($array !== null) { |
|
|
|
|
|
|
32
|
15 |
|
if (!is_array($array) && !$array instanceof \Traversable) { |
|
|
|
|
|
|
33
|
|
|
throw new \InvalidArgumentException('Parameter must be an array or an instance of Traversable'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
15 |
|
foreach ($array as $key => $item) { |
|
37
|
15 |
|
if (is_array($item)) { |
|
38
|
6 |
|
$item = new static($item); |
|
39
|
|
|
} |
|
40
|
15 |
|
$this[$key] = $item; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
15 |
|
return $this; |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
60 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
1 |
|
public function isEmpty() |
|
51
|
|
|
{ |
|
52
|
1 |
|
return $this->count() === 0; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
7 |
|
public function count() |
|
59
|
|
|
{ |
|
60
|
7 |
|
return count($this->container); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
*/ |
|
66
|
|
|
public function clear() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->container = []; |
|
69
|
|
|
|
|
70
|
|
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
2 |
|
public function serialize() |
|
77
|
|
|
{ |
|
78
|
2 |
|
return serialize($this->container); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
1 |
|
public function unserialize($serialized) |
|
85
|
|
|
{ |
|
86
|
1 |
|
$this->container = unserialize($serialized); |
|
87
|
|
|
|
|
88
|
1 |
|
return $this->container; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritdoc} |
|
93
|
|
|
*/ |
|
94
|
3 |
|
public function tryGet($index, $default = null) |
|
95
|
|
|
{ |
|
96
|
3 |
|
if ($this->containsKey($index) === false) { |
|
97
|
2 |
|
return $default; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
return $this->get($index); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return BaseObservable |
|
105
|
|
|
*/ |
|
106
|
|
|
public function toObservable() |
|
107
|
|
|
{ |
|
108
|
|
|
return new ArrayObservable($this->toArray()); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* (PHP 5 >= 5.4.0)<br/> |
|
113
|
|
|
* Specify data which should be serialized to JSON |
|
114
|
|
|
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
115
|
|
|
* @return mixed data which can be serialized by <b>json_encode</b>, |
|
116
|
|
|
* which is a value of any type other than a resource. |
|
117
|
|
|
*/ |
|
118
|
|
|
public function jsonSerialize() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->container; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
1 |
|
public function toVector() |
|
124
|
|
|
{ |
|
125
|
1 |
|
return new ArrayList($this); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function toImmVector() |
|
129
|
|
|
{ |
|
130
|
|
|
return new ImmArrayList($this); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function toSet() |
|
134
|
|
|
{ |
|
135
|
|
|
return new Set($this); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function toImmSet() |
|
139
|
|
|
{ |
|
140
|
|
|
return new ImmSet($this); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function lazy() |
|
144
|
|
|
{ |
|
145
|
|
|
return new LazyIterableView($this); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function toMap() |
|
149
|
|
|
{ |
|
150
|
|
|
return new Dictionary($this); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function toImmMap() |
|
154
|
|
|
{ |
|
155
|
|
|
return new ImmDictionary($this); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
|
|
|
|
|
158
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.