Эх сурвалжийг харах

add support for Type::map() to validate key-value objects and requiredIf method

phpdevcommunity 7 сар өмнө
parent
commit
ff13aff0d1

+ 15 - 0
src/Type/AbstractType.php

@@ -24,6 +24,21 @@ abstract class AbstractType
         return $this;
     }
 
+    final public function requiredIf($condition) : self
+    {
+        if (!is_bool($condition) && !is_callable($condition)) {
+            throw new \InvalidArgumentException('condition must be boolean or callable');
+        }
+
+        if (is_callable($condition)) {
+            $condition = $condition();
+        }
+        if ($condition) {
+            $this->required();
+        }
+        return $this;
+    }
+
     final public function optional(): self
     {
         $this->required = false;