Skip to content

Commit 4315d42

Browse files
authored
IBX-7275: Added ValueObjectVisitor for Content Field (#79)
For more details see https://issues.ibexa.co/browse/IBX-7275 and #79 Key changes: * Added dedicated `ValueObjectVisitor` for `Content` `Field` objects, that allows to generate correct REST output for Fields in a reusable way. * Added new method `serializeContentFieldValue` to `FieldTypeSerializer` to serialize field value based on passed Field object, avoiding passing redundant `ContentType` instance. * Deprecated `FieldTypeSerializer::serializeFieldValue` in favor of `serializeContentFieldValue`.
1 parent 9995600 commit 4315d42

File tree

8 files changed

+359
-298
lines changed

8 files changed

+359
-298
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -775,11 +775,6 @@ parameters:
775775
count: 1
776776
path: src/lib/Message.php
777777

778-
-
779-
message: "#^Cannot access property \\$fieldTypeIdentifier on Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\ContentType\\\\FieldDefinition\\|null\\.$#"
780-
count: 1
781-
path: src/lib/Output/FieldTypeSerializer.php
782-
783778
-
784779
message: "#^Method Ibexa\\\\Rest\\\\Output\\\\FieldTypeSerializer\\:\\:serializeFieldDefaultValue\\(\\) has no return type specified\\.$#"
785780
count: 1
@@ -790,11 +785,6 @@ parameters:
790785
count: 1
791786
path: src/lib/Output/FieldTypeSerializer.php
792787

793-
-
794-
message: "#^Method Ibexa\\\\Rest\\\\Output\\\\FieldTypeSerializer\\:\\:serializeFieldValue\\(\\) has no return type specified\\.$#"
795-
count: 1
796-
path: src/lib/Output/FieldTypeSerializer.php
797-
798788
-
799789
message: "#^Method Ibexa\\\\Rest\\\\Output\\\\FieldTypeSerializer\\:\\:serializeHash\\(\\) has no return type specified\\.$#"
800790
count: 1
@@ -3720,11 +3710,6 @@ parameters:
37203710
count: 1
37213711
path: src/lib/Server/Output/ValueObjectVisitor/Version.php
37223712

3723-
-
3724-
message: "#^Method Ibexa\\\\Rest\\\\Server\\\\Output\\\\ValueObjectVisitor\\\\Version\\:\\:visitField\\(\\) has no return type specified\\.$#"
3725-
count: 1
3726-
path: src/lib/Server/Output/ValueObjectVisitor/Version.php
3727-
37283713
-
37293714
message: "#^Method Ibexa\\\\Rest\\\\Server\\\\Output\\\\ValueObjectVisitor\\\\Version\\:\\:visitVersionAttributes\\(\\) has no return type specified\\.$#"
37303715
count: 1
@@ -7305,56 +7290,11 @@ parameters:
73057290
count: 1
73067291
path: tests/lib/Output/FieldTypeSerializerTest.php
73077292

7308-
-
7309-
message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:getFieldTypeSerializer\\(\\) has no return type specified\\.$#"
7310-
count: 1
7311-
path: tests/lib/Output/FieldTypeSerializerTest.php
7312-
73137293
-
73147294
message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:getFieldTypeServiceMock\\(\\) has no return type specified\\.$#"
73157295
count: 1
73167296
path: tests/lib/Output/FieldTypeSerializerTest.php
73177297

7318-
-
7319-
message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:getGeneratorMock\\(\\) has no return type specified\\.$#"
7320-
count: 1
7321-
path: tests/lib/Output/FieldTypeSerializerTest.php
7322-
7323-
-
7324-
message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:testSerializeFieldDefaultValue\\(\\) has no return type specified\\.$#"
7325-
count: 1
7326-
path: tests/lib/Output/FieldTypeSerializerTest.php
7327-
7328-
-
7329-
message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:testSerializeFieldSettings\\(\\) has no return type specified\\.$#"
7330-
count: 1
7331-
path: tests/lib/Output/FieldTypeSerializerTest.php
7332-
7333-
-
7334-
message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:testSerializeFieldSettingsWithPostProcessing\\(\\) has no return type specified\\.$#"
7335-
count: 1
7336-
path: tests/lib/Output/FieldTypeSerializerTest.php
7337-
7338-
-
7339-
message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:testSerializeFieldValue\\(\\) has no return type specified\\.$#"
7340-
count: 1
7341-
path: tests/lib/Output/FieldTypeSerializerTest.php
7342-
7343-
-
7344-
message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:testSerializeFieldValueWithProcessor\\(\\) has no return type specified\\.$#"
7345-
count: 1
7346-
path: tests/lib/Output/FieldTypeSerializerTest.php
7347-
7348-
-
7349-
message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:testSerializeValidatorConfiguration\\(\\) has no return type specified\\.$#"
7350-
count: 1
7351-
path: tests/lib/Output/FieldTypeSerializerTest.php
7352-
7353-
-
7354-
message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:testSerializeValidatorConfigurationWithPostProcessing\\(\\) has no return type specified\\.$#"
7355-
count: 1
7356-
path: tests/lib/Output/FieldTypeSerializerTest.php
7357-
73587298
-
73597299
message: "#^Property Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:\\$contentTypeMock has no type specified\\.$#"
73607300
count: 1

src/bundle/Resources/config/value_object_visitors.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,12 @@ services:
407407
tags:
408408
- { name: ibexa.rest.output.value_object.visitor, type: Ibexa\Rest\Server\Values\CreatedFieldDefinition }
409409

410+
Ibexa\Rest\Server\Output\ValueObjectVisitor\Field:
411+
parent: Ibexa\Contracts\Rest\Output\ValueObjectVisitor
412+
arguments: [ '@Ibexa\Rest\Output\FieldTypeSerializer' ]
413+
tags:
414+
- { name: ibexa.rest.output.value_object.visitor, type: Ibexa\Contracts\Core\Repository\Values\Content\Field }
415+
410416
Ibexa\Rest\Server\Output\ValueObjectVisitor\RestFieldDefinition:
411417
parent: Ibexa\Contracts\Rest\Output\ValueObjectVisitor
412418
class: Ibexa\Rest\Server\Output\ValueObjectVisitor\RestFieldDefinition

src/lib/Output/FieldTypeSerializer.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,28 @@ public function __construct(FieldTypeService $fieldTypeService, FieldTypeProcess
4141
}
4242

4343
/**
44-
* Serializes the field value of $field through $generator.
44+
* @deprecated 4.6.0 The "FieldTypeSerializer::serializeFieldValue()" method is deprecated, will be removed in 5.0.
45+
* Use "FieldTypeSerializer::serializeContentFieldValue()" instead.
4546
*
46-
* @param \Ibexa\Contracts\Rest\Output\Generator $generator
47-
* @param \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType $contentType
48-
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Field $field
47+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
48+
*/
49+
public function serializeFieldValue(Generator $generator, ContentType $contentType, Field $field): void
50+
{
51+
$this->serializeContentFieldValue($generator, $field);
52+
}
53+
54+
/**
55+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
4956
*/
50-
public function serializeFieldValue(Generator $generator, ContentType $contentType, Field $field)
57+
public function serializeContentFieldValue(Generator $generator, Field $field): void
5158
{
5259
$this->serializeValue(
5360
'fieldValue',
5461
$generator,
5562
$this->fieldTypeService->getFieldType(
56-
$contentType->getFieldDefinition($field->fieldDefIdentifier)->fieldTypeIdentifier
63+
$field->getFieldTypeIdentifier()
5764
),
58-
$field->value
65+
$field->getValue()
5966
);
6067
}
6168

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Rest\Server\Output\ValueObjectVisitor;
10+
11+
use Ibexa\Contracts\Rest\Output\Generator;
12+
use Ibexa\Contracts\Rest\Output\ValueObjectVisitor;
13+
use Ibexa\Contracts\Rest\Output\Visitor;
14+
use Ibexa\Rest\Output\FieldTypeSerializer;
15+
16+
final class Field extends ValueObjectVisitor
17+
{
18+
private FieldTypeSerializer $fieldTypeSerializer;
19+
20+
public function __construct(FieldTypeSerializer $fieldTypeSerializer)
21+
{
22+
$this->fieldTypeSerializer = $fieldTypeSerializer;
23+
}
24+
25+
/**
26+
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Field $data
27+
*
28+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
29+
*/
30+
public function visit(Visitor $visitor, Generator $generator, $data): void
31+
{
32+
$generator->startHashElement('field');
33+
34+
$generator->startValueElement('id', $data->id);
35+
$generator->endValueElement('id');
36+
37+
$generator->startValueElement('fieldDefinitionIdentifier', $data->fieldDefIdentifier);
38+
$generator->endValueElement('fieldDefinitionIdentifier');
39+
40+
$generator->startValueElement('languageCode', $data->languageCode);
41+
$generator->endValueElement('languageCode');
42+
43+
$generator->startValueElement('fieldTypeIdentifier', $data->fieldTypeIdentifier);
44+
$generator->endValueElement('fieldTypeIdentifier');
45+
46+
$this->fieldTypeSerializer->serializeContentFieldValue(
47+
$generator,
48+
$data
49+
);
50+
51+
$generator->endHashElement('field');
52+
}
53+
}

src/lib/Server/Output/ValueObjectVisitor/Version.php

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
*/
77
namespace Ibexa\Rest\Server\Output\ValueObjectVisitor;
88

9-
use Ibexa\Contracts\Core\Repository\Values\Content\Field;
109
use Ibexa\Contracts\Core\Repository\Values\Content\Thumbnail;
11-
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
1210
use Ibexa\Contracts\Rest\Output\Generator;
1311
use Ibexa\Contracts\Rest\Output\ValueObjectVisitor;
1412
use Ibexa\Contracts\Rest\Output\Visitor;
@@ -57,44 +55,11 @@ public function visit(Visitor $visitor, Generator $generator, $data)
5755
$generator->endObjectElement('Version');
5856
}
5957

60-
/**
61-
* Visits a single content field and generates its content.
62-
*
63-
* @param \Ibexa\Contracts\Rest\Output\Generator $generator
64-
* @param \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType $contentType
65-
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Field $field
66-
*/
67-
public function visitField(Generator $generator, ContentType $contentType, Field $field)
68-
{
69-
$generator->startHashElement('field');
70-
71-
$generator->startValueElement('id', $field->id);
72-
$generator->endValueElement('id');
73-
74-
$generator->startValueElement('fieldDefinitionIdentifier', $field->fieldDefIdentifier);
75-
$generator->endValueElement('fieldDefinitionIdentifier');
76-
77-
$generator->startValueElement('languageCode', $field->languageCode);
78-
$generator->endValueElement('languageCode');
79-
80-
$generator->startValueElement('fieldTypeIdentifier', $field->fieldTypeIdentifier);
81-
$generator->endValueElement('fieldTypeIdentifier');
82-
83-
$this->fieldTypeSerializer->serializeFieldValue(
84-
$generator,
85-
$contentType,
86-
$field
87-
);
88-
89-
$generator->endHashElement('field');
90-
}
91-
9258
protected function visitVersionAttributes(Visitor $visitor, Generator $generator, VersionValue $data)
9359
{
9460
$content = $data->content;
9561

9662
$versionInfo = $content->getVersionInfo();
97-
$contentType = $data->contentType;
9863

9964
$path = $data->path;
10065
if ($path == null) {
@@ -115,7 +80,7 @@ protected function visitVersionAttributes(Visitor $visitor, Generator $generator
11580
$generator->startHashElement('Fields');
11681
$generator->startList('field');
11782
foreach ($content->getFields() as $field) {
118-
$this->visitField($generator, $contentType, $field);
83+
$visitor->visitValueObject($field);
11984
}
12085
$generator->endList('field');
12186
$generator->endHashElement('Fields');

0 commit comments

Comments
 (0)