|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the ramsey/collection library |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
* |
|
9
|
|
|
* @copyright Copyright (c) Ben Ramsey <[email protected]> |
|
10
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
11
|
|
|
* @link https://benramsey.com/projects/ramsey-collection/ Documentation |
|
12
|
|
|
* @link https://packagist.org/packages/ramsey/collection Packagist |
|
13
|
|
|
* @link https://github.com/ramsey/collection GitHub |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Ramsey\Collection\Map; |
|
17
|
|
|
|
|
18
|
|
|
use Ramsey\Collection\Tool\TypeTrait; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* A `TypedMap` represent a Map of elements where key and value are typed. |
|
22
|
|
|
* |
|
23
|
|
|
* Each element is identified by a key with defined type and a value of defined type. |
|
24
|
|
|
* The keys of the map must be unique. The values on the map can be repeated but each |
|
25
|
|
|
* with its own different key. |
|
26
|
|
|
* |
|
27
|
|
|
* The most common case is to use a string type key. But it's not limited to this type if keys. |
|
28
|
|
|
* |
|
29
|
|
|
* This is a direct implementation of `TypedMapInterface`, provided for the sake of convenience. |
|
30
|
|
|
* |
|
31
|
|
|
* Example usage: |
|
32
|
|
|
* |
|
33
|
|
|
* ```php |
|
34
|
|
|
* $map = new TypedMap('string', Foo::class); |
|
35
|
|
|
* $map['x'] = new Foo(); |
|
36
|
|
|
* foreach($map as $key => $value) { |
|
37
|
|
|
* // do something with $key, it will be a Foo::class |
|
38
|
|
|
* } |
|
39
|
|
|
* |
|
40
|
|
|
* // this will throw an exception since key must be string |
|
41
|
|
|
* $map[10] = new Foo(); |
|
42
|
|
|
* |
|
43
|
|
|
* // this will throw an exception since value must be a Foo |
|
44
|
|
|
* $map['bar'] = 'bar'; |
|
45
|
|
|
* |
|
46
|
|
|
* // initialize map with contents |
|
47
|
|
|
* $map = new TypedMap('string', Foo::class, [ |
|
48
|
|
|
* new Foo(), new Foo(), new Foo() |
|
49
|
|
|
* ]); |
|
50
|
|
|
* ``` |
|
51
|
|
|
* |
|
52
|
|
|
* It is preferable to subclass `AbstractTypedMap` |
|
53
|
|
|
* to create your own typed map implementation |
|
54
|
|
|
* |
|
55
|
|
|
* ```php |
|
56
|
|
|
* class FooTypedMap extends AbstractTypedMap |
|
57
|
|
|
* { |
|
58
|
|
|
* public function getKeyType() |
|
59
|
|
|
* { |
|
60
|
|
|
* return "int" |
|
61
|
|
|
* } |
|
62
|
|
|
* |
|
63
|
|
|
* public function getValueType() |
|
64
|
|
|
* { |
|
65
|
|
|
* return Foo::class; |
|
66
|
|
|
* } |
|
67
|
|
|
* } |
|
68
|
|
|
* ``` |
|
69
|
|
|
* |
|
70
|
|
|
* ... but it is not limited, you also can use the `TypedMap` class |
|
71
|
|
|
* |
|
72
|
|
|
* ```php |
|
73
|
|
|
* class FooTypedMap extends TypedMap |
|
74
|
|
|
* { |
|
75
|
|
|
* public function __constructor(array $data = []) |
|
76
|
|
|
* { |
|
77
|
|
|
* parent::__construct("int", Foo::class, $data); |
|
78
|
|
|
* } |
|
79
|
|
|
* } |
|
80
|
|
|
* ``` |
|
81
|
|
|
* |
|
82
|
|
|
* |
|
83
|
|
|
* @package Ramsey\Collection\Map |
|
84
|
|
|
*/ |
|
85
|
|
|
class TypedMap extends AbstractTypedMap |
|
86
|
|
|
{ |
|
87
|
|
|
|
|
88
|
|
|
use TypeTrait; |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* The type of keys stored in this collection |
|
92
|
|
|
* |
|
93
|
|
|
* A map key's type is immutable once it is set. For this reason, this |
|
94
|
|
|
* property is set private |
|
95
|
|
|
* |
|
96
|
|
|
* @var string type of the map key |
|
97
|
|
|
*/ |
|
98
|
|
|
private $keyType; |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* The type of values stored in this collection |
|
102
|
|
|
* |
|
103
|
|
|
* A map values's type is immutable once it is set. For this reason, this |
|
104
|
|
|
* property is set private |
|
105
|
|
|
* |
|
106
|
|
|
* @var string type of the map value |
|
107
|
|
|
*/ |
|
108
|
|
|
private $valueType; |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Constructs a map object of the specified key and value types, |
|
112
|
|
|
* optionally with the specified data |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $keyType |
|
115
|
|
|
* @param string $valueType |
|
116
|
|
|
* @param array $data |
|
117
|
|
|
*/ |
|
118
|
|
|
public function __construct($keyType, $valueType, array $data = []) |
|
119
|
|
|
{ |
|
120
|
|
|
$this->keyType = (string) $keyType; |
|
121
|
|
|
$this->valueType = (string) $valueType; |
|
122
|
|
|
parent::__construct($data); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function getKeyType() |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->keyType; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function getValueType() |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->valueType; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|