1
|
|
|
<?php namespace Mbh\Collection; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* MBHFramework |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/MBHFramework/mbh-framework |
7
|
|
|
* @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos |
8
|
|
|
* @license https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
use JsonSerializable; |
12
|
|
|
use OutOfBoundsException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* A sequence of unique values. |
16
|
|
|
* |
17
|
|
|
* @package structures |
18
|
|
|
* @author Ulises Jeremias Cornejo Fandos <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class Pair implements JsonSerializable |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @param mixed $key The pair's key |
24
|
|
|
*/ |
25
|
|
|
public $key; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param mixed $value The pair's value |
29
|
|
|
*/ |
30
|
|
|
public $value; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Creates a new instance. |
34
|
|
|
* |
35
|
|
|
* @param mixed $key |
36
|
|
|
* @param mixed $value |
37
|
|
|
*/ |
38
|
|
|
public function __construct($key = null, $value = null) |
39
|
|
|
{ |
40
|
|
|
$this->key = $key; |
41
|
|
|
$this->value = $value; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* This allows unset($pair->key) to not completely remove the property, |
46
|
|
|
* but be set to null instead. |
47
|
|
|
* |
48
|
|
|
* @param mixed $name |
49
|
|
|
* |
50
|
|
|
* @return mixed|null |
51
|
|
|
*/ |
52
|
|
|
public function __get($name) |
53
|
|
|
{ |
54
|
|
|
if ($name === 'key' || $name === 'value') { |
55
|
|
|
$this->$name = null; |
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
throw new OutOfBoundsException(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns a copy of the Pair |
64
|
|
|
* |
65
|
|
|
* @return Pair |
66
|
|
|
*/ |
67
|
|
|
public function copy(): Pair |
68
|
|
|
{ |
69
|
|
|
return new self($this->key, $this->value); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns a representation to be used for var_dump and print_r. |
74
|
|
|
* |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
public function __debugInfo() |
78
|
|
|
{ |
79
|
|
|
return $this->toArray(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritDoc |
84
|
|
|
*/ |
85
|
|
|
public function toArray(): array |
86
|
|
|
{ |
87
|
|
|
return ['key' => $this->key, 'value' => $this->value]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritDoc |
92
|
|
|
*/ |
93
|
|
|
public function jsonSerialize() |
94
|
|
|
{ |
95
|
|
|
return $this->toArray(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns a string representation of the pair. |
100
|
|
|
*/ |
101
|
|
|
public function __toString() |
102
|
|
|
{ |
103
|
|
|
return 'object(' . get_class($this) . ')'; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function __clone() |
107
|
|
|
{ |
108
|
|
|
return $this->copy(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|