1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2015-present Ganbaro Digital Ltd |
5
|
|
|
* All rights reserved. |
6
|
|
|
* |
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
8
|
|
|
* modification, are permitted provided that the following conditions |
9
|
|
|
* are met: |
10
|
|
|
* |
11
|
|
|
* * Redistributions of source code must retain the above copyright |
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
13
|
|
|
* |
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
15
|
|
|
* notice, this list of conditions and the following disclaimer in |
16
|
|
|
* the documentation and/or other materials provided with the |
17
|
|
|
* distribution. |
18
|
|
|
* |
19
|
|
|
* * Neither the names of the copyright holders nor the names of his |
20
|
|
|
* contributors may be used to endorse or promote products derived |
21
|
|
|
* from this software without specific prior written permission. |
22
|
|
|
* |
23
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
25
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
26
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
27
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
28
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
29
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
30
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
31
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
32
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
33
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
35
|
|
|
* |
36
|
|
|
* @category Libraries |
37
|
|
|
* @package DataContainers/Filters |
38
|
|
|
* @author Stuart Herbert <[email protected]> |
39
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
40
|
|
|
* @copyright 2015-present Ganbaro Digital Ltd www.ganbarodigital.com |
41
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
42
|
|
|
* @link http://code.ganbarodigital.com/php-data-containers |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
namespace GanbaroDigital\DataContainers\Filters; |
46
|
|
|
|
47
|
|
|
use GanbaroDigital\DataContainers\Checks\IsReadableContainer; |
48
|
|
|
use GanbaroDigital\DataContainers\Exceptions\E4xx_UnsupportedType; |
49
|
|
|
use GanbaroDigital\DataContainers\Requirements\RequireReadableContainer; |
50
|
|
|
use GanbaroDigital\DataContainers\ValueBuilders\DescendDotNotationPath; |
51
|
|
|
use GanbaroDigital\Reflection\Checks\IsAssignable; |
52
|
|
|
use GanbaroDigital\Reflection\Checks\IsIndexable; |
53
|
|
|
use GanbaroDigital\Reflection\Requirements\RequireAssignable; |
54
|
|
|
use GanbaroDigital\Reflection\Requirements\RequireIndexable; |
55
|
|
|
use GanbaroDigital\Reflection\ValueBuilders\FirstMethodMatchingType; |
56
|
|
|
use GanbaroDigital\Reflection\ValueBuilders\SimpleType; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* this is a very simple wrapper around the DescendDotNotationPath |
60
|
|
|
* value builder |
61
|
|
|
*/ |
62
|
|
|
class FilterDotNotationPath |
63
|
|
|
{ |
64
|
|
|
/** |
65
|
|
|
* extract a value from an array, using dot.notation.support |
66
|
|
|
* |
67
|
|
|
* @param array $arr |
68
|
|
|
* the array to extract from |
69
|
|
|
* @param string $index |
70
|
|
|
* the dot.notation.support path to walk |
71
|
|
|
* @return mixed |
72
|
|
|
* whatever we find when we walk the path |
73
|
|
|
*/ |
74
|
|
|
public static function fromArray($arr, $index) |
75
|
|
|
{ |
76
|
|
|
// robustness! |
77
|
|
|
RequireIndexable::check($arr, E4xx_UnsupportedType::class); |
78
|
|
|
|
79
|
|
|
return DescendDotNotationPath::intoArray($arr, $index); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* extract a value from an object, using dot.notation.support |
84
|
|
|
* |
85
|
|
|
* @param object $obj |
86
|
|
|
* the object to extract from |
87
|
|
|
* @param string $property |
88
|
|
|
* the dot.notation.support path to walk |
89
|
|
|
* @return mixed |
90
|
|
|
* whatever we find when we walk the path |
91
|
|
|
*/ |
92
|
|
|
public static function fromObject($obj, $property) |
93
|
|
|
{ |
94
|
|
|
// robustness! |
95
|
|
|
RequireAssignable::check($obj, E4xx_UnsupportedType::class); |
96
|
|
|
|
97
|
|
|
return DescendDotNotationPath::intoObject($obj, $property); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* extract a value from a container, using dot.notation.support |
102
|
|
|
* |
103
|
|
|
* @param mixed $item |
104
|
|
|
* the container to extract from |
105
|
|
|
* @param string $path |
106
|
|
|
* the dot.notation.support path to walk |
107
|
|
|
* @return mixed |
108
|
|
|
* whatever we find when we walk the path |
109
|
|
|
*/ |
110
|
|
|
public static function from($item, $path) |
111
|
|
|
{ |
112
|
|
|
// robustness! |
113
|
|
|
RequireReadableContainer::check($item, E4xx_UnsupportedType::class); |
114
|
|
|
|
115
|
|
|
if (IsAssignable::check($item)) { |
116
|
|
|
return self::fromObject($item, $path); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return self::fromArray($item, $path); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* extract a value from a variable, using dot.notation.support |
124
|
|
|
* |
125
|
|
|
* @deprecated since 2.2.0 |
126
|
|
|
* @codeCoverageIgnore |
127
|
|
|
* |
128
|
|
|
* @param object|array $item |
129
|
|
|
* the variable to extract from |
130
|
|
|
* @param string $property |
131
|
|
|
* the dot.notation.support path to walk |
132
|
|
|
* @return mixed |
133
|
|
|
* whatever we find when we walk the path |
134
|
|
|
*/ |
135
|
|
|
public static function fromMixed($item, $property) |
136
|
|
|
{ |
137
|
|
|
return self::from($item, $property); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* extract a value from a variable, using dot.notation.support |
142
|
|
|
* |
143
|
|
|
* @param object|array $item |
144
|
|
|
* the variable to extract from |
145
|
|
|
* @param string $property |
146
|
|
|
* the dot.notation.support path to walk |
147
|
|
|
* @return mixed |
148
|
|
|
* whatever we find when we walk the path |
149
|
|
|
*/ |
150
|
|
|
public function __invoke($item, $property) |
151
|
|
|
{ |
152
|
|
|
return self::from($item, $property); |
153
|
|
|
} |
154
|
|
|
} |