From c766ab7ebd6b17a456b32ae2f7afd31950f80a8e Mon Sep 17 00:00:00 2001 From: rmrhz Date: Thu, 1 Dec 2016 14:12:11 +0800 Subject: [PATCH 01/18] Change scope of entity property to protected --- src/Prjkt/Component/Repofuck/Repofuck.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Prjkt/Component/Repofuck/Repofuck.php b/src/Prjkt/Component/Repofuck/Repofuck.php index e8910f2..808b1de 100644 --- a/src/Prjkt/Component/Repofuck/Repofuck.php +++ b/src/Prjkt/Component/Repofuck/Repofuck.php @@ -36,7 +36,7 @@ abstract class Repofuck * * @var \Illuminate\Database\Eloquent\Model */ - public $entity; + protected $entity; /** * Entities container From e79d904561905d111d69158d7ce643cbea990bb4 Mon Sep 17 00:00:00 2001 From: rmrhz Date: Mon, 12 Dec 2016 21:23:59 +0800 Subject: [PATCH 02/18] Fixes #93 --- src/Prjkt/Component/Repofuck/Repofuck.php | 70 +++++------------------ 1 file changed, 14 insertions(+), 56 deletions(-) diff --git a/src/Prjkt/Component/Repofuck/Repofuck.php b/src/Prjkt/Component/Repofuck/Repofuck.php index 808b1de..70d578f 100644 --- a/src/Prjkt/Component/Repofuck/Repofuck.php +++ b/src/Prjkt/Component/Repofuck/Repofuck.php @@ -165,12 +165,22 @@ public function register($instance) : bool * Sets the entity to be chained * * @param string $name + * @param Closure $closure + * @throws Prjkt\Component\Repofuck\Exceptions\InstanceNotEntityException * @return self */ - public function entity(string $name = null) : self + public function entity(string $name = null, Closure $closure = null) : self { - $this->entity = $this->entities->resolve($name); + $entity = $this->entity = $this->entities->resolve($name); + if ( $closure instanceof Closure ) { + $return = call_user_func($closure, $entity); + } + + if ( ! $return instanceof Model ) { + throw new InstanceNotEntityException; + } + return $this; } @@ -326,60 +336,6 @@ public function first($params, $value = null) return $entity; } - /** - * Prepares the persistence of a repository or entity - * - * @param \Closure $function - * @return self - */ - public function prepare(Closure $function) : self - { - $return = call_user_func_array($function, [($this)->resetEntity()]); - - switch($return) - { - case null: - - return $this; - - break; - - case ( $return instanceof Builder or $return instanceof Model ): - - // This will persist the entity throughout the repository for the next operation - $this->entity = $return; - - break; - - case ( $return instanceof self && $return instanceof $this ): - - // Returns a modified persistence of itself where operations are contained - return $return; - - break; - - case ( $return instanceof self && ! $return instanceof $this ): - - // This will persist the repository for the next operation - // It also gives an advantage as the repository contained - $this->repositories->set($return); - - return $this->repositories->resolve(); - - break; - - case ( is_array($return) ): - - // This will persist the keys and data returned - $this->setDataAndKeys($return); - - break; - } - - // If there's a repository being persisted, return it, defer to self when there's none - return $this; - } - /** * Gets an entity by parameters * @@ -409,6 +365,8 @@ public function paginate(int $items = null) : LengthAwarePaginator */ public function create() : Model { + $this->entity = new $this->entity; + $entity = $this->map($this->getData(), $this->getkeys()); $entity->save(); From 3bb49468009b298f1a06b52c522b8e1a82451636 Mon Sep 17 00:00:00 2001 From: rmrhz Date: Tue, 17 Jan 2017 14:15:27 +0800 Subject: [PATCH 03/18] ~ #90 Remove deprecated function 'setDataAndKeys' --- src/Prjkt/Component/Repofuck/Repofuck.php | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/Prjkt/Component/Repofuck/Repofuck.php b/src/Prjkt/Component/Repofuck/Repofuck.php index c5b8a4d..c5cc10a 100644 --- a/src/Prjkt/Component/Repofuck/Repofuck.php +++ b/src/Prjkt/Component/Repofuck/Repofuck.php @@ -198,17 +198,6 @@ public function resetEntity(string $name = null) :self return $this->entity($name); } - /** - * [Deprecated] Set the data and keys for the repository - * - * @param array $parameters - * @return self - */ - public function setDataAndKeys(array $parameters) : self - { - return $this->data($parameters, true); - } - /** * Set the data and keys for the repository * From 30aaf896dc2d84845ab87514132de6798f22cf29 Mon Sep 17 00:00:00 2001 From: rmrhz Date: Tue, 17 Jan 2017 14:16:19 +0800 Subject: [PATCH 04/18] ~ #90 Rename 'setData' & 'setKeys' to 'data' & 'keys' --- src/Prjkt/Component/Repofuck/Repofuck.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Prjkt/Component/Repofuck/Repofuck.php b/src/Prjkt/Component/Repofuck/Repofuck.php index c5cc10a..0881cbb 100644 --- a/src/Prjkt/Component/Repofuck/Repofuck.php +++ b/src/Prjkt/Component/Repofuck/Repofuck.php @@ -236,7 +236,7 @@ public function setColumns(array $columns) : self * @param array * @return self */ - public function setData(array $data) : self + public function data(array $data = []) : self { $this->data = $data; @@ -249,7 +249,7 @@ public function setData(array $data) : self * @param array * @return self */ - public function setKeys(array $keys) : self + public function keys(array $keys = []) : self { $this->keys = $keys; From 9114bfdd9325a22937174aed7cd0836fe8ee6cc1 Mon Sep 17 00:00:00 2001 From: rmrhz Date: Tue, 17 Jan 2017 14:17:46 +0800 Subject: [PATCH 05/18] Rename 'data' to 'fill', rewrite functionality --- src/Prjkt/Component/Repofuck/Repofuck.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Prjkt/Component/Repofuck/Repofuck.php b/src/Prjkt/Component/Repofuck/Repofuck.php index 0881cbb..f790fc2 100644 --- a/src/Prjkt/Component/Repofuck/Repofuck.php +++ b/src/Prjkt/Component/Repofuck/Repofuck.php @@ -199,20 +199,18 @@ public function resetEntity(string $name = null) :self } /** - * Set the data and keys for the repository + * Set the fill data and keys for the repository * * @param array $parameters - * @param bool $keys [def=false] Set the keys * @return self */ - public function data(array $parameters, bool $keys = false) : self + public function fill(array $parameters) : self { if ( $keys ) { - $keys = array_keys($parameters); - $this->setKeys($keys); + $this->keys(array_keys($keys)); } - $this->setData($parameters); + $this->data($parameters); return $this; } From ba6316649007f5a297669b8de92ecb367052aad3 Mon Sep 17 00:00:00 2001 From: rmrhz Date: Tue, 17 Jan 2017 14:34:26 +0800 Subject: [PATCH 06/18] ~ #90 Renamed 'getColumns' to 'columns' --- src/Prjkt/Component/Repofuck/Repofuck.php | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/Prjkt/Component/Repofuck/Repofuck.php b/src/Prjkt/Component/Repofuck/Repofuck.php index f790fc2..48b2351 100644 --- a/src/Prjkt/Component/Repofuck/Repofuck.php +++ b/src/Prjkt/Component/Repofuck/Repofuck.php @@ -255,25 +255,16 @@ public function keys(array $keys = []) : self } /** - * Get the columns + * Set the columns for the repository * - * @return array + * @return $this */ - public function getColumns() : array + public function columns(array $columns = []) : array { - return $this->columns; - } + $this->columns = $columns; - /** - * Get the data in the repository - * - * @return array - */ - public function getData() : array - { - return $this->data; + return $this; } - /** * Get the keys in the repository * From 70dd201863e1159090078c36f639a752ffb81cf5 Mon Sep 17 00:00:00 2001 From: rmrhz Date: Tue, 17 Jan 2017 14:34:47 +0800 Subject: [PATCH 07/18] ~ #90 Remove deprecated funciton 'getKeys' --- src/Prjkt/Component/Repofuck/Repofuck.php | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/Prjkt/Component/Repofuck/Repofuck.php b/src/Prjkt/Component/Repofuck/Repofuck.php index 48b2351..a52c4e0 100644 --- a/src/Prjkt/Component/Repofuck/Repofuck.php +++ b/src/Prjkt/Component/Repofuck/Repofuck.php @@ -265,15 +265,6 @@ public function columns(array $columns = []) : array return $this; } - /** - * Get the keys in the repository - * - * @return array - */ - public function getKeys() : array - { - return $this->keys; - } /** * Finds an entity by its ID From ab2810d86e5cd7267ba6f41ed655c135c63409c9 Mon Sep 17 00:00:00 2001 From: rmrhz Date: Tue, 17 Jan 2017 14:43:30 +0800 Subject: [PATCH 08/18] ~ #90 Remove 'setColumns' function --- src/Prjkt/Component/Repofuck/Repofuck.php | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/src/Prjkt/Component/Repofuck/Repofuck.php b/src/Prjkt/Component/Repofuck/Repofuck.php index a52c4e0..c0b89f0 100644 --- a/src/Prjkt/Component/Repofuck/Repofuck.php +++ b/src/Prjkt/Component/Repofuck/Repofuck.php @@ -214,20 +214,7 @@ public function fill(array $parameters) : self return $this; } - - /** - * Set the columns to be queried - * - * @param array $columns - * @return self - */ - public function setColumns(array $columns) : self - { - $this->columns = $columns; - - return $this; - } - + /** * Set the data for the repository * @@ -297,9 +284,9 @@ public function first($params, $value = null) case ( is_array($params) ): - $params = ! $this->hasValues($params) ? $this->getData() : $params; + $params = ! $this->hasValues($params) ? $this->data : $params; - $entity = $this->entity->where($params)->firstOrFail($this->getColumns()); + $entity = $this->entity->where($params)->firstOrFail($this->columns); break; @@ -344,7 +331,7 @@ public function create() : Model { $this->entity = new $this->entity; - $entity = $this->map($this->getData()); + $entity = $this->map($this->data); $entity->save(); @@ -360,7 +347,7 @@ public function create() : Model public function update($identifier) : Model { $entity = $identifier instanceof Model ? $identifier : $this->first($identifier); - $entity = $this->map($this->getData(), $entity); + $entity = $this->map($this->data, $entity); $entity->save(); From 21c206a2c9d0e40d8c62adb3708c13343568316a Mon Sep 17 00:00:00 2001 From: rmrhz Date: Tue, 17 Jan 2017 14:43:49 +0800 Subject: [PATCH 09/18] ~ #90 Altered return type to self --- src/Prjkt/Component/Repofuck/Repofuck.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Prjkt/Component/Repofuck/Repofuck.php b/src/Prjkt/Component/Repofuck/Repofuck.php index c0b89f0..7ea0af3 100644 --- a/src/Prjkt/Component/Repofuck/Repofuck.php +++ b/src/Prjkt/Component/Repofuck/Repofuck.php @@ -214,7 +214,7 @@ public function fill(array $parameters) : self return $this; } - + /** * Set the data for the repository * @@ -246,7 +246,7 @@ public function keys(array $keys = []) : self * * @return $this */ - public function columns(array $columns = []) : array + public function columns(array $columns = []) : self { $this->columns = $columns; From b35211519bb11eefbb8e19890a7ffed3de8a07c7 Mon Sep 17 00:00:00 2001 From: makoru-hikage Date: Tue, 24 Jan 2017 14:03:44 +0800 Subject: [PATCH 10/18] Added updateOrCreate function --- src/Prjkt/Component/Repofuck/Repofuck.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/Prjkt/Component/Repofuck/Repofuck.php b/src/Prjkt/Component/Repofuck/Repofuck.php index 51d563c..9c671a5 100644 --- a/src/Prjkt/Component/Repofuck/Repofuck.php +++ b/src/Prjkt/Component/Repofuck/Repofuck.php @@ -449,6 +449,26 @@ public function update($identifier) : Model return $entity; } + /** + * Finds an entity and updates it. It's created when it's non-existent + * + * @param mixed int|string|array|\Illuminate\Database\Eloquent\Model $entity + * @return \Illuminate\Database\Eloquent\Model $entity + */ + public function updateOrCreate($identifier) : Model + { + $entity = $identifier; + + if (! $entity instanceof Model) { + $entity = $this->first($identifier) ?? new $this->entity; + } + + $entity = $this->map($this->data, $entity); + $entity->save(); + + return $entity; + } + /** * Deletes the entity * From 75a6199b17d180f6c240cc4ea681b556d41d2c02 Mon Sep 17 00:00:00 2001 From: m_hikage Date: Mon, 6 Feb 2017 12:20:19 +0800 Subject: [PATCH 11/18] UpdateOrCreate update... Eloquent Model's `updateOrCreate` also uses `firstOrNew`. It's like the `Model`'s `updateOrCreate`, but integrated with `Repofuck`'s `map()`. --- src/Prjkt/Component/Repofuck/Repofuck.php | 35 +++++++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/src/Prjkt/Component/Repofuck/Repofuck.php b/src/Prjkt/Component/Repofuck/Repofuck.php index 9c671a5..49c6808 100644 --- a/src/Prjkt/Component/Repofuck/Repofuck.php +++ b/src/Prjkt/Component/Repofuck/Repofuck.php @@ -452,17 +452,40 @@ public function update($identifier) : Model /** * Finds an entity and updates it. It's created when it's non-existent * - * @param mixed int|string|array|\Illuminate\Database\Eloquent\Model $entity + * @param mixed int|string|array * @return \Illuminate\Database\Eloquent\Model $entity */ public function updateOrCreate($identifier) : Model { - $entity = $identifier; - - if (! $entity instanceof Model) { - $entity = $this->first($identifier) ?? new $this->entity; + $entity = $this->entity->firstOrNew($identifier); + switch ($identifier) + { + case null: + + $entity = new $this->entity; + + break; + + case ( is_numeric($identifer) ): + + $entity = $this->entity->findOrNew($params, $this->columns); + + break; + + case ( is_array($identifer) ): + + $entity = $this->entity->firstOrNew($params); + + break; + + case ( is_string($params) && ! is_null($value) ): + + $entity = $this->entity->where($params, $value)->firstOrNew(); + + break; + } - + $entity = $this->map($this->data, $entity); $entity->save(); From 56215697f84662027ba8747ca055442e711156d9 Mon Sep 17 00:00:00 2001 From: m_hikage Date: Mon, 6 Feb 2017 12:21:02 +0800 Subject: [PATCH 12/18] Update Repofuck.php --- src/Prjkt/Component/Repofuck/Repofuck.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Prjkt/Component/Repofuck/Repofuck.php b/src/Prjkt/Component/Repofuck/Repofuck.php index 49c6808..1255af9 100644 --- a/src/Prjkt/Component/Repofuck/Repofuck.php +++ b/src/Prjkt/Component/Repofuck/Repofuck.php @@ -457,7 +457,6 @@ public function update($identifier) : Model */ public function updateOrCreate($identifier) : Model { - $entity = $this->entity->firstOrNew($identifier); switch ($identifier) { case null: From e2b879b087087cc3634f7ef3fb7567696373c31f Mon Sep 17 00:00:00 2001 From: m_hikage Date: Mon, 6 Feb 2017 12:22:29 +0800 Subject: [PATCH 13/18] Update Repofuck.php --- src/Prjkt/Component/Repofuck/Repofuck.php | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/Prjkt/Component/Repofuck/Repofuck.php b/src/Prjkt/Component/Repofuck/Repofuck.php index 1255af9..e5b2c2e 100644 --- a/src/Prjkt/Component/Repofuck/Repofuck.php +++ b/src/Prjkt/Component/Repofuck/Repofuck.php @@ -475,14 +475,7 @@ public function updateOrCreate($identifier) : Model $entity = $this->entity->firstOrNew($params); - break; - - case ( is_string($params) && ! is_null($value) ): - - $entity = $this->entity->where($params, $value)->firstOrNew(); - - break; - + break; } $entity = $this->map($this->data, $entity); From 8af31041ffad544dcc84f8d79bf0a9b856f3a598 Mon Sep 17 00:00:00 2001 From: m_hikage Date: Tue, 7 Feb 2017 23:30:37 +0800 Subject: [PATCH 14/18] Changed `case null` to `default` --- src/Prjkt/Component/Repofuck/Repofuck.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Prjkt/Component/Repofuck/Repofuck.php b/src/Prjkt/Component/Repofuck/Repofuck.php index e5b2c2e..656ffd7 100644 --- a/src/Prjkt/Component/Repofuck/Repofuck.php +++ b/src/Prjkt/Component/Repofuck/Repofuck.php @@ -459,12 +459,6 @@ public function updateOrCreate($identifier) : Model { switch ($identifier) { - case null: - - $entity = new $this->entity; - - break; - case ( is_numeric($identifer) ): $entity = $this->entity->findOrNew($params, $this->columns); @@ -475,7 +469,13 @@ public function updateOrCreate($identifier) : Model $entity = $this->entity->firstOrNew($params); - break; + break; + + default: + + $entity = new $this->entity; + + break; } $entity = $this->map($this->data, $entity); From c14656c6368a2fe3a0678b87640b95db9583eb98 Mon Sep 17 00:00:00 2001 From: m_hikage Date: Sat, 11 Feb 2017 08:38:56 +0800 Subject: [PATCH 15/18] Update Repofuck.php --- src/Prjkt/Component/Repofuck/Repofuck.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Prjkt/Component/Repofuck/Repofuck.php b/src/Prjkt/Component/Repofuck/Repofuck.php index 656ffd7..ab45cd6 100644 --- a/src/Prjkt/Component/Repofuck/Repofuck.php +++ b/src/Prjkt/Component/Repofuck/Repofuck.php @@ -328,13 +328,13 @@ public function first($params, $value = null) $params = ! $this->hasValues($params) ? $this->getData() : $params; - $entity = $this->entity->where($params)->firstOrFail($this->getColumns()); + $entity = $this->entity->where($params)->firstOrFail($this->columns); break; case ( is_string($params) && ! is_null($value) ): - $entity = $this->entity->where($params, $value)->firstOrFail(); + $entity = $this->entity->where($params, $value)->firstOrFail($this->columns); break; } @@ -399,11 +399,13 @@ public function prepare(Closure $function) : self /** * Gets an entity by parameters * + * @param array $columns * @return \Illuminate\Database\Eloquent\Collection */ - public function get() : Collection + public function get($columns = null) : Collection { - return $this->entity->get(); + $columns = is_null($columns) ? $this->columns : $columns; + return $this->entity->get($columns); } /** From af2f0759f94d8794400ef18c812cf0628b950384 Mon Sep 17 00:00:00 2001 From: m_hikage Date: Thu, 16 Feb 2017 17:54:02 +0800 Subject: [PATCH 16/18] Must set columns through the `columns()` method --- src/Prjkt/Component/Repofuck/Repofuck.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Prjkt/Component/Repofuck/Repofuck.php b/src/Prjkt/Component/Repofuck/Repofuck.php index ab45cd6..b6bf7dc 100644 --- a/src/Prjkt/Component/Repofuck/Repofuck.php +++ b/src/Prjkt/Component/Repofuck/Repofuck.php @@ -402,10 +402,9 @@ public function prepare(Closure $function) : self * @param array $columns * @return \Illuminate\Database\Eloquent\Collection */ - public function get($columns = null) : Collection + public function get() : Collection { - $columns = is_null($columns) ? $this->columns : $columns; - return $this->entity->get($columns); + return $this->entity->get($this->columns); } /** From c3f1aa6d313f749d433c9bea7c5bd7626aec2350 Mon Sep 17 00:00:00 2001 From: makoru-hikage Date: Wed, 29 Mar 2017 16:08:44 +0800 Subject: [PATCH 17/18] Remove unnecessary function call --- src/Prjkt/Component/Repofuck/Repofuck.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Prjkt/Component/Repofuck/Repofuck.php b/src/Prjkt/Component/Repofuck/Repofuck.php index 6d3a1b4..02a25a8 100644 --- a/src/Prjkt/Component/Repofuck/Repofuck.php +++ b/src/Prjkt/Component/Repofuck/Repofuck.php @@ -327,7 +327,7 @@ public function first($params, $value = null) $params = ! $this->hasValues($params) ? $this->getData() : $params; - $entity = $this->entity->where($params)->first($this->getColumns()); + $entity = $this->entity->where($params)->first($this->columns); break; From 238706918c565a47860760b6f97f44734725d4be Mon Sep 17 00:00:00 2001 From: makoru-hikage Date: Wed, 29 Mar 2017 16:09:53 +0800 Subject: [PATCH 18/18] Supplied the missing argument --- src/Prjkt/Component/Repofuck/Repofuck.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Prjkt/Component/Repofuck/Repofuck.php b/src/Prjkt/Component/Repofuck/Repofuck.php index 02a25a8..9ce7311 100644 --- a/src/Prjkt/Component/Repofuck/Repofuck.php +++ b/src/Prjkt/Component/Repofuck/Repofuck.php @@ -333,7 +333,7 @@ public function first($params, $value = null) case ( is_string($params) && ! is_null($value) ): - $entity = $this->entity->where($params, $value)->first(); + $entity = $this->entity->where($params, $value)->first($this->columns); break; }