TYPO3 CMS  TYPO3_7-6
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  */
16 
28 class FileReference implements FileInterface
29 {
37 
46 
53  protected $name;
54 
60  protected $originalFile;
61 
69  protected $mergedProperties = [];
70 
81  public function __construct(array $fileReferenceData, $factory = null)
82  {
83  $this->propertiesOfFileReference = $fileReferenceData;
84  if (!$fileReferenceData['uid_local']) {
85  throw new \InvalidArgumentException('Incorrect reference to original file given for FileReference.', 1300098528);
86  }
87  if (!$factory) {
89  $factory = ResourceFactory::getInstance();
90  }
91  $this->originalFile = $factory->getFileObject($fileReferenceData['uid_local']);
92  if (!is_object($this->originalFile)) {
93  throw new \RuntimeException(
94  'Original file not found for FileReference. UID given: "' . $fileReferenceData['uid_local'] . '"',
95  1300098529
96  );
97  }
98  $this->name = $fileReferenceData['name'] !== '' ? $fileReferenceData['name'] : $this->originalFile->getName();
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 METHDOS
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 
413  public function delete()
414  {
415  // @todo Implement this function. This should only delete the
416  // FileReference (sys_file_reference) record, not the file itself.
417  throw new \BadMethodCallException('Function not implemented FileReference::delete().', 1333754461);
418  //return $this->fileRepository->removeUsageRecord($this);
419  }
420 
428  public function rename($newName)
429  {
430  // @todo Implement this function. This should only rename the
431  // FileReference (sys_file_reference) record, not the file itself.
432  throw new \BadMethodCallException('Function not implemented FileReference::rename().', 1333754473);
433  //return $this->fileRepository->renameUsageRecord($this, $newName);
434  }
435 
436  /*****************
437  * SPECIAL METHODS
438  *****************/
448  public function getPublicUrl($relativeToCurrentScript = false)
449  {
450  return $this->originalFile->getPublicUrl($relativeToCurrentScript);
451  }
452 
461  public function isIndexed()
462  {
463  return true;
464  }
465 
474  public function getForLocalProcessing($writable = true)
475  {
476  return $this->originalFile->getForLocalProcessing($writable);
477  }
478 
485  public function toArray()
486  {
487  $array = array_merge($this->originalFile->toArray(), $this->propertiesOfFileReference);
488  return $array;
489  }
490 
496  public function getOriginalFile()
497  {
498  return $this->originalFile;
499  }
500 
506  public function getHashedIdentifier()
507  {
508  return $this->getStorage()->hashFileIdentifier($this->getIdentifier());
509  }
510 
516  public function getParentFolder()
517  {
518  return $this->originalFile->getParentFolder();
519  }
520 }
getPublicUrl($relativeToCurrentScript=false)
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)