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