|
1 | | -<?php namespace frictionlessdata\datapackage; |
2 | | - |
| 1 | +<?php |
| 2 | +namespace frictionlessdata\datapackage; |
3 | 3 |
|
4 | 4 | /** |
5 | 5 | * Datapackage representation, supports loading from the following sources: |
|
10 | 10 | */ |
11 | 11 | class Datapackage implements \Iterator |
12 | 12 | { |
13 | | - protected $_descriptor; |
14 | | - protected $_currentResourcePosition = 0; |
15 | | - protected $_basePath; |
16 | | - |
17 | 13 | public function __construct($source, $basePath=null) |
18 | 14 | { |
19 | 15 | if (is_object($source)) { |
20 | | - $this->_descriptor = $source; |
21 | | - $this->_basePath = $basePath; |
| 16 | + $this->descriptor = $source; |
| 17 | + $this->basePath = $basePath; |
22 | 18 | } elseif (is_string($source)) { |
23 | | - if (Utils::is_json_string($source)) { |
| 19 | + if (Utils::isJsonString($source)) { |
24 | 20 | try { |
25 | | - $this->_descriptor = json_decode($source); |
| 21 | + $this->descriptor = json_decode($source); |
26 | 22 | } catch (\Exception $e) { |
27 | | - throw new DatapackageInvalidSourceException("Failed to load source: ".json_encode($source).": ".$e->getMessage()); |
| 23 | + throw new Exceptions\DatapackageInvalidSourceException( |
| 24 | + "Failed to load source: ".json_encode($source).": ".$e->getMessage() |
| 25 | + ); |
28 | 26 | } |
29 | | - $this->_basePath = $basePath; |
30 | | - } elseif ($this->_isHttpSource($source)) { |
| 27 | + $this->basePath = $basePath; |
| 28 | + } elseif ($this->isHttpSource($source)) { |
31 | 29 | try { |
32 | | - $this->_descriptor = json_decode(file_get_contents($this->_normalizeHttpSource($source))); |
| 30 | + $this->descriptor = json_decode(file_get_contents($this->normalizeHttpSource($source))); |
33 | 31 | } catch (\Exception $e) { |
34 | | - throw new DatapackageInvalidSourceException("Failed to load source: ".json_encode($source).": ".$e->getMessage()); |
| 32 | + throw new Exceptions\DatapackageInvalidSourceException( |
| 33 | + "Failed to load source: ".json_encode($source).": ".$e->getMessage() |
| 34 | + ); |
35 | 35 | } |
36 | 36 | // http sources don't allow relative paths, hence basePath should remain null |
37 | | - $this->_basePath = null; |
| 37 | + $this->basePath = null; |
38 | 38 | } else { |
39 | 39 | if (empty($basePath)) { |
40 | | - $this->_basePath = dirname($source); |
| 40 | + $this->basePath = dirname($source); |
41 | 41 | } else { |
42 | | - $this->_basePath = $basePath; |
43 | | - $absPath = $this->_basePath.DIRECTORY_SEPARATOR.$source; |
| 42 | + $this->basePath = $basePath; |
| 43 | + $absPath = $this->basePath.DIRECTORY_SEPARATOR.$source; |
44 | 44 | if (file_exists($absPath)) { |
45 | 45 | $source = $absPath; |
46 | 46 | } |
47 | 47 | } |
48 | 48 | try { |
49 | | - $this->_descriptor = json_decode(file_get_contents($source)); |
| 49 | + $this->descriptor = json_decode(file_get_contents($source)); |
50 | 50 | } catch (\Exception $e) { |
51 | | - throw new DatapackageInvalidSourceException("Failed to load source: ".json_encode($source).": ".$e->getMessage()); |
| 51 | + throw new Exceptions\DatapackageInvalidSourceException( |
| 52 | + "Failed to load source: ".json_encode($source).": ".$e->getMessage() |
| 53 | + ); |
52 | 54 | } |
53 | 55 |
|
54 | 56 | } |
55 | 57 | } else { |
56 | | - throw new DatapackageInvalidSourceException("Invalid source: ".json_encode($source)); |
| 58 | + throw new Exceptions\DatapackageInvalidSourceException( |
| 59 | + "Invalid source: ".json_encode($source) |
| 60 | + ); |
57 | 61 | } |
58 | 62 | } |
59 | 63 |
|
60 | | - protected function _normalizeHttpSource($source) |
| 64 | + public static function validate($source, $basePath=null) |
61 | 65 | { |
62 | | - return $source; |
| 66 | + try { |
| 67 | + $datapackage = new self($source, $basePath); |
| 68 | + return DatapackageValidator::validate($datapackage->descriptor()); |
| 69 | + } catch (\Exception $e) { |
| 70 | + return [new DatapackageValidationError(DatapackageValidationError::LOAD_FAILED, $e->getMessage())]; |
| 71 | + } |
| 72 | + |
63 | 73 | } |
64 | 74 |
|
65 | | - protected function _isHttpSource($source) |
| 75 | + /** |
| 76 | + * get the descriptor as a native PHP object |
| 77 | + * |
| 78 | + * @return object |
| 79 | + */ |
| 80 | + public function descriptor() |
66 | 81 | { |
67 | | - return Utils::is_http_source($source); |
| 82 | + return $this->descriptor; |
68 | 83 | } |
69 | 84 |
|
70 | | - protected function _initResource($resourceDescriptor) |
| 85 | + // standard iterator functions - to iterate over the resources |
| 86 | + public function rewind() {$this->currentResourcePosition = 0;} |
| 87 | + public function current() { return $this->initResource($this->descriptor()->resources[$this->currentResourcePosition]); } |
| 88 | + public function key() { return $this->currentResourcePosition; } |
| 89 | + public function next() { $this->currentResourcePosition++; } |
| 90 | + public function valid() { return isset($this->descriptor()->resources[$this->currentResourcePosition]); } |
| 91 | + |
| 92 | + protected $descriptor; |
| 93 | + protected $currentResourcePosition = 0; |
| 94 | + protected $basePath; |
| 95 | + |
| 96 | + /** |
| 97 | + * allows extending classes to add custom sources |
| 98 | + * used by unit tests to add a mock http source |
| 99 | + * |
| 100 | + * @param string $source |
| 101 | + * @return string |
| 102 | + */ |
| 103 | + protected function normalizeHttpSource($source) |
71 | 104 | { |
72 | | - return new Resource($resourceDescriptor, $this->_basePath); |
| 105 | + return $source; |
73 | 106 | } |
74 | 107 |
|
75 | | - public function descriptor() |
| 108 | + /** |
| 109 | + * allows extending classes to add custom sources |
| 110 | + * used by unit tests to add a mock http source |
| 111 | + * |
| 112 | + * @param string $source |
| 113 | + * @return bool |
| 114 | + */ |
| 115 | + protected function isHttpSource($source) |
76 | 116 | { |
77 | | - return $this->_descriptor; |
| 117 | + return Utils::isHttpSource($source); |
78 | 118 | } |
79 | 119 |
|
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]); } |
| 120 | + /** |
| 121 | + * called by the resources iterator for each iteration |
| 122 | + * |
| 123 | + * @param object $resourceDescriptor |
| 124 | + * @return \frictionlessdata\datapackage\Resource |
| 125 | + */ |
| 126 | + protected function initResource($resourceDescriptor) |
| 127 | + { |
| 128 | + return new Resource($resourceDescriptor, $this->basePath); |
| 129 | + } |
86 | 130 | } |
87 | | - |
88 | | - |
89 | | -class DatapackageInvalidSourceException extends \Exception {}; |
|
0 commit comments