Completed
Push — master ( e86954...7cbcb1 )
by Ítalo
02:48
created

AbstractConstCollectionArray::unserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
crap 1
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;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
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 &gt;= 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
        // TODO: Implement toSet() method.
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