-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCommonAbstract.php
64 lines (61 loc) · 2.33 KB
/
CommonAbstract.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
<?php
/**
* Common abstract for the common classes package (last modified: 2024.09.20).
*
* This file is a part of the "common classes package", utilised by a number of
* packages and projects, including CIDRAM and phpMussel.
* @link https://github.com/Maikuolan/Common
*
* License: GNU/GPLv2
* @see LICENSE.txt
*
* "COMMON CLASSES PACKAGE" COPYRIGHT 2019 and beyond by Caleb Mazalevskis.
*/
namespace Maikuolan\Common;
abstract class CommonAbstract
{
/**
* @var string Common Classes Package tag/release version.
* @link https://github.com/Maikuolan/Common/tags
*/
public const VERSION = '2.12.3';
/**
* Traverse data path.
*
* @param mixed $Data The data to traverse.
* @param string|array $Path The path to traverse.
* @param bool $AllowNonScalar Whether to allow non-scalar returns.
* @param bool $AllowMethodCalls Whether to allow method calls.
* @return mixed The traversed data, or an empty string on failure.
*/
public function dataTraverse(&$Data, $Path = [], bool $AllowNonScalar = false, bool $AllowMethodCalls = false)
{
if (!is_array($Path)) {
$Path = preg_split('~(?<!\\\\)\\.~', $Path) ?: [];
}
$Segment = array_shift($Path);
if ($Segment === null || strlen($Segment) === 0) {
return $AllowNonScalar || is_scalar($Data) ? $Data : '';
}
$Segment = str_replace('\.', '.', $Segment);
if (is_array($Data)) {
return isset($Data[$Segment]) ? $this->dataTraverse($Data[$Segment], $Path, $AllowNonScalar, $AllowMethodCalls) : '';
}
if (is_object($Data)) {
if (property_exists($Data, $Segment)) {
return $this->dataTraverse($Data->$Segment, $Path, $AllowNonScalar, $AllowMethodCalls);
}
if ($AllowMethodCalls && method_exists($Data, $Segment)) {
$Working = $Data->{$Segment}(...$Path);
return $this->dataTraverse($Working, [], $AllowNonScalar);
}
}
if (is_string($Data)) {
if (preg_match('~^(?:trim|str(?:tolower|toupper|len))\\(\\)~i', $Segment)) {
$Segment = substr($Segment, 0, -2);
$Data = $Segment($Data);
}
}
return $this->dataTraverse($Data, $Path, $AllowNonScalar, $AllowMethodCalls);
}
}