-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
1,086 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
composer.phar | ||
/vendor/ | ||
|
||
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control | ||
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file | ||
# composer.lock | ||
/vendor | ||
composer.lock | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "oilstone/php-rsql-parser", | ||
"description": "PHP library for parsing RSQL or FIQL queries. Designed for use in RESTful APIs.", | ||
"homepage": "https://github.com/oilstone/php-rsql-parser", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Online Solutions", | ||
"email": "support@onlinesolutionsltd.com" | ||
} | ||
], | ||
"require": { | ||
"php": ">=7.1.0", | ||
"illuminate/support": "5.8.*" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Oilstone\\RsqlParser\\": "src" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
|
||
namespace Oilstone\RsqlParser; | ||
|
||
use Oilstone\RsqlParser\Exceptions\ConditionFromStringException; | ||
use Oilstone\RsqlParser\Support\Str; | ||
use Oilstone\RsqlParser\Operators\Operator; | ||
|
||
/** | ||
* Class Condition | ||
* @package Oilstone\RsqlParser | ||
*/ | ||
class Condition | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $column; | ||
|
||
/** | ||
* @var Operator | ||
*/ | ||
protected $operator; | ||
|
||
/** | ||
* @var null | ||
*/ | ||
protected $value; | ||
|
||
/** | ||
* Condition constructor. | ||
* @param string $column | ||
* @param Operator $operator | ||
* @param null $value | ||
*/ | ||
public function __construct(string $column, Operator $operator, $value = null) | ||
{ | ||
$this->column = $column; | ||
$this->operator = $operator; | ||
$this->value = Str::unescape($value); | ||
} | ||
|
||
/** | ||
* @param string $condition | ||
* @return Condition | ||
* @throws ConditionFromStringException | ||
*/ | ||
public static function fromString(string $condition): Condition | ||
{ | ||
foreach (Operators::all() as $operator) { | ||
/** @var Operator $operator */ | ||
$operatorPos = stripos($condition, $operator->toUri()); | ||
$value = null; | ||
|
||
if ($operatorPos !== false) { | ||
if ($operator->valueRequired()) { | ||
$value = trim(trim(substr($condition, $operatorPos + strlen($operator->toUri()))), '\'"'); | ||
|
||
if ($operator->expectsArray()) { | ||
$value = array_map('trim', explode(",", substr($value, 1, strlen($value) - 2))); | ||
} | ||
} | ||
|
||
return new Condition( | ||
substr($condition, 0, $operatorPos), | ||
$operator, | ||
$value | ||
); | ||
} | ||
} | ||
|
||
throw new ConditionFromStringException('Failed to identify a valid operation when parsing a condition from a string'); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getColumn(): string | ||
{ | ||
return $this->column; | ||
} | ||
|
||
/** | ||
* @param string $column | ||
* @return Condition | ||
*/ | ||
public function setColumn(string $column): Condition | ||
{ | ||
$this->column = $column; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return Operator | ||
*/ | ||
public function getOperator(): Operator | ||
{ | ||
return $this->operator; | ||
} | ||
|
||
/** | ||
* @param Operator $operator | ||
* @return Condition | ||
*/ | ||
public function setOperator(Operator $operator): Condition | ||
{ | ||
$this->operator = $operator; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return null | ||
*/ | ||
public function getValue() | ||
{ | ||
return $this->value; | ||
} | ||
|
||
/** | ||
* @param null $value | ||
* @return Condition | ||
*/ | ||
public function setValue($value) | ||
{ | ||
$this->value = Str::unescape($value); | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Oilstone\RsqlParser\Exceptions; | ||
|
||
/** | ||
* Class ConditionFromStringException | ||
* @package Oilstone\RsqlParser\Exceptions | ||
*/ | ||
class ConditionFromStringException extends Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Oilstone\RsqlParser\Exceptions; | ||
|
||
use Exception as BaseException; | ||
use Throwable; | ||
|
||
/** | ||
* Class Exception | ||
* @package Oilstone\RsqlParser\Exceptions | ||
*/ | ||
class Exception extends BaseException | ||
{ | ||
/** | ||
* InvalidQueryStringException constructor. | ||
* @param string $message | ||
* @param Throwable|null $previous | ||
*/ | ||
public function __construct(string $message, ?Throwable $previous = null) | ||
{ | ||
parent::__construct($message, 0, $previous); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Oilstone\RsqlParser\Exceptions; | ||
|
||
/** | ||
* Class InvalidQueryStringException | ||
* @package Oilstone\RsqlParser\Exceptions | ||
*/ | ||
class InvalidQueryStringException extends Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Oilstone\RsqlParser\Exceptions; | ||
|
||
/** | ||
* Class UnidentifiedOperatorException | ||
* @package Oilstone\RsqlParser\Exceptions | ||
*/ | ||
class UnidentifiedOperatorException extends Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
namespace Oilstone\RsqlParser; | ||
|
||
use ArrayAccess; | ||
use ArrayIterator; | ||
use Countable; | ||
use IteratorAggregate; | ||
|
||
/** | ||
* Class Expression | ||
* @package Oilstone\RsqlParser | ||
*/ | ||
class Expression implements ArrayAccess, Countable, IteratorAggregate | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $items = []; | ||
|
||
/** | ||
* Expression constructor. | ||
* @param array $items | ||
*/ | ||
public function __construct(array $items = []) | ||
{ | ||
$this->items = $items; | ||
} | ||
|
||
/** | ||
* @param $constraint | ||
* @return Expression | ||
*/ | ||
public function and($constraint): Expression | ||
{ | ||
return $this->add('AND', $constraint); | ||
} | ||
|
||
/** | ||
* @param string $operator | ||
* @param $constraint | ||
* @return Expression | ||
*/ | ||
public function add(string $operator, $constraint): Expression | ||
{ | ||
$this->items[] = [ | ||
'operator' => $operator, | ||
'constraint' => $constraint | ||
]; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param $constraint | ||
* @return Expression | ||
*/ | ||
public function or($constraint): Expression | ||
{ | ||
return $this->add('OR', $constraint); | ||
} | ||
|
||
/** | ||
* @param mixed $offset | ||
* @return mixed|null | ||
*/ | ||
public function offsetGet($offset) | ||
{ | ||
return $this->items[$offset] ?? null; | ||
} | ||
|
||
/** | ||
* @param mixed $offset | ||
* @param mixed $value | ||
* @return void | ||
*/ | ||
public function offsetSet($offset, $value) | ||
{ | ||
$this->items[$offset] = $value; | ||
} | ||
|
||
/** | ||
* @param mixed $offset | ||
* @return void | ||
*/ | ||
public function offsetUnset($offset) | ||
{ | ||
if ($this->offsetExists($offset)) { | ||
unset($this->items[$offset]); | ||
} | ||
} | ||
|
||
/** | ||
* @param mixed $offset | ||
* @return bool | ||
*/ | ||
public function offsetExists($offset): bool | ||
{ | ||
return isset($this->items[$offset]); | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function count(): int | ||
{ | ||
return count($this->items); | ||
} | ||
|
||
/** | ||
* @return ArrayIterator | ||
*/ | ||
public function getIterator(): ArrayIterator | ||
{ | ||
return new ArrayIterator($this->items); | ||
} | ||
} |
Oops, something went wrong.