2017-03-24 17:16:34 +00:00
|
|
|
<?php
|
2017-11-17 01:23:18 +00:00
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
2017-03-24 17:16:34 +00:00
|
|
|
declare(strict_types=1);
|
2017-03-28 04:12:12 +00:00
|
|
|
namespace JKingWeb\Arsse\REST;
|
2017-08-29 14:50:31 +00:00
|
|
|
|
2020-12-21 03:30:59 +00:00
|
|
|
use JKingWeb\Arsse\Arsse;
|
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;
|
2018-01-05 04:08:53 +00:00
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
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();
|
2018-01-05 04:08:53 +00:00
|
|
|
abstract public function dispatch(ServerRequestInterface $req): ResponseInterface;
|
2017-05-19 03:03:33 +00:00
|
|
|
|
2020-12-21 03:30:59 +00:00
|
|
|
protected function now(): \DateTimeImmutable {
|
2021-02-07 04:51:23 +00:00
|
|
|
return Arsse::$obj->get(\DateTimeImmutable::class)->setTimezone(new \DateTimeZone("UTC"));
|
2020-12-21 03:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function isAdmin(): bool {
|
|
|
|
return (bool) Arsse::$user->propertiesGet(Arsse::$user->id, false)['admin'];
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2018-10-26 18:58:04 +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)) {
|
2019-01-11 15:38:06 +00:00
|
|
|
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-10-20 00:35:45 +00:00
|
|
|
protected function normalizeInput(array $data, array $types, string $dateFormat = null, int $mode = 0): array {
|
2017-07-07 12:13:03 +00:00
|
|
|
$out = [];
|
2017-10-20 00:35:45 +00:00
|
|
|
foreach ($types as $key => $type) {
|
|
|
|
if (isset($data[$key])) {
|
|
|
|
$out[$key] = ValueInfo::normalize($data[$key], $type | $mode, $dateFormat);
|
|
|
|
} else {
|
2017-07-07 12:13:03 +00:00
|
|
|
$out[$key] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
}
|