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-03-24 17:16:34 +00:00
|
|
|
|
|
|
|
abstract class AbstractHandler implements Handler {
|
2017-04-07 01:41:21 +00:00
|
|
|
abstract function __construct();
|
|
|
|
abstract function dispatch(Request $req): Response;
|
2017-05-19 03:03:33 +00:00
|
|
|
|
|
|
|
protected function mapFieldNames(array $data, array $map, bool $overwrite = false): array {
|
|
|
|
foreach($map as $from => $to) {
|
|
|
|
if(array_key_exists($from, $data)) {
|
|
|
|
if($overwrite || !array_key_exists($to, $data)) $data[$to] = $data[$from];
|
|
|
|
unset($data[$from]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function mapFieldTypes(array $data, array $map): array {
|
|
|
|
foreach($map as $key => $type) {
|
|
|
|
if(array_key_exists($key, $data)) settype($data[$key], $type);
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2017-05-20 03:52:26 +00:00
|
|
|
protected function validateId($id): bool {
|
2017-05-19 03:03:33 +00:00
|
|
|
try {
|
|
|
|
$ch1 = strval(intval($id));
|
|
|
|
$ch2 = strval($id);
|
|
|
|
} catch(\Throwable $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return ($ch1 === $ch2);
|
|
|
|
}
|
|
|
|
|
2017-03-24 17:16:34 +00:00
|
|
|
}
|