Completed
Pull Request — master (#7)
by Volodymyr
02:27
created

AbstractConstCollectionArray::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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
    /**
30
     * AbstractConstCollectionArray constructor.
31
     *
32
     * @param mixed $array
33
     *
34
     * @throws  \InvalidArgumentException
35
     */
36 60
    public function __construct($array = null)
37
    {
38 60
        if ($array !== null) {
39 15
            if (!is_array($array) && !$array instanceof \Traversable) {
40
                throw new \InvalidArgumentException('Parameter must be an array or an instance of Traversable');
41
            }
42
43 15
            foreach ($array as $key => $item) {
44 15
                if (is_array($item)) {
45 6
                    $item = new static($item);
46 6
                }
47 15
                $this[$key] = $item;
48 15
            }
49 15
        }
50 60
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 1
    public function isEmpty()
56
    {
57 1
        return $this->count() === 0;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 7
    public function count()
64
    {
65 7
        return count($this->container);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function clear()
72
    {
73
        $this->container = [];
74
75
        return $this;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 2
    public function serialize()
82
    {
83 2
        return serialize($this->container);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 1
    public function unserialize($serialized)
90
    {
91 1
        $this->container = unserialize($serialized);
92
93 1
        return $this->container;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 3
    public function tryGet($index, $default = null)
100
    {
101 3
        if ($this->containsKey($index) === false) {
102 2
            return $default;
103
        }
104
105 1
        return $this->get($index);
106
    }
107
108
    /**
109
     * @return BaseObservable
110
     */
111
    public function toObservable()
112
    {
113
        return new ArrayObservable($this->toArray());
114
    }
115
116
    /**
117
     * (PHP 5 &gt;= 5.4.0)<br/>
118
     * Specify data which should be serialized to JSON
119
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
120
     * @return mixed data which can be serialized by <b>json_encode</b>,
121
     * which is a value of any type other than a resource.
122
     */
123
    public function jsonSerialize()
124
    {
125
        return $this->container;
126
    }
127
128
    /**
129
     * @return ArrayList
130
     */
131 1
    public function toVector()
132
    {
133 1
        return new ArrayList($this);
134
    }
135
136
    /**
137
     * @return ImmArrayList
138
     */
139
    public function toImmVector()
140
    {
141
        return new ImmArrayList($this);
142
    }
143
144
    /**
145
     * TODO: Implement toSet() method.
146
     */
147
    public function toSet()
148
    {
149
    }
150
151
    /**
152
     * @return ImmSet
153
     */
154
    public function toImmSet()
155
    {
156
        return new ImmSet($this);
157
    }
158
159
    /**
160
     * @return LazyIterableView
161
     */
162
    public function lazy()
163
    {
164
        return new LazyIterableView($this);
165
    }
166
167
    /**
168
     * @return Dictionary
169
     */
170
    public function toMap()
171
    {
172
        return new Dictionary($this);
173
    }
174
175
    /**
176
     * @return ImmDictionary
177
     */
178
    public function toImmMap()
179
    {
180
        return new ImmDictionary($this);
181
    }
182
183
    /**
184
     * @return array
185
     */
186 2
    public function getAll()
187
    {
188 2
        return $this->container;
189
    }
190
}
191