2017-03-24 17:16:34 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
2017-03-28 04:12:12 +00:00
|
|
|
namespace JKingWeb\Arsse\REST;
|
2017-08-29 14:50:31 +00:00
|
|
|
|
2017-07-17 11:47:57 +00:00
|
|
|
use JKingWeb\Arsse\Misc\Date;
|
2017-09-26 20:45:41 +00:00
|
|
|
use JKingWeb\Arsse\Misc\ValueInfo;
|
2017-03-24 17:16:34 +00:00
|
|
|
|
|
|
|
abstract class AbstractHandler implements Handler {
|
2017-08-29 14:50:31 +00:00
|
|
|
abstract public function __construct();
|
|
|
|
abstract public function dispatch(Request $req): Response;
|
2017-05-19 03:03:33 +00:00
|
|
|
|
2017-07-07 12:13:03 +00:00
|
|
|
protected function fieldMapNames(array $data, array $map): array {
|
|
|
|
$out = [];
|
2017-08-29 14:50:31 +00:00
|
|
|
foreach ($map as $to => $from) {
|
|
|
|
if (array_key_exists($from, $data)) {
|
2017-07-07 12:13:03 +00:00
|
|
|
$out[$to] = $data[$from];
|
2017-05-19 03:03:33 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-07 12:13:03 +00:00
|
|
|
return $out;
|
2017-08-29 14:50:31 +00:00
|
|
|
}
|
2017-05-19 03:03:33 +00:00
|
|
|
|
2017-07-07 19:25:47 +00:00
|
|
|
protected function fieldMapTypes(array $data, array $map, string $dateFormat = "sql"): array {
|
2017-08-29 14:50:31 +00:00
|
|
|
foreach ($map as $key => $type) {
|
|
|
|
if (array_key_exists($key, $data)) {
|
|
|
|
if ($type=="datetime" && $dateFormat != "sql") {
|
2017-07-17 11:47:57 +00:00
|
|
|
$data[$key] = Date::transform($data[$key], $dateFormat, "sql");
|
2017-07-07 19:25:47 +00:00
|
|
|
} else {
|
|
|
|
settype($data[$key], $type);
|
|
|
|
}
|
|
|
|
}
|
2017-05-19 03:03:33 +00:00
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2017-07-07 19:25:47 +00:00
|
|
|
protected function NormalizeInput(array $data, array $types, string $dateFormat = null): array {
|
2017-07-07 12:13:03 +00:00
|
|
|
$out = [];
|
2017-08-29 14:50:31 +00:00
|
|
|
foreach ($data as $key => $value) {
|
|
|
|
if (!isset($types[$key])) {
|
2017-07-07 12:13:03 +00:00
|
|
|
$out[$key] = $value;
|
|
|
|
continue;
|
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
if (is_null($value)) {
|
2017-07-07 12:13:03 +00:00
|
|
|
$out[$key] = null;
|
|
|
|
continue;
|
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
switch ($types[$key]) {
|
2017-07-07 12:13:03 +00:00
|
|
|
case "int":
|
2017-09-28 14:16:24 +00:00
|
|
|
if (valueInfo::int($value) & ValueInfo::VALID) {
|
2017-07-21 02:40:09 +00:00
|
|
|
$out[$key] = (int) $value;
|
|
|
|
}
|
2017-07-07 12:13:03 +00:00
|
|
|
break;
|
|
|
|
case "string":
|
2017-09-28 14:16:24 +00:00
|
|
|
if (is_bool($value)) {
|
|
|
|
$out[$key] = var_export($value, true);
|
|
|
|
} elseif (!is_scalar($value)) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
$out[$key] = (string) $value;
|
|
|
|
}
|
2017-07-07 12:13:03 +00:00
|
|
|
break;
|
|
|
|
case "bool":
|
2017-09-28 14:16:24 +00:00
|
|
|
$test = filter_var($value, \FILTER_VALIDATE_BOOLEAN, \FILTER_NULL_ON_FAILURE);
|
|
|
|
if (!is_null($test)) {
|
|
|
|
$out[$key] = $test;
|
2017-07-07 12:13:03 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "float":
|
2017-09-28 14:16:24 +00:00
|
|
|
$test = filter_var($value, \FILTER_VALIDATE_FLOAT);
|
|
|
|
if ($test !== false) {
|
|
|
|
$out[$key] = $test;
|
2017-07-21 02:40:09 +00:00
|
|
|
}
|
2017-07-07 12:13:03 +00:00
|
|
|
break;
|
|
|
|
case "datetime":
|
2017-07-17 11:47:57 +00:00
|
|
|
$t = Date::normalize($value, $dateFormat);
|
2017-08-29 14:50:31 +00:00
|
|
|
if ($t) {
|
2017-07-21 02:40:09 +00:00
|
|
|
$out[$key] = $t;
|
|
|
|
}
|
2017-07-07 12:13:03 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Exception("typeUnknown", $types[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
}
|