Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for query comparison operators #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 94 additions & 7 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cristal\ApiWrapper;

use Cristal\ApiWrapper\Exceptions\ApiEntityNotFoundException;
use http\Exception\InvalidArgumentException;

class Builder
{
Expand Down Expand Up @@ -52,6 +53,15 @@ public function getQuery()
*/
protected $removedScopes = [];

/**
* All of the available clause operators.
*
* @var string[]
*/
public $operators = [
'=', '<', '>', '<=', '>=', '<>', '!=', '<=>',
];

/**
* Set a model instance for the model being queried.
*
Expand Down Expand Up @@ -114,22 +124,99 @@ public function findOrFail($field, $value = null)
/**
* Add a basic where clause to the query.
*
* @param $field
* @param null $value
* @param string|array $column
* @param mixed $operator
* @param mixed $value
*
* @return $this
*/
public function where($column, $operator = null, $value = null)
{
if (is_array($column)) {
return $this->addArrayOfWheres($column);
}

[$value, $operator] = $this->prepareValueAndOperator(
$value, $operator, func_num_args() === 2
);

if ($this->invalidOperator($operator)) {
[$value, $operator] = [$operator, '='];
}

$this->query[] = [$column, $operator, $value];

return $this;
}

/**
* Add an array of where clauses to the query.
*
* @return self
* @param $column
* @return $this
*/
public function where($field, $value = null)
protected function addArrayOfWheres($column)
{
if (!is_array($field)) {
$field = [$field => $value];
$q = [];
foreach ($column as $key => $value) {
if (is_numeric($key) && is_array($value)) {
$q[] = $value;
} else {
$q[] = [$key, '=', $value];
}
}

$this->query = array_merge($this->query, $field);
$this->query = array_merge($this->query, $q);

return $this;
}

/**
* Prepare the value and operator for a where clause.
*
* @param $value
* @param $operator
* @param false $useDefault
* @return array
*
* @throws \InvalidArgumentException
*/
public function prepareValueAndOperator($value, $operator, $useDefault = false)
{
if ($useDefault) {
return [$operator, '='];
}

if ($this->invalidOperatorAndValue($operator, $value)) {
throw new InvalidArgumentException('Illegal operator and value combination');
}

return [$value, $operator];
}

/**
* Determine if the given operator and value combination is legal.
*
* @param $operator
* @param $value
* @return bool
*/
protected function invalidOperatorAndValue($operator, $value)
{
return is_null($value) && in_array($operator, $this->operators, true);
}

/**
* Determine if the given operator is supported.
*
* @param $operator
* @return bool
*/
protected function invalidOperator($operator)
{
return ! in_array(strtolower($operator), $this->operators, true);
}

/**
* @return self[]
*/
Expand Down