Browse Source

Widget增加when方法

jqh 4 years ago
parent
commit
ae3b245dc6
1 changed files with 17 additions and 0 deletions
  1. 17 0
      src/Widgets/Widget.php

+ 17 - 0
src/Widgets/Widget.php

@@ -68,6 +68,23 @@ abstract class Widget implements Renderable
         return new static(...$params);
     }
 
+    /**
+     * 符合条件则执行.
+     *
+     * @param  mixed  $value
+     * @param  callable  $callback
+     *
+     * @return $this|mixed
+     */
+    public function when($value, $callback)
+    {
+        if ($value) {
+            return $callback($this, $value) ?: $this;
+        }
+
+        return $this;
+    }
+
     /**
      * 批量设置选项.
      *