Jelajahi Sumber

增加"Form\Condition::else"方法

jqh 5 tahun lalu
induk
melakukan
cf5498d6bd
1 mengubah file dengan 23 tambahan dan 1 penghapusan
  1. 23 1
      src/Form/Condition.php

+ 23 - 1
src/Form/Condition.php

@@ -15,6 +15,8 @@ class Condition
 
     protected $condition;
 
+    protected $result;
+
     /**
      * @var \Closure[]
      */
@@ -38,6 +40,21 @@ class Condition
         $this->process($next);
     }
 
+    public function else(\Closure $next = null)
+    {
+        $self = $this;
+
+        $condition = $this->form->if(function () use ($self) {
+            return ! $self->getResult();
+        });
+
+        if ($next) {
+            $condition->then($next);
+        }
+
+        return $condition;
+    }
+
     public function process(\Closure $next = null)
     {
         if ($this->done) {
@@ -64,7 +81,12 @@ class Condition
             $this->condition = $this->call($this->condition);
         }
 
-        return $this->condition ? true : false;
+        return $this->result = $this->condition ? true : false;
+    }
+
+    public function getResult()
+    {
+        return $this->result;
     }
 
     protected function call(\Closure $callback)