Skip to content

Commit

Permalink
Remove useless code.
Browse files Browse the repository at this point in the history
  • Loading branch information
DevDynamo2024 committed Sep 2, 2019
1 parent 3fb97f4 commit cd43bed
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 41 deletions.
6 changes: 2 additions & 4 deletions src/Uniqueway/Repositories/Contracts/RepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@ public function find($id, $columns = ['*']);

/**
* @param $attribute
* @param string $value
* @param $value
* @param array $columns
* @param bool $or
* @return mixed
* @author Taylor <[email protected]>
*/
public function findBy($attribute, $value = '', $columns = ['*'], $or = false);
public function findBy($attribute, $value = '', $columns = ['*']);

/**
* @param $field
Expand Down
58 changes: 58 additions & 0 deletions src/Uniqueway/Repositories/Criteria/FuzzyGroupSearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Uniqueway\Repositories\Criteria;

use Uniqueway\Repositories\Contracts\RepositoryInterface as Repository;

class FuzzyGroupSearch extends Criteria
{
/**
* Search query
*
* @var string
*/
protected $query;

/**
* Search attributes
*
* @var array
*/
protected $attributes;

/**
*
* @param string $query
* @param array $attributes
*/
public function __construct($query, array $attributes)
{
$query = trim($query);
$query = str_replace(['\\', '%', '_'], ['\\\\', '\%', '\_'], $query);
$this->query = $query;
$this->attributes = $attributes;
}

/**
* @param $model
* @param Repository $repository
* @return mixed
*/
public function apply($model, Repository $repository)
{
if (empty($this->query)) {
return $model;
}

return $model->where(function ($query) {
foreach ($this->attributes as $attribute) {
if ($attribute == 'id' && preg_match('/^\d+$/', $this->query)) {
$query->orWhere($attribute, '=', $this->query);
} else {
$pattern = '%' . $this->query . '%';
$query->orWhere($attribute, 'LIKE', $pattern);
}
}
});
}
}
40 changes: 3 additions & 37 deletions src/Uniqueway/Repositories/Eloquent/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,49 +152,15 @@ public function find($id, $columns = ['*'])
* @param array $columns
* @return mixed
*/
/**
* @param $attribute
* @param string $value
* @param array $columns
* @param bool $or
* @return mixed
* @author Taylor <[email protected]>
*/
public function findBy($attribute, $value = '', $columns = ['*'], $or = false)
public function findBy($attribute, $value = '', $columns = ['*'])
{
$this->applyCriteria();

$model = $this->model;

if (is_array($attribute)) {
foreach ($attribute as $field => $value) {
if ($value instanceof \Closure) {
$model = (!$or)
? $model->where($value)
: $model->orWhere($value);
} elseif (is_array($value)) {
if (count($value) === 3) {
list($field, $operator, $search) = $value;
$model = (!$or)
? $model->where($field, $operator, $search)
: $model->orWhere($field, $operator, $search);
} elseif (count($value) === 2) {
list($field, $search) = $value;
$model = (!$or)
? $model->where($field, '=', $search)
: $model->orWhere($field, '=', $search);
}
} else {
$model = (!$or)
? $model->where($field, '=', $value)
: $model->orWhere($field, '=', $value);
}
}

return $model->first($columns);
return $this->model->where($attribute)->first($columns);
}

return $model->where($attribute, '=', $value)->first($columns);
return $this->model->where($attribute, '=', $value)->first($columns);
}

/**
Expand Down

0 comments on commit cd43bed

Please sign in to comment.