1
|
|
|
<?php |
2
|
|
|
namespace Ds; |
3
|
|
|
|
4
|
|
|
use OutOfBoundsException; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* A pair which represents a key and an associated value. |
8
|
|
|
* |
9
|
|
|
* @property mixed $key |
10
|
|
|
* @property mixed $value |
11
|
|
|
* |
12
|
|
|
* @package Ds |
13
|
|
|
*/ |
14
|
|
|
final class Pair implements \JsonSerializable |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var mixed The pair's key |
18
|
|
|
*/ |
19
|
|
|
public $key; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var mixed The pair's value |
23
|
|
|
*/ |
24
|
|
|
public $value; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Creates a new instance. |
28
|
|
|
* |
29
|
|
|
* @param mixed $key |
30
|
|
|
* @param mixed $value |
31
|
|
|
*/ |
32
|
|
|
public function __construct($key = null, $value = null) |
33
|
|
|
{ |
34
|
|
|
$this->key = $key; |
35
|
|
|
$this->value = $value; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* |
40
|
|
|
* @param mixed $name |
41
|
|
|
* |
42
|
|
|
* @return mixed|null |
|
|
|
|
43
|
|
|
*/ |
44
|
|
|
public function __isset($name) |
45
|
|
|
{ |
46
|
|
|
if ($name === 'key' || $name === 'value') { |
47
|
|
|
return $this->$name !== null; |
48
|
|
|
} |
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* This allows unset($pair->key) to not completely remove the property, |
54
|
|
|
* but be set to null instead. |
55
|
|
|
* |
56
|
|
|
* @param mixed $name |
57
|
|
|
* |
58
|
|
|
* @return mixed|null |
59
|
|
|
*/ |
60
|
|
View Code Duplication |
public function __unset($name) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
if ($name === 'key' || $name === 'value') { |
63
|
|
|
$this->$name = null; |
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
throw new OutOfBoundsException(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param mixed $name |
71
|
|
|
* |
72
|
|
|
* @return mixed|null |
73
|
|
|
*/ |
74
|
|
|
public function &__get($name) |
75
|
|
|
{ |
76
|
|
|
if ($name === 'key' || $name === 'value') { |
77
|
|
|
return $this->$name; |
78
|
|
|
} |
79
|
|
|
throw new OutOfBoundsException(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param mixed $name |
84
|
|
|
* @param mixed $value |
85
|
|
|
* |
86
|
|
|
* @return mixed|null |
87
|
|
|
*/ |
88
|
|
View Code Duplication |
public function __set($name, $value) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
if ($name === 'key' || $name === 'value') { |
91
|
|
|
$this->$name = $value; |
92
|
|
|
return; |
93
|
|
|
} |
94
|
|
|
throw new OutOfBoundsException(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns a copy of the Pair |
99
|
|
|
*/ |
100
|
|
|
public function copy(): self |
101
|
|
|
{ |
102
|
|
|
return new self($this->key, $this->value); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns a representation to be used for var_dump and print_r. |
107
|
|
|
* |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
public function __debugInfo() |
111
|
|
|
{ |
112
|
|
|
return $this->toArray(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
|
public function toArray(): array |
119
|
|
|
{ |
120
|
|
|
return [ |
121
|
|
|
'key' => $this->key, |
122
|
|
|
'value' => $this->value, |
123
|
|
|
]; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @inheritDoc |
128
|
|
|
*/ |
129
|
|
|
public function jsonSerialize() |
130
|
|
|
{ |
131
|
|
|
return $this->toArray(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns a string representation of the pair. |
136
|
|
|
*/ |
137
|
|
|
public function __toString() |
138
|
|
|
{ |
139
|
|
|
return 'object(' . get_class($this) . ')'; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.