-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathJsonSerializeNodeTreeTrait.php
More file actions
84 lines (74 loc) · 3.4 KB
/
JsonSerializeNodeTreeTrait.php
File metadata and controls
84 lines (74 loc) · 3.4 KB
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
<?php
namespace Flowpack\NodeTemplates\Tests\Functional;
use Neos\ContentRepository\Core\ContentRepository;
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindReferencesFilter;
use Neos\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\ContentRepository\Core\Projection\ContentGraph\Subtree;
use Neos\ContentRepository\Core\Projection\NodeHiddenState\NodeHiddenStateFinder;
use Neos\Utility\ObjectAccess;
trait JsonSerializeNodeTreeTrait
{
private readonly ContentRepository $contentRepository;
private function jsonSerializeNodeAndDescendents(Subtree $subtree): array
{
$hiddenStateFinder = $this->contentRepository->projectionState(NodeHiddenStateFinder::class);
$node = $subtree->node;
$subgraph = $this->contentRepository->getContentGraph()->getSubgraph(
$node->subgraphIdentity->contentStreamId,
$node->subgraphIdentity->dimensionSpacePoint,
$node->subgraphIdentity->visibilityConstraints
);
$references = $subgraph->findReferences($node->nodeAggregateId, FindReferencesFilter::create());
$referencesArray = [];
foreach ($references as $reference) {
$referencesArray[$reference->name->value] ??= [];
$referencesArray[$reference->name->value][] = array_filter([
'node' => sprintf('Node(%s, %s)', $reference->node->nodeAggregateId->value, $reference->node->nodeTypeName->value),
'properties' => iterator_to_array($reference->properties ?? [])
]);
}
return array_filter([
'nodeTypeName' => $node->nodeTypeName,
'nodeName' => $node->classification->isTethered() ? $node->nodeName : null,
'isDisabled' => $hiddenStateFinder->findHiddenState(
$node->subgraphIdentity->contentStreamId,
$node->originDimensionSpacePoint->toDimensionSpacePoint(),
$node->nodeAggregateId
)->isHidden,
'properties' => $this->serializeValuesInArray(
iterator_to_array($node->properties->getIterator())
),
'references' => $referencesArray,
'childNodes' => array_map(
fn ($subtree) => $this->jsonSerializeNodeAndDescendents($subtree),
$subtree->children
)
]);
}
private function serializeValuesInArray(array $array): array
{
foreach ($array as $key => $value) {
if (is_array($value)) {
$value = $this->serializeValuesInArray($value);
} elseif ($value instanceof Node) {
$value = sprintf('Node(%s, %s)', $value->nodeAggregateId->value, $value->nodeTypeName->value);
} elseif ($value instanceof \JsonSerializable) {
$value = $value->jsonSerialize();
if (is_array($value)) {
$value = $this->serializeValuesInArray($value);
}
} elseif (is_object($value)) {
try {
$id = ObjectAccess::getProperty($value, 'Persistence_Object_Identifier', true);
} catch (\Exception) {
$id = null;
}
$value = sprintf('object(%s%s)', get_class($value), $id ? (sprintf(', %s', $id)) : '');
} else {
continue;
}
$array[$key] = $value;
}
return $array;
}
}