|
| 1 | +<?php namespace frictionlessdata\datapackage; |
| 2 | + |
| 3 | + |
| 4 | +/** |
| 5 | + * Datapackage representation, supports loading from the following sources: |
| 6 | + * - native PHP object containing the descriptor |
| 7 | + * - JSON encoded object |
| 8 | + * - URL (must be in either 'http' or 'https' schemes) |
| 9 | + * - local filesystem (POSIX) path |
| 10 | + */ |
| 11 | +class Datapackage implements \Iterator |
| 12 | +{ |
| 13 | + protected $_descriptor; |
| 14 | + protected $_currentResourcePosition = 0; |
| 15 | + protected $_basePath; |
| 16 | + |
| 17 | + public function __construct($source, $basePath=null) |
| 18 | + { |
| 19 | + if (is_object($source)) { |
| 20 | + $this->_descriptor = $source; |
| 21 | + $this->_basePath = $basePath; |
| 22 | + } elseif (is_string($source)) { |
| 23 | + if (Utils::is_json_string($source)) { |
| 24 | + try { |
| 25 | + $this->_descriptor = json_decode($source); |
| 26 | + } catch (\Exception $e) { |
| 27 | + throw new DatapackageInvalidSourceException("Failed to load source: ".json_encode($source).": ".$e->getMessage()); |
| 28 | + } |
| 29 | + $this->_basePath = $basePath; |
| 30 | + } elseif ($this->_isHttpSource($source)) { |
| 31 | + try { |
| 32 | + $this->_descriptor = json_decode(file_get_contents($this->_normalizeHttpSource($source))); |
| 33 | + } catch (\Exception $e) { |
| 34 | + throw new DatapackageInvalidSourceException("Failed to load source: ".json_encode($source).": ".$e->getMessage()); |
| 35 | + } |
| 36 | + // http sources don't allow relative paths, hence basePath should remain null |
| 37 | + $this->_basePath = null; |
| 38 | + } else { |
| 39 | + if (empty($basePath)) { |
| 40 | + $this->_basePath = dirname($source); |
| 41 | + } else { |
| 42 | + $this->_basePath = $basePath; |
| 43 | + $absPath = $this->_basePath.DIRECTORY_SEPARATOR.$source; |
| 44 | + if (file_exists($absPath)) { |
| 45 | + $source = $absPath; |
| 46 | + } |
| 47 | + } |
| 48 | + try { |
| 49 | + $this->_descriptor = json_decode(file_get_contents($source)); |
| 50 | + } catch (\Exception $e) { |
| 51 | + throw new DatapackageInvalidSourceException("Failed to load source: ".json_encode($source).": ".$e->getMessage()); |
| 52 | + } |
| 53 | + |
| 54 | + } |
| 55 | + } else { |
| 56 | + throw new DatapackageInvalidSourceException("Invalid source: ".json_encode($source)); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + protected function _normalizeHttpSource($source) |
| 61 | + { |
| 62 | + return $source; |
| 63 | + } |
| 64 | + |
| 65 | + protected function _isHttpSource($source) |
| 66 | + { |
| 67 | + return Utils::is_http_source($source); |
| 68 | + } |
| 69 | + |
| 70 | + protected function _initResource($resourceDescriptor) |
| 71 | + { |
| 72 | + return new Resource($resourceDescriptor, $this->_basePath); |
| 73 | + } |
| 74 | + |
| 75 | + public function descriptor() |
| 76 | + { |
| 77 | + return $this->_descriptor; |
| 78 | + } |
| 79 | + |
| 80 | + // standard iterator functions - to iterate over the resources |
| 81 | + public function rewind() { $this->_currentResourcePosition = 0; } |
| 82 | + public function current() { return $this->_initResource($this->descriptor()->resources[$this->_currentResourcePosition]); } |
| 83 | + public function key() { return $this->_currentResourcePosition; } |
| 84 | + public function next() { $this->_currentResourcePosition++; } |
| 85 | + public function valid() { return isset($this->descriptor()->resources[$this->_currentResourcePosition]); } |
| 86 | +} |
| 87 | + |
| 88 | + |
| 89 | +class DatapackageInvalidSourceException extends \Exception {}; |
0 commit comments