1
|
|
|
<?php |
2
|
|
|
namespace Jmw\Collection\Lists; |
3
|
|
|
|
4
|
|
|
use Jmw\Collection\CollectionAbstract; |
5
|
|
|
use Jmw\Collection\Exception\UnsupportedOperationException; |
6
|
|
|
use Jmw\Collection\CollectionInterface; |
7
|
|
|
use Jmw\Collection\ImmutableInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Tuples are useful structures for passing around simple, immutable data sets. |
11
|
|
|
* @author john |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
class Tuple extends CollectionAbstract implements ListInterface, ImmutableInterface |
15
|
|
|
{ |
16
|
|
|
use ImmutableListTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* An internal, immutable array containing values |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $values; |
23
|
|
|
|
24
|
|
|
public function __construct() |
25
|
|
|
{ |
26
|
|
|
$this->values = func_get_args(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function hashCode() |
30
|
|
|
{ |
31
|
|
|
$iterator = $this->iterator(); |
32
|
|
|
|
33
|
|
|
$code = 0; |
34
|
|
|
while($iterator->hasNext()) |
35
|
|
|
{ |
36
|
|
|
$element = $iterator->next(); |
37
|
|
|
$hash = hash('sha256', serialize($element)); |
38
|
|
|
$value = base_convert($hash, 16, 10); |
39
|
|
|
$code += $value; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $code; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/******************************************************** |
46
|
|
|
** Collection methods |
47
|
|
|
********************************************************/ |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns an iterator over the elements in this collection. |
51
|
|
|
* @return IteratorInterface |
52
|
|
|
*/ |
53
|
|
|
public function iterator() |
54
|
|
|
{ |
55
|
|
|
return new ListIterator($this); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns the number of elements in this collection. |
60
|
|
|
*/ |
61
|
|
|
public function size() |
62
|
|
|
{ |
63
|
|
|
return count($this->values); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns an array containing all of the elements in this collection. |
68
|
|
|
*/ |
69
|
|
|
public function toArray() |
70
|
|
|
{ |
71
|
|
|
return $this->values; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/******************************************************** |
75
|
|
|
** List methods |
76
|
|
|
********************************************************/ |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns the element at the specified position in this list. |
80
|
|
|
* @param int $index |
81
|
|
|
*/ |
82
|
|
|
public function get($index) |
83
|
|
|
{ |
84
|
|
|
return $this->values[$index]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. |
89
|
|
|
* @param multitype $element |
90
|
|
|
*/ |
91
|
|
View Code Duplication |
public function indexOf($element) |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$index = array_search($element, $this->values, true); |
94
|
|
|
|
95
|
|
|
$index = $index ? $index : -1; |
96
|
|
|
|
97
|
|
|
return $index; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. |
102
|
|
|
* @param multitype $element |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
public function lastIndexOf($element) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$index = array_search($element, array_reverse($this->values), true); |
107
|
|
|
|
108
|
|
|
$index = $index ? $index : -1; |
109
|
|
|
|
110
|
|
|
return $index; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Returns a list iterator over the elements in this list (in proper sequence) |
115
|
|
|
*/ |
116
|
|
|
public function listIterator() |
117
|
|
|
{ |
118
|
|
|
return $this->iterator(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. |
123
|
|
|
* @param int $index |
124
|
|
|
*/ |
125
|
|
|
public function listIteratorAt($index) |
126
|
|
|
{ |
127
|
|
|
return new ListIterator($this, $index); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. |
133
|
|
|
* @param int $from |
134
|
|
|
* @param int $to |
135
|
|
|
* @return ArrayList |
136
|
|
|
*/ |
137
|
|
|
public function subList($from, $to) |
138
|
|
|
{ |
139
|
|
|
return new ArrayList(array_slice($this->values, $from, $to - $from)); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/********************************************* |
143
|
|
|
** Array Access Methods |
144
|
|
|
*********************************************/ |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* |
148
|
|
|
* {@inheritDoc} |
149
|
|
|
* @see ArrayAccess::offsetExists() |
150
|
|
|
*/ |
151
|
|
View Code Duplication |
public function offsetExists($offset) |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
if(!is_int($offset) || $offset >= $this->size() || $offset < 0) { |
154
|
|
|
return false; |
155
|
|
|
} else { |
156
|
|
|
return true; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* |
162
|
|
|
* {@inheritDoc} |
163
|
|
|
* @see ArrayAccess::offsetGet() |
164
|
|
|
*/ |
165
|
|
|
public function offsetGet($offset) |
166
|
|
|
{ |
167
|
|
|
return $this->get($offset); |
168
|
|
|
} |
169
|
|
|
} |
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.