‪TYPO3CMS  11.5
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 
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  static function ($aspectFieldName) use ($data) {
104  return (int)($data[$aspectFieldName] ?? 0);
105  },
107  );
108  }
109 
114  protected function ‪resolveLanguageLink(array $aspectFieldNames): ?‪EntityPointerLink
115  {
116  $languageSourceLink = null;
117  $languageParentLink = null;
118  if (!empty($aspectFieldNames['languageSource'])) {
119  $languageSourceLink = GeneralUtility::makeInstance(
120  EntityPointerLink::class,
121  $this->‪createEntityPointer($aspectFieldNames['languageSource'])
122  );
123  }
124 
125  if (!empty($aspectFieldNames['languageParent'])) {
126  $languageParentLink = GeneralUtility::makeInstance(
127  EntityPointerLink::class,
128  $this->‪createEntityPointer($aspectFieldNames['languageParent'])
129  );
130  }
131 
132  if (empty($languageSourceLink) || empty($languageParentLink)
133  || $languageSourceLink->getSubject()->isEqualTo(
134  $languageParentLink->getSubject()
135  )
136  ) {
137  return $languageSourceLink ?? $languageParentLink ?? null;
138  }
139  return $languageSourceLink->withAncestor($languageParentLink);
140  }
141 
146  protected function ‪resolveVersionLink(array $aspectFieldNames): ?‪EntityPointerLink
147  {
148  if (!empty($aspectFieldNames['versionParent'])) {
149  return GeneralUtility::makeInstance(
150  EntityPointerLink::class,
151  $this->‪createEntityPointer($aspectFieldNames['versionParent'])
152  );
153  }
154  return null;
155  }
156 
163  protected function ‪createEntityPointer($identifier, string ‪$name = null): ‪EntityPointer
164  {
165  if ($identifier === null) {
166  throw new \LogicException(
167  'Cannot create null pointer',
168  1536407967
169  );
170  }
171 
172  $identifier = (string)$identifier;
173 
174  return GeneralUtility::makeInstance(
175  EntityUidPointer::class,
176  ‪$name ?? $this->name,
177  $identifier
178  );
179  }
180 }
‪TYPO3\CMS\Core\DataHandling\Model\EntityPointer
Definition: EntityPointer.php:24
‪TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory\resolveAspectFieldNames
‪array resolveAspectFieldNames()
Definition: RecordStateFactory.php:84
‪TYPO3\CMS\Core\DataHandling\Model
Definition: CorrelationId.php:18
‪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: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:99
‪TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory\resolveVersionLink
‪EntityPointerLink null resolveVersionLink(array $aspectFieldNames)
Definition: RecordStateFactory.php:145
‪TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory\createEntityPointer
‪EntityPointer createEntityPointer($identifier, string $name=null)
Definition: RecordStateFactory.php:162
‪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:25
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪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