Browse Source

Rewrite post params (class vars.)

Joachim M. Giæver 2 years ago
parent
commit
ed0c24c7df

+ 22 - 2
src/AbstractPostSerializable.php

@@ -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;
+    }
+
 }

+ 0 - 4
src/Assignment.php

@@ -26,8 +26,4 @@ class Assignment extends AbstractPostSerializable implements PostSerializableInt
             'assignmentNumber' => $this->number
         ];
     }
-
-    public static function getClassVars(): array {
-        return array_keys(get_class_vars(__CLASS__));
-    }
 }

+ 2 - 5
src/Customer.php

@@ -11,8 +11,8 @@ class Customer extends AbstractPostSerializable implements PostSerializableInter
 
     public function __construct(
         string $name,
-        ?string $email,
         string $mobilePhone,
+        ?string $email = null,
         ?string $zipCode = null,
         ?string $comment = null
     ) {
@@ -26,14 +26,11 @@ class Customer extends AbstractPostSerializable implements PostSerializableInter
     public function toPostParams(): array {
         $arr = [];
 
-        foreach (static::getClassVars() as $key)
+        foreach (array_keys(static::getPostParams()) as $key)
             if (!is_null($this->{$key}))
                 $arr[$key] = $this->{$key};
 
         return $arr;
     }
 
-    public static function getClassVars(): array {
-        return array_keys(get_class_vars(__CLASS__));
-    }
 }

+ 1 - 0
src/PostSerializableInterface.php

@@ -5,4 +5,5 @@ namespace DNB;
 interface PostSerializableInterface {
     public function toPostParams(): array;
     public static function fromPostParams(array $post): self;
+    public static function getPostParams();
 }

+ 0 - 3
src/Prospectus.php

@@ -28,7 +28,4 @@ class Prospectus extends AbstractPostSerializable implements PostSerializableInt
         );
     }
 
-    public static function getClassVars(): array {
-        return array_keys(get_class_vars(__CLASS__));
-    }
 }

+ 1 - 4
src/Questionaire.php

@@ -21,13 +21,10 @@ class Questionaire extends AbstractPostSerializable implements PostSerializableI
     public function toPostParams(): array {
         $arr = [];
 
-        foreach (static::getClassVars() as $key)
+        foreach (array_keys(static::getPostParams()) as $key)
             $arr[$key] = !is_null($this->{$key}) ? $this->{$key} : false;
 
         return $arr;
     }
 
-    public static function getClassVars(): array {
-        return array_keys(get_class_vars(__CLASS__));
-    }
 }