OriHoch /
tableschema-php
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace frictionlessdata\tableschema; |
||
| 4 | |||
| 5 | use frictionlessdata\tableschema\DataSources\CsvDataSource; |
||
| 6 | use frictionlessdata\tableschema\Exceptions\DataSourceException; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * represents a data source which validates against a table schema |
||
| 10 | * provides interfaces for validating the data and iterating over it |
||
| 11 | * casts all values to their native values according to the table schema. |
||
| 12 | */ |
||
| 13 | class Table implements \Iterator |
||
| 14 | { |
||
| 15 | public $csvDialect; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @param DataSources\DataSourceInterface $dataSource |
||
| 19 | * @param Schema $schema |
||
| 20 | * @param object $csvDialect |
||
| 21 | * |
||
| 22 | * @throws Exceptions\DataSourceException |
||
| 23 | */ |
||
| 24 | public function __construct($dataSource, $schema = null, $csvDialect = null) |
||
| 25 | { |
||
| 26 | $this->csvDialect = new CsvDialect($csvDialect); |
||
| 27 | if (!is_a($dataSource, 'frictionlessdata\\tableschema\\DataSources\\BaseDataSource')) { |
||
| 28 | // TODO: more advanced data source detection |
||
| 29 | $dataSource = new CsvDataSource($dataSource); |
||
| 30 | } |
||
| 31 | if (is_a($dataSource, 'frictionlessdata\\tableschema\\DataSources\\CsvDataSource')) { |
||
| 32 | $dataSource->setCsvDialect($this->csvDialect); |
||
| 33 | } |
||
| 34 | $this->dataSource = $dataSource; |
||
| 35 | if (!is_a($schema, 'frictionlessdata\\tableschema\\Schema')) { |
||
| 36 | if ($schema) { |
||
| 37 | $schema = new Schema($schema); |
||
| 38 | } else { |
||
| 39 | $schema = new InferSchema(); |
||
| 40 | } |
||
| 41 | } |
||
| 42 | $this->schema = $schema; |
||
| 43 | $this->dataSource->open(); |
||
| 44 | $this->uniqueFieldValues = []; |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param DataSources\DataSourceInterface $dataSource |
||
| 49 | * @param Schema $schema |
||
| 50 | * @param int $numPeekRows |
||
| 51 | * |
||
| 52 | * @return array of validation errors |
||
| 53 | */ |
||
| 54 | public static function validate($dataSource, $schema, $numPeekRows = 10, $csvDialect = null) |
||
| 55 | { |
||
| 56 | try { |
||
| 57 | $table = new static($dataSource, $schema, $csvDialect); |
||
| 58 | } catch (Exceptions\DataSourceException $e) { |
||
| 59 | return [new SchemaValidationError(SchemaValidationError::LOAD_FAILED, $e->getMessage())]; |
||
| 60 | } |
||
| 61 | if ($numPeekRows > 0) { |
||
| 62 | $i = 0; |
||
| 63 | try { |
||
| 64 | foreach ($table as $row) { |
||
| 65 | if (++$i > $numPeekRows) { |
||
| 66 | break; |
||
| 67 | } |
||
| 68 | } |
||
| 69 | } catch (Exceptions\DataSourceException $e) { |
||
| 70 | // general error in getting the next row from the data source |
||
| 71 | return [new SchemaValidationError(SchemaValidationError::ROW_VALIDATION, [ |
||
| 72 | 'row' => $i, |
||
| 73 | 'error' => $e->getMessage(), |
||
| 74 | ])]; |
||
| 75 | } catch (Exceptions\FieldValidationException $e) { |
||
| 76 | // validation error in one of the fields |
||
| 77 | return array_map(function ($validationError) use ($i) { |
||
| 78 | return new SchemaValidationError(SchemaValidationError::ROW_FIELD_VALIDATION, [ |
||
| 79 | 'row' => $i + 1, |
||
| 80 | 'field' => $validationError->extraDetails['field'], |
||
| 81 | 'error' => $validationError->extraDetails['error'], |
||
| 82 | 'value' => $validationError->extraDetails['value'], |
||
| 83 | ]); |
||
| 84 | }, $e->validationErrors); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | return []; |
||
| 89 | } |
||
| 90 | |||
| 91 | public function schema($numPeekRows = 10) |
||
| 92 | { |
||
| 93 | $this->ensureInferredSchema($numPeekRows); |
||
| 94 | |||
| 95 | return $this->schema; |
||
| 96 | } |
||
| 97 | |||
| 98 | public function headers($numPeekRows = 10) |
||
| 99 | { |
||
| 100 | $this->ensureInferredSchema($numPeekRows); |
||
| 101 | |||
| 102 | return array_keys($this->schema->fields()); |
||
| 103 | } |
||
| 104 | |||
| 105 | public function read() |
||
| 106 | { |
||
| 107 | $rows = []; |
||
| 108 | foreach ($this as $row) { |
||
| 109 | $rows[] = $row; |
||
| 110 | } |
||
| 111 | |||
| 112 | return $rows; |
||
| 113 | } |
||
| 114 | |||
| 115 | public function save($outputDataSource) |
||
| 116 | { |
||
| 117 | return $this->dataSource->save($outputDataSource); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * called on each iteration to get the next row |
||
| 122 | * does validation and casting on the row. |
||
| 123 | * |
||
| 124 | * @return mixed[] |
||
| 125 | * |
||
| 126 | * @throws Exceptions\FieldValidationException |
||
| 127 | * @throws Exceptions\DataSourceException |
||
| 128 | */ |
||
| 129 | public function current() |
||
| 130 | { |
||
| 131 | if (count($this->castRows) > 0) { |
||
| 132 | $row = array_shift($this->castRows); |
||
| 133 | } else { |
||
| 134 | $row = $this->schema->castRow($this->dataSource->getNextLine()); |
||
| 135 | foreach ($this->schema->fields() as $field) { |
||
| 136 | if ($field->unique()) { |
||
| 137 | if (!array_key_exists($field->name(), $this->uniqueFieldValues)) { |
||
| 138 | $this->uniqueFieldValues[$field->name()] = []; |
||
| 139 | } |
||
| 140 | $value = $row[$field->name()]; |
||
| 141 | if (in_array($value, $this->uniqueFieldValues[$field->name()])) { |
||
| 142 | throw new DataSourceException('field must be unique', $this->currentLine); |
||
| 143 | } else { |
||
| 144 | $this->uniqueFieldValues[$field->name()][] = $value; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | return $row; |
||
| 151 | } |
||
| 152 | |||
| 153 | // not interesting, standard iterator functions |
||
| 154 | // to simplify we prevent rewinding - so you can only iterate once |
||
| 155 | public function __destruct() |
||
| 156 | { |
||
| 157 | $this->dataSource->close(); |
||
| 158 | } |
||
| 159 | |||
| 160 | public function rewind() |
||
| 161 | { |
||
| 162 | if ($this->currentLine == 0) { |
||
| 163 | $this->currentLine = 1; |
||
| 164 | } elseif (count($this->castRows) == 0) { |
||
| 165 | $this->currentLine = 1; |
||
| 166 | $this->dataSource->open(); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | public function key() |
||
| 171 | { |
||
| 172 | return $this->currentLine - count($this->castRows); |
||
| 173 | } |
||
| 174 | |||
| 175 | public function next() |
||
| 176 | { |
||
| 177 | if (count($this->castRows) == 0) { |
||
| 178 | ++$this->currentLine; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | public function valid() |
||
| 183 | { |
||
| 184 | return count($this->castRows) > 0 || !$this->dataSource->isEof(); |
||
| 185 | } |
||
| 186 | |||
| 187 | protected $currentLine = 0; |
||
| 188 | protected $dataSource; |
||
| 189 | protected $schema; |
||
| 190 | protected $uniqueFieldValues; |
||
| 191 | protected $castRows = []; |
||
| 192 | |||
| 193 | protected function isInferSchema() |
||
| 194 | { |
||
| 195 | return is_a($this->schema, 'frictionlessdata\\tableschema\\InferSchema'); |
||
| 196 | } |
||
| 197 | |||
| 198 | protected function ensureInferredSchema($numPeekRows = 10) |
||
| 199 | { |
||
| 200 | if ($this->isInferSchema() && count($this->schema->fields()) == 0) { |
||
| 201 | // need to fetch some rows first |
||
| 202 | if ($numPeekRows > 0) { |
||
| 203 | $i = 0; |
||
| 204 | foreach ($this as $row) { |
||
| 205 | if (++$i > $numPeekRows) { |
||
| 206 | break; |
||
| 207 | } |
||
| 208 | } |
||
| 209 | // these rows will be returned by next current() call |
||
| 210 | $this->castRows = $this->schema->lock(); |
||
|
0 ignored issues
–
show
|
|||
| 211 | } |
||
| 212 | } |
||
| 213 | } |
||
| 214 | } |
||
| 215 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: