TYPO3 CMS  TYPO3_6-2
FileReference.php
Go to the documentation of this file.
1 <?php
3 
29 class FileReference implements FileInterface {
30 
38 
47 
54  protected $name;
55 
61  protected $originalFile;
62 
70  protected $mergedProperties = array();
71 
82  public function __construct(array $fileReferenceData, $factory = NULL) {
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('Original File not found for FileReference.', 1300098529);
94  }
95  $this->name = $fileReferenceData['name'] !== '' ? $fileReferenceData['name'] : $this->originalFile->getName();
96  }
97 
98  /*******************************
99  * VARIOUS FILE PROPERTY GETTERS
100  *******************************/
107  public function hasProperty($key) {
108  return array_key_exists($key, $this->getProperties());
109  }
110 
118  public function getProperty($key) {
119  if (!$this->hasProperty($key)) {
120  throw new \InvalidArgumentException('Property "' . $key . '" was not found in file reference or original file.', 1314226805);
121  }
122  $properties = $this->getProperties();
123  return $properties[$key];
124  }
125 
133  public function getReferenceProperty($key) {
134  if (!array_key_exists($key, $this->propertiesOfFileReference)) {
135  throw new \InvalidArgumentException('Property "' . $key . '" of file reference was not found.', 1360684914);
136  }
137  return $this->propertiesOfFileReference[$key];
138  }
139 
145  public function getProperties() {
146  if (empty($this->mergedProperties)) {
147  $this->mergedProperties = $this->propertiesOfFileReference;
149  $this->mergedProperties,
150  $this->originalFile->getProperties(),
151  TRUE,
152  TRUE,
153  FALSE
154  );
155  array_walk($this->mergedProperties, array($this, 'restoreNonNullValuesCallback'));
156  }
157 
159  }
160 
167  protected function restoreNonNullValuesCallback(&$value, $key) {
168  if (array_key_exists($key, $this->propertiesOfFileReference) && $this->propertiesOfFileReference[$key] !== NULL) {
169  $value = $this->propertiesOfFileReference[$key];
170  }
171  }
172 
178  public function getReferenceProperties() {
180  }
181 
187  public function getName() {
188  return $this->originalFile->getName();
189  }
190 
198  public function getTitle() {
199  return $this->getProperty('title');
200  }
201 
209  public function getAlternative() {
210  return $this->getProperty('alternative');
211  }
212 
220  public function getDescription() {
221  return $this->getProperty('description');
222  }
223 
231  public function getLink() {
232  return $this->propertiesOfFileReference['link'];
233  }
234 
240  public function getUid() {
241  return (int)$this->propertiesOfFileReference['uid'];
242  }
243 
249  public function getSize() {
250  return (int)$this->originalFile->getSize();
251  }
252 
258  public function getSha1() {
259  return $this->originalFile->getSha1();
260  }
261 
267  public function getExtension() {
268  return $this->originalFile->getExtension();
269  }
270 
276  public function getNameWithoutExtension() {
277  return $this->originalFile->getNameWithoutExtension();
278  }
279 
285  public function getMimeType() {
286  return $this->originalFile->getMimeType();
287  }
288 
294  public function getModificationTime() {
295  return (int)$this->originalFile->getModificationTime();
296  }
297 
303  public function getCreationTime() {
304  return (int)$this->originalFile->getCreationTime();
305  }
306 
312  public function getType() {
313  return (int)$this->originalFile->getType();
314  }
315 
321  public function isMissing() {
322  return (bool) $this->originalFile->getProperty('missing');
323  }
324 
325  /******************
326  * CONTENTS RELATED
327  ******************/
333  public function getContents() {
334  return $this->originalFile->getContents();
335  }
336 
343  public function setContents($contents) {
344  return $this->originalFile->setContents($contents);
345  }
346 
347  /****************************************
348  * STORAGE AND MANAGEMENT RELATED METHDOS
349  ****************************************/
355  public function getStorage() {
356  return $this->originalFile->getStorage();
357  }
358 
364  public function getIdentifier() {
365  return $this->originalFile->getIdentifier();
366  }
367 
373  public function getCombinedIdentifier() {
374  return $this->originalFile->getCombinedIdentifier();
375  }
376 
384  public function delete() {
385  // TODO: Implement this function. This should only delete the
386  // FileReference (sys_file_reference) record, not the file itself.
387  throw new \BadMethodCallException('Function not implemented FileReference::delete().', 1333754461);
388  //return $this->fileRepository->removeUsageRecord($this);
389  }
390 
399  public function rename($newName) {
400  // TODO: Implement this function. This should only rename the
401  // FileReference (sys_file_reference) record, not the file itself.
402  throw new \BadMethodCallException('Function not implemented FileReference::rename().', 1333754473);
403  //return $this->fileRepository->renameUsageRecord($this, $newName);
404  }
405 
406  /*****************
407  * SPECIAL METHODS
408  *****************/
418  public function getPublicUrl($relativeToCurrentScript = FALSE) {
419  return $this->originalFile->getPublicUrl($relativeToCurrentScript);
420  }
421 
430  public function isIndexed() {
431  return TRUE;
432  }
433 
442  public function getForLocalProcessing($writable = TRUE) {
443  return $this->originalFile->getForLocalProcessing($writable);
444  }
445 
452  public function toArray() {
453  $array = array_merge($this->originalFile->toArray(), $this->propertiesOfFileReference);
454  return $array;
455  }
456 
462  public function getOriginalFile() {
463  return $this->originalFile;
464  }
465 
471  public function getHashedIdentifier() {
472  return $this->getStorage()->hashFileIdentifier($this->getIdentifier());
473  }
474 
480  public function getParentFolder() {
481  return $this->originalFile->getParentFolder();
482  }
483 }
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=TRUE, $includeEmptyValues=TRUE, $enableUnsetFeature=TRUE)
getPublicUrl($relativeToCurrentScript=FALSE)