Kaynağa Gözat

add if_true/if_false method in FormMapper

Thomas Rabaix 10 yıl önce
ebeveyn
işleme
5c4a538dd4
3 değiştirilmiş dosya ile 58 ekleme ve 0 silme
  1. 4 0
      Form/FormMapper.php
  2. 50 0
      Mapper/BaseGroupedMapper.php
  3. 4 0
      Show/ShowMapper.php

+ 4 - 0
Form/FormMapper.php

@@ -56,6 +56,10 @@ class FormMapper extends BaseGroupedMapper
      */
     public function add($name, $type = null, array $options = array(), array $fieldDescriptionOptions = array())
     {
+        if ($this->apply !== null && !$this->apply) {
+            return $this;
+        }
+
         if ($name instanceof FormBuilder) {
             $fieldName = $name->getName();
         } else {

+ 50 - 0
Mapper/BaseGroupedMapper.php

@@ -19,6 +19,8 @@ abstract class BaseGroupedMapper extends BaseMapper
     protected $currentGroup;
     protected $currentTab;
 
+    protected $apply;
+
     abstract protected function getGroups();
     abstract protected function getTabs();
 
@@ -130,6 +132,54 @@ abstract class BaseGroupedMapper extends BaseMapper
         return $this;
     }
 
+    /**
+     * Only nested add if the condition match FALSE
+     *
+     * @param $bool
+     *
+     * @return $this
+     * @throws \RuntimeException
+     */
+    public function if_true($bool)
+    {
+        if (!$this->apply == null) {
+            throw new \RuntimeException('Cannot nest ifTrue call');
+        }
+
+        $this->apply = ($bool === true);
+
+        return $this;
+    }
+
+    /**
+     * Only nested add if the condition match FALSE
+     *
+     * @param $bool
+     *
+     * @return $this
+     * @throws \RuntimeException
+     */
+    public function if_false($bool)
+    {
+        if (!$this->apply == null) {
+            throw new \RuntimeException('Cannot nest ifTrue call');
+        }
+
+        $this->apply = ($bool === false);
+
+        return $this;
+    }
+
+    /**
+     * @return $this
+     */
+    public function end_if()
+    {
+        $this->apply = null;
+
+        return $this;
+    }
+
     /**
      * @param       $name
      * @param array $options

+ 4 - 0
Show/ShowMapper.php

@@ -46,6 +46,10 @@ class ShowMapper extends BaseGroupedMapper
      */
     public function add($name, $type = null, array $fieldDescriptionOptions = array())
     {
+        if ($this->apply !== null && !$this->apply) {
+            return $this;
+        }
+
         $fieldKey = ($name instanceof FieldDescriptionInterface) ? $name->getName() : $name;
 
         $this->addFieldToCurrentGroup($fieldKey);