Skip to content

Commit

Permalink
Merge pull request #14 from RonasIT/dpankratov/version-update
Browse files Browse the repository at this point in the history
#-:
  • Loading branch information
DenTray authored Nov 9, 2021
2 parents 5cc8863 + 4ff3ae7 commit fce98a8
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 56 deletions.
105 changes: 58 additions & 47 deletions src/Traits/EntityControlTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function truncate()
$modelInstance::truncate();
}

public function force($value = true)
public function force($value = true): self
{
$this->forceMode = $value;

Expand All @@ -56,16 +56,24 @@ public function setModel($modelClass)
$this->checkPrimaryKey();
}

public function makeHidden(array $hiddenAttributes = [])
/**
* @param array|string $hiddenAttributes
* @return $this
*/
public function makeHidden($hiddenAttributes = []): self
{
$this->hiddenAttributes = $hiddenAttributes;
$this->hiddenAttributes = Arr::wrap($hiddenAttributes);

return $this;
}

public function makeVisible(array $visibleAttributes = [])
/**
* @param array|string $visibleAttributes
* @return $this
*/
public function makeVisible($visibleAttributes = []): self
{
$this->visibleAttributes = $visibleAttributes;
$this->visibleAttributes = Arr::wrap($visibleAttributes);

return $this;
}
Expand Down Expand Up @@ -111,9 +119,10 @@ protected function getQuery($where = [])

/**
* @param $relations array|string
*
* @return $this
*/
public function withRelations($relations)
public function withRelations($relations): self
{
$this->requiredRelations = Arr::wrap($relations);

Expand All @@ -122,9 +131,10 @@ public function withRelations($relations)

/**
* @param $relations array|string
*
* @return $this
*/
public function withRelationsCount($relations)
public function withRelationsCount($relations): self
{
$this->requiredRelationsCount = Arr::wrap($relations);

Expand All @@ -138,7 +148,7 @@ public function withRelationsCount($relations)
*
* @return boolean
*/
public function exists($where)
public function exists($where): bool
{
return $this->getQuery($where)->exists();
}
Expand All @@ -151,7 +161,7 @@ public function exists($where)
*
* @return boolean
*/
public function existsBy($field, $value)
public function existsBy($field, $value): bool
{
return $this->getQuery([$field => $value])->exists();
}
Expand Down Expand Up @@ -191,7 +201,7 @@ public function create($data)
* @param bool $updatedRecordsAsResult
* @param int $limit
*
* @return array
* @return array|int
*/
public function updateMany($where, array $data, bool $updatedRecordsAsResult = true, int $limit = 50)
{
Expand Down Expand Up @@ -221,7 +231,7 @@ public function updateMany($where, array $data, bool $updatedRecordsAsResult = t
*
* @return array
*/
public function update($where, array $data)
public function update($where, array $data): array
{
$item = $this->getQuery($where)->first();

Expand Down Expand Up @@ -268,19 +278,13 @@ public function count($where = [])
return $this->getQuery($where)->count();
}

/**
* @param array $where
*
* @return array
*/
public function get($where = [])
public function get(array $where = []): array
{
return $this
->getQuery($where)
->get()
->makeHidden($this->hiddenAttributes)
->makeVisible($this->visibleAttributes)
->toArray();
$result = $this->getQuery($where)->get();

$this->hideUnHideFields($result);

return $result->toArray();
}

/**
Expand All @@ -297,7 +301,7 @@ public function getOrCreate($data)
return $entities;
}

public function first($where)
public function first(array $where = []): array
{
$entity = $this->getQuery($where)->first();

Expand All @@ -307,17 +311,23 @@ public function first($where)
->toArray();
}

public function findBy($field, $value)
public function findBy(string $field, $value): array
{
return $this->first([$field => $value]);
}

public function find($id)
public function find($id): array
{
return $this->first([$this->primaryKey => $id]);
}

public function firstOrCreate($where, $data = [])
/**
* @param array|string|int $where array of conditions or primary key value
* @param array $data
*
* @return array|mixed
*/
public function firstOrCreate($where, array $data = [])
{
$entity = $this->first($where);

Expand Down Expand Up @@ -353,21 +363,21 @@ public function forceDelete($where)
$this->getQuery($where)->forceDelete();
}

public function withTrashed($enable = true)
public function withTrashed($enable = true): self
{
$this->withTrashed = $enable;

return $this;
}

public function onlyTrashed($enable = true)
public function onlyTrashed($enable = true): self
{
$this->onlyTrashed = $enable;

return $this;
}

public function restore($where)
public function restore($where): int
{
return $this->getQuery($where)->onlyTrashed()->restore();
}
Expand All @@ -378,11 +388,9 @@ public function chunk($limit, $callback, $where = [])
->getQuery($where)
->orderBy($this->primaryKey)
->chunk($limit, function ($items) use ($callback) {
$callback($items
->makeHidden($this->hiddenAttributes)
->makeVisible($this->visibleAttributes)
->toArray()
);
$this->hideUnHideFields($items);

$callback($items->toArray());
});
}

Expand All @@ -408,7 +416,7 @@ public function deleteByList(array $values, $field = null): int
}
}

public function restoreByList($values, $field = null)
public function restoreByList($values, $field = null): array
{
$field = (empty($field)) ? $this->primaryKey : $field;

Expand All @@ -417,34 +425,37 @@ public function restoreByList($values, $field = null)
->onlyTrashed()
->whereIn($field, $values);

$entities = $query->get()->toArray();
$entities = $query->get();

$this->hideUnHideFields($entities);

$query->restore();

return $entities;
return $entities->toArray();
}

public function getByList(array $values, $field = null)
public function getByList(array $values, $field = null): array
{
$field = (empty($field)) ? $this->primaryKey : $field;

return $this
$result = $this
->getQuery()
->whereIn($field, $values)
->get()
->makeHidden($this->hiddenAttributes)
->makeVisible($this->visibleAttributes)
->toArray();
->get();

$this->hideUnHideFields($result);

return $result->toArray();
}

public function countByList(array $values, $field = null)
public function countByList(array $values, $field = null): int
{
$field = (empty($field)) ? $this->primaryKey : $field;

return $this->getQuery()->whereIn($field, $values)->count();
}

public function updateByList(array $values, $data, $field = null)
public function updateByList(array $values, $data, $field = null): int
{
$field = (empty($field)) ? $this->primaryKey : $field;

Expand All @@ -462,7 +473,7 @@ protected function getEntityName()
return end($explodedModel);
}

protected function isSoftDelete()
protected function isSoftDelete(): bool
{
$traits = class_uses(get_class($this->model));

Expand Down
46 changes: 37 additions & 9 deletions src/Traits/SearchTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace RonasIT\Support\Traits;

use Illuminate\Foundation\Application;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\DB;
Expand All @@ -10,7 +11,7 @@
use Illuminate\Support\Str;

/**
* @property Query query
* @property Query query
*/
trait SearchTrait
{
Expand Down Expand Up @@ -104,7 +105,10 @@ public function getSearchResults()

public function wrapPaginatedData($data)
{
$paginator = new LengthAwarePaginator($data, count($data), count($data), 1, [
$total = count($data);
$perPage = $this->calculatePerPage($total);

$paginator = new LengthAwarePaginator($data, count($data), $perPage, 1, [
'path' => Paginator::resolveCurrentPath(),
'pageName' => 'page'
]);
Expand All @@ -114,11 +118,11 @@ public function wrapPaginatedData($data)

public function getModifiedPaginator($paginator)
{
return $paginator->setCollection($paginator
->getCollection()
->makeHidden($this->hiddenAttributes)
->makeVisible($this->visibleAttributes)
);
$collection = $paginator->getCollection();

$this->hideUnHideFields($collection);

return $paginator->setCollection($collection);
}

public function orderBy($default = null, $defaultDesc = false)
Expand Down Expand Up @@ -238,7 +242,7 @@ public function filterFrom($field, $strict = true, $filterName = null)
{
$filterName = empty($filterName) ? 'from' : $filterName;
$sign = $strict ? '>' : '>=';

if (!empty($this->filter[$filterName])) {
$this->addWhere($this->query, $field, $this->filter[$filterName], $sign);
}
Expand All @@ -250,7 +254,7 @@ public function filterTo($field, $strict = true, $filterName = null)
{
$filterName = empty($filterName) ? 'to' : $filterName;
$sign = $strict ? '<' : '<=';

if (!empty($this->filter[$filterName])) {
$this->addWhere($this->query, $field, $this->filter[$filterName], $sign);
}
Expand Down Expand Up @@ -323,4 +327,28 @@ protected function applyWhereCallback($query, $field, $callback) {
$callback($query, $field);
}
}

protected function calculatePerPage($total)
{
if ($total > 0) {
return $total;
}

if (!empty($this->filter['per_page'])) {
return $this->filter['per_page'];
}

return config('defaults.items_per_page', 1);
}

protected function hideUnHideFields(&$collection)
{
if (Application::VERSION >= '5.8') {
$collection->makeHidden($this->hiddenAttributes)->makeVisible($this->visibleAttributes);
} else {
$collection = $collection->each(function (&$item) {
$item->makeHidden($this->hiddenAttributes)->makeVisible($this->visibleAttributes);
});
}
}
}

0 comments on commit fce98a8

Please sign in to comment.