|
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\Comparable\Comparator; |
|
13
|
|
|
use Cubiche\Core\Comparable\ComparatorInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* SortedArrayHashMap Class. |
|
17
|
|
|
* |
|
18
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
|
19
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class SortedArrayHashMap extends ArrayHashMap |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var ComparatorInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $criteria; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* SortedArrayHashMap constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param array $elements |
|
32
|
|
|
* @param ComparatorInterface|null $criteria |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(array $elements = array(), ComparatorInterface $criteria = null) |
|
35
|
|
|
{ |
|
36
|
|
|
if ($criteria === null) { |
|
37
|
|
|
$criteria = new Comparator(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$this->criteria = $criteria; |
|
41
|
|
|
foreach ($elements as $key => $element) { |
|
42
|
|
|
parent::set($key, $element); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$this->sort(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public function set($key, $value) |
|
52
|
|
|
{ |
|
53
|
|
|
parent::set($key, $value); |
|
54
|
|
|
|
|
55
|
|
|
$this->sort(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
|
|
public function removeAt($key) |
|
62
|
|
|
{ |
|
63
|
|
|
$removed = parent::removeAt($key); |
|
64
|
|
|
if ($removed !== null) { |
|
65
|
|
|
$this->sort(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $removed; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc} |
|
73
|
|
|
*/ |
|
74
|
|
|
public function sort(ComparatorInterface $criteria = null) |
|
75
|
|
|
{ |
|
76
|
|
|
if ($criteria !== null) { |
|
77
|
|
|
$this->criteria = $criteria; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
parent::sort($this->criteria); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.