1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Longman\LaravelLodash\Testing; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
use function array_shift; |
10
|
|
|
use function explode; |
11
|
|
|
use function is_callable; |
12
|
|
|
use function str_contains; |
13
|
|
|
use function str_starts_with; |
14
|
|
|
use function trim; |
15
|
|
|
|
16
|
|
|
abstract class DataStructuresBuilder |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Caller object instance, where defined data structure getters like caller::getUserStructure() |
20
|
|
|
*/ |
21
|
|
|
protected static object $caller; |
|
|
|
|
22
|
|
|
|
23
|
|
|
public static function includeNestedRelations(object $caller, array &$item, array $relations): void |
24
|
|
|
{ |
25
|
|
|
if (empty($relations)) { |
26
|
|
|
return; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
self::setCallerObject($caller); |
30
|
|
|
|
31
|
|
|
foreach ($relations as $relation) { |
32
|
|
|
$parentRelations = explode('.', $relation); |
33
|
|
|
self::includeNestedRelation($item, $parentRelations); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected static function includeNestedRelation(array &$item, array $parentRelations = []): void |
38
|
|
|
{ |
39
|
|
|
$currentRelation = array_shift($parentRelations); |
40
|
|
|
/* check if we reached bottom of the relation tree, if so add new relation to the tree*/ |
41
|
|
|
if (empty($parentRelations)) { |
42
|
|
|
// Set relation collection by default to false |
43
|
|
|
$isRelationCollection = false; |
44
|
|
|
if (str_starts_with($currentRelation, '[')) { |
45
|
|
|
$currentRelation = trim($currentRelation, '[]'); |
46
|
|
|
$isRelationCollection = true; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (str_contains($currentRelation, ':')) { |
50
|
|
|
[$relationKey, $relationItem] = explode(':', $currentRelation); |
51
|
|
|
} else { |
52
|
|
|
$relationKey = $currentRelation; |
53
|
|
|
$relationItem = $currentRelation; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($isRelationCollection) { |
57
|
|
|
$item['relationships'][$relationKey]['data'][0] = self::getItemStructure($relationItem); |
58
|
|
|
} else { |
59
|
|
|
$item['relationships'][$relationKey]['data'] = self::getItemStructure($relationItem); |
60
|
|
|
} |
61
|
|
|
} else { |
62
|
|
|
// get to the bottom of the relation tree |
63
|
|
|
if (str_starts_with($currentRelation, '[')) { |
64
|
|
|
$currentRelation = trim($currentRelation, '[]'); |
65
|
|
|
self::includeNestedRelation($item['relationships'][$currentRelation]['data'][0], $parentRelations); |
66
|
|
|
} else { |
67
|
|
|
self::includeNestedRelation($item['relationships'][$currentRelation]['data'], $parentRelations); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected static function getItemStructure(string $relationItem): array |
73
|
|
|
{ |
74
|
|
|
$caller = self::getCallerObject(); |
75
|
|
|
$method = 'get' . $relationItem . 'Structure'; |
76
|
|
|
if (! is_callable([$caller, $method])) { |
77
|
|
|
throw new InvalidArgumentException('Getter method for structure "' . $relationItem . '" does not exists'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $caller->$method(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected static function setCallerObject(object $caller): void |
84
|
|
|
{ |
85
|
|
|
self::$caller = $caller; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected static function getCallerObject(): object |
89
|
|
|
{ |
90
|
|
|
return self::$caller; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|