‪TYPO3CMS  10.4
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 
55  public function ‪__construct(‪File ‪$file)
56  {
57  $this->file = ‪$file;
58  }
59 
67  public function ‪add(array ‪$metaData): self
68  {
69  $this->loaded = true;
70  $this->metaData = array_merge($this->metaData, ‪$metaData);
71 
72  return $this;
73  }
74 
80  public function get(): array
81  {
82  if (!$this->loaded) {
83  $this->loaded = true;
84  $this->metaData = $this->‪loadFromRepository();
85  }
86  return ‪$this->metaData;
87  }
88 
93  public function ‪offsetExists($offset): bool
94  {
95  return array_key_exists($offset, $this->get());
96  }
97 
102  public function ‪offsetGet($offset)
103  {
104  return $this->get()[$offset];
105  }
106 
111  public function ‪offsetSet($offset, $value): void
112  {
113  $this->loaded = true;
114  $this->metaData[$offset] = $value;
115  }
116 
120  public function ‪offsetUnset($offset): void
121  {
122  $this->metaData[$offset] = null;
123  }
124 
128  public function ‪count(): int
129  {
130  return ‪count($this->get());
131  }
132 
136  public function ‪rewind(): void
137  {
138  $this->indexPosition = 0;
139  }
140 
146  public function ‪current()
147  {
148  $key = array_keys($this->metaData)[‪$this->indexPosition];
149  return $this->metaData[$key];
150  }
151 
157  public function ‪key(): string
158  {
159  return array_keys($this->metaData)[‪$this->indexPosition];
160  }
161 
165  public function ‪next(): void
166  {
168  }
169 
173  public function ‪valid(): bool
174  {
175  $key = array_keys($this->metaData)[‪$this->indexPosition];
176  return array_key_exists($key, $this->metaData);
177  }
178 
184  public function ‪save(): void
185  {
186  $metaDataInDatabase = $this->‪loadFromRepository();
187  if ($metaDataInDatabase === []) {
188  $this->metaData = $this->‪getMetaDataRepository()->‪createMetaDataRecord($this->file->getUid(), $this->metaData);
189  } else {
190  $this->‪getMetaDataRepository()->‪update($this->file->getUid(), $this->metaData);
191  $this->metaData = array_merge($metaDataInDatabase, $this->metaData);
192  }
193  }
194 
200  public function remove(): void
201  {
202  $this->‪getMetaDataRepository()->‪removeByFileUid($this->file->getUid());
203  $this->metaData = [];
204  }
205 
209  protected function ‪getMetaDataRepository(): ‪MetaDataRepository
210  {
211  return GeneralUtility::makeInstance(MetaDataRepository::class);
212  }
213 
217  protected function ‪loadFromRepository(): array
218  {
219  return $this->‪getMetaDataRepository()->‪findByFileUid((int)$this->file->getUid());
220  }
221 }
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository
Definition: MetaDataRepository.php:40
‪TYPO3\CMS\Core\Resource\MetaDataAspect\valid
‪bool valid()
Definition: MetaDataAspect.php:169
‪TYPO3\CMS\Core\Resource\MetaDataAspect\offsetExists
‪bool offsetExists($offset)
Definition: MetaDataAspect.php:89
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository\findByFileUid
‪array findByFileUid($uid)
Definition: MetaDataRepository.php:102
‪TYPO3\CMS\Core\Resource\MetaDataAspect\offsetGet
‪mixed offsetGet($offset)
Definition: MetaDataAspect.php:98
‪TYPO3\CMS\Core\Resource\MetaDataAspect\getMetaDataRepository
‪MetaDataRepository getMetaDataRepository()
Definition: MetaDataAspect.php:205
‪TYPO3\CMS\Core\Resource\MetaDataAspect\count
‪int count()
Definition: MetaDataAspect.php:124
‪TYPO3\CMS\Core\Resource\MetaDataAspect\current
‪mixed current()
Definition: MetaDataAspect.php:142
‪TYPO3\CMS\Core\Resource\MetaDataAspect\save
‪save()
Definition: MetaDataAspect.php:180
‪TYPO3\CMS\Core\Resource\MetaDataAspect\loadFromRepository
‪array loadFromRepository()
Definition: MetaDataAspect.php:213
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository\removeByFileUid
‪removeByFileUid($fileUid)
Definition: MetaDataRepository.php:206
‪TYPO3\CMS\Core\Resource\MetaDataAspect\__construct
‪__construct(File $file)
Definition: MetaDataAspect.php:51
‪TYPO3\CMS\Core\Resource\MetaDataAspect
Definition: MetaDataAspect.php:27
‪TYPO3\CMS\Core\Resource\MetaDataAspect\add
‪self add(array $metaData)
Definition: MetaDataAspect.php:63
‪TYPO3\CMS\Core\Resource\MetaDataAspect\$loaded
‪bool $loaded
Definition: MetaDataAspect.php:40
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Resource\MetaDataAspect\offsetUnset
‪offsetUnset($offset)
Definition: MetaDataAspect.php:116
‪TYPO3\CMS\Core\Resource\MetaDataAspect\offsetSet
‪offsetSet($offset, $value)
Definition: MetaDataAspect.php:107
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:52
‪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\Index\MetaDataRepository\update
‪update($fileUid, array $data)
Definition: MetaDataRepository.php:170
‪TYPO3\CMS\Core\Resource\MetaDataAspect\next
‪next()
Definition: MetaDataAspect.php:161
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Resource\MetaDataAspect\rewind
‪rewind()
Definition: MetaDataAspect.php:132
‪TYPO3\CMS\Core\Resource\MetaDataAspect\key
‪string key()
Definition: MetaDataAspect.php:153
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository\createMetaDataRecord
‪array createMetaDataRecord($fileUid, array $additionalFields=[])
Definition: MetaDataRepository.php:137