1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
4
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\Value; |
6
|
|
|
|
7
|
|
|
use Countable; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use Ktomk\Pipelines\Lib; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Properties value object (named set) |
13
|
|
|
* |
14
|
|
|
* @package Ktomk\Pipelines\Value |
15
|
|
|
*/ |
16
|
|
|
class Properties implements Countable |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $properties = array(); |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* import properties from an array |
25
|
|
|
* |
26
|
|
|
* @param array $array |
27
|
|
|
* |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
1 |
|
public function importPropertiesArray(array $array) |
31
|
|
|
{ |
32
|
1 |
|
foreach ($array as $key => $value) { |
33
|
1 |
|
$this->properties[$key] = $value; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* export a set of named entries as array |
39
|
|
|
* |
40
|
|
|
* @param array $keys named entries to export in two forms: |
41
|
|
|
* 1. array of strings |
42
|
|
|
* 2. array of two arrays of string, first is for |
43
|
|
|
* required, second for optional entries |
44
|
|
|
* |
45
|
|
|
* @throws InvalidArgumentException |
46
|
|
|
* |
47
|
|
|
* @return array export product |
48
|
|
|
*/ |
49
|
4 |
|
public function export(array $keys) |
50
|
|
|
{ |
51
|
|
|
if ( |
52
|
4 |
|
2 === count($keys) |
53
|
4 |
|
&& array(0, 1) === array_keys($keys) |
54
|
4 |
|
&& 2 === count(array_filter($keys, 'is_array')) |
55
|
|
|
) { |
56
|
2 |
|
list($required, $optional) = $keys; |
57
|
2 |
|
$this->missingKeys($required); |
58
|
1 |
|
$keys = array_merge($required, $optional); |
59
|
|
|
} |
60
|
|
|
|
61
|
3 |
|
return $this->exportPropertiesByName($keys); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* export properties by name(s) |
66
|
|
|
* |
67
|
|
|
* properties not set are not exported |
68
|
|
|
* |
69
|
|
|
* @param array $keys |
70
|
|
|
* @param array $export [optional] array to extend |
71
|
|
|
* |
72
|
|
|
* @return array w/ exported properties |
73
|
|
|
*/ |
74
|
4 |
|
public function exportPropertiesByName(array $keys, array $export = array()) |
75
|
|
|
{ |
76
|
4 |
|
foreach ($keys as $key) { |
77
|
3 |
|
if (array_key_exists($key, $this->properties)) { |
78
|
3 |
|
$export[$key] = $this->properties[$key]; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
4 |
|
return $export; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* has all named entities |
87
|
|
|
* |
88
|
|
|
* @param array|string $keys ... |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
2 |
|
public function has($keys) |
93
|
|
|
{ |
94
|
2 |
|
$args = func_get_args(); |
95
|
2 |
|
$allKeys = Lib::mergeArray($args); |
96
|
|
|
|
97
|
2 |
|
foreach ($allKeys as $key) { |
98
|
2 |
|
if (!array_key_exists($key, $this->properties)) { |
99
|
1 |
|
return false; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* import named entries from array as properties |
108
|
|
|
* |
109
|
|
|
* @param array $array to import from |
110
|
|
|
* @param array $keys named entries to import |
111
|
|
|
* |
112
|
|
|
* @return array passed in array with the imported entries removed |
113
|
|
|
*/ |
114
|
3 |
|
public function import(array $array, array $keys) |
115
|
|
|
{ |
116
|
3 |
|
foreach ($keys as $key) { |
117
|
3 |
|
if (array_key_exists($key, $array)) { |
118
|
3 |
|
$this->properties[$key] = $array[$key]; |
119
|
3 |
|
unset($array[$key]); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
3 |
|
return $array; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
2 |
|
public function toArray() |
130
|
|
|
{ |
131
|
2 |
|
return $this->properties; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** Countable */ |
135
|
|
|
|
136
|
2 |
|
#[\ReturnTypeWillChange] |
137
|
|
|
/** |
138
|
|
|
* Count elements of an object |
139
|
|
|
* |
140
|
|
|
* @link http://php.net/manual/en/countable.count.php |
141
|
|
|
* |
142
|
|
|
* @return int The custom count as an integer. |
143
|
|
|
*/ |
144
|
|
|
public function count() |
145
|
|
|
{ |
146
|
2 |
|
return count($this->properties); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* obtain a list of keys that are not available in properties |
151
|
|
|
* |
152
|
|
|
* @param array $keys |
153
|
|
|
* |
154
|
|
|
* @throws InvalidArgumentException |
155
|
|
|
* |
156
|
|
|
* @return void |
157
|
|
|
*/ |
158
|
2 |
|
private function missingKeys(array $keys) |
159
|
|
|
{ |
160
|
2 |
|
$missing = array_diff_key(array_flip($keys), $this->properties); |
161
|
2 |
|
if (!empty($missing)) { |
162
|
1 |
|
throw new InvalidArgumentException(sprintf( |
163
|
|
|
'property/ies "%s" required', |
164
|
1 |
|
implode('", "', array_keys($missing)) |
165
|
|
|
)); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|