Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / file_mdm / src / Plugin / FileMetadata / GetImageSize.php
1 <?php
2
3 namespace Drupal\file_mdm\Plugin\FileMetadata;
4
5 use Drupal\file_mdm\FileMetadataException;
6
7 /**
8  * FileMetadata plugin for getimagesize.
9  *
10  * @FileMetadata(
11  *   id = "getimagesize",
12  *   title = @Translation("Getimagesize"),
13  *   help = @Translation("File metadata plugin for PHP getimagesize()."),
14  * )
15  */
16 class GetImageSize extends FileMetadataPluginBase {
17
18   /**
19    * {@inheritdoc}
20    */
21   public function getSupportedKeys($options = NULL) {
22     return [0, 1, 2, 3, 'mime', 'channels', 'bits'];
23   }
24
25   /**
26    * {@inheritdoc}
27    */
28   protected function doGetMetadataFromFile() {
29     if ($data = @getimagesize($this->getLocalTempPath())) {
30       return $data;
31     }
32     else {
33       return NULL;
34     }
35   }
36
37   /**
38    * Validates a file metadata key.
39    *
40    * @return bool
41    *   TRUE if the key is valid.
42    *
43    * @throws \Drupal\file_mdm\FileMetadataException
44    *   In case the key is invalid.
45    */
46   protected function validateKey($key, $method) {
47     if (!is_int($key) && !is_string($key)) {
48       throw new FileMetadataException("Invalid metadata key specified", $this->getPluginId(), $method);
49     }
50     if (!in_array($key, $this->getSupportedKeys(), TRUE)) {
51       throw new FileMetadataException("Invalid metadata key '{$key}' specified", $this->getPluginId(), $method);
52     }
53     return TRUE;
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   protected function doGetMetadata($key = NULL) {
60     if ($key === NULL) {
61       return $this->metadata;
62     }
63     else {
64       $this->validateKey($key, __FUNCTION__);
65       return isset($this->metadata[$key]) ? $this->metadata[$key] : NULL;
66     }
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   protected function doSetMetadata($key, $value) {
73     $this->validateKey($key, __FUNCTION__);
74     $this->metadata[$key] = $value;
75     return TRUE;
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   protected function doRemoveMetadata($key) {
82     $this->validateKey($key, __FUNCTION__);
83     if (isset($this->metadata[$key])) {
84       unset($this->metadata[$key]);
85     }
86     return TRUE;
87   }
88
89 }