From 01e66f3645fe82d63da3e98cdcb899211974349d Mon Sep 17 00:00:00 2001 From: dpankratov Date: Wed, 10 Nov 2021 12:07:53 +0600 Subject: [PATCH] #-: - renamed variables; - removed types from the base repository methods; - renamed methods; - implemented an ability to set export mode via the third argument of assertEqualsFixture method; --- src/Traits/EntityControlTrait.php | 39 ++++++++++++++++--------------- src/Traits/FixturesTrait.php | 6 ++++- src/Traits/SearchTrait.php | 4 ++-- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/Traits/EntityControlTrait.php b/src/Traits/EntityControlTrait.php index 1861068d..657e5e3b 100755 --- a/src/Traits/EntityControlTrait.php +++ b/src/Traits/EntityControlTrait.php @@ -209,12 +209,13 @@ public function updateMany($where, array $data, bool $updatedRecordsAsResult = t $fields = $this->forceMode ? $modelClass::getFields() : $this->model->getFillable(); $entityData = Arr::only($data, $fields); - $unUpdatedIds = []; - $this->chunk($limit, function ($items) use (&$unUpdatedIds) { - $unUpdatedIds = array_merge($unUpdatedIds, Arr::pluck($items, 'id')); + $idsToUpdate = []; + + $this->chunk($limit, function ($items) use (&$idsToUpdate) { + $idsToUpdate = array_merge($idsToUpdate, Arr::pluck($items, 'id')); }, $where); - $updatedRowsCount = $this->updateByList($unUpdatedIds, $entityData); + $updatedRowsCount = $this->updateByList($idsToUpdate, $entityData); if (!$updatedRecordsAsResult) { return $updatedRowsCount; @@ -231,7 +232,7 @@ public function updateMany($where, array $data, bool $updatedRecordsAsResult = t * * @return array */ - public function update($where, array $data): array + public function update($where, array $data) { $item = $this->getQuery($where)->first(); @@ -278,11 +279,11 @@ public function count($where = []) return $this->getQuery($where)->count(); } - public function get(array $where = []): array + public function get(array $where = []) { $result = $this->getQuery($where)->get(); - $this->hideUnHideFields($result); + $this->applyHidingShowingFieldsRules($result); return $result->toArray(); } @@ -301,7 +302,7 @@ public function getOrCreate($data) return $entities; } - public function first(array $where = []): array + public function first($where = []) { $entity = $this->getQuery($where)->first(); @@ -311,12 +312,12 @@ public function first(array $where = []): array ->toArray(); } - public function findBy(string $field, $value): array + public function findBy(string $field, $value) { return $this->first([$field => $value]); } - public function find($id): array + public function find($id) { return $this->first([$this->primaryKey => $id]); } @@ -344,7 +345,7 @@ public function firstOrCreate($where, array $data = []) * @param array|integer|string $where * @return integer count of deleted rows */ - public function delete($where): int + public function delete($where) { $query = $this->getQuery($where); @@ -377,7 +378,7 @@ public function onlyTrashed($enable = true): self return $this; } - public function restore($where): int + public function restore($where) { return $this->getQuery($where)->onlyTrashed()->restore(); } @@ -388,7 +389,7 @@ public function chunk($limit, $callback, $where = []) ->getQuery($where) ->orderBy($this->primaryKey) ->chunk($limit, function ($items) use ($callback) { - $this->hideUnHideFields($items); + $this->applyHidingShowingFieldsRules($items); $callback($items->toArray()); }); @@ -401,7 +402,7 @@ public function chunk($limit, $callback, $where = []) * @param string|null $field condition field, primary key is default value * @return integer count of deleted rows */ - public function deleteByList(array $values, $field = null): int + public function deleteByList(array $values, $field = null) { $field = (empty($field)) ? $this->primaryKey : $field; @@ -416,7 +417,7 @@ public function deleteByList(array $values, $field = null): int } } - public function restoreByList($values, $field = null): array + public function restoreByList($values, $field = null) { $field = (empty($field)) ? $this->primaryKey : $field; @@ -427,14 +428,14 @@ public function restoreByList($values, $field = null): array $entities = $query->get(); - $this->hideUnHideFields($entities); + $this->applyHidingShowingFieldsRules($entities); $query->restore(); return $entities->toArray(); } - public function getByList(array $values, $field = null): array + public function getByList(array $values, $field = null) { $field = (empty($field)) ? $this->primaryKey : $field; @@ -443,7 +444,7 @@ public function getByList(array $values, $field = null): array ->whereIn($field, $values) ->get(); - $this->hideUnHideFields($result); + $this->applyHidingShowingFieldsRules($result); return $result->toArray(); } @@ -455,7 +456,7 @@ public function countByList(array $values, $field = null): int return $this->getQuery()->whereIn($field, $values)->count(); } - public function updateByList(array $values, $data, $field = null): int + public function updateByList(array $values, $data, $field = null) { $field = (empty($field)) ? $this->primaryKey : $field; diff --git a/src/Traits/FixturesTrait.php b/src/Traits/FixturesTrait.php index 4fe554bb..046ccdcc 100644 --- a/src/Traits/FixturesTrait.php +++ b/src/Traits/FixturesTrait.php @@ -102,8 +102,12 @@ public function getJsonResponse() return json_decode($response, true); } - public function assertEqualsFixture($fixture, $data) + public function assertEqualsFixture($fixture, $data, bool $exportMode = false) { + if ($exportMode) { + $this->exportJson($fixture, $data); + } + $this->assertEquals($this->getJsonFixture($fixture), $data); } diff --git a/src/Traits/SearchTrait.php b/src/Traits/SearchTrait.php index f43719a4..72a8f252 100644 --- a/src/Traits/SearchTrait.php +++ b/src/Traits/SearchTrait.php @@ -120,7 +120,7 @@ public function getModifiedPaginator($paginator) { $collection = $paginator->getCollection(); - $this->hideUnHideFields($collection); + $this->applyHidingShowingFieldsRules($collection); return $paginator->setCollection($collection); } @@ -341,7 +341,7 @@ protected function calculatePerPage($total) return config('defaults.items_per_page', 1); } - protected function hideUnHideFields(&$collection) + protected function applyHidingShowingFieldsRules(&$collection) { if (Application::VERSION >= '5.8') { $collection->makeHidden($this->hiddenAttributes)->makeVisible($this->visibleAttributes);