|
@@ -2,15 +2,35 @@
|
|
|
|
|
|
namespace DNB;
|
|
|
|
|
|
+use Reflection;
|
|
|
+use ReflectionClass;
|
|
|
+use ReflectionFunction;
|
|
|
+
|
|
|
abstract class AbstractPostSerializable implements PostSerializableInterface {
|
|
|
public static function fromPostParams(array $post): PostSerializableInterface {
|
|
|
$vars = [];
|
|
|
|
|
|
- foreach (static::getClassVars() as $key)
|
|
|
+ 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));
|
|
|
}
|
|
|
|
|
|
- abstract public static function getClassVars(): array;
|
|
|
+ final public static function getPostParams(): array {
|
|
|
+
|
|
|
+ $vars = [];
|
|
|
+
|
|
|
+ foreach ((new ReflectionClass(static::class))->getConstructor()->getParameters() as $param)
|
|
|
+ $vars[$param->getName()] = !$param->isOptional();
|
|
|
+
|
|
|
+ var_dump($vars);
|
|
|
+ return $vars;
|
|
|
+ }
|
|
|
+
|
|
|
}
|