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\Exception\InvalidOperationException; |
7
|
|
|
use Collections\Iterator\PairIterator; |
8
|
|
|
use Collections\Traits\ImmVectorLikeTrait; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* `Pair` is a fixed-size collection with exactly two elements (possibly of |
12
|
|
|
* different types). HHVM provides a native implementation for this class. |
13
|
|
|
* The PHP class definition below is not actually used at run time; it is |
14
|
|
|
* simply provided for the typechecker and for developer reference. |
15
|
|
|
* |
16
|
|
|
* Like all objects in PHP, `Pair`s have reference-like semantics. The elements |
17
|
|
|
* or a `Pair` cannot be mutated (i.e. you can assign to the elements of a |
18
|
|
|
* `Pair`) though `Pair`s may contain mutable objects. |
19
|
|
|
* |
20
|
|
|
* `Pair`s only support integer keys. If a non-integer key is used, an |
21
|
|
|
* exception will be thrown. |
22
|
|
|
* |
23
|
|
|
* `Pair`s support `$m[$k]` style syntax for getting and setting values by |
24
|
|
|
* key. `Pair`s also support `isset($m[$k])` and `empty($m[$k])` syntax, and |
25
|
|
|
* they provide similar semantics as arrays. Elements can be added to a `Pair` |
26
|
|
|
* using `$m[] = ..` syntax. |
27
|
|
|
* |
28
|
|
|
* `Pair`s do not support taking elements by reference. If binding assignment |
29
|
|
|
* (`=&`) is used with an element of a `Pair`, or if an element of a `Pair` is |
30
|
|
|
* passed by reference, of if a `Pair` is used with foreach by reference, an |
31
|
|
|
* exception will be thrown. |
32
|
|
|
* |
33
|
|
|
* `Pair` keys are always 0 and 1, repsectively. |
34
|
|
|
* |
35
|
|
|
* You may notice that many methods affecting the instace of `Pair` return an |
36
|
|
|
* `ImmVector` -- `Pair`s are essentially backed by 2-element `ImmVector`s. |
37
|
|
|
* |
38
|
|
|
*/ |
39
|
|
|
final class Pair implements ConstVectorInterface, \ArrayAccess |
40
|
|
|
{ |
41
|
|
|
use ImmVectorLikeTrait; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Pair constructor. |
45
|
|
|
* @param $item1 |
46
|
|
|
* @param $item2 |
47
|
|
|
*/ |
48
|
|
|
public function __construct($item1, $item2) |
49
|
|
|
{ |
50
|
|
|
$this->container[] = $item1; |
51
|
|
|
$this->container[] = $item2; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
*/ |
57
|
|
|
public function getIterator() |
58
|
|
|
{ |
59
|
|
|
return new PairIterator($this->container); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns `false`; a `Pair` cannot be empty. |
64
|
|
|
* @return bool - `false` |
65
|
|
|
*/ |
66
|
|
|
public function isEmpty() |
67
|
|
|
{ |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns 2; a `Pair` always has two values. |
73
|
|
|
* @return int |
74
|
|
|
*/ |
75
|
|
|
public function count() |
76
|
|
|
{ |
77
|
|
|
return 2; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @internal |
82
|
|
|
*/ |
83
|
|
|
public function __get($name) |
84
|
|
|
{ |
85
|
|
|
throw InvalidOperationException::unsupportedGet($this); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @internal |
90
|
|
|
*/ |
91
|
|
|
public function __set($name, $value) |
92
|
|
|
{ |
93
|
|
|
throw InvalidOperationException::unsupportedSet($this); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @internal |
98
|
|
|
*/ |
99
|
|
|
public function __isset($name) |
100
|
|
|
{ |
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @internal |
106
|
|
|
*/ |
107
|
|
|
public function __unset($name) |
108
|
|
|
{ |
109
|
|
|
throw InvalidOperationException::unsupportedUnset($this); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|