Skip to content

Commit

Permalink
Merge branch 'release/v1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
bullenb committed Jun 24, 2019
2 parents 2ca1479 + 559c3df commit e63fee9
Show file tree
Hide file tree
Showing 24 changed files with 1,086 additions and 6 deletions.
9 changes: 3 additions & 6 deletions .gitignore
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
21 changes: 21 additions & 0 deletions composer.json
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"
}
}
}
132 changes: 132 additions & 0 deletions src/Condition.php
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;
}
}
12 changes: 12 additions & 0 deletions src/Exceptions/ConditionFromStringException.php
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
{

}
23 changes: 23 additions & 0 deletions src/Exceptions/Exception.php
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);
}
}
12 changes: 12 additions & 0 deletions src/Exceptions/InvalidQueryStringException.php
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
{

}
12 changes: 12 additions & 0 deletions src/Exceptions/UnidentifiedOperatorException.php
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
{

}
117 changes: 117 additions & 0 deletions src/Expression.php
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);
}
}
Loading

0 comments on commit e63fee9

Please sign in to comment.