|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
// Copyright (c) Lellys Informática. All rights reserved. See License.txt in the project root for license information. |
|
4
|
|
|
namespace Collections\Traits; |
|
5
|
|
|
|
|
6
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Provides utility protected methods for extracting a property or column |
|
10
|
|
|
* from an array or object. |
|
11
|
|
|
*/ |
|
12
|
|
|
trait ExtractTrait |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Returns a callable that can be used to extract a property or column from |
|
16
|
|
|
* an array or object based on a dot separated path. |
|
17
|
|
|
* |
|
18
|
|
|
* @param string|callable $callback A dot separated path of column to follow |
|
19
|
|
|
* so that the final one can be returned or a callable that will take care |
|
20
|
|
|
* of doing that. |
|
21
|
|
|
* @return callable |
|
22
|
|
|
*/ |
|
23
|
4 |
|
public function propertyExtractor($callback) |
|
24
|
|
|
{ |
|
25
|
4 |
|
if (is_string($callback)) { |
|
26
|
4 |
|
$path = explode('.', $callback); |
|
27
|
|
|
$callback = function ($element) use ($path) { |
|
28
|
4 |
|
return $this->extractData($element, $path); |
|
29
|
4 |
|
}; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
4 |
|
return $callback; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Returns a column from $data that can be extracted |
|
37
|
|
|
* by iterating over the column names contained in $path |
|
38
|
|
|
* |
|
39
|
|
|
* @param array|\ArrayAccess $data Data. |
|
40
|
|
|
* @param array $path Path to extract from. |
|
41
|
|
|
* @return mixed |
|
42
|
|
|
*/ |
|
43
|
4 |
|
protected function extractData($data, $path) |
|
44
|
|
|
{ |
|
45
|
4 |
|
$accessor = PropertyAccess::createPropertyAccessor(); |
|
46
|
4 |
|
$value = null; |
|
47
|
4 |
|
foreach ($path as $column) { |
|
48
|
4 |
|
if (is_array($data) || $data instanceof \ArrayAccess) { |
|
49
|
4 |
|
$value = $accessor->getValue($data, "[$column]"); |
|
50
|
|
|
} else { |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
if (!$accessor->isReadable($data, $column)) { |
|
53
|
|
|
return null; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$value = $accessor->getValue($data, $column); |
|
57
|
|
|
} |
|
58
|
4 |
|
$data = $value; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
4 |
|
return $value; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Returns a callable that receives a value and will return whether or not |
|
66
|
|
|
* it matches certain condition. |
|
67
|
|
|
* |
|
68
|
|
|
* @param array $conditions A key-value list of conditions to match where the |
|
69
|
|
|
* key is the property path to get from the current item and the value is the |
|
70
|
|
|
* value to be compared the item with. |
|
71
|
|
|
* @return callable |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function createMatcherFilter(array $conditions) |
|
74
|
|
|
{ |
|
75
|
|
|
$matchers = []; |
|
76
|
|
|
foreach ($conditions as $property => $value) { |
|
77
|
|
|
$extractor = $this->propertyExtractor($property); |
|
|
|
|
|
|
78
|
|
|
$matchers[] = function ($v) use ($extractor, $value) { |
|
79
|
|
|
return $extractor($v) == $value; |
|
80
|
|
|
}; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return function ($value) use ($matchers) { |
|
84
|
|
|
foreach ($matchers as $match) { |
|
85
|
|
|
if (!$match($value)) { |
|
86
|
|
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return true; |
|
91
|
|
|
}; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|