Skip to content

Commit

Permalink
IBX-7275: Added ValueObjectVisitor for Content Field (#79)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
ciastektk authored Dec 11, 2023
1 parent 9995600 commit 4315d42
Show file tree
Hide file tree
Showing 8 changed files with 359 additions and 298 deletions.
60 changes: 0 additions & 60 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -775,11 +775,6 @@ parameters:
count: 1
path: src/lib/Message.php

-
message: "#^Cannot access property \\$fieldTypeIdentifier on Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\ContentType\\\\FieldDefinition\\|null\\.$#"
count: 1
path: src/lib/Output/FieldTypeSerializer.php

-
message: "#^Method Ibexa\\\\Rest\\\\Output\\\\FieldTypeSerializer\\:\\:serializeFieldDefaultValue\\(\\) has no return type specified\\.$#"
count: 1
Expand All @@ -790,11 +785,6 @@ parameters:
count: 1
path: src/lib/Output/FieldTypeSerializer.php

-
message: "#^Method Ibexa\\\\Rest\\\\Output\\\\FieldTypeSerializer\\:\\:serializeFieldValue\\(\\) has no return type specified\\.$#"
count: 1
path: src/lib/Output/FieldTypeSerializer.php

-
message: "#^Method Ibexa\\\\Rest\\\\Output\\\\FieldTypeSerializer\\:\\:serializeHash\\(\\) has no return type specified\\.$#"
count: 1
Expand Down Expand Up @@ -3720,11 +3710,6 @@ parameters:
count: 1
path: src/lib/Server/Output/ValueObjectVisitor/Version.php

-
message: "#^Method Ibexa\\\\Rest\\\\Server\\\\Output\\\\ValueObjectVisitor\\\\Version\\:\\:visitField\\(\\) has no return type specified\\.$#"
count: 1
path: src/lib/Server/Output/ValueObjectVisitor/Version.php

-
message: "#^Method Ibexa\\\\Rest\\\\Server\\\\Output\\\\ValueObjectVisitor\\\\Version\\:\\:visitVersionAttributes\\(\\) has no return type specified\\.$#"
count: 1
Expand Down Expand Up @@ -7305,56 +7290,11 @@ parameters:
count: 1
path: tests/lib/Output/FieldTypeSerializerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:getFieldTypeSerializer\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/Output/FieldTypeSerializerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:getFieldTypeServiceMock\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/Output/FieldTypeSerializerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:getGeneratorMock\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/Output/FieldTypeSerializerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:testSerializeFieldDefaultValue\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/Output/FieldTypeSerializerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:testSerializeFieldSettings\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/Output/FieldTypeSerializerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:testSerializeFieldSettingsWithPostProcessing\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/Output/FieldTypeSerializerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:testSerializeFieldValue\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/Output/FieldTypeSerializerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:testSerializeFieldValueWithProcessor\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/Output/FieldTypeSerializerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:testSerializeValidatorConfiguration\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/Output/FieldTypeSerializerTest.php

-
message: "#^Method Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:testSerializeValidatorConfigurationWithPostProcessing\\(\\) has no return type specified\\.$#"
count: 1
path: tests/lib/Output/FieldTypeSerializerTest.php

-
message: "#^Property Ibexa\\\\Tests\\\\Rest\\\\Output\\\\FieldTypeSerializerTest\\:\\:\\$contentTypeMock has no type specified\\.$#"
count: 1
Expand Down
6 changes: 6 additions & 0 deletions src/bundle/Resources/config/value_object_visitors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,12 @@ services:
tags:
- { name: ibexa.rest.output.value_object.visitor, type: Ibexa\Rest\Server\Values\CreatedFieldDefinition }

Ibexa\Rest\Server\Output\ValueObjectVisitor\Field:
parent: Ibexa\Contracts\Rest\Output\ValueObjectVisitor
arguments: [ '@Ibexa\Rest\Output\FieldTypeSerializer' ]
tags:
- { name: ibexa.rest.output.value_object.visitor, type: Ibexa\Contracts\Core\Repository\Values\Content\Field }

Ibexa\Rest\Server\Output\ValueObjectVisitor\RestFieldDefinition:
parent: Ibexa\Contracts\Rest\Output\ValueObjectVisitor
class: Ibexa\Rest\Server\Output\ValueObjectVisitor\RestFieldDefinition
Expand Down
21 changes: 14 additions & 7 deletions src/lib/Output/FieldTypeSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,28 @@ public function __construct(FieldTypeService $fieldTypeService, FieldTypeProcess
}

/**
* Serializes the field value of $field through $generator.
* @deprecated 4.6.0 The "FieldTypeSerializer::serializeFieldValue()" method is deprecated, will be removed in 5.0.
* Use "FieldTypeSerializer::serializeContentFieldValue()" instead.
*
* @param \Ibexa\Contracts\Rest\Output\Generator $generator
* @param \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType $contentType
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Field $field
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
*/
public function serializeFieldValue(Generator $generator, ContentType $contentType, Field $field): void
{
$this->serializeContentFieldValue($generator, $field);
}

/**
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
*/
public function serializeFieldValue(Generator $generator, ContentType $contentType, Field $field)
public function serializeContentFieldValue(Generator $generator, Field $field): void
{
$this->serializeValue(
'fieldValue',
$generator,
$this->fieldTypeService->getFieldType(
$contentType->getFieldDefinition($field->fieldDefIdentifier)->fieldTypeIdentifier
$field->getFieldTypeIdentifier()
),
$field->value
$field->getValue()
);
}

Expand Down
53 changes: 53 additions & 0 deletions src/lib/Server/Output/ValueObjectVisitor/Field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Rest\Server\Output\ValueObjectVisitor;

use Ibexa\Contracts\Rest\Output\Generator;
use Ibexa\Contracts\Rest\Output\ValueObjectVisitor;
use Ibexa\Contracts\Rest\Output\Visitor;
use Ibexa\Rest\Output\FieldTypeSerializer;

final class Field extends ValueObjectVisitor
{
private FieldTypeSerializer $fieldTypeSerializer;

public function __construct(FieldTypeSerializer $fieldTypeSerializer)
{
$this->fieldTypeSerializer = $fieldTypeSerializer;
}

/**
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Field $data
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
*/
public function visit(Visitor $visitor, Generator $generator, $data): void
{
$generator->startHashElement('field');

$generator->startValueElement('id', $data->id);
$generator->endValueElement('id');

$generator->startValueElement('fieldDefinitionIdentifier', $data->fieldDefIdentifier);
$generator->endValueElement('fieldDefinitionIdentifier');

$generator->startValueElement('languageCode', $data->languageCode);
$generator->endValueElement('languageCode');

$generator->startValueElement('fieldTypeIdentifier', $data->fieldTypeIdentifier);
$generator->endValueElement('fieldTypeIdentifier');

$this->fieldTypeSerializer->serializeContentFieldValue(
$generator,
$data
);

$generator->endHashElement('field');
}
}
37 changes: 1 addition & 36 deletions src/lib/Server/Output/ValueObjectVisitor/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
*/
namespace Ibexa\Rest\Server\Output\ValueObjectVisitor;

use Ibexa\Contracts\Core\Repository\Values\Content\Field;
use Ibexa\Contracts\Core\Repository\Values\Content\Thumbnail;
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
use Ibexa\Contracts\Rest\Output\Generator;
use Ibexa\Contracts\Rest\Output\ValueObjectVisitor;
use Ibexa\Contracts\Rest\Output\Visitor;
Expand Down Expand Up @@ -57,44 +55,11 @@ public function visit(Visitor $visitor, Generator $generator, $data)
$generator->endObjectElement('Version');
}

/**
* Visits a single content field and generates its content.
*
* @param \Ibexa\Contracts\Rest\Output\Generator $generator
* @param \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType $contentType
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Field $field
*/
public function visitField(Generator $generator, ContentType $contentType, Field $field)
{
$generator->startHashElement('field');

$generator->startValueElement('id', $field->id);
$generator->endValueElement('id');

$generator->startValueElement('fieldDefinitionIdentifier', $field->fieldDefIdentifier);
$generator->endValueElement('fieldDefinitionIdentifier');

$generator->startValueElement('languageCode', $field->languageCode);
$generator->endValueElement('languageCode');

$generator->startValueElement('fieldTypeIdentifier', $field->fieldTypeIdentifier);
$generator->endValueElement('fieldTypeIdentifier');

$this->fieldTypeSerializer->serializeFieldValue(
$generator,
$contentType,
$field
);

$generator->endHashElement('field');
}

protected function visitVersionAttributes(Visitor $visitor, Generator $generator, VersionValue $data)
{
$content = $data->content;

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

$path = $data->path;
if ($path == null) {
Expand All @@ -115,7 +80,7 @@ protected function visitVersionAttributes(Visitor $visitor, Generator $generator
$generator->startHashElement('Fields');
$generator->startList('field');
foreach ($content->getFields() as $field) {
$this->visitField($generator, $contentType, $field);
$visitor->visitValueObject($field);
}
$generator->endList('field');
$generator->endHashElement('Fields');
Expand Down
Loading

0 comments on commit 4315d42

Please sign in to comment.