Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

AuthorConverter.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter;
8
9 use eZ\Publish\Core\FieldType\Author\Type as AuthorType;
10 use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter;
11 use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue;
12 use eZ\Publish\Core\FieldType\FieldSettings;
13 use eZ\Publish\SPI\Persistence\Content\FieldValue;
14 use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition;
15 use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition;
16 use DOMDocument;
17
18 class AuthorConverter implements Converter
19 {
29 public static function create()
30 {
31 return new self();
32 }
33
40 public function toStorageValue(FieldValue $value, StorageFieldValue $storageFieldValue)
41 {
42 $storageFieldValue->dataText = $this->generateXmlString($value->data);
43 $storageFieldValue->sortKeyString = $value->sortKey;
44 }
45
52 public function toFieldValue(StorageFieldValue $value, FieldValue $fieldValue)
53 {
54 $fieldValue->data = $this->restoreValueFromXmlString($value->dataText);
55 $fieldValue->sortKey = $value->sortKeyString;
56 }
57
64 public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef)
65 {
66 $fieldSettings = $fieldDef->fieldTypeConstraints->fieldSettings;
67
68 if ($fieldSettings !== null) {
69 $storageDef->dataInt1 = (int)$fieldSettings['defaultAuthor'];
70 }
71 }
72
79 public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef)
80 {
81 $fieldDef->fieldTypeConstraints->fieldSettings = new FieldSettings(
82 [
83 'defaultAuthor' => $storageDef->dataInt1 ?? AuthorType::DEFAULT_VALUE_EMPTY,
84 ]
85 );
86
87 $fieldDef->defaultValue->data = [];
88 }
89
99 public function getIndexColumn()
100 {
101 return 'sort_key_string';
102 }
103
111 private function generateXmlString(array $authorValue)
112 {
113 $doc = new DOMDocument('1.0', 'utf-8');
114
115 $root = $doc->createElement('ezauthor');
116 $doc->appendChild($root);
117
118 $authors = $doc->createElement('authors');
119 $root->appendChild($authors);
120
121 foreach ($authorValue as $author) {
122 $authorNode = $doc->createElement('author');
123 $authorNode->setAttribute('id', $author['id']);
124 $authorNode->setAttribute('name', $author['name']);
125 $authorNode->setAttribute('email', $author['email']);
126 $authors->appendChild($authorNode);
127 unset($authorNode);
128 }
129
130 return $doc->saveXML();
131 }
132
140 private function restoreValueFromXmlString($xmlString)
141 {
142 $dom = new DOMDocument('1.0', 'utf-8');
143 $authors = [];
144
145 if ($dom->loadXML($xmlString) === true) {
146 foreach ($dom->getElementsByTagName('author') as $author) {
147 $authors[] = [
148 'id' => $author->getAttribute('id'),
149 'name' => $author->getAttribute('name'),
150 'email' => $author->getAttribute('email'),
151 ];
152 }
153 }
154
155 return $authors;
156 }
157 }
eZ\Publish\Core\FieldType\Author\Type
Author field type.
Definition: Core/FieldType/Author/Type.php:22
eZ\Publish\Core\FieldType\FieldSettings
Container for field type specific properties.
Definition: FieldSettings.php:19
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\AuthorConverter
Definition: AuthorConverter.php:19
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\AuthorConverter\create
static create()
Factory for current class.
Definition: AuthorConverter.php:29
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\AuthorConverter\generateXmlString
generateXmlString(array $authorValue)
Generates XML string from $authorValue to be stored in storage engine.
Definition: AuthorConverter.php:111
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\AuthorConverter\toStorageValue
toStorageValue(FieldValue $value, StorageFieldValue $storageFieldValue)
Converts data from $value to $storageFieldValue.
Definition: AuthorConverter.php:40
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\AuthorConverter\toStorageFieldDefinition
toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef)
Converts field definition data in $fieldDef into $storageFieldDef.
Definition: AuthorConverter.php:64
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\AuthorConverter\restoreValueFromXmlString
restoreValueFromXmlString($xmlString)
Restores an author Value object from $xmlString.
Definition: AuthorConverter.php:140
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\AuthorConverter\toFieldValue
toFieldValue(StorageFieldValue $value, FieldValue $fieldValue)
Converts data from $value to $fieldValue.
Definition: AuthorConverter.php:52
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\AuthorConverter\toFieldDefinition
toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef)
Converts field definition data in $storageDef into $fieldDef.
Definition: AuthorConverter.php:79
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\AuthorConverter\getIndexColumn
getIndexColumn()
Returns the name of the index column in the attribute table.
Definition: AuthorConverter.php:99
eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition
Definition: StorageFieldDefinition.php:12
eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue
Definition: StorageFieldValue.php:12
eZ\Publish\SPI\Persistence\Content\FieldValue
Definition: FieldValue.php:12
eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition
Definition: SPI/Persistence/Content/Type/FieldDefinition.php:18
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter
Converter for field values in legacy storage.
Definition: Persistence/Legacy/Content/FieldValue/Converter.php:18
eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter
Definition: AuthorConverter.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.