@@ -9,369 +9,369 @@ |
||
| 9 | 9 | |
| 10 | 10 | trait AutoApi |
| 11 | 11 | { |
| 12 | - /* ======================================================================= |
|
| 12 | + /* ======================================================================= |
|
| 13 | 13 | * ===================== Automatic API Support =========================== |
| 14 | 14 | * ======================================================================= */ |
| 15 | 15 | |
| 16 | - /** @var array A map of column name to functions that hook the insert function */ |
|
| 17 | - protected $registeredCreateHooks; |
|
| 16 | + /** @var array A map of column name to functions that hook the insert function */ |
|
| 17 | + protected $registeredCreateHooks; |
|
| 18 | 18 | |
| 19 | - /** @var array A map of column name to functions that hook the read function */ |
|
| 20 | - protected $registeredReadHooks; |
|
| 19 | + /** @var array A map of column name to functions that hook the read function */ |
|
| 20 | + protected $registeredReadHooks; |
|
| 21 | 21 | |
| 22 | - /** @var array A map of column name to functions that hook the update function */ |
|
| 23 | - protected $registeredUpdateHooks; |
|
| 22 | + /** @var array A map of column name to functions that hook the update function */ |
|
| 23 | + protected $registeredUpdateHooks; |
|
| 24 | 24 | |
| 25 | - /** @var array A map of column name to functions that hook the update function */ |
|
| 26 | - protected $registeredDeleteHooks; |
|
| 25 | + /** @var array A map of column name to functions that hook the update function */ |
|
| 26 | + protected $registeredDeleteHooks; |
|
| 27 | 27 | |
| 28 | - /** @var array A map of column name to functions that hook the search function */ |
|
| 29 | - protected $registeredSearchHooks; |
|
| 28 | + /** @var array A map of column name to functions that hook the search function */ |
|
| 29 | + protected $registeredSearchHooks; |
|
| 30 | 30 | |
| 31 | - /** @var array A list of table column definitions */ |
|
| 32 | - protected $tableDefinition; |
|
| 31 | + /** @var array A list of table column definitions */ |
|
| 32 | + protected $tableDefinition; |
|
| 33 | 33 | |
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * @param Array $queryparams associative array of query params. Reserved options are |
|
| 37 | - * "search_order_by", "search_order_direction", "search_limit", "search_offset" |
|
| 38 | - * or column names corresponding to an instance of miBadger\Query\QueryExpression |
|
| 39 | - * @param Array $fieldWhitelist names of the columns that will appear in the output results |
|
| 40 | - */ |
|
| 41 | - public function apiSearch(Array $queryParams, Array $fieldWhitelist, ?QueryExpression $whereClause = null) |
|
| 42 | - { |
|
| 43 | - $query = $this->search(); |
|
| 35 | + /** |
|
| 36 | + * @param Array $queryparams associative array of query params. Reserved options are |
|
| 37 | + * "search_order_by", "search_order_direction", "search_limit", "search_offset" |
|
| 38 | + * or column names corresponding to an instance of miBadger\Query\QueryExpression |
|
| 39 | + * @param Array $fieldWhitelist names of the columns that will appear in the output results |
|
| 40 | + */ |
|
| 41 | + public function apiSearch(Array $queryParams, Array $fieldWhitelist, ?QueryExpression $whereClause = null) |
|
| 42 | + { |
|
| 43 | + $query = $this->search(); |
|
| 44 | 44 | |
| 45 | - // Build query |
|
| 46 | - $orderColumn = $queryParams['search_order_by'] ?? null; |
|
| 47 | - $orderDirection = $queryParams['search_order_direction'] ?? null; |
|
| 48 | - if ($orderColumn !== null) { |
|
| 49 | - $query->orderBy($orderColumn, $orderDirection); |
|
| 50 | - } |
|
| 45 | + // Build query |
|
| 46 | + $orderColumn = $queryParams['search_order_by'] ?? null; |
|
| 47 | + $orderDirection = $queryParams['search_order_direction'] ?? null; |
|
| 48 | + if ($orderColumn !== null) { |
|
| 49 | + $query->orderBy($orderColumn, $orderDirection); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - $limit = $queryParams['search_limit'] ?? null; |
|
| 53 | - if ($limit !== null) { |
|
| 54 | - $query->limit($limit); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - $offset = $queryParams['search_offset'] ?? null; |
|
| 58 | - if ($offset !== null) { |
|
| 59 | - $query->offset($offset); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - if ($whereClause !== null) { |
|
| 63 | - $query->where($whereClause); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - // Fetch results |
|
| 67 | - $results = $query->fetchAll(); |
|
| 68 | - |
|
| 69 | - $resultsArray = []; |
|
| 70 | - foreach ($results as $result) { |
|
| 71 | - $resultsArray[] = $result->toArray($fieldWhitelist); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - return $resultsArray; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - public function toArray($fieldWhitelist) |
|
| 78 | - { |
|
| 79 | - $output = []; |
|
| 80 | - foreach ($this->tableDefinition as $colName => $definition) { |
|
| 81 | - if (in_array($colName, $fieldWhitelist)) { |
|
| 82 | - $output[$colName] = $definition['value']; |
|
| 83 | - } |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - return $output; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - public function apiRead($id, Array $fieldWhitelist) |
|
| 90 | - { |
|
| 91 | - $this->read($id); |
|
| 92 | - return $this->toArray($fieldWhitelist); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /* ============================================================= |
|
| 52 | + $limit = $queryParams['search_limit'] ?? null; |
|
| 53 | + if ($limit !== null) { |
|
| 54 | + $query->limit($limit); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + $offset = $queryParams['search_offset'] ?? null; |
|
| 58 | + if ($offset !== null) { |
|
| 59 | + $query->offset($offset); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + if ($whereClause !== null) { |
|
| 63 | + $query->where($whereClause); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + // Fetch results |
|
| 67 | + $results = $query->fetchAll(); |
|
| 68 | + |
|
| 69 | + $resultsArray = []; |
|
| 70 | + foreach ($results as $result) { |
|
| 71 | + $resultsArray[] = $result->toArray($fieldWhitelist); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + return $resultsArray; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + public function toArray($fieldWhitelist) |
|
| 78 | + { |
|
| 79 | + $output = []; |
|
| 80 | + foreach ($this->tableDefinition as $colName => $definition) { |
|
| 81 | + if (in_array($colName, $fieldWhitelist)) { |
|
| 82 | + $output[$colName] = $definition['value']; |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + return $output; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + public function apiRead($id, Array $fieldWhitelist) |
|
| 90 | + { |
|
| 91 | + $this->read($id); |
|
| 92 | + return $this->toArray($fieldWhitelist); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /* ============================================================= |
|
| 96 | 96 | * ===================== Constraint validation ================= |
| 97 | 97 | * ============================================================= */ |
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * Copy all table variables between two instances |
|
| 101 | - */ |
|
| 102 | - public function syncInstanceFrom($from) |
|
| 103 | - { |
|
| 104 | - foreach ($this->tableDefinition as $colName => $definition) { |
|
| 105 | - $this->tableDefinition[$colName]['value'] = $from->tableDefinition[$colName]['value']; |
|
| 106 | - } |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - private function filterInputColumns($input, $whitelist) |
|
| 110 | - { |
|
| 111 | - $filteredInput = $input; |
|
| 112 | - foreach ($input as $colName => $value) { |
|
| 113 | - if (!in_array($colName, $whitelist)) { |
|
| 114 | - unset($filteredInput[$colName]); |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - return $filteredInput; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - private function validateExcessKeys($input) |
|
| 121 | - { |
|
| 122 | - $errors = []; |
|
| 123 | - foreach ($input as $colName => $value) { |
|
| 124 | - if (!array_key_exists($colName, $this->tableDefinition)) { |
|
| 125 | - $errors[$colName] = "Unknown input field"; |
|
| 126 | - continue; |
|
| 127 | - } |
|
| 128 | - } |
|
| 129 | - return $errors; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - private function validateImmutableColumns($input) |
|
| 133 | - { |
|
| 134 | - $errors = []; |
|
| 135 | - foreach ($this->tableDefinition as $colName => $definition) { |
|
| 136 | - $property = $definition['properties'] ?? null; |
|
| 137 | - if (array_key_exists($colName, $input) |
|
| 138 | - && $property & ColumnProperty::IMMUTABLE) { |
|
| 139 | - $errors[$colName] = "Field cannot be changed"; |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - return $errors; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Checks whether input values are correct: |
|
| 147 | - * 1. Checks whether a value passes the validation function for that column |
|
| 148 | - * 2. Checks whether a value supplied to a relationship column is a valid value |
|
| 149 | - */ |
|
| 150 | - private function validateInputValues($input) |
|
| 151 | - { |
|
| 152 | - $errors = []; |
|
| 153 | - foreach ($this->tableDefinition as $colName => $definition) { |
|
| 154 | - // Validation check 1: If validate function is present |
|
| 155 | - if (array_key_exists($colName, $input) |
|
| 156 | - && is_callable($definition['validate'] ?? null)) { |
|
| 157 | - $inputValue = $input[$colName]; |
|
| 158 | - |
|
| 159 | - // If validation function fails |
|
| 160 | - [$status, $message] = $definition['validate']($inputValue); |
|
| 161 | - if (!$status) { |
|
| 162 | - $errors[$colName] = $message; |
|
| 163 | - } |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - // Validation check 2: If relation column, check whether entity exists |
|
| 167 | - $properties = $definition['properties'] ?? null; |
|
| 168 | - if (isset($definition['relation']) |
|
| 169 | - && ($properties & ColumnProperty::NOT_NULL)) { |
|
| 170 | - $instance = clone $definition['relation']; |
|
| 171 | - try { |
|
| 172 | - $instance->read($input[$colName] ?? null); |
|
| 173 | - } catch (ActiveRecordException $e) { |
|
| 174 | - $errors[$colName] = "Entity for this value doesn't exist"; |
|
| 175 | - } |
|
| 176 | - } |
|
| 177 | - } |
|
| 178 | - return $errors; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * This function is only used for API Update calls (direct getter/setter functions are unconstrained) |
|
| 183 | - * Determines whether there are required columns for which no data is provided |
|
| 184 | - */ |
|
| 185 | - private function validateMissingKeys($input) |
|
| 186 | - { |
|
| 187 | - $errors = []; |
|
| 188 | - |
|
| 189 | - foreach ($this->tableDefinition as $colName => $colDefinition) { |
|
| 190 | - $default = $colDefinition['default'] ?? null; |
|
| 191 | - $properties = $colDefinition['properties'] ?? null; |
|
| 192 | - $value = $colDefinition['value']; |
|
| 193 | - |
|
| 194 | - // If nullable and default not set => null |
|
| 195 | - // If nullable and default null => default (null) |
|
| 196 | - // If nullable and default set => default (value) |
|
| 197 | - |
|
| 198 | - // if not nullable and default not set => error |
|
| 199 | - // if not nullable and default null => error |
|
| 200 | - // if not nullable and default st => default (value) |
|
| 201 | - // => if not nullable and default null and value not set (or null) => error message in this method |
|
| 202 | - if ($properties & ColumnProperty::NOT_NULL |
|
| 203 | - && $default === null |
|
| 204 | - && !($properties & ColumnProperty::AUTO_INCREMENT) |
|
| 205 | - && (!array_key_exists($colName, $input) || $input[$colName] === null) |
|
| 206 | - && $value === null) { |
|
| 207 | - $errors[$colName] = sprintf("The required field \"%s\" is missing", $colName); |
|
| 208 | - } |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - return $errors; |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Copies the values for entries in the input with matching variable names in the record definition |
|
| 216 | - * @param Array $input The input data to be loaded into $this record |
|
| 217 | - */ |
|
| 218 | - private function loadData($input) |
|
| 219 | - { |
|
| 220 | - foreach ($this->tableDefinition as $colName => $definition) { |
|
| 221 | - if (array_key_exists($colName, $input)) { |
|
| 222 | - $definition['value'] = $input[$colName]; |
|
| 223 | - } |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * @param Array $input Associative array of input values |
|
| 229 | - * @param Array $fieldWhitelist array of column names that are allowed to be filled by the input array |
|
| 230 | - * @return Array Array containing the set of optional errors (associative array) and an optional array representation (associative) |
|
| 231 | - * of the modified data. |
|
| 232 | - */ |
|
| 233 | - public function apiCreate($input, Array $createWhitelist, Array $readWhitelist) |
|
| 234 | - { |
|
| 235 | - // Clone $this to new instance (for restoring if validation goes wrong) |
|
| 236 | - $transaction = $this->newInstance(); |
|
| 237 | - $errors = []; |
|
| 238 | - |
|
| 239 | - // Filter out all non-whitelisted input values |
|
| 240 | - $input = $this->filterInputColumns($input, $createWhitelist); |
|
| 241 | - |
|
| 242 | - // Validate excess keys |
|
| 243 | - $errors += $transaction->validateExcessKeys($input); |
|
| 244 | - |
|
| 245 | - // Validate input values (using validation function) |
|
| 246 | - $errors += $transaction->validateInputValues($input); |
|
| 247 | - |
|
| 248 | - // "Copy" data into transaction |
|
| 249 | - $transaction->loadData($input); |
|
| 250 | - |
|
| 251 | - // Run create hooks |
|
| 252 | - foreach ($this->registeredCreateHooks as $colName => $fn) { |
|
| 253 | - $fn(); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - // Validate missing keys |
|
| 257 | - $errors += $transaction->validateMissingKeys($input); |
|
| 258 | - |
|
| 259 | - // If no errors, commit the pending data |
|
| 260 | - if (empty($errors)) { |
|
| 261 | - $this->syncInstanceFrom($transaction); |
|
| 262 | - |
|
| 263 | - try { |
|
| 264 | - (new Query($this->getPdo(), $this->getTableName())) |
|
| 265 | - ->insert($this->getActiveRecordColumns()) |
|
| 266 | - ->execute(); |
|
| 267 | - |
|
| 268 | - $this->setId(intval($this->getPdo()->lastInsertId())); |
|
| 269 | - } catch (\PDOException $e) { |
|
| 270 | - // @TODO: Potentially filter and store mysql messages (where possible) in error messages |
|
| 271 | - throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - return [null, $this->toArray($readWhitelist)]; |
|
| 275 | - } else { |
|
| 276 | - return [$errors, null]; |
|
| 277 | - } |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * @param Array $input Associative array of input values |
|
| 282 | - * @param Array $fieldWhitelist array of column names that are allowed to be filled by the input array |
|
| 283 | - * @return Array Array containing the set of optional errors (associative array) and an optional array representation (associative) |
|
| 284 | - * of the modified data. |
|
| 285 | - */ |
|
| 286 | - public function apiUpdate($input, Array $updateWhitelist, Array $readWhitelist) |
|
| 287 | - { |
|
| 288 | - $transaction = $this->newInstance(); |
|
| 289 | - $transaction->syncInstanceFrom($this); |
|
| 290 | - $errors = []; |
|
| 291 | - |
|
| 292 | - // Filter out all non-whitelisted input values |
|
| 293 | - $input = $this->filterInputColumns($input, $updateWhitelist); |
|
| 294 | - |
|
| 295 | - // Check for excess keys |
|
| 296 | - $errors += $transaction->validateExcessKeys($input); |
|
| 297 | - |
|
| 298 | - // Check for immutable keys |
|
| 299 | - $errors += $transaction->validateImmutableColumns($input); |
|
| 300 | - |
|
| 301 | - // Validate input values (using validation function) |
|
| 302 | - $errors += $transaction->validateInputValues($input); |
|
| 303 | - |
|
| 304 | - // "Copy" data into transaction |
|
| 305 | - $transaction->loadData($input); |
|
| 306 | - |
|
| 307 | - // Run create hooks |
|
| 308 | - foreach ($this->registeredUpdateHooks as $colName => $fn) { |
|
| 309 | - $fn(); |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - // Validate missing keys |
|
| 313 | - $errors += $transaction->validateMissingKeys($input); |
|
| 314 | - |
|
| 315 | - // Update database |
|
| 316 | - if (empty($errors)) { |
|
| 317 | - $this->syncInstanceFrom($transaction); |
|
| 318 | - |
|
| 319 | - try { |
|
| 320 | - (new Query($this->getPdo(), $this->getTableName())) |
|
| 321 | - ->update($this->getActiveRecordColumns()) |
|
| 322 | - ->where(Query::Equal('id', $this->getId())) |
|
| 323 | - ->execute(); |
|
| 324 | - } catch (\PDOException $e) { |
|
| 325 | - throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - return [null, $this->toArray($readWhitelist)]; |
|
| 329 | - } else { |
|
| 330 | - return [$errors, null]; |
|
| 331 | - } |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - /** |
|
| 335 | - * Returns this active record after reading the attributes from the entry with the given identifier. |
|
| 336 | - * |
|
| 337 | - * @param mixed $id |
|
| 338 | - * @return $this |
|
| 339 | - * @throws ActiveRecordException on failure. |
|
| 340 | - */ |
|
| 341 | - abstract public function read($id); |
|
| 342 | - |
|
| 343 | - /** |
|
| 344 | - * Returns the PDO. |
|
| 345 | - * |
|
| 346 | - * @return \PDO the PDO. |
|
| 347 | - */ |
|
| 348 | - abstract public function getPdo(); |
|
| 349 | - |
|
| 350 | - /** |
|
| 351 | - * Set the ID. |
|
| 352 | - * |
|
| 353 | - * @param int $id |
|
| 354 | - * @return $this |
|
| 355 | - */ |
|
| 356 | - abstract protected function setId($id); |
|
| 357 | - |
|
| 358 | - /** |
|
| 359 | - * Returns the ID. |
|
| 360 | - * |
|
| 361 | - * @return null|int The ID. |
|
| 362 | - */ |
|
| 363 | - abstract protected function getId(); |
|
| 364 | - |
|
| 365 | - /** |
|
| 366 | - * Returns the active record table. |
|
| 367 | - * |
|
| 368 | - * @return string the active record table name. |
|
| 369 | - */ |
|
| 370 | - abstract protected function getTableName(); |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * Returns the name -> variable mapping for the table definition. |
|
| 374 | - * @return Array The mapping |
|
| 375 | - */ |
|
| 376 | - abstract protected function getActiveRecordColumns(); |
|
| 99 | + /** |
|
| 100 | + * Copy all table variables between two instances |
|
| 101 | + */ |
|
| 102 | + public function syncInstanceFrom($from) |
|
| 103 | + { |
|
| 104 | + foreach ($this->tableDefinition as $colName => $definition) { |
|
| 105 | + $this->tableDefinition[$colName]['value'] = $from->tableDefinition[$colName]['value']; |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + private function filterInputColumns($input, $whitelist) |
|
| 110 | + { |
|
| 111 | + $filteredInput = $input; |
|
| 112 | + foreach ($input as $colName => $value) { |
|
| 113 | + if (!in_array($colName, $whitelist)) { |
|
| 114 | + unset($filteredInput[$colName]); |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + return $filteredInput; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + private function validateExcessKeys($input) |
|
| 121 | + { |
|
| 122 | + $errors = []; |
|
| 123 | + foreach ($input as $colName => $value) { |
|
| 124 | + if (!array_key_exists($colName, $this->tableDefinition)) { |
|
| 125 | + $errors[$colName] = "Unknown input field"; |
|
| 126 | + continue; |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | + return $errors; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + private function validateImmutableColumns($input) |
|
| 133 | + { |
|
| 134 | + $errors = []; |
|
| 135 | + foreach ($this->tableDefinition as $colName => $definition) { |
|
| 136 | + $property = $definition['properties'] ?? null; |
|
| 137 | + if (array_key_exists($colName, $input) |
|
| 138 | + && $property & ColumnProperty::IMMUTABLE) { |
|
| 139 | + $errors[$colName] = "Field cannot be changed"; |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + return $errors; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Checks whether input values are correct: |
|
| 147 | + * 1. Checks whether a value passes the validation function for that column |
|
| 148 | + * 2. Checks whether a value supplied to a relationship column is a valid value |
|
| 149 | + */ |
|
| 150 | + private function validateInputValues($input) |
|
| 151 | + { |
|
| 152 | + $errors = []; |
|
| 153 | + foreach ($this->tableDefinition as $colName => $definition) { |
|
| 154 | + // Validation check 1: If validate function is present |
|
| 155 | + if (array_key_exists($colName, $input) |
|
| 156 | + && is_callable($definition['validate'] ?? null)) { |
|
| 157 | + $inputValue = $input[$colName]; |
|
| 158 | + |
|
| 159 | + // If validation function fails |
|
| 160 | + [$status, $message] = $definition['validate']($inputValue); |
|
| 161 | + if (!$status) { |
|
| 162 | + $errors[$colName] = $message; |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + // Validation check 2: If relation column, check whether entity exists |
|
| 167 | + $properties = $definition['properties'] ?? null; |
|
| 168 | + if (isset($definition['relation']) |
|
| 169 | + && ($properties & ColumnProperty::NOT_NULL)) { |
|
| 170 | + $instance = clone $definition['relation']; |
|
| 171 | + try { |
|
| 172 | + $instance->read($input[$colName] ?? null); |
|
| 173 | + } catch (ActiveRecordException $e) { |
|
| 174 | + $errors[$colName] = "Entity for this value doesn't exist"; |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | + return $errors; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * This function is only used for API Update calls (direct getter/setter functions are unconstrained) |
|
| 183 | + * Determines whether there are required columns for which no data is provided |
|
| 184 | + */ |
|
| 185 | + private function validateMissingKeys($input) |
|
| 186 | + { |
|
| 187 | + $errors = []; |
|
| 188 | + |
|
| 189 | + foreach ($this->tableDefinition as $colName => $colDefinition) { |
|
| 190 | + $default = $colDefinition['default'] ?? null; |
|
| 191 | + $properties = $colDefinition['properties'] ?? null; |
|
| 192 | + $value = $colDefinition['value']; |
|
| 193 | + |
|
| 194 | + // If nullable and default not set => null |
|
| 195 | + // If nullable and default null => default (null) |
|
| 196 | + // If nullable and default set => default (value) |
|
| 197 | + |
|
| 198 | + // if not nullable and default not set => error |
|
| 199 | + // if not nullable and default null => error |
|
| 200 | + // if not nullable and default st => default (value) |
|
| 201 | + // => if not nullable and default null and value not set (or null) => error message in this method |
|
| 202 | + if ($properties & ColumnProperty::NOT_NULL |
|
| 203 | + && $default === null |
|
| 204 | + && !($properties & ColumnProperty::AUTO_INCREMENT) |
|
| 205 | + && (!array_key_exists($colName, $input) || $input[$colName] === null) |
|
| 206 | + && $value === null) { |
|
| 207 | + $errors[$colName] = sprintf("The required field \"%s\" is missing", $colName); |
|
| 208 | + } |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + return $errors; |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Copies the values for entries in the input with matching variable names in the record definition |
|
| 216 | + * @param Array $input The input data to be loaded into $this record |
|
| 217 | + */ |
|
| 218 | + private function loadData($input) |
|
| 219 | + { |
|
| 220 | + foreach ($this->tableDefinition as $colName => $definition) { |
|
| 221 | + if (array_key_exists($colName, $input)) { |
|
| 222 | + $definition['value'] = $input[$colName]; |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * @param Array $input Associative array of input values |
|
| 229 | + * @param Array $fieldWhitelist array of column names that are allowed to be filled by the input array |
|
| 230 | + * @return Array Array containing the set of optional errors (associative array) and an optional array representation (associative) |
|
| 231 | + * of the modified data. |
|
| 232 | + */ |
|
| 233 | + public function apiCreate($input, Array $createWhitelist, Array $readWhitelist) |
|
| 234 | + { |
|
| 235 | + // Clone $this to new instance (for restoring if validation goes wrong) |
|
| 236 | + $transaction = $this->newInstance(); |
|
| 237 | + $errors = []; |
|
| 238 | + |
|
| 239 | + // Filter out all non-whitelisted input values |
|
| 240 | + $input = $this->filterInputColumns($input, $createWhitelist); |
|
| 241 | + |
|
| 242 | + // Validate excess keys |
|
| 243 | + $errors += $transaction->validateExcessKeys($input); |
|
| 244 | + |
|
| 245 | + // Validate input values (using validation function) |
|
| 246 | + $errors += $transaction->validateInputValues($input); |
|
| 247 | + |
|
| 248 | + // "Copy" data into transaction |
|
| 249 | + $transaction->loadData($input); |
|
| 250 | + |
|
| 251 | + // Run create hooks |
|
| 252 | + foreach ($this->registeredCreateHooks as $colName => $fn) { |
|
| 253 | + $fn(); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + // Validate missing keys |
|
| 257 | + $errors += $transaction->validateMissingKeys($input); |
|
| 258 | + |
|
| 259 | + // If no errors, commit the pending data |
|
| 260 | + if (empty($errors)) { |
|
| 261 | + $this->syncInstanceFrom($transaction); |
|
| 262 | + |
|
| 263 | + try { |
|
| 264 | + (new Query($this->getPdo(), $this->getTableName())) |
|
| 265 | + ->insert($this->getActiveRecordColumns()) |
|
| 266 | + ->execute(); |
|
| 267 | + |
|
| 268 | + $this->setId(intval($this->getPdo()->lastInsertId())); |
|
| 269 | + } catch (\PDOException $e) { |
|
| 270 | + // @TODO: Potentially filter and store mysql messages (where possible) in error messages |
|
| 271 | + throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + return [null, $this->toArray($readWhitelist)]; |
|
| 275 | + } else { |
|
| 276 | + return [$errors, null]; |
|
| 277 | + } |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * @param Array $input Associative array of input values |
|
| 282 | + * @param Array $fieldWhitelist array of column names that are allowed to be filled by the input array |
|
| 283 | + * @return Array Array containing the set of optional errors (associative array) and an optional array representation (associative) |
|
| 284 | + * of the modified data. |
|
| 285 | + */ |
|
| 286 | + public function apiUpdate($input, Array $updateWhitelist, Array $readWhitelist) |
|
| 287 | + { |
|
| 288 | + $transaction = $this->newInstance(); |
|
| 289 | + $transaction->syncInstanceFrom($this); |
|
| 290 | + $errors = []; |
|
| 291 | + |
|
| 292 | + // Filter out all non-whitelisted input values |
|
| 293 | + $input = $this->filterInputColumns($input, $updateWhitelist); |
|
| 294 | + |
|
| 295 | + // Check for excess keys |
|
| 296 | + $errors += $transaction->validateExcessKeys($input); |
|
| 297 | + |
|
| 298 | + // Check for immutable keys |
|
| 299 | + $errors += $transaction->validateImmutableColumns($input); |
|
| 300 | + |
|
| 301 | + // Validate input values (using validation function) |
|
| 302 | + $errors += $transaction->validateInputValues($input); |
|
| 303 | + |
|
| 304 | + // "Copy" data into transaction |
|
| 305 | + $transaction->loadData($input); |
|
| 306 | + |
|
| 307 | + // Run create hooks |
|
| 308 | + foreach ($this->registeredUpdateHooks as $colName => $fn) { |
|
| 309 | + $fn(); |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + // Validate missing keys |
|
| 313 | + $errors += $transaction->validateMissingKeys($input); |
|
| 314 | + |
|
| 315 | + // Update database |
|
| 316 | + if (empty($errors)) { |
|
| 317 | + $this->syncInstanceFrom($transaction); |
|
| 318 | + |
|
| 319 | + try { |
|
| 320 | + (new Query($this->getPdo(), $this->getTableName())) |
|
| 321 | + ->update($this->getActiveRecordColumns()) |
|
| 322 | + ->where(Query::Equal('id', $this->getId())) |
|
| 323 | + ->execute(); |
|
| 324 | + } catch (\PDOException $e) { |
|
| 325 | + throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + return [null, $this->toArray($readWhitelist)]; |
|
| 329 | + } else { |
|
| 330 | + return [$errors, null]; |
|
| 331 | + } |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + /** |
|
| 335 | + * Returns this active record after reading the attributes from the entry with the given identifier. |
|
| 336 | + * |
|
| 337 | + * @param mixed $id |
|
| 338 | + * @return $this |
|
| 339 | + * @throws ActiveRecordException on failure. |
|
| 340 | + */ |
|
| 341 | + abstract public function read($id); |
|
| 342 | + |
|
| 343 | + /** |
|
| 344 | + * Returns the PDO. |
|
| 345 | + * |
|
| 346 | + * @return \PDO the PDO. |
|
| 347 | + */ |
|
| 348 | + abstract public function getPdo(); |
|
| 349 | + |
|
| 350 | + /** |
|
| 351 | + * Set the ID. |
|
| 352 | + * |
|
| 353 | + * @param int $id |
|
| 354 | + * @return $this |
|
| 355 | + */ |
|
| 356 | + abstract protected function setId($id); |
|
| 357 | + |
|
| 358 | + /** |
|
| 359 | + * Returns the ID. |
|
| 360 | + * |
|
| 361 | + * @return null|int The ID. |
|
| 362 | + */ |
|
| 363 | + abstract protected function getId(); |
|
| 364 | + |
|
| 365 | + /** |
|
| 366 | + * Returns the active record table. |
|
| 367 | + * |
|
| 368 | + * @return string the active record table name. |
|
| 369 | + */ |
|
| 370 | + abstract protected function getTableName(); |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * Returns the name -> variable mapping for the table definition. |
|
| 374 | + * @return Array The mapping |
|
| 375 | + */ |
|
| 376 | + abstract protected function getActiveRecordColumns(); |
|
| 377 | 377 | } |