From 588ba7f64e37e07d26864ed7add2ea5cb6779464 Mon Sep 17 00:00:00 2001 From: Simon Asika Date: Tue, 13 Jun 2017 00:46:32 +0800 Subject: [PATCH] Add pipe methods to view --- src/Core/View/AbstractView.php | 53 ++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/src/Core/View/AbstractView.php b/src/Core/View/AbstractView.php index 9b6df4aa..86ad373e 100644 --- a/src/Core/View/AbstractView.php +++ b/src/Core/View/AbstractView.php @@ -592,16 +592,59 @@ public function addModel($name, ModelRepository $model, $handler = null, $defaul } /** - * configureModel + * Configure a model by name alias. * - * @param callable $handler - * @param string $name + * @param string|callable $name The name alias of model, keep NULL as default model. + * Or just send a callable here as handler. + * @param callable $handler The callback handler. * * @return static */ - public function configureModel(callable $handler, $name = null) + public function configureModel($name, $handler = null) { - $handler($this->getModel($name), $this); + $this->pipe($name, $handler); + + return $this; + } + + /** + * Pipe a callback to model and view then return value. + * + * @param string|callable $name The name alias of model, keep NULL as default model. + * Or just send a callable here as handler. + * @param callable $handler The callback handler. + * + * @return mixed + */ + public function pipe($name, $handler = null) + { + if (is_callable($name)) + { + $handler = $name; + $name = null; + } + + return $handler($this->getModel($name), $this); + } + + /** + * Apply a callback to model and view data. + * + * @param string|callable $name The name alias of model, keep NULL as default model. + * Or just send a callable here as handler. + * @param callable $handler The callback handler. + * + * @return $this + */ + public function applyData($name, $handler = null) + { + if (is_callable($name)) + { + $handler = $name; + $name = null; + } + + $handler($this->getModel($name), $this->getData()); return $this; }