TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
core/Classes/Resource/FileReference.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Resource;
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  */
17 
29 class FileReference implements FileInterface
30 {
38 
47 
54  protected $name;
55 
61  protected $originalFile;
62 
70  protected $mergedProperties = [];
71 
82  public function __construct(array $fileReferenceData, $factory = null)
83  {
84  $this->propertiesOfFileReference = $fileReferenceData;
85  if (!$fileReferenceData['uid_local']) {
86  throw new \InvalidArgumentException('Incorrect reference to original file given for FileReference.', 1300098528);
87  }
88  if (!$factory) {
90  $factory = ResourceFactory::getInstance();
91  }
92  $this->originalFile = $factory->getFileObject($fileReferenceData['uid_local']);
93  if (!is_object($this->originalFile)) {
94  throw new \RuntimeException(
95  'Original file not found for FileReference. UID given: "' . $fileReferenceData['uid_local'] . '"',
96  1300098529
97  );
98  }
99  $this->name = $fileReferenceData['name'] !== '' ? $fileReferenceData['name'] : $this->originalFile->getName();
100  }
101 
102  /*******************************
103  * VARIOUS FILE PROPERTY GETTERS
104  *******************************/
111  public function hasProperty($key)
112  {
113  return array_key_exists($key, $this->getProperties());
114  }
115 
123  public function getProperty($key)
124  {
125  if (!$this->hasProperty($key)) {
126  throw new \InvalidArgumentException('Property "' . $key . '" was not found in file reference or original file.', 1314226805);
127  }
128  $properties = $this->getProperties();
129  return $properties[$key];
130  }
131 
139  public function getReferenceProperty($key)
140  {
141  if (!array_key_exists($key, $this->propertiesOfFileReference)) {
142  throw new \InvalidArgumentException('Property "' . $key . '" of file reference was not found.', 1360684914);
143  }
144  return $this->propertiesOfFileReference[$key];
145  }
146 
152  public function getProperties()
153  {
154  if (empty($this->mergedProperties)) {
155  $this->mergedProperties = $this->propertiesOfFileReference;
157  $this->mergedProperties,
158  $this->originalFile->getProperties(),
159  true,
160  true,
161  false
162  );
163  array_walk($this->mergedProperties, [$this, 'restoreNonNullValuesCallback']);
164  }
165 
167  }
168 
175  protected function restoreNonNullValuesCallback(&$value, $key)
176  {
177  if (array_key_exists($key, $this->propertiesOfFileReference) && $this->propertiesOfFileReference[$key] !== null) {
178  $value = $this->propertiesOfFileReference[$key];
179  }
180  }
181 
187  public function getReferenceProperties()
188  {
190  }
191 
197  public function getName()
198  {
199  return $this->originalFile->getName();
200  }
201 
209  public function getTitle()
210  {
211  return $this->getProperty('title');
212  }
213 
221  public function getAlternative()
222  {
223  return $this->getProperty('alternative');
224  }
225 
233  public function getDescription()
234  {
235  return $this->getProperty('description');
236  }
237 
245  public function getLink()
246  {
247  return $this->propertiesOfFileReference['link'];
248  }
249 
255  public function getUid()
256  {
257  return (int)$this->propertiesOfFileReference['uid'];
258  }
259 
265  public function getSize()
266  {
267  return (int)$this->originalFile->getSize();
268  }
269 
275  public function getSha1()
276  {
277  return $this->originalFile->getSha1();
278  }
279 
285  public function getExtension()
286  {
287  return $this->originalFile->getExtension();
288  }
289 
295  public function getNameWithoutExtension()
296  {
297  return $this->originalFile->getNameWithoutExtension();
298  }
299 
305  public function getMimeType()
306  {
307  return $this->originalFile->getMimeType();
308  }
309 
315  public function getModificationTime()
316  {
317  return (int)$this->originalFile->getModificationTime();
318  }
319 
325  public function getCreationTime()
326  {
327  return (int)$this->originalFile->getCreationTime();
328  }
329 
335  public function getType()
336  {
337  return (int)$this->originalFile->getType();
338  }
339 
345  public function isMissing()
346  {
347  return (bool)$this->originalFile->getProperty('missing');
348  }
349 
350  /******************
351  * CONTENTS RELATED
352  ******************/
358  public function getContents()
359  {
360  return $this->originalFile->getContents();
361  }
362 
369  public function setContents($contents)
370  {
371  return $this->originalFile->setContents($contents);
372  }
373 
374  /****************************************
375  * STORAGE AND MANAGEMENT RELATED METHDOS
376  ****************************************/
382  public function getStorage()
383  {
384  return $this->originalFile->getStorage();
385  }
386 
392  public function getIdentifier()
393  {
394  return $this->originalFile->getIdentifier();
395  }
396 
402  public function getCombinedIdentifier()
403  {
404  return $this->originalFile->getCombinedIdentifier();
405  }
406 
414  public function delete()
415  {
416  // @todo Implement this function. This should only delete the
417  // FileReference (sys_file_reference) record, not the file itself.
418  throw new \BadMethodCallException('Function not implemented FileReference::delete().', 1333754461);
419  //return $this->fileRepository->removeUsageRecord($this);
420  }
421 
429  public function rename($newName)
430  {
431  // @todo Implement this function. This should only rename the
432  // FileReference (sys_file_reference) record, not the file itself.
433  throw new \BadMethodCallException('Function not implemented FileReference::rename().', 1333754473);
434  //return $this->fileRepository->renameUsageRecord($this, $newName);
435  }
436 
437  /*****************
438  * SPECIAL METHODS
439  *****************/
449  public function getPublicUrl($relativeToCurrentScript = false)
450  {
451  return $this->originalFile->getPublicUrl($relativeToCurrentScript);
452  }
453 
462  public function isIndexed()
463  {
464  return true;
465  }
466 
475  public function getForLocalProcessing($writable = true)
476  {
477  return $this->originalFile->getForLocalProcessing($writable);
478  }
479 
486  public function toArray()
487  {
488  $array = array_merge($this->originalFile->toArray(), $this->propertiesOfFileReference);
489  return $array;
490  }
491 
497  public function getOriginalFile()
498  {
499  return $this->originalFile;
500  }
501 
507  public function getHashedIdentifier()
508  {
509  return $this->getStorage()->hashFileIdentifier($this->getIdentifier());
510  }
511 
517  public function getParentFolder()
518  {
519  return $this->originalFile->getParentFolder();
520  }
521 }
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)