‪TYPO3CMS  10.4
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 
48  protected ‪$name;
49 
55  protected ‪$originalFile;
56 
64  protected ‪$mergedProperties = [];
65 
76  public function ‪__construct(array $fileReferenceData, $factory = null)
77  {
78  $this->propertiesOfFileReference = $fileReferenceData;
79  if (!$fileReferenceData['uid_local']) {
80  throw new \InvalidArgumentException('Incorrect reference to original file given for FileReference.', 1300098528);
81  }
82  $this->originalFile = $this->‪getFileObject((int)$fileReferenceData['uid_local'], $factory);
83  $this->name = $fileReferenceData['name'] !== '' ? $fileReferenceData['name'] : $this->originalFile->getName();
84  }
85 
93  private function ‪getFileObject(int $uidLocal, ‪ResourceFactory $factory = null): ‪FileInterface
94  {
95  if ($factory === null) {
96  $factory = GeneralUtility::makeInstance(ResourceFactory::class);
97  }
98  return $factory->getFileObject($uidLocal);
99  }
100 
101  /*******************************
102  * VARIOUS FILE PROPERTY GETTERS
103  *******************************/
110  public function ‪hasProperty($key)
111  {
112  return array_key_exists($key, $this->‪getProperties());
113  }
114 
122  public function ‪getProperty($key)
123  {
124  if (!$this->‪hasProperty($key)) {
125  throw new \InvalidArgumentException('Property "' . $key . '" was not found in file reference or original file.', 1314226805);
126  }
127  $properties = $this->‪getProperties();
128  return $properties[$key];
129  }
130 
138  public function ‪getReferenceProperty($key)
139  {
140  if (!array_key_exists($key, $this->propertiesOfFileReference)) {
141  throw new \InvalidArgumentException('Property "' . $key . '" of file reference was not found.', 1360684914);
142  }
143  return $this->propertiesOfFileReference[$key];
144  }
145 
151  public function ‪getProperties()
152  {
153  if (empty($this->mergedProperties)) {
154  $this->mergedProperties = ‪$this->propertiesOfFileReference;
156  $this->mergedProperties,
157  $this->originalFile->getProperties(),
158  true,
159  true,
160  false
161  );
162  array_walk($this->mergedProperties, [$this, 'restoreNonNullValuesCallback']);
163  }
164 
166  }
167 
174  protected function ‪restoreNonNullValuesCallback(&$value, $key)
175  {
176  if (array_key_exists($key, $this->propertiesOfFileReference) && $this->propertiesOfFileReference[$key] !== null) {
177  $value = $this->propertiesOfFileReference[$key];
178  }
179  }
180 
186  public function ‪getReferenceProperties()
187  {
189  }
190 
196  public function ‪getName()
197  {
198  return $this->originalFile->getName();
199  }
200 
208  public function ‪getTitle()
209  {
210  return $this->‪getProperty('title');
211  }
212 
220  public function ‪getAlternative()
221  {
222  return $this->‪getProperty('alternative');
223  }
224 
232  public function ‪getDescription()
233  {
234  return $this->‪getProperty('description');
235  }
236 
244  public function ‪getLink()
245  {
246  return $this->propertiesOfFileReference['link'];
247  }
248 
254  public function ‪getUid()
255  {
256  return (int)$this->propertiesOfFileReference['uid'];
257  }
258 
264  public function ‪getSize()
265  {
266  return (int)$this->originalFile->getSize();
267  }
268 
274  public function ‪getSha1()
275  {
276  return $this->originalFile->getSha1();
277  }
278 
284  public function ‪getExtension()
285  {
286  return $this->originalFile->getExtension();
287  }
288 
294  public function ‪getNameWithoutExtension()
295  {
296  return $this->originalFile->getNameWithoutExtension();
297  }
298 
304  public function ‪getMimeType()
305  {
306  return $this->originalFile->getMimeType();
307  }
308 
314  public function ‪getModificationTime()
315  {
316  return (int)$this->originalFile->getModificationTime();
317  }
318 
324  public function ‪getCreationTime()
325  {
326  return (int)$this->originalFile->getCreationTime();
327  }
328 
334  public function ‪getType()
335  {
336  return (int)$this->originalFile->getType();
337  }
338 
344  public function ‪isMissing()
345  {
346  return (bool)$this->originalFile->getProperty('missing');
347  }
348 
349  /******************
350  * CONTENTS RELATED
351  ******************/
357  public function ‪getContents()
358  {
359  return $this->originalFile->getContents();
360  }
361 
368  public function ‪setContents($contents)
369  {
370  return $this->originalFile->‪setContents($contents);
371  }
372 
373  /****************************************
374  * STORAGE AND MANAGEMENT RELATED METHODS
375  ****************************************/
381  public function ‪getStorage()
382  {
383  return $this->originalFile->getStorage();
384  }
385 
391  public function ‪getIdentifier()
392  {
393  return $this->originalFile->getIdentifier();
394  }
395 
401  public function ‪getCombinedIdentifier()
402  {
403  return $this->originalFile->getCombinedIdentifier();
404  }
405 
412  public function delete()
413  {
414  // @todo Implement this function. This should only delete the
415  // FileReference (sys_file_reference) record, not the file itself.
416  throw new \BadMethodCallException('Function not implemented FileReference::delete().', 1333754461);
417  //return $this->fileRepository->removeUsageRecord($this);
418  }
419 
426  public function ‪rename($newName, $conflictMode = ‪DuplicationBehavior::RENAME)
427  {
428  // @todo Implement this function. This should only rename the
429  // FileReference (sys_file_reference) record, not the file itself.
430  throw new \BadMethodCallException('Function not implemented FileReference::rename().', 1333754473);
431  //return $this->fileRepository->renameUsageRecord($this, $newName);
432  }
433 
434  /*****************
435  * SPECIAL METHODS
436  *****************/
446  public function ‪getPublicUrl($relativeToCurrentScript = false)
447  {
448  return $this->originalFile->getPublicUrl($relativeToCurrentScript);
449  }
450 
459  public function ‪isIndexed()
460  {
461  return true;
462  }
463 
472  public function ‪getForLocalProcessing($writable = true)
473  {
474  return $this->originalFile->getForLocalProcessing($writable);
475  }
476 
483  public function ‪toArray()
484  {
485  $array = array_merge($this->originalFile->toArray(), $this->propertiesOfFileReference);
486  return $array;
487  }
488 
494  public function ‪getOriginalFile()
495  {
496  return ‪$this->originalFile;
497  }
498 
504  public function ‪getHashedIdentifier()
505  {
506  return $this->‪getStorage()->‪hashFileIdentifier($this->‪getIdentifier());
507  }
508 
514  public function ‪getParentFolder()
515  {
516  return $this->originalFile->‪getParentFolder();
517  }
518 
525  public function ‪__sleep(): array
526  {
527  $keys = get_object_vars($this);
528  unset($keys['originalFile'], $keys['mergedProperties']);
529  return array_keys($keys);
530  }
531 
532  public function ‪__wakeup(): void
533  {
534  $factory = GeneralUtility::makeInstance(ResourceFactory::class);
535  $this->originalFile = $this->‪getFileObject(
536  (int)$this->propertiesOfFileReference['uid_local'],
537  $factory
538  );
539  }
540 }
‪TYPO3\CMS\Core\Resource\FileReference\getAlternative
‪string getAlternative()
Definition: FileReference.php:216
‪TYPO3\CMS\Core\Resource\FileReference\getForLocalProcessing
‪string getForLocalProcessing($writable=true)
Definition: FileReference.php:468
‪TYPO3\CMS\Core\Resource\FileReference\getSize
‪int getSize()
Definition: FileReference.php:260
‪TYPO3\CMS\Core\Resource\FileReference\getFileObject
‪FileInterface getFileObject(int $uidLocal, ResourceFactory $factory=null)
Definition: FileReference.php:89
‪TYPO3\CMS\Core\Resource\File\setContents
‪File setContents($contents)
Definition: File.php:152
‪TYPO3\CMS\Core\Resource\FileReference\$mergedProperties
‪array $mergedProperties
Definition: FileReference.php:60
‪TYPO3\CMS\Core\Resource\FileReference\getName
‪string getName()
Definition: FileReference.php:192
‪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:320
‪TYPO3\CMS\Core\Resource\FileReference\getDescription
‪string getDescription()
Definition: FileReference.php:228
‪TYPO3\CMS\Core\Resource\FileReference\getProperty
‪mixed getProperty($key)
Definition: FileReference.php:118
‪TYPO3\CMS\Core\Resource\FileReference\getPublicUrl
‪string null getPublicUrl($relativeToCurrentScript=false)
Definition: FileReference.php:442
‪TYPO3\CMS\Core\Resource\FileReference\getSha1
‪string getSha1()
Definition: FileReference.php:270
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:33
‪TYPO3\CMS\Core\Resource\FileReference\hasProperty
‪bool hasProperty($key)
Definition: FileReference.php:106
‪TYPO3\CMS\Core\Resource\FileReference\isMissing
‪bool isMissing()
Definition: FileReference.php:340
‪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:521
‪TYPO3\CMS\Core\Resource\FileReference\getExtension
‪string getExtension()
Definition: FileReference.php:280
‪TYPO3\CMS\Core\Resource\FileReference\getProperties
‪array getProperties()
Definition: FileReference.php:147
‪TYPO3\CMS\Core\Resource\FileReference\getIdentifier
‪string getIdentifier()
Definition: FileReference.php:387
‪TYPO3\CMS\Core\Resource\ResourceStorage\hashFileIdentifier
‪string hashFileIdentifier($file)
Definition: ResourceStorage.php:1350
‪TYPO3\CMS\Core\Resource\FileReference\getMimeType
‪string getMimeType()
Definition: FileReference.php:300
‪TYPO3\CMS\Core\Resource\FileReference\getLink
‪string getLink()
Definition: FileReference.php:240
‪TYPO3\CMS\Core\Resource\FileReference\getModificationTime
‪int getModificationTime()
Definition: FileReference.php:310
‪TYPO3\CMS\Core\Resource\FileReference\getUid
‪int getUid()
Definition: FileReference.php:250
‪TYPO3\CMS\Core\Resource\FileReference\getType
‪int getType()
Definition: FileReference.php:330
‪TYPO3\CMS\Core\Resource\FileReference\getOriginalFile
‪File getOriginalFile()
Definition: FileReference.php:490
‪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:455
‪TYPO3\CMS\Core\Resource\FileReference\getHashedIdentifier
‪string getHashedIdentifier()
Definition: FileReference.php:500
‪TYPO3\CMS\Core\Resource\FileReference\getTitle
‪string getTitle()
Definition: FileReference.php:204
‪TYPO3\CMS\Core\Resource\FileReference\getReferenceProperties
‪array getReferenceProperties()
Definition: FileReference.php:182
‪TYPO3\CMS\Core\Resource\FileReference\__wakeup
‪__wakeup()
Definition: FileReference.php:528
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:52
‪TYPO3\CMS\Core\Resource\FileReference\$name
‪string $name
Definition: FileReference.php:46
‪TYPO3\CMS\Core\Resource\FileReference\getReferenceProperty
‪mixed getReferenceProperty($key)
Definition: FileReference.php:134
‪TYPO3\CMS\Core\Resource\FileReference\getContents
‪string getContents()
Definition: FileReference.php:353
‪TYPO3\CMS\Core\Resource\FileReference\restoreNonNullValuesCallback
‪restoreNonNullValuesCallback(&$value, $key)
Definition: FileReference.php:170
‪TYPO3\CMS\Core\Resource\FileReference\__construct
‪__construct(array $fileReferenceData, $factory=null)
Definition: FileReference.php:72
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:122
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:24
‪TYPO3\CMS\Core\Resource\FileReference\getNameWithoutExtension
‪string getNameWithoutExtension()
Definition: FileReference.php:290
‪TYPO3\CMS\Core\Resource\FolderInterface
Definition: FolderInterface.php:22
‪TYPO3\CMS\Core\Resource\FileReference\getParentFolder
‪FolderInterface getParentFolder()
Definition: FileReference.php:510
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Resource\FileReference\$originalFile
‪File $originalFile
Definition: FileReference.php:52
‪TYPO3\CMS\Core\Resource\FileReference\getStorage
‪ResourceStorage getStorage()
Definition: FileReference.php:377
‪TYPO3\CMS\Core\Resource\ResourceInterface\getParentFolder
‪FolderInterface getParentFolder()
‪TYPO3\CMS\Core\Resource\FileReference\toArray
‪array toArray()
Definition: FileReference.php:479
‪TYPO3\CMS\Core\Resource\FileReference\getCombinedIdentifier
‪string getCombinedIdentifier()
Definition: FileReference.php:397
‪TYPO3\CMS\Core\Resource\FileReference\setContents
‪File setContents($contents)
Definition: FileReference.php:364
‪TYPO3\CMS\Core\Resource\FileReference\rename
‪rename($newName, $conflictMode=DuplicationBehavior::RENAME)
Definition: FileReference.php:422