浏览代码

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

phpdevcommunity 7 月之前
父节点
当前提交
ff13aff0d1
共有 1 个文件被更改,包括 15 次插入0 次删除
  1. 15 0
      src/Type/AbstractType.php

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