From 3a0f4293dcf2b5e086ec79777a297bcd751a0f25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Selahattin=20=C3=9Cnl=C3=BC?= Date: Sun, 27 Nov 2016 23:10:33 +0300 Subject: [PATCH] #12 - add dynamic appends support --- src/QueryBuilder.php | 38 ++++++++++++++++++++++++++++++++++++-- src/UriParser.php | 3 ++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 64599a2..fa088b7 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -38,6 +38,8 @@ class QueryBuilder protected $excludedParameters = []; + protected $appends = []; + protected $query; protected $result; @@ -88,7 +90,13 @@ public function build() public function get() { - return $this->query->get(); + $result = $this->query->get(); + + if ($this->hasAppends()) { + $result = $this->addAppendsToModel($result); + } + + return $result; } public function paginate() @@ -97,7 +105,13 @@ public function paginate() throw new Exception("You can't use unlimited option for pagination", 1); } - return $this->basePaginate($this->limit); + $result = $this->basePaginate($this->limit); + + if ($this->hasAppends()) { + $result = $this->addAppendsToModel($result); + } + + return $result; } public function lists($value, $key) @@ -234,6 +248,11 @@ private function setWheres($parameters) $this->wheres = $parameters; } + private function setAppends($appends) + { + $this->appends = explode(',', $appends); + } + private function addWhereToQuery($where) { extract($where); @@ -310,6 +329,11 @@ private function hasIncludes() return (count($this->includes) > 0); } + private function hasAppends() + { + return (count($this->appends) > 0); + } + private function hasGroupBy() { return (count($this->groupBy) > 0); @@ -352,6 +376,16 @@ private function customFilterName($key) return 'filterBy' . studly_case($key); } + private function addAppendsToModel($result) + { + $result->map(function($item) { + $item->append($this->appends); + return $item; + }); + + return $result; + } + /** * Paginate the given query. * diff --git a/src/UriParser.php b/src/UriParser.php index 88533b7..0702330 100644 --- a/src/UriParser.php +++ b/src/UriParser.php @@ -18,7 +18,8 @@ class UriParser 'limit', 'page', 'columns', - 'includes' + 'includes', + 'appends' ]; protected $uri;