‪TYPO3CMS  ‪main
MetaDataAspect.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 
22 
26 class ‪MetaDataAspect implements \ArrayAccess, \Countable, \Iterator
27 {
31  private ‪$file;
32 
36  private ‪$metaData = [];
37 
43  private ‪$loaded = false;
44 
48  private ‪$indexPosition = 0;
49 
53  public function ‪__construct(‪File ‪$file)
54  {
55  $this->file = ‪$file;
56  }
57 
63  public function ‪add(array ‪$metaData): self
64  {
65  $this->loaded = true;
66  $this->metaData = array_merge($this->metaData, ‪$metaData);
67 
68  return $this;
69  }
70 
74  public function get(): array
75  {
76  if (!$this->loaded) {
77  $this->loaded = true;
78  $this->metaData = $this->‪loadFromRepository();
79  }
80  return ‪$this->metaData;
81  }
82 
86  public function ‪offsetExists($offset): bool
87  {
88  return array_key_exists($offset, $this->get());
89  }
90 
94  public function ‪offsetGet($offset): mixed
95  {
96  return $this->get()[$offset] ?? null;
97  }
98 
103  public function ‪offsetSet($offset, $value): void
104  {
105  $this->loaded = true;
106  $this->metaData[$offset] = $value;
107  }
108 
112  public function ‪offsetUnset($offset): void
113  {
114  $this->metaData[$offset] = null;
115  }
116 
117  public function ‪count(): int
118  {
119  return ‪count($this->get());
120  }
121 
125  public function ‪rewind(): void
126  {
127  $this->indexPosition = 0;
128  }
129 
133  public function ‪current(): mixed
134  {
135  $key = array_keys($this->metaData)[‪$this->indexPosition];
136  return $this->metaData[$key];
137  }
138 
142  public function ‪key(): string
143  {
144  return array_keys($this->metaData)[‪$this->indexPosition];
145  }
146 
150  public function ‪next(): void
151  {
153  }
154 
155  public function ‪valid(): bool
156  {
157  $key = array_keys($this->metaData)[‪$this->indexPosition] ?? '';
158  return array_key_exists($key, $this->metaData);
159  }
160 
166  public function ‪save(): void
167  {
168  $metaDataInDatabase = $this->‪loadFromRepository();
169  if ($metaDataInDatabase === []) {
170  $this->metaData = $this->‪getMetaDataRepository()->createMetaDataRecord($this->file->getUid(), $this->metaData);
171  } else {
172  $this->metaData = $this->‪getMetaDataRepository()->update($this->file->getUid(), $this->metaData, $metaDataInDatabase);
173  }
174  }
175 
181  public function remove(): void
182  {
183  $this->‪getMetaDataRepository()->removeByFileUid($this->file->getUid());
184  $this->metaData = [];
185  }
186 
187  protected function ‪getMetaDataRepository(): ‪MetaDataRepository
188  {
189  return GeneralUtility::makeInstance(MetaDataRepository::class);
190  }
191 
192  protected function ‪loadFromRepository(): array
193  {
194  return $this->‪getMetaDataRepository()->findByFileUid((int)$this->file->getUid());
195  }
196 }
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository
Definition: MetaDataRepository.php:40
‪TYPO3\CMS\Core\Resource\MetaDataAspect\offsetExists
‪offsetExists($offset)
Definition: MetaDataAspect.php:82
‪TYPO3\CMS\Core\Resource\MetaDataAspect\count
‪count()
Definition: MetaDataAspect.php:113
‪TYPO3\CMS\Core\Resource\MetaDataAspect\current
‪current()
Definition: MetaDataAspect.php:129
‪TYPO3\CMS\Core\Resource\MetaDataAspect\offsetGet
‪offsetGet($offset)
Definition: MetaDataAspect.php:90
‪TYPO3\CMS\Core\Resource\MetaDataAspect\valid
‪valid()
Definition: MetaDataAspect.php:151
‪TYPO3\CMS\Core\Resource\MetaDataAspect\save
‪save()
Definition: MetaDataAspect.php:162
‪TYPO3\CMS\Core\Resource\MetaDataAspect\add
‪add(array $metaData)
Definition: MetaDataAspect.php:59
‪TYPO3\CMS\Core\Resource\MetaDataAspect\key
‪key()
Definition: MetaDataAspect.php:138
‪TYPO3\CMS\Core\Resource\MetaDataAspect\__construct
‪__construct(File $file)
Definition: MetaDataAspect.php:49
‪TYPO3\CMS\Core\Resource\MetaDataAspect
Definition: MetaDataAspect.php:27
‪TYPO3\CMS\Core\Resource\MetaDataAspect\$loaded
‪bool $loaded
Definition: MetaDataAspect.php:40
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Core\Resource\MetaDataAspect\offsetUnset
‪offsetUnset($offset)
Definition: MetaDataAspect.php:108
‪TYPO3\CMS\Core\Resource\MetaDataAspect\loadFromRepository
‪loadFromRepository()
Definition: MetaDataAspect.php:188
‪TYPO3\CMS\Core\Resource\MetaDataAspect\offsetSet
‪offsetSet($offset, $value)
Definition: MetaDataAspect.php:99
‪TYPO3\CMS\Core\Resource\MetaDataAspect\getMetaDataRepository
‪getMetaDataRepository()
Definition: MetaDataAspect.php:183
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:54
‪TYPO3\CMS\Core\Resource\MetaDataAspect\$file
‪File $file
Definition: MetaDataAspect.php:30
‪TYPO3\CMS\Core\Resource\MetaDataAspect\$metaData
‪array $metaData
Definition: MetaDataAspect.php:34
‪TYPO3\CMS\Core\Resource\MetaDataAspect\$indexPosition
‪int $indexPosition
Definition: MetaDataAspect.php:44
‪TYPO3\CMS\Core\Resource\MetaDataAspect\next
‪next()
Definition: MetaDataAspect.php:146
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Resource\MetaDataAspect\rewind
‪rewind()
Definition: MetaDataAspect.php:121