|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ValueObjects\Structure; |
|
4
|
|
|
|
|
5
|
|
|
use ValueObjects\StringLiteral\StringLiteral; |
|
6
|
|
|
use ValueObjects\Util\Util; |
|
7
|
|
|
use ValueObjects\ValueObjectInterface; |
|
8
|
|
|
|
|
9
|
|
|
class KeyValuePair implements ValueObjectInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var ValueObjectInterface */ |
|
12
|
|
|
protected $key; |
|
13
|
|
|
|
|
14
|
|
|
/** @var ValueObjectInterface */ |
|
15
|
|
|
protected $value; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Returns a KeyValuePair from native PHP arguments evaluated as strings |
|
19
|
|
|
* |
|
20
|
|
|
* @param string $key |
|
|
|
|
|
|
21
|
|
|
* @param string $value |
|
|
|
|
|
|
22
|
|
|
* @return self |
|
23
|
|
|
* @throws \InvalidArgumentException |
|
24
|
|
|
*/ |
|
25
|
2 |
|
public static function fromNative() |
|
26
|
|
|
{ |
|
27
|
2 |
|
$args = func_get_args(); |
|
28
|
|
|
|
|
29
|
2 |
|
if (count($args) != 2) { |
|
30
|
1 |
|
throw new \BadMethodCallException('This methods expects two arguments. One for the key and one for the value.'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
1 |
|
$keyString = \strval($args[0]); |
|
34
|
1 |
|
$valueString = \strval($args[1]); |
|
35
|
1 |
|
$key = new StringLiteral($keyString); |
|
36
|
1 |
|
$value = new StringLiteral($valueString); |
|
37
|
|
|
|
|
38
|
1 |
|
return new self($key, $value); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Returns a KeyValuePair |
|
43
|
|
|
* |
|
44
|
|
|
* @param ValueObjectInterface $key |
|
45
|
|
|
* @param ValueObjectInterface $value |
|
46
|
|
|
*/ |
|
47
|
13 |
|
public function __construct(ValueObjectInterface $key, ValueObjectInterface $value) |
|
48
|
|
|
{ |
|
49
|
13 |
|
$this->key = $key; |
|
50
|
13 |
|
$this->value = $value; |
|
51
|
13 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Tells whether two KeyValuePair are equal |
|
55
|
|
|
* |
|
56
|
|
|
* @param ValueObjectInterface $keyValuePair |
|
57
|
|
|
* @return bool |
|
58
|
|
|
*/ |
|
59
|
4 |
|
public function sameValueAs(ValueObjectInterface $keyValuePair) |
|
60
|
|
|
{ |
|
61
|
4 |
|
if (false === Util::classEquals($this, $keyValuePair)) { |
|
62
|
1 |
|
return false; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
4 |
|
return $this->getKey()->sameValueAs($keyValuePair->getKey()) && $this->getValue()->sameValueAs($keyValuePair->getValue()); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Returns key |
|
70
|
|
|
* |
|
71
|
|
|
* @return ValueObjectInterface |
|
72
|
|
|
*/ |
|
73
|
8 |
|
public function getKey() |
|
74
|
|
|
{ |
|
75
|
8 |
|
return clone $this->key; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Returns value |
|
80
|
|
|
* |
|
81
|
|
|
* @return ValueObjectInterface |
|
82
|
|
|
*/ |
|
83
|
8 |
|
public function getValue() |
|
84
|
|
|
{ |
|
85
|
8 |
|
return clone $this->value; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Returns a string representation of the KeyValuePair in format "$key => $value" |
|
90
|
|
|
* |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
1 |
|
public function __toString() |
|
94
|
|
|
{ |
|
95
|
1 |
|
$string = sprintf('%s => %s', $this->getKey(), $this->getValue()); |
|
96
|
|
|
|
|
97
|
1 |
|
return $string; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
function jsonSerialize() |
|
|
|
|
|
|
101
|
|
|
{ |
|
102
|
|
|
return [$this->getKey(), $this->getValue()]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.