-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathExtractTrait.php
154 lines (135 loc) · 4.34 KB
/
ExtractTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Collection;
use Closure;
use Traversable;
/**
* Provides utility protected methods for extracting a property or column
* from an array or object.
*/
trait ExtractTrait
{
/**
* Returns a callable that can be used to extract a property or column from
* an array or object based on a dot separated path.
*
* @param string|callable $path A dot separated path of column to follow
* so that the final one can be returned or a callable that will take care
* of doing that.
* @return callable
*/
protected function _propertyExtractor($path): callable
{
if (!is_string($path)) {
return $path;
}
$parts = explode('.', $path);
if (strpos($path, '{*}') !== false) {
return function ($element) use ($parts) {
return $this->_extract($element, $parts);
};
}
return function ($element) use ($parts) {
return $this->_simpleExtract($element, $parts);
};
}
/**
* Returns a column from $data that can be extracted
* by iterating over the column names contained in $path.
* It will return arrays for elements in represented with `{*}`
*
* @param array|\ArrayAccess $data Data.
* @param string[] $parts Path to extract from.
* @return mixed
*/
protected function _extract($data, array $parts)
{
$value = null;
$collectionTransform = false;
foreach ($parts as $i => $column) {
if ($column === '{*}') {
$collectionTransform = true;
continue;
}
if (
$collectionTransform &&
!(
$data instanceof Traversable ||
is_array($data)
)
) {
return null;
}
if ($collectionTransform) {
$rest = implode('.', array_slice($parts, $i));
return (new Collection($data))->extract($rest);
}
if (!isset($data[$column])) {
return null;
}
$value = $data[$column];
$data = $value;
}
return $value;
}
/**
* Returns a column from $data that can be extracted
* by iterating over the column names contained in $path
*
* @param array|\ArrayAccess $data Data.
* @param string[] $parts Path to extract from.
* @return mixed
*/
protected function _simpleExtract($data, array $parts)
{
$value = null;
foreach ($parts as $column) {
if (!isset($data[$column])) {
return null;
}
$value = $data[$column];
$data = $value;
}
return $value;
}
/**
* Returns a callable that receives a value and will return whether or not
* it matches certain condition.
*
* @param array $conditions A key-value list of conditions to match where the
* key is the property path to get from the current item and the value is the
* value to be compared the item with.
* @return \Closure
*/
protected function _createMatcherFilter(array $conditions): Closure
{
$matchers = [];
foreach ($conditions as $property => $value) {
$extractor = $this->_propertyExtractor($property);
$matchers[] = function ($v) use ($extractor, $value) {
return $extractor($v) == $value;
};
}
return function ($value) use ($matchers) {
foreach ($matchers as $match) {
if (!$match($value)) {
return false;
}
}
return true;
};
}
}