1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Netgen\InformationCollection\Core\Mapper; |
6
|
|
|
|
7
|
|
|
use DateTimeImmutable; |
8
|
|
|
use DateTimeInterface; |
9
|
|
|
use eZ\Publish\API\Repository\Exceptions\NotFoundException; |
10
|
|
|
use eZ\Publish\API\Repository\Repository; |
11
|
|
|
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition; |
12
|
|
|
use eZ\Publish\API\Repository\Values\Content\Content as APIContent; |
13
|
|
|
use Netgen\InformationCollection\API\Value\Attribute; |
14
|
|
|
use Netgen\InformationCollection\API\Value\AttributeValue; |
15
|
|
|
use Netgen\InformationCollection\API\Value\Collection; |
16
|
|
|
use Netgen\InformationCollection\API\Value\Content; |
17
|
|
|
use Netgen\InformationCollection\Doctrine\Entity\EzInfoCollection; |
18
|
|
|
use Netgen\InformationCollection\Doctrine\Entity\EzInfoCollectionAttribute; |
19
|
|
|
|
20
|
|
|
final class DomainObjectMapper |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var \eZ\Publish\API\Repository\Repository |
24
|
|
|
*/ |
25
|
|
|
protected $repository; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \eZ\Publish\API\Repository\ContentService |
29
|
|
|
*/ |
30
|
|
|
protected $contentService; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \eZ\Publish\API\Repository\ContentTypeService |
34
|
|
|
*/ |
35
|
|
|
protected $contentTypeService; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var \eZ\Publish\API\Repository\UserService |
39
|
|
|
*/ |
40
|
|
|
protected $userService; |
41
|
|
|
|
42
|
|
|
public function __construct(Repository $repository) |
43
|
|
|
{ |
44
|
|
|
$this->repository = $repository; |
45
|
|
|
$this->contentService = $repository->getContentService(); |
46
|
|
|
$this->contentTypeService = $repository->getContentTypeService(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function mapContent(array $data, EzInfoCollection $first, EzInfoCollection $last, int $childCount): Content |
50
|
|
|
{ |
51
|
|
|
$content = $this->contentService->loadContent((int) $data['content_id']); |
52
|
|
|
$contentType = $this->contentTypeService->loadContentType($content->contentInfo->contentTypeId); |
53
|
|
|
$hasLocation = empty($object['main_node_id']) ? false : true; |
|
|
|
|
54
|
|
|
|
55
|
|
|
return new Content( |
56
|
|
|
$content, |
57
|
|
|
$contentType, |
58
|
|
|
$this->mapCollection($first, []), |
59
|
|
|
$this->mapCollection($last, []), |
60
|
|
|
$childCount, |
61
|
|
|
$hasLocation |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function mapCollection(EzInfoCollection $collection, array $attributes): Collection |
66
|
|
|
{ |
67
|
|
|
$content = $this->contentService->loadContent($collection->getContentObjectId()); |
68
|
|
|
$contentType = $this->contentTypeService |
69
|
|
|
->loadContentType( |
70
|
|
|
$content->contentInfo->contentTypeId |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
$attributeValues = []; |
74
|
|
|
foreach ($attributes as $attr) { |
75
|
|
|
if (empty($contentType->fieldDefinitionsById[$attr->getContentClassAttributeId()])) { |
|
|
|
|
76
|
|
|
continue; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$attributeValues[] = $this->mapAttribute($attr, $content, $contentType->fieldDefinitionsById[$attr->getContentClassAttributeId()]); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$user = $this->getUser($collection->getCreatorId()); |
83
|
|
|
|
84
|
|
|
return new Collection( |
85
|
|
|
$collection->getId(), |
86
|
|
|
$content, |
87
|
|
|
$user, |
88
|
|
|
$this->getDateTime($collection->getCreated()), |
89
|
|
|
$this->getDateTime($collection->getModified()), |
90
|
|
|
$attributeValues |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function mapAttribute(EzInfoCollectionAttribute $attribute, APIContent $content, FieldDefinition $fieldDefinition): Attribute |
95
|
|
|
{ |
96
|
|
|
$classField = null; |
97
|
|
|
foreach ($content->getFields() as $field) { |
98
|
|
|
if ($field->id == $attribute->getContentObjectAttributeId()) { |
99
|
|
|
$classField = $field; |
100
|
|
|
break; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$value = new AttributeValue($attribute->getDataInt(), $attribute->getDataFloat(), $attribute->getDataText()); |
105
|
|
|
dump($classField); |
106
|
|
|
return new Attribute( |
107
|
|
|
$attribute->getId(), $content, $classField, $fieldDefinition, $value |
|
|
|
|
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function getUser($userId) |
112
|
|
|
{ |
113
|
|
|
try { |
114
|
|
|
return $this->repository |
115
|
|
|
->getUserService() |
116
|
|
|
->loadUser($userId); |
117
|
|
|
|
118
|
|
|
} catch (NotFoundException $exception) { |
|
|
|
|
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
protected function getDateTime(int $timestamp): DateTimeInterface |
124
|
|
|
{ |
125
|
|
|
$date = new DateTimeImmutable(); |
126
|
|
|
$date->setTimestamp($timestamp); |
127
|
|
|
|
128
|
|
|
return $date; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
This check looks for calls to
isset(...)
orempty()
on variables that are yet undefined. These calls will always produce the same result and can be removed.This is most likely caused by the renaming of a variable or the removal of a function/method parameter.