Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .github/workflows/pr-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
operating-system: [ubuntu-latest]
php-versions: ['8.0','8.2','8.3']
php-versions: ['8.0','8.2','8.3', '8.4']
steps:
- uses: actions/checkout@v3

Expand Down
4 changes: 3 additions & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
SimplifyEmptyCheckOnEmptyArrayRector::class,
])
->withSets([
SetList::PHP_80,
SetList::PHP_82,
SetList::PHP_83,
SetList::PHP_84,
SetList::DEAD_CODE,
SetList::CODE_QUALITY,
SetList::CODING_STYLE,
Expand Down
27 changes: 27 additions & 0 deletions src/Endpoint/Data/Filters/AbstractPredefinedFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Sugarcrm\REST\Endpoint\Data\Filters;

use Sugarcrm\REST\Endpoint\Data\Filters\Operator\AbstractOperator;

abstract class AbstractPredefinedFilter extends AbstractOperator
{
public function __construct(array $arguments = [])
{
if (!empty($arguments) && count($arguments) < 2) {
$arguments = [
$this->getOperator(),
$arguments[0] ?? '',
];
}

parent::__construct($arguments);
}

public function compile(): array
{
return [
$this->getOperator() => $this->getValue(),
];
}
}
8 changes: 8 additions & 0 deletions src/Endpoint/Data/Filters/Creator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Sugarcrm\REST\Endpoint\Data\Filters;

class Creator extends AbstractPredefinedFilter
{
public const OPERATOR = '$creator';
}
86 changes: 50 additions & 36 deletions src/Endpoint/Data/Filters/Expression/AbstractExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

namespace Sugarcrm\REST\Endpoint\Data\Filters\Expression;

use Sugarcrm\REST\Endpoint\Data\Filters\Creator;
use Sugarcrm\REST\Endpoint\Data\Filters\Favorite;
use Sugarcrm\REST\Endpoint\Data\Filters\Following;
use Sugarcrm\REST\Endpoint\Data\Filters\Operator\Equals;
use Sugarcrm\REST\Endpoint\Data\Filters\Operator\InRadiusFromCoords;
use Sugarcrm\REST\Endpoint\Data\Filters\Operator\InRadiusFromZip;
use Sugarcrm\REST\Endpoint\Data\Filters\Operator\NotEquals;
use Sugarcrm\REST\Endpoint\Data\Filters\Operator\Starts;
use Sugarcrm\REST\Endpoint\Data\Filters\Operator\Ends;
Expand All @@ -22,48 +27,52 @@
use Sugarcrm\REST\Endpoint\Data\Filters\Operator\Between;
use Sugarcrm\REST\Endpoint\Data\Filters\Operator\DateBetween;
use Sugarcrm\REST\Endpoint\Data\Filters\FilterInterface;
use Sugarcrm\REST\Endpoint\Data\Filters\Owner;
use Sugarcrm\REST\Endpoint\Data\Filters\Tracker;
use Sugarcrm\REST\Exception\Filter\UnknownFilterOperator;

/**
* The default expression implementation provides an API for acess to all Sugar filter operators
* @package Sugarcrm\REST\Endpoint\Data\Filters\Expression
* @method AndExpression and()
* @method OrExpression or()
* @method DateExpression date($field)
* @method $this equals($field,$value)
* @method $this notEquals($field,$value)
* @method $this starts($field,$value)
* @method $this ends($field,$value)
* @method $this contains($field,$value)
* @method $this in($field,array $value)
* @method $this notIn($field,array $value)
* @method $this isNull($field)
* @method $this notNull($field)
* @method $this lt($field,$value)
* @method $this lessThan($field,$value)
* @method $this lte($field,$value)
* @method $this lessThanOrEqualTo($field,$value)
* @method $this lessThanOrEquals($field,$value)
* @method $this greaterThan($field,$value)
* @method $this gte($field,$value)
* @method $this greaterThanOrEqualTo($field,$value)
* @method $this greaterThanOrEquals($field,$value)
* @method $this between($field,$value)
* @method $this dateBetween($field,$value)
* @method DateExpression date(string $field)
* @method DistanceExpression distance(string $field)
* @method static equals(string $field, mixed $value)
* @method static notEquals(string $field, mixed $value)
* @method static starts(string $field,mixed $value)
* @method static ends(string $field, mixed $value)
* @method static contains(string $field, mixed $value)
* @method static in(string $field, array $value)
* @method static notIn(string $field, array $value)
* @method static isNull(string $field)
* @method static notNull(string $field)
* @method static lt(string $field,int|float $value)
* @method static lessThan(string $field, int|float $value)
* @method static lte(string $field, int|float $value)
* @method static lessThanOrEqualTo(string $field, int|float $value)
* @method static lessThanOrEquals(string $field, int|float $value)
* @method static greaterThan(string $field, int|float $value)
* @method static gte(string $field, int|float $value)
* @method static greaterThanOrEqualTo(string $field, int|float $value)
* @method static greaterThanOrEquals(string $field, int|float$value)
* @method static between(string $field, array $value)
* @method static dateBetween(string $field,array $value)
* @method static inRadiusFromCoords(string $field, array $coords, int|float $radius, string $unitType = 'km')
* @method static inRadiusFromZip(string $field, string $zip, int|float $radius, string $country, string $unitType = 'km')
* @method static favorite()
* @method static following()
* @method static owner()
* @method static creator()
* @method static tracker(string $interval)
*/
abstract class AbstractExpression implements FilterInterface, ExpressionInterface
{
/**
* @var array
*/
protected $filters = [];
protected array $filters = [];

private ?\Sugarcrm\REST\Endpoint\Data\Filters\Expression\AbstractExpression $parentExpression = null;
private AbstractExpression $parentExpression;

/**
* @var array
*/
protected $operators = [
protected array $operators = [
'equals' => Equals::class,
'notEquals' => NotEquals::class,
'starts' => Starts::class,
Expand All @@ -85,15 +94,20 @@ abstract class AbstractExpression implements FilterInterface, ExpressionInterfac
'greaterThanOrEquals' => GreaterThanOrEqual::class,
'between' => Between::class,
'dateBetween' => DateBetween::class,
'inRadiusFromCoords' => InRadiusFromCoords::class,
'inRadiusFromZip' => InRadiusFromZip::class,
'favorite' => Favorite::class,
'following' => Following::class,
'owner' => Owner::class,
'tracker' => Tracker::class,
'creator' => Creator::class,
];

/**
* @var array
*/
protected $expressions = [
protected array $expressions = [
'and' => AndExpression::class,
'or' => OrExpression::class,
'date' => DateExpression::class,
'distance' => DistanceExpression::class,
];

/**
Expand Down Expand Up @@ -126,7 +140,7 @@ public function __call($name, $arguments)
* Sets Parent Expression to allow for nested tree structure
* @return $this
*/
public function setParentExpression(AbstractExpression $Expression)
public function setParentExpression(AbstractExpression $Expression): static
{
$this->parentExpression = $Expression;
return $this;
Expand Down Expand Up @@ -156,7 +170,7 @@ public function compile(): array
/**
* @inheritDoc
*/
public function clear()
public function clear(): static
{
$this->filters = [];
return $this;
Expand Down
35 changes: 20 additions & 15 deletions src/Endpoint/Data/Filters/Expression/DateExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Sugarcrm\REST\Endpoint\Data\Filters\Operator\GreaterThanOrEqual;
use Sugarcrm\REST\Endpoint\Data\Filters\Operator\DateBetween;
use Sugarcrm\REST\Endpoint\Data\Filters\Operator\DateRange;
use Sugarcrm\REST\Exception\Filter\MissingFieldForDateExpression;
use Sugarcrm\REST\Exception\Filter\MissingFieldForFilterExpression;
use Sugarcrm\REST\Exception\Filter\UnknownFilterOperator;

/**
Expand Down Expand Up @@ -54,9 +54,9 @@ class DateExpression extends AbstractExpression
{
public const OPERATOR = '';

protected $dateField;
protected string $dateField;

protected $ranges = [
protected array $ranges = [
'yesterday' => 'yesterday',
'today' => 'today',
'tomorrow' => 'tomorrow',
Expand All @@ -72,10 +72,7 @@ class DateExpression extends AbstractExpression
'nextYear' => 'next_year',
];

/**
* @var array
*/
protected $operators = [
protected array $operators = [
'equals' => Equals::class,
'notEquals' => NotEquals::class,
'isNull' => IsNull::class,
Expand All @@ -94,10 +91,7 @@ class DateExpression extends AbstractExpression
'between' => DateBetween::class,
];

/**
* @var array
*/
protected $expressions = [];
protected array $expressions = [];

/**
* DateExpression constructor.
Expand All @@ -114,21 +108,32 @@ public function __construct(array $arguments = [])
* @param $field
* @return $this
*/
public function field($field): self
public function field(string $field): self
{
$this->dateField = $field;
return $this;
}

protected function isDateRange(string $name): string|false
{
$range = false;
if (array_key_exists($name, $this->ranges)) {
$range = $this->ranges[$name];
} elseif (in_array($name, $this->ranges)) {
$range = $name;
}

return $range;
}

public function __call($name, $arguments)
{
if (empty($this->dateField)) {
throw new MissingFieldForDateExpression();
throw new MissingFieldForFilterExpression();
}

$args = [$this->dateField];
if (array_key_exists($name, $this->ranges)) {
$range = $this->ranges[$name];
if ($range = $this->isDateRange($name)) {
$args[] = $range;
$Op = new DateRange($args);
$this->filters[0] = $Op;
Expand Down
75 changes: 75 additions & 0 deletions src/Endpoint/Data/Filters/Expression/DistanceExpression.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Sugarcrm\REST\Endpoint\Data\Filters\Expression;

use Sugarcrm\REST\Endpoint\Data\Filters\Operator\InRadiusFromCoords;
use Sugarcrm\REST\Endpoint\Data\Filters\Operator\InRadiusFromZip;
use Sugarcrm\REST\Exception\Filter\MissingFieldForFilterExpression;
use Sugarcrm\REST\Exception\Filter\UnknownFilterOperator;

/**
* Class DistanceExpression
* @package Sugarcrm\REST\Endpoint\Data\Filters\Expression
*
* @method DistanceExpression radiusFromZip(string $zipCode, string $country = 'US', float $radius = 0, string $unitType = 'km')
* @method DistanceExpression radiusFromCoords(float|array $latitude, float|null $longitude = null, float $radius = 0, string $unitType = 'km')
*/
class DistanceExpression extends AbstractExpression
{
protected array $operators = [
'radiusFromZip' => InRadiusFromZip::class,
'radiusFromCoords' => InRadiusFromCoords::class,
];

protected array $expressions = [];

protected string $distanceField;

/**
* DateExpression constructor.
*/
public function __construct(array $arguments = [])
{
if (isset($arguments[0])) {
$this->field($arguments[0]);
}
}

/**
* Set the field that date expression is against
* @param $field
* @return $this
*/
public function field(string $field): self
{
$this->distanceField = $field;
return $this;
}

public function __call($name, $arguments)
{
if (empty($this->distanceField)) {
throw new MissingFieldForFilterExpression();
}

$args = [$this->distanceField];
if (array_key_exists($name, $this->operators)) {
$args = array_merge($args, $arguments);
$Operator = $this->operators[$name];
$O = new $Operator($args);
$this->filters[0] = $O;
return $this;
}

throw new UnknownFilterOperator([$name]);
}

/**
* Human Friendly Expression End, allow you to traverse back up the Filter expression
* @codeCoverageIgnore
*/
public function endDistance(): AbstractExpression
{
return $this->getParentExpression();
}
}
11 changes: 4 additions & 7 deletions src/Endpoint/Data/Filters/Expression/ExpressionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,17 @@

namespace Sugarcrm\REST\Endpoint\Data\Filters\Expression;

use Sugarcrm\REST\Endpoint\Data\Filters\FilterInterface;

/**
* The Expression Interface defines the basic API needed for an Expression object used in the Filter API Data Layer
* @package Sugarcrm\REST\Endpoint\Data\Filters\Expression
**/
interface ExpressionInterface
interface ExpressionInterface extends FilterInterface
{
/**
* Compiles the Filter Expression into an array to be passed to Sugar Filter API
*/
public function compile(): array;

/**
* Clear out Filters included in Expression
* @return $this
*/
public function clear();
public function clear(): static;
}
8 changes: 8 additions & 0 deletions src/Endpoint/Data/Filters/Favorite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Sugarcrm\REST\Endpoint\Data\Filters;

class Favorite extends AbstractPredefinedFilter
{
public const OPERATOR = '$favorite';
}
8 changes: 8 additions & 0 deletions src/Endpoint/Data/Filters/Following.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Sugarcrm\REST\Endpoint\Data\Filters;

class Following extends AbstractPredefinedFilter
{
public const OPERATOR = '$following';
}
Loading