This repository was archived by the owner on Dec 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathParser.php
More file actions
164 lines (135 loc) · 5.22 KB
/
Parser.php
File metadata and controls
164 lines (135 loc) · 5.22 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace Bolt\Extension\Bolt\JsonApi\Parser;
use Bolt\Configuration\ResourceManager;
use Bolt\Extension\Bolt\JsonApi\Config\Config;
use Bolt\Extension\Bolt\JsonApi\Converter\Parameter\Type\Fields;
use Bolt\Extension\Bolt\JsonApi\Parser\Field\FieldCollection;
use Bolt\Extension\Bolt\JsonApi\Parser\Field\FieldFactory;
use Bolt\Storage\Entity\Relations;
use Bolt\Storage\Mapping\MetadataDriver;
use Bolt\Users;
class Parser
{
/** @var Config $config */
protected $config;
/** @var ResourceManager $resourceManager */
protected $resourceManager;
/** @var MetadataDriver $metadata */
protected $metadata;
/** @var Users $users */
protected $users;
public function __construct(Config $config, ResourceManager $resourceManager, MetadataDriver $metadata, Users $users)
{
$this->config = $config;
$this->resourceManager = $resourceManager;
$this->metadata = $metadata;
$this->users = $users;
}
public function parseItem($item, $fields = [])
{
$contentType = (string) $item->getContenttype();
if (empty($fields)) {
$fields = array_keys($item->getContenttype()['fields']);
//$fields = array_keys($item->_fields);
}
// Both 'id' and 'type' are always required. So remove them from $fields.
// The remaining $fields go into 'attributes'.
if (($key = array_search('id', $fields)) !== false) {
unset($fields[$key]);
}
$id = $item->getId();
$values = [
'id' => strval($id),
'type' => $contentType,
];
$fields = array_unique($fields);
//Get field type information
$metadata = $this->metadata->getClassMetadata($contentType);
/** @var FieldCollection $fieldCollection */
$fieldCollection = FieldFactory::build(
$metadata,
$this->resourceManager,
$this->config,
$fields,
$item
);
/** @var array $attributes */
$attributes = $fieldCollection->getAttributes();
if (!empty($attributes)) {
// Check for TemplateFields and show it properly
if (isset($attributes['templatefields']) && !empty($attributes['templatefields'])) {
$attributes['templatefields'] = $attributes['templatefields']->jsonSerialize();
}
// Recursively walk the array..
array_walk_recursive($attributes, function (&$item) {
// Make sure that any \Twig_Markup objects are cast to plain strings.
if ($item instanceof \Twig_Markup) {
$item = $item->__toString();
}
// Handle replacements.
$replacements = $this->config->getReplacements();
if (!empty($replacements)) {
foreach ($replacements as $from => $to) {
$item = str_replace($from, $to, $item);
}
}
});
$values['attributes'] = $attributes;
}
if ($this->config->isEnableDisplayNames() && isset($values['attributes']['ownerid'])) {
$ownerid = $values['attributes']['ownerid'];
$owner = $this->users->getUser($ownerid);
if ($owner) {
$values['attributes']['ownerdisplayname'] = $owner['displayname'];
}
}
$values['links'] = [
'self' => sprintf('%s/%s/%s', $this->config->getBasePath(), $contentType, $id),
];
// todo: Since Bolt relationships are a bit _different_ than the ones in
// relational databases, I am not sure if we need to do an
// additional check for `multiple` = true|false in the definitions
// in `contenttypes.yml`.
//
// todo: Depending on multiple, empty relationships need a null or [],
// if they don't exist.
if (count($item->getRelation()) > 0) {
$relationships = [];
foreach ($item->getRelation() as $relatedType) {
$data = [];
$relationships[] = $this->relatedJSONParser($relatedType, $data, $contentType);
}
$values['relationships'] = $relationships;
}
return $values;
}
/**
* @param Relations $relatedType
*/
protected function relatedJSONParser($relatedType, $data, $contentType)
{
$toID = $relatedType->getToId();
$fromID = $relatedType->getFromId();
$fromType = $relatedType->getFromContenttype();
$toType = $relatedType->getToContenttype();
if ($contentType === $fromType) {
$toContentType = $toType;
$id = $toID;
} else {
$toContentType = $fromType;
$id = $fromID;
}
$data[] = [
'type' => $toContentType,
'id' => $id,
];
$relationships[$toContentType] = [
'links' => [
'self' => $this->config->getBasePath() . "/$toContentType/$id",
'related' => $this->config->getBasePath() . "/$toContentType/$id/$contentType"
],
'data' => $data,
];
return $relationships;
}
}