1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-23 17:12:41 +00:00
Arsse/lib/REST/AbstractHandler.php

53 lines
1.7 KiB
PHP
Raw Normal View History

<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
2017-03-28 04:12:12 +00:00
namespace JKingWeb\Arsse\REST;
2017-08-29 14:50:31 +00:00
use JKingWeb\Arsse\Misc\Date;
use JKingWeb\Arsse\Misc\ValueInfo;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
abstract class AbstractHandler implements Handler {
2017-08-29 14:50:31 +00:00
abstract public function __construct();
abstract public function dispatch(ServerRequestInterface $req): ResponseInterface;
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)) {
$out[$to] = $data[$from];
}
}
return $out;
2017-08-29 14:50:31 +00:00
}
2018-10-26 18:58:04 +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") {
$data[$key] = Date::transform($data[$key], $dateFormat, "sql");
} else {
settype($data[$key], $type);
}
}
}
return $data;
}
protected function normalizeInput(array $data, array $types, string $dateFormat = null, int $mode = 0): array {
$out = [];
foreach ($types as $key => $type) {
if (isset($data[$key])) {
$out[$key] = ValueInfo::normalize($data[$key], $type | $mode, $dateFormat);
} else {
$out[$key] = null;
}
}
return $out;
}
2017-08-29 14:50:31 +00:00
}