From accb81ee308d16ee8d7b47e1de3c3b6b4b9e76d4 Mon Sep 17 00:00:00 2001 From: xwiz Date: Sun, 16 Feb 2020 14:49:22 +0100 Subject: [PATCH 1/3] Enable default response transformation with bound transformers --- src/Http/Response/Factory.php | 20 +++++++++++++++++--- src/Transformer/Factory.php | 2 +- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Http/Response/Factory.php b/src/Http/Response/Factory.php index 53b505baa..60a541c51 100644 --- a/src/Http/Response/Factory.php +++ b/src/Http/Response/Factory.php @@ -93,7 +93,7 @@ public function noContent() * * @return \Dingo\Api\Http\Response */ - public function collection(Collection $collection, $transformer, $parameters = [], Closure $after = null) + public function collection(Collection $collection, $transformer = null, $parameters = [], Closure $after = null) { if ($collection->isEmpty()) { $class = get_class($collection); @@ -106,6 +106,12 @@ public function collection(Collection $collection, $transformer, $parameters = [ $parameters = []; } + if($transformer !== null) { + $binding = $this->transformer->register($class, $transformer, $parameters, $after); + } else { + $binding = $this->transformer->getBinding($collection); + } + $binding = $this->transformer->register($class, $transformer, $parameters, $after); return new Response($collection, 200, [], $binding); @@ -130,7 +136,11 @@ public function item($item, $transformer, $parameters = [], Closure $after = nul $parameters = []; } - $binding = $this->transformer->register($class, $transformer, $parameters, $after); + if($transformer !== null) { + $binding = $this->transformer->register($class, $transformer, $parameters, $after); + } else { + $binding = $this->transformer->getBinding($item); + } return new Response($item, 200, [], $binding); } @@ -153,7 +163,11 @@ public function paginator(Paginator $paginator, $transformer, array $parameters $class = get_class($paginator->first()); } - $binding = $this->transformer->register($class, $transformer, $parameters, $after); + if($transformer !== null) { + $binding = $this->transformer->register($class, $transformer, $parameters, $after); + } else { + $binding = $this->transformer->getBinding($paginator->first()); + } return new Response($paginator, 200, [], $binding); } diff --git a/src/Transformer/Factory.php b/src/Transformer/Factory.php index fb3f72a15..ce8676bbe 100644 --- a/src/Transformer/Factory.php +++ b/src/Transformer/Factory.php @@ -110,7 +110,7 @@ public function transformableType($value) * * @return \Dingo\Api\Transformer\Binding */ - protected function getBinding($class) + public function getBinding($class) { if ($this->isCollection($class) && ! $class->isEmpty()) { return $this->getBindingFromCollection($class); From 3209284987d0d70d4904af23182586fadb1bef11 Mon Sep 17 00:00:00 2001 From: xwiz Date: Sun, 16 Feb 2020 15:00:29 +0100 Subject: [PATCH 2/3] Fix stylce CI issues --- src/Http/Response/Factory.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Http/Response/Factory.php b/src/Http/Response/Factory.php index 60a541c51..9517c58e4 100644 --- a/src/Http/Response/Factory.php +++ b/src/Http/Response/Factory.php @@ -106,7 +106,7 @@ public function collection(Collection $collection, $transformer = null, $paramet $parameters = []; } - if($transformer !== null) { + if ($transformer !== null) { $binding = $this->transformer->register($class, $transformer, $parameters, $after); } else { $binding = $this->transformer->getBinding($collection); @@ -136,7 +136,7 @@ public function item($item, $transformer, $parameters = [], Closure $after = nul $parameters = []; } - if($transformer !== null) { + if ($transformer !== null) { $binding = $this->transformer->register($class, $transformer, $parameters, $after); } else { $binding = $this->transformer->getBinding($item); @@ -163,7 +163,7 @@ public function paginator(Paginator $paginator, $transformer, array $parameters $class = get_class($paginator->first()); } - if($transformer !== null) { + if ($transformer !== null) { $binding = $this->transformer->register($class, $transformer, $parameters, $after); } else { $binding = $this->transformer->getBinding($paginator->first()); From 8c0fff4079b9d9b608320a88635d39aa5cb2a1f2 Mon Sep 17 00:00:00 2001 From: xwiz Date: Sun, 16 Feb 2020 15:10:30 +0100 Subject: [PATCH 3/3] Fix multiple binding call --- src/Http/Response/Factory.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Http/Response/Factory.php b/src/Http/Response/Factory.php index 9517c58e4..aa6caa690 100644 --- a/src/Http/Response/Factory.php +++ b/src/Http/Response/Factory.php @@ -112,8 +112,6 @@ public function collection(Collection $collection, $transformer = null, $paramet $binding = $this->transformer->getBinding($collection); } - $binding = $this->transformer->register($class, $transformer, $parameters, $after); - return new Response($collection, 200, [], $binding); }