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

FieldTypeContext.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Bundle\EzPublishCoreBundle\Features\Context;
8
9 use Behat\Behat\Context\Context;
10 use Behat\Gherkin\Node\TableNode;
11 use EzSystems\PlatformBehatBundle\Context\RepositoryContext;
12 use eZ\Publish\API\Repository\ContentTypeService;
13 use eZ\Publish\API\Repository\LocationService;
14 use eZ\Publish\API\Repository\ContentService;
15 use eZ\Publish\API\Repository\Repository;
16
20 class FieldTypeContext implements Context
21 {
22 use RepositoryContext;
23
27 const FIELD_TYPE_NOT_CREATED = -1;
28 const FIELD_TYPE_CREATED = 0;
29 const CONTENT_TYPE_CREATED = 1;
30 const FIELD_TYPE_ASSOCIATED = 2;
31 const CONTENT_TYPE_PUBLISHED = 3;
32 const CONTENT_PUBLISHED = 4;
33
37 const DEFAULT_LANGUAGE = 'eng-GB';
38
43 private $fieldConstructionObject = [
44 'contentType' => null,
45 'fieldType' => null,
46 'content' => null,
47 'objectState' => self::FIELD_TYPE_NOT_CREATED,
48 ];
49
51 private $fieldTypeInternalIdentifier = [
52 'integer' => 'ezinteger',
53 ];
54
56 private $validatorMappings = [
57 'integer' => 'IntegerValue',
58 ];
59
61 private $defaultValues = [
62 'integer' => 1,
63 ];
64
66 private $contentTypeService;
67
69 private $contentService;
70
72 private $locationService;
73
80 public function __construct(
81 Repository $repository,
82 ContentTypeService $contentTypeService,
83 ContentService $contentService,
84 LocationService $locationService
85 ) {
86 $this->setRepository($repository);
87 $this->contentTypeService = $contentTypeService;
88 $this->contentService = $contentService;
89 $this->locationService = $locationService;
90 }
91
99 public function getFieldTypeInternalIdentifier($identifier)
100 {
101 return $this->fieldTypeInternalIdentifier[$identifier];
102 }
103
111 public function getFieldValidator($field)
112 {
113 return $this->validatorMappings[$field];
114 }
115
123 public function createField($fieldType, $name = null, $required = false)
124 {
125 $fieldPosition = $this->getActualFieldPosition();
126 $name = ($name == null ? $fieldType : $name);
127 $fieldCreateStruct = $this->contentTypeService->newFieldDefinitionCreateStruct(
128 $name,
129 $this->fieldTypeInternalIdentifier[$fieldType]
130 );
131 $fieldCreateStruct->names = [self::DEFAULT_LANGUAGE => $name];
132 $fieldCreateStruct->position = $fieldPosition;
133 $fieldCreateStruct->isRequired = $required;
134 $fieldCreateStruct->defaultValue = $this->defaultValues[$fieldType];
135 $this->fieldConstructionObject['fieldType'] = $fieldCreateStruct;
136 $this->fieldConstructionObject['objectState'] = self::FIELD_TYPE_CREATED;
137 }
138
146 public function addValueConstraint($fieldType, $value, $constraint)
147 {
148 $validatorName = $this->getFieldValidator($fieldType);
149 $validatorParent = $validatorName . 'Validator';
150 if ($this->fieldConstructionObject['fieldType']->validatorConfiguration == null) {
151 $this->fieldConstructionObject['fieldType']->validatorConfiguration = [
152 $validatorParent => [],
153 ];
154 }
155 $value = is_numeric($value) ? $value + 0 : $value;
156
157 $this->fieldConstructionObject['fieldType']
158 ->validatorConfiguration[$validatorParent][$constraint . $validatorName] = $value;
159 }
160
167 public function createContent($field, $value)
168 {
169 $this->setFieldContentState(self::CONTENT_PUBLISHED, $field, $value);
170 }
171
180 public function setFieldContentState($stateFlag, $field = null, $value = null)
181 {
182 if ($stateFlag <= $this->fieldConstructionObject['objectState']
183 || $stateFlag < self::FIELD_TYPE_NOT_CREATED
184 ) {
185 return;
186 }
187
188 // recursively set previous states if necessary
189 $this->setFieldContentState($stateFlag - 1, $field, $value);
190
191 switch ($stateFlag) {
192 case self::FIELD_TYPE_NOT_CREATED:
193 throw new \Exception('A field type must be declared before anything else');
194 break;
195 case self::CONTENT_TYPE_CREATED:
196 $this->createContentType();
197 break;
198 case self::FIELD_TYPE_ASSOCIATED:
199 $this->associateFieldToContentType();
200 break;
201 case self::CONTENT_TYPE_PUBLISHED:
202 $this->publishContentType();
203 break;
204 case self::CONTENT_PUBLISHED:
205 $this->createAndPublishContent($field, $value);
206 break;
207 }
208 }
209
210 public function getFieldContentState()
211 {
212 return $this->fieldConstructionObject['objectState'];
213 }
214
222 private function createAndPublishContent($field, $value)
223 {
224 $languageCode = self::DEFAULT_LANGUAGE;
225
226 $locationCreateStruct = $this->locationService->newLocationCreateStruct('2');
227 $contentType = $this->fieldConstructionObject['contentType'];
228 $contentCreateStruct = $this->contentService->newContentCreateStruct($contentType, $languageCode);
229 if ($field != null && $value != null) {
230 $value = ($value == 'empty') ? null : $value;
231 $value = is_numeric($value) ? $value + 0 : $value;
232 $contentCreateStruct->setField($field, $value);
233 }
234 $draft = $this->contentService->createContent($contentCreateStruct, [$locationCreateStruct]);
235 $content = $this->contentService->publishVersion($draft->versionInfo);
236
237 $this->fieldConstructionObject['content'] = $content;
238 $this->fieldConstructionObject['objectState'] = self::CONTENT_PUBLISHED;
239 }
240
244 private function associateFieldToContentType()
245 {
246 $fieldCreateStruct = $this->fieldConstructionObject['fieldType'];
247 $this->fieldConstructionObject['contentType']->addFieldDefinition($fieldCreateStruct);
248 $this->fieldConstructionObject['objectState'] = self::FIELD_TYPE_ASSOCIATED;
249 }
250
254 private function publishContentType()
255 {
256 $contentTypeGroup = $this->contentTypeService->loadContentTypeGroupByIdentifier('Content');
257 $contentTypeCreateStruct = $this->fieldConstructionObject['contentType'];
258 $contentTypeDraft = $this->contentTypeService->createContentType(
259 $contentTypeCreateStruct,
260 [$contentTypeGroup]
261 );
262 $this->contentTypeService->publishContentTypeDraft($contentTypeDraft);
263 $contentTypeIdentifier = $this->fieldConstructionObject['contentType']->identifier;
264 $contentType = $this->contentTypeService->loadContentTypeByIdentifier($contentTypeIdentifier);
265 $this->fieldConstructionObject['contentType'] = $contentType;
266 $this->fieldConstructionObject['objectState'] = self::CONTENT_TYPE_PUBLISHED;
267 }
268
276 public function getThisContentTypeName($language = self::DEFAULT_LANGUAGE)
277 {
278 return $this->fieldConstructionObject['contentType']->names[$language];
279 }
280
288 public function getThisContentName($language = self::DEFAULT_LANGUAGE)
289 {
290 return $this->fieldConstructionObject['content']->versionInfo->names[$language];
291 }
292
300 public function getThisFieldTypeName($language = self::DEFAULT_LANGUAGE)
301 {
302 return $this->fieldConstructionObject['fieldType']->names[$language];
303 }
304
310 public function getThisFieldTypeIdentifier()
311 {
312 return $this->fieldConstructionObject['fieldType']->identifier;
313 }
314
320 public function getThisContentId()
321 {
322 return $this->fieldConstructionObject['content']->id;
323 }
324
328 private function createContentType()
329 {
330 $name = $this->fieldConstructionObject['fieldType']->identifier;
331 $name = uniqid($name . '#', true);
332 $identifier = str_replace('.', '', strtolower($name));
333 $contentTypeCreateStruct = $this->contentTypeService->newContentTypeCreateStruct($identifier);
334 $contentTypeCreateStruct->mainLanguageCode = self::DEFAULT_LANGUAGE;
335 $contentTypeCreateStruct->names = [self::DEFAULT_LANGUAGE => $name];
336 $contentTypeCreateStruct->nameSchema = $name;
337 $this->fieldConstructionObject['contentType'] = $contentTypeCreateStruct;
338 $this->fieldConstructionObject['objectState'] = self::CONTENT_TYPE_CREATED;
339 }
340
344 private function getActualFieldPosition()
345 {
346 if ($this->fieldConstructionObject['fieldType'] == null) {
347 return 10;
348 } else {
349 return $this->fieldConstructionObject['fieldType']->position + 10;
350 }
351 }
352
359 public function createContentTypeWithFieldType($fieldType, $name = null)
360 {
361 return $this->createField($fieldType, $name);
362 }
363
370 public function createContentTypeWithRequiredFieldType($fieldType, $name = null)
371 {
372 return $this->createField($fieldType, $name, true);
373 }
374
381 public function createContentOfThisType($field = null, $value = null)
382 {
383 return $this->createContent($field, $value);
384 }
385
390 public function createContentOfThisTypeWithProperties($fieldType, TableNode $properties, $name = null)
391 {
392 $this->createField($fieldType, $name);
393 foreach ($properties as $property) {
394 if ($property['Validator'] == 'maximum value validator') {
395 $this->addValueConstraint($fieldType, $property['Value'], 'max');
396 } elseif ($property['Validator'] == 'minimum value validator') {
397 $this->addValueConstraint($fieldType, $property['Value'], 'min');
398 }
399 }
400 }
401 }
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext
Sentences for Fields.
Definition: FieldTypeContext.php:21
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\addValueConstraint
addValueConstraint($fieldType, $value, $constraint)
Adds a validator to the stored field.
Definition: FieldTypeContext.php:146
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\publishContentType
publishContentType()
Publishes the stored contenttype.
Definition: FieldTypeContext.php:254
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\getThisFieldTypeName
getThisFieldTypeName($language=self::DEFAULT_LANGUAGE)
Getter method for the name of the stored fieldtype.
Definition: FieldTypeContext.php:300
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\getThisContentName
getThisContentName($language=self::DEFAULT_LANGUAGE)
Getter method for the name of the stored content.
Definition: FieldTypeContext.php:288
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\FIELD_TYPE_ASSOCIATED
const FIELD_TYPE_ASSOCIATED
Definition: FieldTypeContext.php:30
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\getActualFieldPosition
getActualFieldPosition()
Getter method for the position of the field, relative to other possible fields.
Definition: FieldTypeContext.php:344
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\createContentOfThisType
createContentOfThisType($field=null, $value=null)
@Given a Content of this type exists @Given a Content of this type exists with :field Field Value set...
Definition: FieldTypeContext.php:381
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\createContentType
createContentType()
Creates an instance of a contenttype and stores it for later publishing.
Definition: FieldTypeContext.php:328
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\DEFAULT_LANGUAGE
const DEFAULT_LANGUAGE
Default language.
Definition: FieldTypeContext.php:37
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\createAndPublishContent
createAndPublishContent($field, $value)
Creates and publishes the content with a given field and based on the internal ContentType.
Definition: FieldTypeContext.php:222
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\$contentTypeService
$contentTypeService
Definition: FieldTypeContext.php:66
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\getThisFieldTypeIdentifier
getThisFieldTypeIdentifier()
Getter method for the identifier of the stored fieldtype.
Definition: FieldTypeContext.php:310
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\$fieldTypeInternalIdentifier
$fieldTypeInternalIdentifier
Definition: FieldTypeContext.php:51
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\$contentService
$contentService
Definition: FieldTypeContext.php:69
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\getFieldContentState
getFieldContentState()
Definition: FieldTypeContext.php:210
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\createContentOfThisTypeWithProperties
createContentOfThisTypeWithProperties($fieldType, TableNode $properties, $name=null)
@Given a Content Type with an :fieldType field exists with Properties: @Given a Content Type with an ...
Definition: FieldTypeContext.php:390
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\__construct
__construct(Repository $repository, ContentTypeService $contentTypeService, ContentService $contentService, LocationService $locationService)
@injectService $repository @ezpublish.api.repository @injectService $contentTypeService @ezpublish....
Definition: FieldTypeContext.php:80
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\getFieldTypeInternalIdentifier
getFieldTypeInternalIdentifier($identifier)
Getter method for fieldtype internal identifier.
Definition: FieldTypeContext.php:99
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\CONTENT_TYPE_PUBLISHED
const CONTENT_TYPE_PUBLISHED
Definition: FieldTypeContext.php:31
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\getThisContentTypeName
getThisContentTypeName($language=self::DEFAULT_LANGUAGE)
Getter method for the name of the stored contenttype.
Definition: FieldTypeContext.php:276
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\FIELD_TYPE_NOT_CREATED
const FIELD_TYPE_NOT_CREATED
Defines the state of the Construction object, if it's not published, partialy or completely published...
Definition: FieldTypeContext.php:27
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\associateFieldToContentType
associateFieldToContentType()
Associates the stored fieldtype to the stored contenttype.
Definition: FieldTypeContext.php:244
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\getThisContentId
getThisContentId()
Get the content id for the published content.
Definition: FieldTypeContext.php:320
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\FIELD_TYPE_CREATED
const FIELD_TYPE_CREATED
Definition: FieldTypeContext.php:28
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\$defaultValues
$defaultValues
Definition: FieldTypeContext.php:61
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\getFieldValidator
getFieldValidator($field)
Getter method for the validator mappings.
Definition: FieldTypeContext.php:111
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\CONTENT_TYPE_CREATED
const CONTENT_TYPE_CREATED
Definition: FieldTypeContext.php:29
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\createField
createField($fieldType, $name=null, $required=false)
Creates a fieldtype ans stores it for later use.
Definition: FieldTypeContext.php:123
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\createContentTypeWithFieldType
createContentTypeWithFieldType($fieldType, $name=null)
@Given a Content Type with an :fieldType field exists @Given a Content Type with an :fieldType with f...
Definition: FieldTypeContext.php:359
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\$fieldConstructionObject
$fieldConstructionObject
Definition: FieldTypeContext.php:43
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\setFieldContentState
setFieldContentState($stateFlag, $field=null, $value=null)
Executes necessary operations to guarantee a given state, recursive function that calls it self to ma...
Definition: FieldTypeContext.php:180
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\createContentTypeWithRequiredFieldType
createContentTypeWithRequiredFieldType($fieldType, $name=null)
@Given a Content Type with a required :fieldType field exists @Given a Content Type with a required :...
Definition: FieldTypeContext.php:370
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\$validatorMappings
$validatorMappings
Definition: FieldTypeContext.php:56
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\CONTENT_PUBLISHED
const CONTENT_PUBLISHED
Definition: FieldTypeContext.php:32
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\$locationService
$locationService
Definition: FieldTypeContext.php:72
eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext\createContent
createContent($field, $value)
Creates a content and publishes it.
Definition: FieldTypeContext.php:167
eZ\Publish\API\Repository\ContentService
Definition: API/Repository/ContentService.php:27
eZ\Publish\API\Repository\ContentTypeService
Definition: API/Repository/ContentTypeService.php:25
eZ\Publish\API\Repository\LocationService
Definition: API/Repository/LocationService.php:21
eZ\Publish\API\Repository\Repository
Repository interface.
Definition: API/Repository/Repository.php:16
eZ\Bundle\EzPublishCoreBundle\Features\Context
Definition: BasicContentContext.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.