‪TYPO3CMS  11.5
FileReference.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 
20 
33 {
41 
47  protected ‪$originalFile;
48 
56  protected ‪$mergedProperties = [];
57 
68  public function ‪__construct(array $fileReferenceData, $factory = null)
69  {
70  $this->propertiesOfFileReference = $fileReferenceData;
71  if (!$fileReferenceData['uid_local']) {
72  throw new \InvalidArgumentException('Incorrect reference to original file given for FileReference.', 1300098528);
73  }
74  $this->originalFile = $this->‪getFileObject((int)$fileReferenceData['uid_local'], $factory);
75  }
76 
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  *******************************/
101  public function ‪hasProperty($key)
102  {
103  return array_key_exists($key, $this->‪getProperties());
104  }
105 
113  public function ‪getProperty($key)
114  {
115  if (!$this->‪hasProperty($key)) {
116  throw new \InvalidArgumentException('Property "' . $key . '" was not found in file reference or original file.', 1314226805);
117  }
118  $properties = $this->‪getProperties();
119  return $properties[$key];
120  }
121 
129  public function ‪getReferenceProperty($key)
130  {
131  if (!array_key_exists($key, $this->propertiesOfFileReference)) {
132  throw new \InvalidArgumentException('Property "' . $key . '" of file reference was not found.', 1360684914);
133  }
134  return $this->propertiesOfFileReference[$key];
135  }
136 
142  public function ‪getProperties()
143  {
144  if (empty($this->mergedProperties)) {
145  $this->mergedProperties = ‪$this->propertiesOfFileReference;
147  $this->mergedProperties,
148  $this->originalFile->getProperties(),
149  true,
150  true,
151  false
152  );
153  array_walk($this->mergedProperties, [$this, 'restoreNonNullValuesCallback']);
154  }
155 
157  }
158 
165  protected function ‪restoreNonNullValuesCallback(&$value, $key)
166  {
167  if (array_key_exists($key, $this->propertiesOfFileReference) && $this->propertiesOfFileReference[$key] !== null) {
168  $value = $this->propertiesOfFileReference[$key];
169  }
170  }
171 
177  public function ‪getReferenceProperties()
178  {
180  }
181 
187  public function ‪getName()
188  {
189  return $this->originalFile->getName();
190  }
191 
199  public function ‪getTitle()
200  {
201  return (string)$this->‪getProperty('title');
202  }
203 
211  public function ‪getAlternative()
212  {
213  return (string)$this->‪getProperty('alternative');
214  }
215 
223  public function ‪getDescription()
224  {
225  return (string)$this->‪getProperty('description');
226  }
227 
235  public function ‪getLink()
236  {
237  return $this->propertiesOfFileReference['link'];
238  }
239 
245  public function ‪getUid()
246  {
247  return (int)$this->propertiesOfFileReference['uid'];
248  }
249 
255  public function ‪getSize()
256  {
257  return (int)$this->originalFile->getSize();
258  }
259 
265  public function ‪getSha1()
266  {
267  return $this->originalFile->getSha1();
268  }
269 
275  public function ‪getExtension()
276  {
277  return $this->originalFile->getExtension();
278  }
279 
285  public function ‪getNameWithoutExtension()
286  {
287  return $this->originalFile->getNameWithoutExtension();
288  }
289 
295  public function ‪getMimeType()
296  {
297  return $this->originalFile->getMimeType();
298  }
299 
305  public function ‪getModificationTime()
306  {
307  return (int)$this->originalFile->getModificationTime();
308  }
309 
315  public function ‪getCreationTime()
316  {
317  return (int)$this->originalFile->getCreationTime();
318  }
319 
325  public function ‪getType()
326  {
327  return (int)$this->originalFile->getType();
328  }
329 
335  public function ‪isMissing()
336  {
337  return (bool)$this->originalFile->getProperty('missing');
338  }
339 
340  /******************
341  * CONTENTS RELATED
342  ******************/
348  public function ‪getContents()
349  {
350  return $this->originalFile->getContents();
351  }
352 
359  public function ‪setContents($contents)
360  {
361  return $this->originalFile->‪setContents($contents);
362  }
363 
364  /****************************************
365  * STORAGE AND MANAGEMENT RELATED METHODS
366  ****************************************/
372  public function ‪getStorage()
373  {
374  return $this->originalFile->getStorage();
375  }
376 
382  public function ‪getIdentifier()
383  {
384  return $this->originalFile->getIdentifier();
385  }
386 
392  public function ‪getCombinedIdentifier()
393  {
394  return $this->originalFile->getCombinedIdentifier();
395  }
396 
403  public function delete()
404  {
405  // @todo Implement this function. This should only delete the
406  // FileReference (sys_file_reference) record, not the file itself.
407  throw new \BadMethodCallException('Function not implemented FileReference::delete().', 1333754461);
408  //return $this->fileRepository->removeUsageRecord($this);
409  }
410 
417  public function ‪rename($newName, $conflictMode = ‪DuplicationBehavior::RENAME)
418  {
419  // @todo Implement this function. This should only rename the
420  // FileReference (sys_file_reference) record, not the file itself.
421  throw new \BadMethodCallException('Function not implemented FileReference::rename().', 1333754473);
422  //return $this->fileRepository->renameUsageRecord($this, $newName);
423  }
424 
425  /*****************
426  * SPECIAL METHODS
427  *****************/
437  public function ‪getPublicUrl($relativeToCurrentScript = false)
438  {
439  // @deprecated $relativeToCurrentScript since v11, will be removed in TYPO3 v12.0
440  return $this->originalFile->getPublicUrl($relativeToCurrentScript);
441  }
442 
451  public function ‪isIndexed()
452  {
453  return true;
454  }
455 
464  public function ‪getForLocalProcessing($writable = true)
465  {
466  return $this->originalFile->getForLocalProcessing($writable);
467  }
468 
475  public function ‪toArray()
476  {
477  $array = array_merge($this->originalFile->toArray(), $this->propertiesOfFileReference);
478  return $array;
479  }
480 
486  public function ‪getOriginalFile()
487  {
488  return ‪$this->originalFile;
489  }
490 
496  public function ‪getHashedIdentifier()
497  {
498  return $this->‪getStorage()->‪hashFileIdentifier($this->‪getIdentifier());
499  }
500 
506  public function ‪getParentFolder()
507  {
508  return $this->originalFile->‪getParentFolder();
509  }
510 
517  public function ‪__sleep(): array
518  {
519  $keys = get_object_vars($this);
520  unset($keys['originalFile'], $keys['mergedProperties']);
521  return array_keys($keys);
522  }
523 
524  public function ‪__wakeup(): void
525  {
526  $factory = GeneralUtility::makeInstance(ResourceFactory::class);
527  $this->originalFile = $this->‪getFileObject(
528  (int)$this->propertiesOfFileReference['uid_local'],
529  $factory
530  );
531  }
532 }
‪TYPO3\CMS\Core\Resource\FileReference\getAlternative
‪string getAlternative()
Definition: FileReference.php:208
‪TYPO3\CMS\Core\Resource\FileReference\getForLocalProcessing
‪string getForLocalProcessing($writable=true)
Definition: FileReference.php:461
‪TYPO3\CMS\Core\Resource\FileReference\getSize
‪int getSize()
Definition: FileReference.php:252
‪TYPO3\CMS\Core\Resource\FileReference\getFileObject
‪FileInterface getFileObject(int $uidLocal, ResourceFactory $factory=null)
Definition: FileReference.php:81
‪TYPO3\CMS\Core\Resource\File\setContents
‪File setContents($contents)
Definition: File.php:136
‪TYPO3\CMS\Core\Resource\FileReference\$mergedProperties
‪array $mergedProperties
Definition: FileReference.php:53
‪TYPO3\CMS\Core\Resource\FileReference\getName
‪string getName()
Definition: FileReference.php:184
‪TYPO3\CMS\Core\Resource\FileReference\$propertiesOfFileReference
‪array $propertiesOfFileReference
Definition: FileReference.php:39
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:22
‪TYPO3\CMS\Core\Resource\FileReference\getCreationTime
‪int getCreationTime()
Definition: FileReference.php:312
‪TYPO3\CMS\Core\Resource\FileReference\getDescription
‪string getDescription()
Definition: FileReference.php:220
‪TYPO3\CMS\Core\Resource\FileReference\getProperty
‪mixed getProperty($key)
Definition: FileReference.php:110
‪TYPO3\CMS\Core\Resource\FileReference\getPublicUrl
‪string null getPublicUrl($relativeToCurrentScript=false)
Definition: FileReference.php:434
‪TYPO3\CMS\Core\Resource\FileReference\getSha1
‪string getSha1()
Definition: FileReference.php:262
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:33
‪TYPO3\CMS\Core\Resource\FileReference\hasProperty
‪bool hasProperty($key)
Definition: FileReference.php:98
‪TYPO3\CMS\Core\Resource\FileReference\isMissing
‪bool isMissing()
Definition: FileReference.php:332
‪TYPO3\CMS\Core\Utility\ArrayUtility\mergeRecursiveWithOverrule
‪static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
Definition: ArrayUtility.php:654
‪TYPO3\CMS\Core\Resource\FileReference\__sleep
‪string[] __sleep()
Definition: FileReference.php:514
‪TYPO3\CMS\Core\Resource\FileReference\getExtension
‪string getExtension()
Definition: FileReference.php:272
‪TYPO3\CMS\Core\Resource\FileReference\getProperties
‪array getProperties()
Definition: FileReference.php:139
‪TYPO3\CMS\Core\Resource\FileReference\getIdentifier
‪string getIdentifier()
Definition: FileReference.php:379
‪TYPO3\CMS\Core\Resource\ResourceStorage\hashFileIdentifier
‪string hashFileIdentifier($file)
Definition: ResourceStorage.php:1355
‪TYPO3\CMS\Core\Resource\FileReference\getMimeType
‪string getMimeType()
Definition: FileReference.php:292
‪TYPO3\CMS\Core\Resource\FileReference\getLink
‪string getLink()
Definition: FileReference.php:232
‪TYPO3\CMS\Core\Resource\FileReference\getModificationTime
‪int getModificationTime()
Definition: FileReference.php:302
‪TYPO3\CMS\Core\Resource\FileReference\getUid
‪int getUid()
Definition: FileReference.php:242
‪TYPO3\CMS\Core\Resource\FileReference\getType
‪int getType()
Definition: FileReference.php:322
‪TYPO3\CMS\Core\Resource\FileReference\getOriginalFile
‪File getOriginalFile()
Definition: FileReference.php:483
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Resource\DuplicationBehavior\RENAME
‪const RENAME
Definition: DuplicationBehavior.php:32
‪TYPO3\CMS\Core\Resource\FileReference\isIndexed
‪bool isIndexed()
Definition: FileReference.php:448
‪TYPO3\CMS\Core\Resource\FileReference\getHashedIdentifier
‪string getHashedIdentifier()
Definition: FileReference.php:493
‪TYPO3\CMS\Core\Resource\FileReference\getTitle
‪string getTitle()
Definition: FileReference.php:196
‪TYPO3\CMS\Core\Resource\FileReference\getReferenceProperties
‪array getReferenceProperties()
Definition: FileReference.php:174
‪TYPO3\CMS\Core\Resource\FileReference\__wakeup
‪__wakeup()
Definition: FileReference.php:521
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:52
‪TYPO3\CMS\Core\Resource\FileReference\getReferenceProperty
‪mixed getReferenceProperty($key)
Definition: FileReference.php:126
‪TYPO3\CMS\Core\Resource\FileReference\getContents
‪string getContents()
Definition: FileReference.php:345
‪TYPO3\CMS\Core\Resource\FileReference\restoreNonNullValuesCallback
‪restoreNonNullValuesCallback(&$value, $key)
Definition: FileReference.php:162
‪TYPO3\CMS\Core\Resource\FileReference\__construct
‪__construct(array $fileReferenceData, $factory=null)
Definition: FileReference.php:65
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:125
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:24
‪TYPO3\CMS\Core\Resource\FileReference\getNameWithoutExtension
‪string getNameWithoutExtension()
Definition: FileReference.php:282
‪TYPO3\CMS\Core\Resource\FolderInterface
Definition: FolderInterface.php:22
‪TYPO3\CMS\Core\Resource\FileReference\getParentFolder
‪FolderInterface getParentFolder()
Definition: FileReference.php:503
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Resource\FileReference\getStorage
‪ResourceStorage getStorage()
Definition: FileReference.php:369
‪TYPO3\CMS\Core\Resource\ResourceInterface\getParentFolder
‪FolderInterface getParentFolder()
‪TYPO3\CMS\Core\Resource\FileReference\toArray
‪array toArray()
Definition: FileReference.php:472
‪TYPO3\CMS\Core\Resource\FileReference\getCombinedIdentifier
‪string getCombinedIdentifier()
Definition: FileReference.php:389
‪TYPO3\CMS\Core\Resource\FileReference\$originalFile
‪FileInterface $originalFile
Definition: FileReference.php:45
‪TYPO3\CMS\Core\Resource\FileReference\setContents
‪File setContents($contents)
Definition: FileReference.php:356
‪TYPO3\CMS\Core\Resource\FileReference\rename
‪rename($newName, $conflictMode=DuplicationBehavior::RENAME)
Definition: FileReference.php:414