Completed
Push — master ( d6f0bd...41a21b )
by Ivannis Suárez
05:43
created

ArrayHashMap::containsValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * This file is part of the Cubiche package.
4
 *
5
 * Copyright (c) Cubiche
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Cubiche\Core\Collections\ArrayCollection;
11
12
use Cubiche\Core\Collections\CollectionInterface;
13
use Cubiche\Core\Collections\DataSource\ArrayDataSource;
14
use Cubiche\Core\Collections\DataSourceHashMap;
15
use Cubiche\Core\Comparable\Comparator;
16
use Cubiche\Core\Comparable\ComparatorInterface;
17
use Cubiche\Core\Specification\SpecificationInterface;
18
19
/**
20
 * ArrayHashMap Class.
21
 *
22
 * @author Karel Osorio Ramírez <[email protected]>
23
 * @author Ivannis Suárez Jerez <[email protected]>
24
 */
25
class ArrayHashMap extends ArrayCollection implements ArrayHashMapInterface
26
{
27
    /**
28
     * ArrayHashMap constructor.
29
     *
30
     * @param array $elements
31
     */
32
    public function __construct(array $elements = array())
33
    {
34
        foreach ($elements as $key => $element) {
35
            $this->set($key, $element);
36
        }
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function get($key)
43
    {
44
        if ($this->containsKey($key)) {
45
            return $this->elements[$key];
46
        }
47
48
        return;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function set($key, $value)
55
    {
56
        $this->validateKey($key);
57
58
        $this->elements[$key] = $value;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function containsKey($key)
65
    {
66
        return $this->hasKey($key);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function containsValue($value)
73
    {
74
        return $this->values()->contains($value);
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function removeAt($key)
81
    {
82
        if ($this->containsKey($key)) {
83
            $removed = $this->elements[$key];
84
            unset($this->elements[$key]);
85
86
            return $removed;
87
        }
88
89
        return;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function find(SpecificationInterface $criteria)
96
    {
97
        return new DataSourceHashMap(new ArrayDataSource($this->elements, $criteria));
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function keys()
104
    {
105
        return new ArraySet(\array_keys($this->elements));
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function values()
112
    {
113
        return new ArrayList(\array_values($this->elements));
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 View Code Duplication
    public function sort(ComparatorInterface $criteria = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
    {
121
        if ($criteria === null) {
122
            $criteria = new Comparator();
123
        }
124
125
        uksort($this->elements, function ($a, $b) use ($criteria) {
126
            return $criteria->compare($a, $b);
127
        });
128
    }
129
130
    /**
131
     * @param ComparatorInterface $criteria
132
     *
133
     * @return CollectionInterface
134
     */
135
    public function sorted(ComparatorInterface $criteria)
136
    {
137
        return new DataSourceHashMap(new ArrayDataSource($this->elements, null, $criteria));
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function offsetUnset($offset)
144
    {
145
        return $this->removeAt($offset);
146
    }
147
}
148