2017-03-24 13:16:34 -04:00
|
|
|
<?php
|
2017-11-16 20:23:18 -05:00
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
2017-03-24 13:16:34 -04:00
|
|
|
declare(strict_types=1);
|
2017-03-27 23:12:12 -05:00
|
|
|
namespace JKingWeb\Arsse\REST;
|
2017-08-29 10:50:31 -04:00
|
|
|
|
2017-07-17 07:47:57 -04:00
|
|
|
use JKingWeb\Arsse\Misc\Date;
|
2017-09-26 16:45:41 -04:00
|
|
|
use JKingWeb\Arsse\Misc\ValueInfo;
|
2017-03-24 13:16:34 -04:00
|
|
|
|
|
|
|
abstract class AbstractHandler implements Handler {
|
2017-08-29 10:50:31 -04:00
|
|
|
abstract public function __construct();
|
|
|
|
abstract public function dispatch(Request $req): Response;
|
2017-05-18 23:03:33 -04:00
|
|
|
|
2017-07-07 08:13:03 -04:00
|
|
|
protected function fieldMapNames(array $data, array $map): array {
|
|
|
|
$out = [];
|
2017-08-29 10:50:31 -04:00
|
|
|
foreach ($map as $to => $from) {
|
|
|
|
if (array_key_exists($from, $data)) {
|
2017-07-07 08:13:03 -04:00
|
|
|
$out[$to] = $data[$from];
|
2017-05-18 23:03:33 -04:00
|
|
|
}
|
|
|
|
}
|
2017-07-07 08:13:03 -04:00
|
|
|
return $out;
|
2017-08-29 10:50:31 -04:00
|
|
|
}
|
2017-05-18 23:03:33 -04:00
|
|
|
|
2017-07-07 15:25:47 -04:00
|
|
|
protected function fieldMapTypes(array $data, array $map, string $dateFormat = "sql"): array {
|
2017-08-29 10:50:31 -04:00
|
|
|
foreach ($map as $key => $type) {
|
|
|
|
if (array_key_exists($key, $data)) {
|
|
|
|
if ($type=="datetime" && $dateFormat != "sql") {
|
2017-07-17 07:47:57 -04:00
|
|
|
$data[$key] = Date::transform($data[$key], $dateFormat, "sql");
|
2017-07-07 15:25:47 -04:00
|
|
|
} else {
|
|
|
|
settype($data[$key], $type);
|
|
|
|
}
|
|
|
|
}
|
2017-05-18 23:03:33 -04:00
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2017-10-19 20:35:45 -04:00
|
|
|
protected function normalizeInput(array $data, array $types, string $dateFormat = null, int $mode = 0): array {
|
2017-07-07 08:13:03 -04:00
|
|
|
$out = [];
|
2017-10-19 20:35:45 -04:00
|
|
|
foreach ($types as $key => $type) {
|
|
|
|
if (isset($data[$key])) {
|
|
|
|
$out[$key] = ValueInfo::normalize($data[$key], $type | $mode, $dateFormat);
|
|
|
|
} else {
|
2017-07-07 08:13:03 -04:00
|
|
|
$out[$key] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
2017-08-29 10:50:31 -04:00
|
|
|
}
|