-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPair.php
More file actions
129 lines (115 loc) · 2.72 KB
/
Copy pathPair.php
File metadata and controls
129 lines (115 loc) · 2.72 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
declare(strict_types=1);
/*
* This file is part of the univeros/framework
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Altair\Structure;
use Altair\Structure\Contracts\HashableInterface;
use Altair\Structure\Contracts\PairInterface;
use JsonSerializable;
use OutOfBoundsException;
use Override;
use ReturnTypeWillChange;
use Stringable;
/**
* A pair which represents a key, and an associated value.
*
* @template TKey
* @template TValue
*
* @implements PairInterface<TKey, TValue>
*
* @phpstan-consistent-constructor
*/
class Pair implements PairInterface, JsonSerializable, Stringable
{
/**
* Constructor.
*
* @param TKey $key
* @param TValue $value
*/
public function __construct(
/** @var TKey */
public mixed $key = null,
/** @var TValue */
public mixed $value = null,
) {}
/**
* Resolves reads of $key/$value after they have been unset, returning null
* rather than triggering an "undefined property" error. The property is not
* re-initialised, so its declared TKey/TValue type is never violated; every
* subsequent read routes back through this accessor and yields null.
*/
public function __get(mixed $name): mixed
{
if ($name === 'key' || $name === 'value') {
return null;
}
throw new OutOfBoundsException('Out of bounds');
}
/**
* Debug Info.
*
* @return array{key: TKey, value: TValue}
*/
public function __debugInfo()
{
return $this->toArray();
}
/**
* To String.
*/
#[Override]
public function __toString(): string
{
return 'object(' . static::class . ')';
}
/**
* {@inheritDoc}
*
* @param TKey $key
*/
#[Override]
public function equalsKey($key): bool
{
if ($this->key instanceof HashableInterface) {
return $this->key::class === $key::class && $this->key->equals($key);
}
return $this->key === $key;
}
/**
* Returns a copy of the Pair.
*
* @return static
*/
#[Override]
public function copy(): PairInterface
{
return new static($this->key, $this->value);
}
/**
* {@inheritDoc}
*
* @return array{key: TKey, value: TValue}
*/
#[Override]
public function toArray(): array
{
return ['key' => $this->key, 'value' => $this->value];
}
/**
* {@inheritDoc}
*
* @return array{key: TKey, value: TValue}
*/
#[ReturnTypeWillChange]
#[Override]
public function jsonSerialize()
{
return $this->toArray();
}
}