Open Data Catalog v2.0.0
Resource.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Drupal\common;
4 
5 use Procrastinator\HydratableTrait;
6 use Procrastinator\JsonSerializeTrait;
7 
28 class Resource implements \JsonSerializable {
29  use HydratableTrait, JsonSerializeTrait;
30 
31  const DEFAULT_SOURCE_PERSPECTIVE = 'source';
32 
33  private $filePath;
34  private $identifier;
35  private $mimeType;
36  private $perspective;
37  private $version;
38 
42  public function __construct($file_path, $mime_type, $perspective = self::DEFAULT_SOURCE_PERSPECTIVE) {
43  // @todo generate UUID instead.
44  $this->identifier = md5($file_path);
45  $this->filePath = $file_path;
46  $this->mimeType = $mime_type;
47  $this->perspective = $perspective;
48  // @todo Create a timestamp property and generate uuid for version.
49  $this->version = time();
50  }
51 
63  public function createNewVersion() {
64  $newVersion = time();
65  if ($newVersion == $this->version) {
66  $newVersion++;
67  }
68 
69  return $this->createCommon('version', $newVersion);
70  }
71 
83  public function createNewPerspective($perspective, $uri) {
84  $new = $this->createCommon('perspective', $perspective);
85  $new->changeFilePath($uri);
86  return $new;
87  }
88 
92  public function changeFilePath($newPath) {
93  $this->filePath = $newPath;
94  }
95 
99  private function createCommon($property, $value) {
100  $current = $this->{$property};
101  $new = $value;
102  $this->{$property} = $new;
103  $newResource = clone $this;
104  $this->{$property} = $current;
105  return $newResource;
106  }
107 
111  public function getIdentifier() {
112  return $this->identifier;
113  }
114 
118  public function getFilePath() {
119  return $this->filePath;
120  }
121 
125  public function getFolder() {
126  return dirname($this->filePath);
127  }
128 
132  public function getMimeType() {
133  return $this->mimeType;
134  }
135 
139  public function getVersion() {
140  return $this->version;
141  }
142 
146  public function getPerspective() {
147  return $this->perspective;
148  }
149 
153  public function getUniqueIdentifier() {
154  return "{$this->identifier}__{$this->version}__{$this->perspective}";
155  }
156 
162  public function jsonSerialize() {
163  return $this->serialize();
164  }
165 
178  public static function parseUniqueIdentifier(string $uid): array {
179  $pieces = explode("__", $uid);
180  if (count($pieces) != 3) {
181  throw new \Exception("Badly constructed unique identifier {$uid}");
182  }
183  return [
184  'identifier' => $pieces[0],
185  'version' => $pieces[1],
186  'perspective' => $pieces[2],
187  ];
188  }
189 
197  public static function getIdentifierAndVersion($string) {
198  // Complete unique identifier.
199  try {
200  $parts = self::parseUniqueIdentifier($string);
201  return [$parts['identifier'], $parts['version']];
202  }
203  catch (\Exception $e) {
204  }
205 
206  // Partial identifier.
207  if (substr_count($string, '__') > 0) {
208  $parts = explode("__", $string);
209  if (count($parts) == 2) {
210  return $parts;
211  }
212  }
213 
214  $distribution = self::getDistribution($string);
215 
216  // Are we dealing with a distribution id?
217  if (isset($distribution->data->{"%Ref:downloadURL"})) {
218  $resource = $distribution->data->{"%Ref:downloadURL"}[0]->data;
219  return [$resource->identifier, $resource->version];
220  }
221 
222  throw new \Exception("Could not find identifier and version for {$string}");
223  }
224 
228  private static function getDistribution($identifier) {
229  /* @var \Drupal\metastore\Storage\DataFactory $factory */
230  $factory = \Drupal::service('dkan.metastore.storage');
231 
232  /* @var \Drupal\metastore\Storage\Data $storage */
233  $storage = $factory->getInstance('distribution');
234 
235  $distroJson = $storage->retrieve($identifier);
236  return json_decode($distroJson);
237  }
238 
239 }
Drupal\common\Resource\getFilePath
getFilePath()
Definition: Resource.php:118
Drupal\common\Resource\getFolder
getFolder()
Definition: Resource.php:125
Drupal\common\Resource\createNewVersion
createNewVersion()
Definition: Resource.php:63
Drupal\common\Resource
Definition: Resource.php:28
Drupal\common\Resource\createNewPerspective
createNewPerspective($perspective, $uri)
Definition: Resource.php:83
Drupal\common\Resource\__construct
__construct($file_path, $mime_type, $perspective=self::DEFAULT_SOURCE_PERSPECTIVE)
Definition: Resource.php:42
Drupal\common\Resource\DEFAULT_SOURCE_PERSPECTIVE
const DEFAULT_SOURCE_PERSPECTIVE
Definition: Resource.php:31
Drupal\common\Resource\getPerspective
getPerspective()
Definition: Resource.php:146
Drupal\common\Resource\getVersion
getVersion()
Definition: Resource.php:139
Drupal\common\Resource\changeFilePath
changeFilePath($newPath)
Definition: Resource.php:92
Drupal\common\Resource\getIdentifierAndVersion
static getIdentifierAndVersion($string)
Definition: Resource.php:197
Drupal\common
Drupal\common\Resource\jsonSerialize
jsonSerialize()
Definition: Resource.php:162
Drupal\common\Resource\getMimeType
getMimeType()
Definition: Resource.php:132
Drupal\common\Resource\getUniqueIdentifier
getUniqueIdentifier()
Definition: Resource.php:153
Drupal\common\Resource\parseUniqueIdentifier
static parseUniqueIdentifier(string $uid)
Definition: Resource.php:178
Drupal\common\Resource\getIdentifier
getIdentifier()
Definition: Resource.php:111