‪TYPO3CMS  ‪main
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 {
27  protected string ‪$name;
28 
29  public static function ‪forName(string ‪$name): self
30  {
31  return GeneralUtility::makeInstance(static::class, ‪$name);
32  }
33 
34  public function ‪__construct(string ‪$name)
35  {
36  $this->name = ‪$name;
37  }
38 
43  public function ‪fromArray(array $data, $pageId = null, $recordId = null): ‪RecordState
44  {
45  $pageId = $pageId ?? $data['pid'] ?? null;
46  $recordId = $recordId ?? $data['uid'] ?? null;
47 
48  $aspectFieldValues = $this->‪resolveAspectFieldValues($data);
49 
50  $context = GeneralUtility::makeInstance(EntityContext::class)
51  ->withWorkspaceId($aspectFieldValues['workspace'])
52  ->withLanguageId($aspectFieldValues['language']);
53  $node = $this->‪createEntityPointer($pageId, 'pages');
54  $subject = $this->‪createEntityPointer($recordId);
55 
56  $target = GeneralUtility::makeInstance(
57  RecordState::class,
58  $context,
59  $node,
60  $subject
61  );
62  return $target
63  ->withLanguageLink($this->‪resolveLanguageLink($aspectFieldValues))
64  ->withVersionLink($this->‪resolveVersionLink($aspectFieldValues));
65  }
66 
70  protected function resolveAspectFieldNames(): array
71  {
72  return [
73  'workspace' => 't3ver_wsid',
74  'versionParent' => 't3ver_oid',
75  'language' => ‪$GLOBALS['TCA'][‪$this->name]['ctrl']['languageField'] ?? null,
76  'languageParent' => ‪$GLOBALS['TCA'][‪$this->name]['ctrl']['transOrigPointerField'] ?? null,
77  'languageSource' => ‪$GLOBALS['TCA'][‪$this->name]['ctrl']['translationSource'] ?? null,
78  ];
79  }
80 
81  protected function ‪resolveAspectFieldValues(array $data): array
82  {
83  return array_map(
84  static function (?string $aspectFieldName) use ($data): int {
85  return (int)($data[$aspectFieldName] ?? 0);
86  },
87  $this->resolveAspectFieldNames()
88  );
89  }
90 
91  protected function ‪resolveLanguageLink(array $aspectFieldNames): ?‪EntityPointerLink
92  {
93  $languageSourceLink = null;
94  $languageParentLink = null;
95  if (!empty($aspectFieldNames['languageSource'])) {
96  $languageSourceLink = GeneralUtility::makeInstance(
97  EntityPointerLink::class,
98  $this->‪createEntityPointer($aspectFieldNames['languageSource'])
99  );
100  }
101 
102  if (!empty($aspectFieldNames['languageParent'])) {
103  $languageParentLink = GeneralUtility::makeInstance(
104  EntityPointerLink::class,
105  $this->‪createEntityPointer($aspectFieldNames['languageParent'])
106  );
107  }
108 
109  if (empty($languageSourceLink) || empty($languageParentLink)
110  || $languageSourceLink->getSubject()->isEqualTo(
111  $languageParentLink->getSubject()
112  )
113  ) {
114  return $languageSourceLink ?? $languageParentLink ?? null;
115  }
116  return $languageSourceLink->withAncestor($languageParentLink);
117  }
118 
119  protected function ‪resolveVersionLink(array $aspectFieldNames): ?‪EntityPointerLink
120  {
121  if (!empty($aspectFieldNames['versionParent'])) {
122  return GeneralUtility::makeInstance(
123  EntityPointerLink::class,
124  $this->‪createEntityPointer($aspectFieldNames['versionParent'])
125  );
126  }
127  return null;
128  }
129 
136  {
137  if (‪$identifier === null) {
138  throw new \LogicException(
139  'Cannot create null pointer',
140  1536407967
141  );
142  }
143 
144  ‪$identifier = (string)‪$identifier;
145 
146  return GeneralUtility::makeInstance(
147  EntityUidPointer::class,
148  ‪$name ?? $this->name,
150  );
151  }
152 }
‪TYPO3\CMS\Core\DataHandling\Model\EntityPointer
Definition: EntityPointer.php:24
‪TYPO3\CMS\Core\DataHandling\Model
Definition: CorrelationId.php:18
‪TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory\resolveLanguageLink
‪resolveLanguageLink(array $aspectFieldNames)
Definition: RecordStateFactory.php:91
‪TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory
Definition: RecordStateFactory.php:26
‪TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory\resolveAspectFieldValues
‪resolveAspectFieldValues(array $data)
Definition: RecordStateFactory.php:81
‪TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory\createEntityPointer
‪createEntityPointer($identifier, string $name=null)
Definition: RecordStateFactory.php:135
‪TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory\resolveVersionLink
‪resolveVersionLink(array $aspectFieldNames)
Definition: RecordStateFactory.php:119
‪TYPO3\CMS\Core\DataHandling\Model\RecordState
Definition: RecordState.php:32
‪TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory\__construct
‪__construct(string $name)
Definition: RecordStateFactory.php:34
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory\fromArray
‪fromArray(array $data, $pageId=null, $recordId=null)
Definition: RecordStateFactory.php:43
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory\forName
‪static forName(string $name)
Definition: RecordStateFactory.php:29
‪TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory\$name
‪string $name
Definition: RecordStateFactory.php:27
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37