‪TYPO3CMS  9.5
FileReference.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
18 
31 {
39 
46  protected ‪$name;
47 
53  protected ‪$originalFile;
54 
62  protected ‪$mergedProperties = [];
63 
74  public function ‪__construct(array $fileReferenceData, $factory = null)
75  {
76  $this->propertiesOfFileReference = $fileReferenceData;
77  if (!$fileReferenceData['uid_local']) {
78  throw new \InvalidArgumentException('Incorrect reference to original file given for FileReference.', 1300098528);
79  }
80  $this->originalFile = $this->‪getFileObject((int)$fileReferenceData['uid_local'], $factory);
81  $this->name = $fileReferenceData['name'] !== '' ? $fileReferenceData['name'] : $this->originalFile->getName();
82  }
83 
91  private function ‪getFileObject(int $uidLocal, ‪ResourceFactory $factory = null): ‪FileInterface
92  {
93  if ($factory === null) {
96  }
97  return $factory->getFileObject($uidLocal);
98  }
99 
100  /*******************************
101  * VARIOUS FILE PROPERTY GETTERS
102  *******************************/
109  public function ‪hasProperty($key)
110  {
111  return array_key_exists($key, $this->‪getProperties());
112  }
113 
121  public function ‪getProperty($key)
122  {
123  if (!$this->‪hasProperty($key)) {
124  throw new \InvalidArgumentException('Property "' . $key . '" was not found in file reference or original file.', 1314226805);
125  }
126  $properties = $this->‪getProperties();
127  return $properties[$key];
128  }
129 
137  public function ‪getReferenceProperty($key)
138  {
139  if (!array_key_exists($key, $this->propertiesOfFileReference)) {
140  throw new \InvalidArgumentException('Property "' . $key . '" of file reference was not found.', 1360684914);
141  }
142  return $this->propertiesOfFileReference[$key];
143  }
144 
150  public function ‪getProperties()
151  {
152  if (empty($this->mergedProperties)) {
153  $this->mergedProperties = ‪$this->propertiesOfFileReference;
155  $this->mergedProperties,
156  $this->originalFile->getProperties(),
157  true,
158  true,
159  false
160  );
161  array_walk($this->mergedProperties, [$this, 'restoreNonNullValuesCallback']);
162  }
163 
165  }
166 
173  protected function ‪restoreNonNullValuesCallback(&$value, $key)
174  {
175  if (array_key_exists($key, $this->propertiesOfFileReference) && $this->propertiesOfFileReference[$key] !== null) {
176  $value = $this->propertiesOfFileReference[$key];
177  }
178  }
179 
185  public function ‪getReferenceProperties()
186  {
188  }
189 
195  public function ‪getName()
196  {
197  return $this->originalFile->getName();
198  }
199 
207  public function ‪getTitle()
208  {
209  return $this->‪getProperty('title');
210  }
211 
219  public function ‪getAlternative()
220  {
221  return $this->‪getProperty('alternative');
222  }
223 
231  public function ‪getDescription()
232  {
233  return $this->‪getProperty('description');
234  }
235 
243  public function ‪getLink()
244  {
245  return $this->propertiesOfFileReference['link'];
246  }
247 
253  public function ‪getUid()
254  {
255  return (int)$this->propertiesOfFileReference['uid'];
256  }
257 
263  public function ‪getSize()
264  {
265  return (int)$this->originalFile->getSize();
266  }
267 
273  public function ‪getSha1()
274  {
275  return $this->originalFile->getSha1();
276  }
277 
283  public function ‪getExtension()
284  {
285  return $this->originalFile->getExtension();
286  }
287 
293  public function ‪getNameWithoutExtension()
294  {
295  return $this->originalFile->getNameWithoutExtension();
296  }
297 
303  public function ‪getMimeType()
304  {
305  return $this->originalFile->getMimeType();
306  }
307 
313  public function ‪getModificationTime()
314  {
315  return (int)$this->originalFile->getModificationTime();
316  }
317 
323  public function ‪getCreationTime()
324  {
325  return (int)$this->originalFile->getCreationTime();
326  }
327 
333  public function ‪getType()
334  {
335  return (int)$this->originalFile->getType();
336  }
337 
343  public function ‪isMissing()
344  {
345  return (bool)$this->originalFile->getProperty('missing');
346  }
347 
348  /******************
349  * CONTENTS RELATED
350  ******************/
356  public function ‪getContents()
357  {
358  return $this->originalFile->getContents();
359  }
360 
367  public function ‪setContents($contents)
368  {
369  return $this->originalFile->‪setContents($contents);
370  }
371 
372  /****************************************
373  * STORAGE AND MANAGEMENT RELATED METHDOS
374  ****************************************/
380  public function ‪getStorage()
381  {
382  return $this->originalFile->getStorage();
383  }
384 
390  public function ‪getIdentifier()
391  {
392  return $this->originalFile->getIdentifier();
393  }
394 
400  public function ‪getCombinedIdentifier()
401  {
402  return $this->originalFile->getCombinedIdentifier();
403  }
404 
411  public function delete()
412  {
413  // @todo Implement this function. This should only delete the
414  // FileReference (sys_file_reference) record, not the file itself.
415  throw new \BadMethodCallException('Function not implemented FileReference::delete().', 1333754461);
416  //return $this->fileRepository->removeUsageRecord($this);
417  }
418 
425  public function ‪rename($newName, $conflictMode = ‪DuplicationBehavior::RENAME)
426  {
427  // @todo Implement this function. This should only rename the
428  // FileReference (sys_file_reference) record, not the file itself.
429  throw new \BadMethodCallException('Function not implemented FileReference::rename().', 1333754473);
430  //return $this->fileRepository->renameUsageRecord($this, $newName);
431  }
432 
433  /*****************
434  * SPECIAL METHODS
435  *****************/
445  public function ‪getPublicUrl($relativeToCurrentScript = false)
446  {
447  return $this->originalFile->getPublicUrl($relativeToCurrentScript);
448  }
449 
458  public function ‪isIndexed()
459  {
460  return true;
461  }
462 
471  public function ‪getForLocalProcessing($writable = true)
472  {
473  return $this->originalFile->getForLocalProcessing($writable);
474  }
475 
482  public function ‪toArray()
483  {
484  $array = array_merge($this->originalFile->toArray(), $this->propertiesOfFileReference);
485  return $array;
486  }
487 
493  public function ‪getOriginalFile()
494  {
495  return ‪$this->originalFile;
496  }
497 
503  public function ‪getHashedIdentifier()
504  {
505  return $this->‪getStorage()->‪hashFileIdentifier($this->‪getIdentifier());
506  }
507 
513  public function ‪getParentFolder()
514  {
515  return $this->originalFile->‪getParentFolder();
516  }
517 
524  public function ‪__sleep(): array
525  {
526  $keys = get_object_vars($this);
527  unset($keys['originalFile'], $keys['mergedProperties']);
528  return array_keys($keys);
529  }
530 
531  public function ‪__wakeup(): void
532  {
533  $factory = GeneralUtility::makeInstance(ResourceFactory::class);
534  $this->originalFile = $this->‪getFileObject(
535  (int)$this->propertiesOfFileReference['uid_local'],
536  $factory
537  );
538  }
539 }
‪TYPO3\CMS\Core\Resource\FileReference\getAlternative
‪string getAlternative()
Definition: FileReference.php:215
‪TYPO3\CMS\Core\Resource\FileReference\getForLocalProcessing
‪string getForLocalProcessing($writable=true)
Definition: FileReference.php:467
‪TYPO3\CMS\Core\Resource\FileReference\getSize
‪int getSize()
Definition: FileReference.php:259
‪TYPO3\CMS\Core\Resource\FileReference\getFileObject
‪FileInterface getFileObject(int $uidLocal, ResourceFactory $factory=null)
Definition: FileReference.php:87
‪TYPO3\CMS\Core\Resource\File\setContents
‪File setContents($contents)
Definition: File.php:154
‪TYPO3\CMS\Core\Resource\FileReference\$mergedProperties
‪array $mergedProperties
Definition: FileReference.php:58
‪TYPO3\CMS\Core\Resource\FileReference\getName
‪string getName()
Definition: FileReference.php:191
‪TYPO3\CMS\Core\Resource\FileReference\$propertiesOfFileReference
‪array $propertiesOfFileReference
Definition: FileReference.php:37
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:21
‪TYPO3\CMS\Core\Resource\FileReference\getCreationTime
‪int getCreationTime()
Definition: FileReference.php:319
‪TYPO3\CMS\Core\Resource\FileReference\getDescription
‪string getDescription()
Definition: FileReference.php:227
‪TYPO3\CMS\Core\Resource\FileReference\getProperty
‪mixed getProperty($key)
Definition: FileReference.php:117
‪TYPO3\CMS\Core\Resource\FileReference\getPublicUrl
‪string null getPublicUrl($relativeToCurrentScript=false)
Definition: FileReference.php:441
‪TYPO3\CMS\Core\Resource\FileReference\getSha1
‪string getSha1()
Definition: FileReference.php:269
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:31
‪TYPO3\CMS\Core\Resource\FileReference\hasProperty
‪bool hasProperty($key)
Definition: FileReference.php:105
‪TYPO3\CMS\Core\Resource\ResourceFactory\getInstance
‪static ResourceFactory getInstance()
Definition: ResourceFactory.php:39
‪TYPO3\CMS\Core\Resource\FileReference\isMissing
‪bool isMissing()
Definition: FileReference.php:339
‪TYPO3\CMS\Core\Utility\ArrayUtility\mergeRecursiveWithOverrule
‪static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
Definition: ArrayUtility.php:614
‪TYPO3\CMS\Core\Resource\FileReference\__sleep
‪string[] __sleep()
Definition: FileReference.php:520
‪TYPO3\CMS\Core\Resource\FileReference\getExtension
‪string getExtension()
Definition: FileReference.php:279
‪TYPO3\CMS\Core\Resource\FileReference\getProperties
‪array getProperties()
Definition: FileReference.php:146
‪TYPO3\CMS\Core\Resource\FileReference\getIdentifier
‪string getIdentifier()
Definition: FileReference.php:386
‪TYPO3\CMS\Core\Resource\ResourceStorage\hashFileIdentifier
‪string hashFileIdentifier($file)
Definition: ResourceStorage.php:1298
‪TYPO3\CMS\Core\Resource\FileReference\getMimeType
‪string getMimeType()
Definition: FileReference.php:299
‪TYPO3\CMS\Core\Resource\FileReference\getLink
‪string getLink()
Definition: FileReference.php:239
‪TYPO3\CMS\Core\Resource\FileReference\getModificationTime
‪int getModificationTime()
Definition: FileReference.php:309
‪TYPO3\CMS\Core\Resource\FileReference\getUid
‪int getUid()
Definition: FileReference.php:249
‪TYPO3\CMS\Core\Resource\FileReference\getType
‪int getType()
Definition: FileReference.php:329
‪TYPO3\CMS\Core\Resource\FileReference\getOriginalFile
‪File getOriginalFile()
Definition: FileReference.php:489
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:33
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:23
‪TYPO3\CMS\Core\Resource\DuplicationBehavior\RENAME
‪const RENAME
Definition: DuplicationBehavior.php:31
‪TYPO3\CMS\Core\Resource\FileReference\isIndexed
‪bool isIndexed()
Definition: FileReference.php:454
‪TYPO3\CMS\Core\Resource\FileReference\getHashedIdentifier
‪string getHashedIdentifier()
Definition: FileReference.php:499
‪TYPO3\CMS\Core\Resource\FileReference\getTitle
‪string getTitle()
Definition: FileReference.php:203
‪TYPO3\CMS\Core\Resource\FileReference\getReferenceProperties
‪array getReferenceProperties()
Definition: FileReference.php:181
‪TYPO3\CMS\Core\Resource\FileReference\__wakeup
‪__wakeup()
Definition: FileReference.php:527
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:37
‪TYPO3\CMS\Core\Resource\FileReference\$name
‪string $name
Definition: FileReference.php:44
‪TYPO3\CMS\Core\Resource\FileReference\getReferenceProperty
‪mixed getReferenceProperty($key)
Definition: FileReference.php:133
‪TYPO3\CMS\Core\Resource\FileReference\getContents
‪string getContents()
Definition: FileReference.php:352
‪TYPO3\CMS\Core\Resource\FileReference\restoreNonNullValuesCallback
‪restoreNonNullValuesCallback(&$value, $key)
Definition: FileReference.php:169
‪TYPO3\CMS\Core\Resource\FileReference\__construct
‪__construct(array $fileReferenceData, $factory=null)
Definition: FileReference.php:70
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:74
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:23
‪TYPO3\CMS\Core\Resource\FileReference\getNameWithoutExtension
‪string getNameWithoutExtension()
Definition: FileReference.php:289
‪TYPO3\CMS\Core\Resource\FolderInterface
Definition: FolderInterface.php:21
‪TYPO3\CMS\Core\Resource\FileReference\getParentFolder
‪FolderInterface getParentFolder()
Definition: FileReference.php:509
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Resource\FileReference\$originalFile
‪File $originalFile
Definition: FileReference.php:50
‪TYPO3\CMS\Core\Resource\FileReference\getStorage
‪ResourceStorage getStorage()
Definition: FileReference.php:376
‪TYPO3\CMS\Core\Resource\ResourceInterface\getParentFolder
‪FolderInterface getParentFolder()
‪TYPO3\CMS\Core\Resource\FileReference\toArray
‪array toArray()
Definition: FileReference.php:478
‪TYPO3\CMS\Core\Resource\FileReference\getCombinedIdentifier
‪string getCombinedIdentifier()
Definition: FileReference.php:396
‪TYPO3\CMS\Core\Resource\FileReference\setContents
‪File setContents($contents)
Definition: FileReference.php:363
‪TYPO3\CMS\Core\Resource\FileReference\rename
‪rename($newName, $conflictMode=DuplicationBehavior::RENAME)
Definition: FileReference.php:421