Completed
Push — master ( 813757...d8be45 )
by Ítalo
03:56 queued 02:00
created

AbstractConstCollectionArray::toMap()   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
10
/**
11
 * Provides the abstract base class for a strongly typed collection.
12
 */
13
abstract class AbstractConstCollectionArray extends AbstractCollection implements
14
    ConstIndexAccessInterface,
15
    CollectionConvertableInterface,
16
    \Serializable,
17
    \JsonSerializable
18
{
19
20
    use SortTrait;
21
22
    /**
23
     * @var array
24
     */
25
    protected $container = [];
26
27 60
    public function __construct($array = null)
28
    {
29 60
        if ($array !== null) {
30 15
            if (!is_array($array) && !$array instanceof \Traversable) {
31
                throw new \InvalidArgumentException('Parameter must be an array or an instance of Traversable');
32
            }
33
34 15
            foreach ($array as $key => $item) {
35 15
                if (is_array($item)) {
36 6
                    $item = new static($item);
37 6
                }
38 15
                $this[$key] = $item;
39 15
            }
40
41 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...
42
        }
43 60
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 1
    public function isEmpty()
49
    {
50 1
        return $this->count() === 0;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 7
    public function count()
57
    {
58 7
        return count($this->container);
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function clear()
65
    {
66
        $this->container = [];
67
68
        return $this;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 2
    public function serialize()
75
    {
76 2
        return serialize($this->container);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 1
    public function unserialize($serialized)
83
    {
84 1
        $this->container = unserialize($serialized);
85
86 1
        return $this->container;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 3
    public function tryGet($index, $default = null)
93
    {
94 3
        if ($this->containsKey($index) === false) {
95 2
            return $default;
96
        }
97
98 1
        return $this->get($index);
99
    }
100
101
    /**
102
     * (PHP 5 &gt;= 5.4.0)<br/>
103
     * Specify data which should be serialized to JSON
104
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
105
     * @return mixed data which can be serialized by <b>json_encode</b>,
106
     * which is a value of any type other than a resource.
107
     */
108
    public function jsonSerialize()
109
    {
110
        return $this->container;
111
    }
112
113 1
    public function toVector()
114
    {
115 1
        return new ArrayList($this);
116
    }
117
118
    public function toImmVector()
119
    {
120
        return new ImmArrayList($this);
121
    }
122
123
    public function toSet()
124
    {
125
        return new Set($this);
126
    }
127
128
    public function toImmSet()
129
    {
130
        return new ImmSet($this);
131
    }
132
133
    public function toMap()
134
    {
135
        return new Dictionary($this);
136
    }
137
138
    public function toImmMap()
139
    {
140
        return new ImmDictionary($this);
141
    }
142
}
143