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