‪TYPO3CMS  9.5
RecordStateFactory.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
20 
25 {
29  protected ‪$name;
30 
35  public static function ‪forName(string ‪$name): self
36  {
37  return GeneralUtility::makeInstance(
38  static::class,
39  ‪$name
40  );
41  }
42 
46  public function ‪__construct(string ‪$name)
47  {
48  $this->name = ‪$name;
49  }
50 
57  public function ‪fromArray(array $data, $pageId = null, $recordId = null): ‪RecordState
58  {
59  $pageId = $pageId ?? $data['pid'] ?? null;
60  $recordId = $recordId ?? $data['uid'] ?? null;
61 
62  $aspectFieldValues = $this->‪resolveAspectFieldValues($data);
63 
64  $context = GeneralUtility::makeInstance(EntityContext::class)
65  ->withWorkspaceId($aspectFieldValues['workspace'])
66  ->withLanguageId($aspectFieldValues['language']);
67  $node = $this->‪createEntityPointer($pageId, 'pages');
68  $subject = $this->‪createEntityPointer($recordId);
69 
71  $target = GeneralUtility::makeInstance(
72  RecordState::class,
73  $context,
74  $node,
75  $subject
76  );
77  return $target
78  ->withLanguageLink($this->‪resolveLanguageLink($aspectFieldValues))
79  ->withVersionLink($this->‪resolveVersionLink($aspectFieldValues));
80  }
81 
85  protected function ‪resolveAspectFieldNames(): array
86  {
87  return [
88  'workspace' => 't3ver_wsid',
89  'versionParent' => 't3ver_oid',
90  'language' => ‪$GLOBALS['TCA'][‪$this->name]['ctrl']['languageField'] ?? null,
91  'languageParent' => ‪$GLOBALS['TCA'][‪$this->name]['ctrl']['transOrigPointerField'] ?? null,
92  'languageSource' => ‪$GLOBALS['TCA'][‪$this->name]['ctrl']['translationSource'] ?? null,
93  ];
94  }
95 
100  protected function ‪resolveAspectFieldValues(array $data): array
101  {
102  return array_map(
103  function ($aspectFieldName) use ($data) {
104  return (int)($data[$aspectFieldName] ?? 0);
105  },
107  );
108  }
109 
114  protected function ‪resolveLanguageLink(array $aspectFieldNames): ?‪EntityPointerLink
115  {
116  if (!empty($aspectFieldNames['languageSource'])) {
117  $languageSourceLink = GeneralUtility::makeInstance(
118  EntityPointerLink::class,
119  $this->‪createEntityPointer($aspectFieldNames['languageSource'])
120  );
121  }
122 
123  if (!empty($aspectFieldNames['languageParent'])) {
124  $languageParentLink = GeneralUtility::makeInstance(
125  EntityPointerLink::class,
126  $this->‪createEntityPointer($aspectFieldNames['languageParent'])
127  );
128  }
129 
130  if (empty($languageSourceLink) || empty($languageParentLink)
131  || $languageSourceLink->getSubject()->isEqualTo(
132  $languageParentLink->getSubject()
133  )
134  ) {
135  return $languageSourceLink ?? $languageParentLink ?? null;
136  }
137  return $languageSourceLink->withAncestor($languageParentLink);
138  }
139 
144  protected function ‪resolveVersionLink(array $aspectFieldNames): ?‪EntityPointerLink
145  {
146  if (!empty($aspectFieldNames['versionParent'])) {
147  return GeneralUtility::makeInstance(
148  EntityPointerLink::class,
149  $this->‪createEntityPointer($aspectFieldNames['versionParent'])
150  );
151  }
152  return null;
153  }
154 
161  protected function ‪createEntityPointer($identifier, string ‪$name = null): ‪EntityPointer
162  {
163  if ($identifier === null) {
164  throw new \LogicException(
165  'Cannot create null pointer',
166  1536407967
167  );
168  }
169 
170  $identifier = (string)$identifier;
171 
172  return GeneralUtility::makeInstance(
173  EntityUidPointer::class,
174  ‪$name ?? $this->name,
175  $identifier
176  );
177  }
178 }
‪TYPO3\CMS\Core\DataHandling\Model\EntityPointer
Definition: EntityPointer.php:23
‪TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory\resolveAspectFieldNames
‪array resolveAspectFieldNames()
Definition: RecordStateFactory.php:84
‪TYPO3\CMS\Core\DataHandling\Model
Definition: EntityContext.php:4
‪TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory\fromArray
‪object RecordState fromArray(array $data, $pageId=null, $recordId=null)
Definition: RecordStateFactory.php:56
‪TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory\resolveLanguageLink
‪EntityPointerLink null resolveLanguageLink(array $aspectFieldNames)
Definition: RecordStateFactory.php:113
‪TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory
Definition: RecordStateFactory.php:25
‪TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory\resolveAspectFieldValues
‪array resolveAspectFieldValues(array $data)
Definition: RecordStateFactory.php:99
‪TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory\resolveVersionLink
‪EntityPointerLink null resolveVersionLink(array $aspectFieldNames)
Definition: RecordStateFactory.php:143
‪TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory\createEntityPointer
‪EntityPointer createEntityPointer($identifier, string $name=null)
Definition: RecordStateFactory.php:160
‪TYPO3\CMS\Core\DataHandling\Model\RecordState
Definition: RecordState.php:31
‪TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory\__construct
‪__construct(string $name)
Definition: RecordStateFactory.php:45
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory\$name
‪string $name
Definition: RecordStateFactory.php:28
‪TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory\forName
‪static static forName(string $name)
Definition: RecordStateFactory.php:34