‪TYPO3CMS  ‪main
AbstractRecordCollection.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
23 
40 {
46  protected static ‪$storageItemsField = 'items';
47 
53  protected static ‪$storageTableName = '';
54 
60  protected ‪$uid = 0;
61 
67  protected ‪$title;
68 
74  protected ‪$description;
75 
81  protected ‪$itemTableName;
82 
88  protected ‪$storage;
89 
93  public function ‪__construct()
94  {
95  $this->storage = new \SplDoublyLinkedList();
96  }
97 
103  public function ‪current(): mixed
104  {
105  return $this->storage->current();
106  }
107 
111  public function ‪next(): void
112  {
113  $this->storage->next();
114  }
115 
121  public function ‪key(): mixed
122  {
123  $currentRecord = $this->storage->current();
124  return $currentRecord['uid'] ?? 0;
125  }
126 
132  public function ‪valid(): bool
133  {
134  return $this->storage->valid();
135  }
136 
140  public function ‪rewind(): void
141  {
142  $this->storage->rewind();
143  }
144 
148  public function ‪__serialize(): array
149  {
150  return [
151  'uid' => $this->‪getIdentifier(),
152  ];
153  }
154 
158  public function ‪__unserialize(array $arrayRepresentation): void
159  {
160  ‪self::load($arrayRepresentation['uid']);
161  }
162 
168  public function ‪count(): int
169  {
170  return $this->storage->count();
171  }
172 
178  public function ‪getTitle()
179  {
180  return ‪$this->title;
181  }
182 
188  public function ‪getUid()
189  {
190  return ‪$this->uid;
191  }
192 
198  public function ‪getDescription()
199  {
200  return ‪$this->description;
201  }
202 
208  public function ‪setTitle(‪$title)
209  {
210  $this->title = ‪$title;
211  }
212 
218  public function ‪setDescription($desc)
219  {
220  $this->‪description = $desc;
221  }
222 
228  public function ‪getItemTableName()
229  {
231  }
232 
238  public function ‪setItemTableName($tableName)
239  {
240  $this->itemTableName = $tableName;
241  }
242 
252  public function ‪usort($callbackFunction)
253  {
254  // @todo Implement usort() method with TCEforms in mind
255  throw new \RuntimeException('This method is not yet supported.', 1322545589);
256  }
257 
267  public function ‪moveItemAt($currentPosition, $newPosition = 0)
268  {
269  // @todo Implement usort() method with TCEforms in mind
270  throw new \RuntimeException('This method is not yet supported.', 1322545626);
271  }
272 
278  public function ‪getIdentifier()
279  {
280  return ‪$this->uid;
281  }
282 
288  public function ‪setIdentifier($id)
289  {
290  $this->uid = (int)$id;
291  }
292 
304  public static function ‪load($id, $fillItems = false)
305  {
306  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(static::getCollectionDatabaseTable());
307  $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
308  $collectionRecord = $queryBuilder->select('*')
309  ->from(static::getCollectionDatabaseTable())
310  ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($id, ‪Connection::PARAM_INT)))
311  ->executeQuery()
312  ->fetchAssociative();
313  return ‪self::create($collectionRecord ?: [], $fillItems);
314  }
315 
324  public static function ‪create(array $collectionRecord, $fillItems = false)
325  {
326  // [phpstan] Unsafe usage of new static()
327  // todo: Either mark this class or its constructor final or use new self instead.
328  $collection = new static();
329  $collection->fromArray($collectionRecord);
330  if ($fillItems) {
331  $collection->loadContents();
332  }
333  return $collection;
334  }
335 
339  public function ‪persist()
340  {
341  ‪$uid = $this->‪getIdentifier() == 0 ? 'NEW' . random_int(100000, 999999) : $this->‪getIdentifier();
342  $data = [
343  trim(static::getCollectionDatabaseTable()) => [
345  ],
346  ];
347  // New records always must have a pid
348  if ($this->‪getIdentifier() == 0) {
349  $data[trim(static::getCollectionDatabaseTable())][‪$uid]['pid'] = 0;
350  }
351  $tce = GeneralUtility::makeInstance(DataHandler::class);
352  $tce->start($data, []);
353  $tce->process_datamap();
354  }
355 
364  abstract protected function ‪getPersistableDataArray();
365 
375  protected function ‪getItemUidList($includeTableName = true)
376  {
377  $list = [];
378  foreach ($this->storage as $entry) {
379  $list[] = ($includeTableName ? $this->‪getItemTableName() . '_' : '') . $entry['uid'];
380  }
381  return implode(',', $list);
382  }
383 
389  public function ‪toArray()
390  {
391  $itemArray = [];
392  foreach ($this->storage as $item) {
393  $itemArray[] = $item;
394  }
395  return [
396  'uid' => $this->‪getIdentifier(),
397  'title' => $this->‪getTitle(),
398  'description' => $this->‪getDescription(),
399  'table_name' => $this->‪getItemTableName(),
400  'items' => $itemArray,
401  ];
402  }
403 
407  public function ‪fromArray(array $array)
408  {
409  $this->uid = $array['uid'];
410  $this->title = $array['title'];
411  $this->‪description = $array['description'];
412  $this->itemTableName = $array['table_name'];
413  }
414 
415  protected static function ‪getCollectionDatabaseTable(): string
416  {
417  if (!empty(static::$storageTableName)) {
418  return static::$storageTableName;
419  }
420  throw new \RuntimeException('No storage table name was defined the class "' . static::class . '".', 1592207959);
421  }
422 }
‪TYPO3\CMS\Core\DataHandling\DataHandler
Definition: DataHandler.php:94
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\$title
‪string $title
Definition: AbstractRecordCollection.php:63
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\__unserialize
‪__unserialize(array $arrayRepresentation)
Definition: AbstractRecordCollection.php:151
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\getCollectionDatabaseTable
‪static getCollectionDatabaseTable()
Definition: AbstractRecordCollection.php:408
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:52
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\__construct
‪__construct()
Definition: AbstractRecordCollection.php:86
‪TYPO3\CMS\Core\Collection\RecordCollectionInterface
Definition: RecordCollectionInterface.php:25
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\moveItemAt
‪moveItemAt($currentPosition, $newPosition=0)
Definition: AbstractRecordCollection.php:260
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\$storage
‪SplDoublyLinkedList $storage
Definition: AbstractRecordCollection.php:81
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\rewind
‪rewind()
Definition: AbstractRecordCollection.php:133
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\getTitle
‪string getTitle()
Definition: AbstractRecordCollection.php:171
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection
Definition: AbstractRecordCollection.php:40
‪TYPO3\CMS\Core\Collection\CollectionInterface
Definition: CollectionInterface.php:28
‪TYPO3\CMS\Core\Collection
Definition: AbstractRecordCollection.php:16
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\getItemUidList
‪string getItemUidList($includeTableName=true)
Definition: AbstractRecordCollection.php:368
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\usort
‪usort($callbackFunction)
Definition: AbstractRecordCollection.php:245
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\setTitle
‪setTitle($title)
Definition: AbstractRecordCollection.php:201
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\persist
‪persist()
Definition: AbstractRecordCollection.php:332
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\$storageItemsField
‪static string $storageItemsField
Definition: AbstractRecordCollection.php:45
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\setIdentifier
‪setIdentifier($id)
Definition: AbstractRecordCollection.php:281
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\getItemTableName
‪string getItemTableName()
Definition: AbstractRecordCollection.php:221
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\fromArray
‪fromArray(array $array)
Definition: AbstractRecordCollection.php:400
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\current
‪T null current()
Definition: AbstractRecordCollection.php:96
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\toArray
‪array toArray()
Definition: AbstractRecordCollection.php:382
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\$uid
‪int $uid
Definition: AbstractRecordCollection.php:57
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\valid
‪bool valid()
Definition: AbstractRecordCollection.php:125
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\count
‪int count()
Definition: AbstractRecordCollection.php:161
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\getUid
‪int getUid()
Definition: AbstractRecordCollection.php:181
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\getPersistableDataArray
‪array getPersistableDataArray()
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\next
‪next()
Definition: AbstractRecordCollection.php:104
‪TYPO3\CMS\Core\Collection\SortableCollectionInterface
Definition: SortableCollectionInterface.php:28
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\$description
‪string $description
Definition: AbstractRecordCollection.php:69
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\create
‪static CollectionInterface create(array $collectionRecord, $fillItems=false)
Definition: AbstractRecordCollection.php:317
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\__serialize
‪__serialize()
Definition: AbstractRecordCollection.php:141
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\getIdentifier
‪int getIdentifier()
Definition: AbstractRecordCollection.php:271
‪TYPO3\CMS\Core\Collection\PersistableCollectionInterface
Definition: PersistableCollectionInterface.php:27
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\load
‪static CollectionInterface load($id, $fillItems=false)
Definition: AbstractRecordCollection.php:297
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:28
‪TYPO3\CMS\Redirects\Message\description
‪identifier description
Definition: RedirectWasHitMessage.php:32
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\key
‪int string key()
Definition: AbstractRecordCollection.php:114
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\setItemTableName
‪setItemTableName($tableName)
Definition: AbstractRecordCollection.php:231
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\getDescription
‪string getDescription()
Definition: AbstractRecordCollection.php:191
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\setDescription
‪setDescription($desc)
Definition: AbstractRecordCollection.php:211
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\$storageTableName
‪static string $storageTableName
Definition: AbstractRecordCollection.php:51
‪TYPO3\CMS\Core\Collection\AbstractRecordCollection\$itemTableName
‪string $itemTableName
Definition: AbstractRecordCollection.php:75