mirror of
https://code.mensbeam.com/MensBeam/Arsse.git
synced 2024-12-22 21:22:40 +00:00
9eadd602bd
While the test suite passes, this commit yields a broken server: replacing ad hoc request objectss with PSR-7 ones is still required, as is emission of PSR-7 responses. Both will come in subsequent commits, with tests Diactoros was chosen specifically because it includes facilities for emitting responses, something which is awkward to test. The end of this refactoring should see both the Response and Request classes disappear, and the general REST class fully covered (as well as any speculative additions to AbstractHanlder).
50 lines
1.6 KiB
PHP
50 lines
1.6 KiB
PHP
<?php
|
|
/** @license MIT
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
declare(strict_types=1);
|
|
namespace JKingWeb\Arsse\REST;
|
|
|
|
use JKingWeb\Arsse\Misc\Date;
|
|
use JKingWeb\Arsse\Misc\ValueInfo;
|
|
|
|
abstract class AbstractHandler implements Handler {
|
|
abstract public function __construct();
|
|
abstract public function dispatch(Request $req): \Psr\Http\Message\ResponseInterface;
|
|
|
|
protected function fieldMapNames(array $data, array $map): array {
|
|
$out = [];
|
|
foreach ($map as $to => $from) {
|
|
if (array_key_exists($from, $data)) {
|
|
$out[$to] = $data[$from];
|
|
}
|
|
}
|
|
return $out;
|
|
}
|
|
|
|
protected function fieldMapTypes(array $data, array $map, string $dateFormat = "sql"): array {
|
|
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;
|
|
}
|
|
}
|