44 * @copyright 2017 Tom Butler <tom@r.je> | https://r.je/ *
55 * @license http://www.opensource.org/licenses/bsd-license.php BSD License *
66 * @version 1.2 */
7+
78namespace Transphporm \Parser ;
9+
810/** Holds the data used by `ValueParser` */
9- class ValueData {
11+ class ValueData
12+ {
1013 private $ data ;
1114
12- public function __construct ($ data ) {
15+ public function __construct ($ data )
16+ {
1317 $ this ->data = $ data ;
1418 }
1519
1620 //Read $key from array, $this->data = $this->data[$key] but also works for objects
17- private function traverseInto ($ key ) {
21+ private function traverseInto ($ key )
22+ {
1823 if (isset ($ this ->data ->{$ key })) $ this ->data = $ this ->data ->{$ key };
1924 else if ($ this ->isArray () && isset ($ this ->data [$ key ])) $ this ->data = $ this ->data [$ key ];
2025 }
2126
22- public function traverse ($ key , $ result ) {
27+ public function traverse ($ key , $ result )
28+ {
2329 if ($ key !== null ) $ this ->traverseInto ($ key );
2430 else {
2531 //But if the key is null, replace the data structure with the result of the last function call
@@ -31,47 +37,59 @@ public function traverse($key, $result) {
3137 }
3238 }
3339
34- private function isArray () {
40+ private function isArray ()
41+ {
3542 return is_array ($ this ->data ) || $ this ->data instanceof \ArrayAccess;
3643 }
3744
38- public function getData () {
45+ public function getData ()
46+ {
3947 return $ this ->data ;
4048 }
4149
42- public function read ($ value ) {
50+ public function read ($ value )
51+ {
4352 if ($ this ->isArray ()) {
4453 if (isset ($ this ->data [$ value ])) return $ this ->data [$ value ];
45- }
46- else if (isset ($ this ->data ->$ value )) return $ this ->data ->$ value ;
54+ } else if (isset ($ this ->data ->$ value )) return $ this ->data ->$ value ;
4755 else return null ;
4856 }
4957
50- public function call ($ func , $ args ) {
58+ public function call ($ func , $ args )
59+ {
5160 return $ this ->data ->$ func (...$ args );
5261 }
5362
54- public function methodExists ($ name ) {
55- return method_exists ($ this ->data , $ name );
63+ public function methodExists ($ name )
64+ {
65+ // PHP 8+ will throw a type error if not an object or string
66+ try {
67+ return method_exists ($ this ->data , $ name );
68+ } catch (\TypeError $ e ) {
69+ return false ;
70+ }
5671 }
5772
58- public function parseNested ($ parser , $ token , $ funcName ) {
73+ public function parseNested ($ parser , $ token , $ funcName )
74+ {
5975 $ args = $ parser ->parseTokens ($ token ['value ' ], $ this ->data );
6076 if ($ args [0 ] === $ this ->data ) $ args = [];
6177 return $ this ->callFuncOnObject ($ this ->data , $ funcName , $ args );
6278 }
6379
64- private function callFuncOnObject ($ obj , $ func , $ args ) {
80+ private function callFuncOnObject ($ obj , $ func , $ args )
81+ {
6582 if (isset ($ obj ->$ func ) && is_callable ($ obj ->$ func )) return call_user_func_array ($ obj ->$ func , $ args );
6683 else if (is_callable ([$ obj , $ func ])) return call_user_func_array ([$ obj , $ func ], $ args );
6784 else return false ;
6885 }
6986
70- public function extract ($ last , $ autoLookup , $ traversing ) {
87+ public function extract ($ last , $ autoLookup , $ traversing )
88+ {
7189 $ value = $ this ->read ($ last );
72- if ($ value !== null && ($ autoLookup || $ traversing ) ) {
90+ if ($ value !== null && ($ autoLookup || $ traversing )) {
7391 return $ value ;
7492 }
7593 throw new \UnexpectedValueException ('Not found ' );
7694 }
77- }
95+ }
0 commit comments