1234567891011121314151617181920212223242526272829303132333435 |
- <?php
- namespace DNB;
- use Reflection;
- use ReflectionClass;
- use ReflectionFunction;
- abstract class AbstractPostSerializable implements PostSerializableInterface {
- public static function fromPostParams(array $post): PostSerializableInterface {
- $vars = [];
- foreach (static::getPostParams() as $key => $required) {
- $vars[$key] = isset($post[$key]) && ((bool)$post[$key]) && strtolower((string)$post[$key]) != "off" ? htmlspecialchars($post[$key], ENT_QUOTES | ENT_SUBSTITUTE) : null;
- if ($vars[$key] == null && $required)
- throw new \Symfony\Component\HttpClient\Exception\InvalidArgumentException(
- sprintf("Missing required argmuent: %s", $key)
- );
- }
- return new static(...array_values($vars));
- }
- final public static function getPostParams(): array {
-
- $vars = [];
- foreach ((new ReflectionClass(static::class))->getConstructor()->getParameters() as $param)
- $vars[$param->getName()] = !$param->isOptional() || !$param->allowsNull();
- return $vars;
- }
- }
|