‪TYPO3CMS  ‪main
FileReference.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 
24 
37 {
45 
51  protected ‪$originalFile;
52 
60  protected ‪$mergedProperties = [];
61 
71  public function ‪__construct(array $fileReferenceData, $factory = null)
72  {
73  $this->propertiesOfFileReference = $fileReferenceData;
74  if (!$fileReferenceData['uid_local']) {
75  throw new \InvalidArgumentException('Incorrect reference to original file given for FileReference.', 1300098528);
76  }
77  $this->‪originalFile = $this->‪getFileObject((int)$fileReferenceData['uid_local'], $factory);
78  }
79 
84  private function ‪getFileObject(int $uidLocal, ‪ResourceFactory $factory = null): ‪FileInterface
85  {
86  if ($factory === null) {
87  $factory = GeneralUtility::makeInstance(ResourceFactory::class);
88  }
89  return $factory->getFileObject($uidLocal);
90  }
91 
92  /*******************************
93  * VARIOUS FILE PROPERTY GETTERS
94  *******************************/
100  public function ‪hasProperty(string $key): bool
101  {
102  return array_key_exists($key, $this->‪getProperties());
103  }
104 
111  public function ‪getProperty(string $key): mixed
112  {
113  if (!$this->‪hasProperty($key)) {
114  throw new \InvalidArgumentException('Property "' . $key . '" was not found in file reference or original file.', 1314226805);
115  }
116  $properties = $this->‪getProperties();
117  return $properties[$key];
118  }
119 
127  public function ‪getReferenceProperty($key)
128  {
129  if (!array_key_exists($key, $this->propertiesOfFileReference)) {
130  throw new \InvalidArgumentException('Property "' . $key . '" of file reference was not found.', 1360684914);
131  }
132  return $this->propertiesOfFileReference[$key];
133  }
134 
140  public function ‪getProperties()
141  {
142  if (empty($this->mergedProperties)) {
143  $this->mergedProperties = ‪$this->propertiesOfFileReference;
144  ArrayUtility::mergeRecursiveWithOverrule(
145  $this->mergedProperties,
147  true,
148  true,
149  false
150  );
151  array_walk($this->mergedProperties, $this->‪restoreNonNullValuesCallback(...));
152  }
153 
155  }
156 
163  protected function ‪restoreNonNullValuesCallback(&$value, $key)
164  {
165  if (array_key_exists($key, $this->propertiesOfFileReference) && $this->propertiesOfFileReference[$key] !== null) {
166  $value = $this->propertiesOfFileReference[$key];
167  }
168  }
169 
175  public function ‪getReferenceProperties()
176  {
178  }
179 
180  public function ‪getName(): string
181  {
182  return $this->‪originalFile->‪getName();
183  }
184 
192  public function ‪getTitle()
193  {
194  return (string)$this->‪getProperty('title');
195  }
196 
204  public function ‪getAlternative()
205  {
206  return (string)$this->‪getProperty('alternative');
207  }
208 
216  public function ‪getDescription()
217  {
218  return (string)$this->‪getProperty('description');
219  }
220 
228  public function ‪getLink()
229  {
230  return $this->propertiesOfFileReference['link'];
231  }
232 
238  public function ‪getUid()
239  {
240  return (int)$this->propertiesOfFileReference['uid'];
241  }
242 
246  public function ‪getSize(): int
247  {
248  return $this->‪originalFile->getSize();
249  }
250 
256  public function ‪getSha1(): string
257  {
258  return $this->‪originalFile->‪getSha1();
259  }
260 
266  public function ‪getExtension(): string
267  {
268  return $this->‪originalFile->‪getExtension();
269  }
270 
274  public function ‪getNameWithoutExtension(): string
275  {
277  }
278 
284  public function ‪getMimeType(): string
285  {
286  return $this->‪originalFile->‪getMimeType();
287  }
288 
292  public function ‪getModificationTime(): int
293  {
294  return (int)$this->‪originalFile->‪getModificationTime();
295  }
296 
300  public function ‪getCreationTime(): int
301  {
302  return (int)$this->‪originalFile->‪getCreationTime();
303  }
304 
310  public function ‪getType()
311  {
312  return (int)$this->‪originalFile->‪getType();
313  }
314 
320  public function ‪isMissing()
321  {
322  return (bool)$this->‪originalFile->‪getProperty('missing');
323  }
324 
325  /******************
326  * CONTENTS RELATED
327  ******************/
331  public function ‪getContents(): string
332  {
333  return $this->‪originalFile->‪getContents();
334  }
335 
342  public function ‪setContents(string $contents): self
343  {
344  $this->‪originalFile->‪setContents($contents);
345  return $this;
346  }
347 
348  /****************************************
349  * STORAGE AND MANAGEMENT RELATED METHODS
350  ****************************************/
354  public function ‪getStorage(): ResourceStorage
355  {
356  return $this->‪originalFile->‪getStorage();
357  }
358 
364  public function ‪getIdentifier(): string
365  {
366  return $this->‪originalFile->‪getIdentifier();
367  }
368 
374  public function ‪getCombinedIdentifier()
375  {
376  return $this->‪originalFile->‪getCombinedIdentifier();
377  }
378 
383  public function delete(): bool
384  {
385  $deletedRows = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_file_reference')
386  ->delete(
387  'sys_file_reference',
388  [
389  'uid' => $this->‪getUid(),
390  ]
391  );
392 
393  return $deletedRows === 1;
394  }
395 
403  public function ‪rename(string $newName, $conflictMode = ‪DuplicationBehavior::RENAME): FileInterface
404  {
405  // @todo Implement this function. This should only rename the
406  // FileReference (sys_file_reference) record, not the file itself.
407  throw new \BadMethodCallException('Function not implemented FileReference::rename().', 1333754473);
408  //return $this->fileRepository->renameUsageRecord($this, $newName);
409  }
410 
411  /*****************
412  * SPECIAL METHODS
413  *****************/
422  public function ‪getPublicUrl(): ?string
423  {
424  return $this->‪originalFile->‪getPublicUrl();
425  }
426 
433  public function ‪isIndexed(): bool
434  {
435  return true;
436  }
437 
446  public function ‪getForLocalProcessing(bool $writable = true): string
447  {
448  return $this->‪originalFile->‪getForLocalProcessing($writable);
449  }
450 
457  public function toArray(): array
458  {
459  $array = array_merge($this->‪originalFile->toArray(), $this->propertiesOfFileReference);
460  return $array;
461  }
462 
468  public function getOriginalFile()
469  {
470  return ‪$this->originalFile;
471  }
472 
476  public function ‪getHashedIdentifier(): string
477  {
478  return $this->‪getStorage()->hashFileIdentifier($this->‪getIdentifier());
479  }
480 
481  public function ‪getParentFolder(): ‪FolderInterface
482  {
483  return $this->‪originalFile->‪getParentFolder();
484  }
485 
492  public function ‪__sleep(): array
493  {
494  $keys = get_object_vars($this);
495  unset($keys['originalFile'], $keys['mergedProperties']);
496  return array_keys($keys);
497  }
498 
499  public function ‪__wakeup(): void
500  {
501  $factory = GeneralUtility::makeInstance(ResourceFactory::class);
502  $this->‪originalFile = $this->‪getFileObject(
503  (int)$this->propertiesOfFileReference['uid_local'],
504  $factory
505  );
506  }
507 }
‪TYPO3\CMS\Core\Resource\FileReference\getAlternative
‪string getAlternative()
Definition: FileReference.php:201
‪TYPO3\CMS\Core\Resource\FileReference\getIdentifier
‪non empty string getIdentifier()
Definition: FileReference.php:361
‪TYPO3\CMS\Core\Resource\FileReference\getProperty
‪getProperty(string $key)
Definition: FileReference.php:108
‪TYPO3\CMS\Core\Resource\FileReference\getSha1
‪non empty string getSha1()
Definition: FileReference.php:253
‪TYPO3\CMS\Core\Resource\AbstractFile\getType
‪int getType()
Definition: AbstractFile.php:281
‪TYPO3\CMS\Core\Resource\FileReference\setContents
‪$this setContents(string $contents)
Definition: FileReference.php:339
‪TYPO3\CMS\Core\Resource\FileReference\$mergedProperties
‪array $mergedProperties
Definition: FileReference.php:57
‪TYPO3\CMS\Core\Resource\FileReference\$propertiesOfFileReference
‪array $propertiesOfFileReference
Definition: FileReference.php:43
‪TYPO3\CMS\Core\Resource\FileReference\originalFile
‪array< non-empty-string, function toArray():array { $array=array_merge( $this->originalFile->toArray(), $this->propertiesOfFileReference);return $array;} public File function getOriginalFile() { return $this-> originalFile
Definition: FileReference.php:467
‪TYPO3\CMS\Core\Resource\AbstractFile\getForLocalProcessing
‪non empty string getForLocalProcessing(bool $writable=true)
Definition: AbstractFile.php:576
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:26
‪TYPO3\CMS\Core\Resource\FileReference\getNameWithoutExtension
‪getNameWithoutExtension()
Definition: FileReference.php:271
‪TYPO3\CMS\Core\Resource\AbstractFile\getParentFolder
‪getParentFolder()
Definition: AbstractFile.php:594
‪TYPO3\CMS\Core\Resource\File\getContents
‪getContents()
Definition: File.php:104
‪TYPO3\CMS\Core\Resource\FileReference\getDescription
‪string getDescription()
Definition: FileReference.php:213
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:37
‪TYPO3\CMS\Core\Resource\FileReference\isMissing
‪bool isMissing()
Definition: FileReference.php:317
‪TYPO3\CMS\Core\Resource\AbstractFile\getExtension
‪getExtension()
Definition: AbstractFile.php:243
‪TYPO3\CMS\Core\Resource\AbstractFile\getName
‪getName()
Definition: AbstractFile.php:157
‪TYPO3\CMS\Core\Resource\FileReference\getForLocalProcessing
‪non empty string getForLocalProcessing(bool $writable=true)
Definition: FileReference.php:443
‪TYPO3\CMS\Core\Resource\FileReference\__sleep
‪string[] __sleep()
Definition: FileReference.php:489
‪TYPO3\CMS\Core\Resource\FileReference\getExtension
‪string getExtension()
Definition: FileReference.php:263
‪TYPO3\CMS\Core\Resource\FileReference\getPublicUrl
‪non empty string null getPublicUrl()
Definition: FileReference.php:419
‪TYPO3\CMS\Core\Resource\File\getProperty
‪getProperty(string $key)
Definition: File.php:62
‪TYPO3\CMS\Core\Resource\FileReference\getProperties
‪array getProperties()
Definition: FileReference.php:137
‪TYPO3\CMS\Core\Resource\FileReference\isIndexed
‪isIndexed()
Definition: FileReference.php:430
‪TYPO3\CMS\Core\Resource\FileReference\getMimeType
‪non empty string getMimeType()
Definition: FileReference.php:281
‪TYPO3\CMS\Core\Resource\FileReference\getContents
‪getContents()
Definition: FileReference.php:328
‪TYPO3\CMS\Core\Resource\FileReference\getLink
‪string getLink()
Definition: FileReference.php:225
‪TYPO3\CMS\Core\Resource\FileReference\getUid
‪int getUid()
Definition: FileReference.php:235
‪TYPO3\CMS\Core\Resource\File\getPublicUrl
‪getPublicUrl()
Definition: File.php:301
‪TYPO3\CMS\Core\Resource\File\getSha1
‪non empty string getSha1()
Definition: File.php:114
‪TYPO3\CMS\Core\Resource\FileReference\getType
‪int getType()
Definition: FileReference.php:307
‪TYPO3\CMS\Core\Resource\AbstractFile\getStorage
‪int< 0, getSize():int { if( $this->deleted) { throw new \RuntimeException( 'File has been deleted.', 1329821480);} if(empty( $this->properties[ 'size'])) { $fileInfo=$this-> getStorage() -> getFileInfoByIdentifier($this->getIdentifier(), ['size'])
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:42
‪TYPO3\CMS\Core\Resource\DuplicationBehavior\RENAME
‪const RENAME
Definition: DuplicationBehavior.php:33
‪TYPO3\CMS\Core\Resource\Enum\DuplicationBehavior
‪DuplicationBehavior
Definition: DuplicationBehavior.php:28
‪TYPO3\CMS\Core\Resource\AbstractFile\getMimeType
‪non empty string getMimeType()
Definition: AbstractFile.php:257
‪TYPO3\CMS\Core\Resource\FileReference\getParentFolder
‪getParentFolder()
Definition: FileReference.php:478
‪TYPO3\CMS\Core\Resource\FileReference\getTitle
‪string getTitle()
Definition: FileReference.php:189
‪TYPO3\CMS\Core\Resource\File\getCombinedIdentifier
‪array< non-empty-string, toArray():array { $array=['id'=> $this getCombinedIdentifier()
‪TYPO3\CMS\Core\Resource\File\getProperties
‪getProperties()
Definition: File.php:87
‪TYPO3\CMS\Core\Resource\FileReference\getReferenceProperties
‪array getReferenceProperties()
Definition: FileReference.php:172
‪TYPO3\CMS\Core\Resource\FileReference\__wakeup
‪__wakeup()
Definition: FileReference.php:496
‪TYPO3\CMS\Core\Resource\AbstractFile\getCreationTime
‪getCreationTime()
Definition: AbstractFile.php:219
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:52
‪TYPO3\CMS\Core\Resource\FileReference\getSize
‪int< 0, getSize():int { return $this-> originalFile getSize()
‪TYPO3\CMS\Core\Resource\FileReference\getCreationTime
‪getCreationTime()
Definition: FileReference.php:297
‪TYPO3\CMS\Core\Resource\FileReference\getReferenceProperty
‪mixed getReferenceProperty($key)
Definition: FileReference.php:124
‪TYPO3\CMS\Core\Resource\FileReference\getHashedIdentifier
‪non empty string getHashedIdentifier()
Definition: FileReference.php:473
‪TYPO3\CMS\Core\Resource\FileReference\restoreNonNullValuesCallback
‪restoreNonNullValuesCallback(&$value, $key)
Definition: FileReference.php:160
‪TYPO3\CMS\Core\Resource\FileReference\__construct
‪__construct(array $fileReferenceData, $factory=null)
Definition: FileReference.php:68
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:129
‪TYPO3\CMS\Core\Resource\File\setContents
‪$this setContents(string $contents)
Definition: File.php:127
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:26
‪TYPO3\CMS\Core\Resource\AbstractFile\getNameWithoutExtension
‪getNameWithoutExtension()
Definition: AbstractFile.php:167
‪TYPO3\CMS\Core\Resource\FileReference\getName
‪getName()
Definition: FileReference.php:177
‪TYPO3\CMS\Core\Resource\FileReference\getFileObject
‪getFileObject(int $uidLocal, ResourceFactory $factory=null)
Definition: FileReference.php:81
‪TYPO3\CMS\Core\Resource\FolderInterface
Definition: FolderInterface.php:24
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Resource\AbstractFile\getModificationTime
‪getModificationTime()
Definition: AbstractFile.php:232
‪TYPO3\CMS\Core\Resource\FileReference\getStorage
‪getStorage()
Definition: FileReference.php:351
‪TYPO3\CMS\Core\Resource\FileReference\getCombinedIdentifier
‪string getCombinedIdentifier()
Definition: FileReference.php:371
‪TYPO3\CMS\Core\Resource\AbstractFile\getIdentifier
‪getIdentifier()
Definition: AbstractFile.php:144
‪TYPO3\CMS\Core\Resource\FileReference\$originalFile
‪FileInterface $originalFile
Definition: FileReference.php:49
‪TYPO3\CMS\Core\Resource\FileReference\rename
‪rename(string $newName, $conflictMode=DuplicationBehavior::RENAME)
Definition: FileReference.php:400
‪TYPO3\CMS\Core\Resource\FileReference\hasProperty
‪hasProperty(string $key)
Definition: FileReference.php:97
‪TYPO3\CMS\Core\Resource\FileReference\getModificationTime
‪getModificationTime()
Definition: FileReference.php:289