-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pair.php
110 lines (97 loc) · 2.08 KB
/
Pair.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace Altair\Structure;
use Altair\Structure\Contracts\HashableInterface;
use Altair\Structure\Contracts\PairInterface;
use JsonSerializable;
use OutOfBoundsException;
/**
* A pair which represents a key, and an associated value.
*
*/
class Pair implements PairInterface, JsonSerializable
{
/**
* @param mixed $key The pair's key
*/
public $key;
/**
* @param mixed $value The pair's value
*/
public $value;
/**
* Constructor.
*
* @param mixed $key
* @param mixed $value
*/
public function __construct($key = null, $value = null)
{
$this->key = $key;
$this->value = $value;
}
/**
* This allows unset($pair->key) to not completely remove the property,
* but be set to null instead.
*
* @param mixed $name
*
* @return mixed|null
*/
public function __get($name)
{
if ($name === 'key' || $name === 'value') {
$this->$name = null;
return;
}
throw new OutOfBoundsException();
}
/**
* Debug Info.
*
* @return array
*/
public function __debugInfo()
{
return $this->toArray();
}
/**
* To String.
*/
public function __toString()
{
return 'object(' . get_class($this) . ')';
}
/**
* {@inheritdoc}
*/
public function equalsKey($key): bool
{
if (is_object($this->key) && $this->key instanceof HashableInterface) {
return get_class($this->key) === get_class($key) && $this->key->equals($key);
}
return $this->key === $key;
}
/**
* Returns a copy of the Pair.
*
* @return PairInterface
*/
public function copy(): PairInterface
{
return new static($this->key, $this->value);
}
/**
* {@inheritdoc}
*/
public function toArray(): array
{
return ['key' => $this->key, 'value' => $this->value];
}
/**
* {@inheritdoc}
*/
public function jsonSerialize()
{
return $this->toArray();
}
}