Skip to content

Commit

Permalink
cakephp 5 compatible version
Browse files Browse the repository at this point in the history
  • Loading branch information
skie committed Sep 11, 2024
1 parent 282d16a commit 3b5cbd9
Show file tree
Hide file tree
Showing 47 changed files with 323 additions and 345 deletions.
11 changes: 5 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
"license": "MIT",
"require": {
"php": ">=8.1",
"cakephp/cakephp": "^4.4",
"skie/cakephp-search": "^4.0"
"cakephp/cakephp": "^5.0",
"skie/cakephp-search": "^5.0"
},
"require-dev": {
"cakephp/cakephp-codesniffer": "^4.0",
"slevomat/coding-standard": "^8.0",
"phpunit/phpunit": "^9.5"
"cakephp/cakephp-codesniffer": "^5.0",
"phpunit/phpunit": "^10.0"
},
"scripts": {
"fixcode": [
Expand All @@ -29,7 +28,7 @@
"test": "phpunit --stderr",
"coverage-test": "phpunit --stderr --coverage-clover=clover.xml",
"stan": "phpstan.phar analyse --memory-limit=-1 src/",
"stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:0.12.94 psalm/phar:~4.9.2 && mv composer.backup composer.json",
"stan-setup": "cp composer.json composer.backup && composer81 require --dev phpstan/phpstan:^1.9.0 psalm/phar:^5.1.0 && mv composer.backup composer.json",
"psalm": "psalm.phar --show-info=false",
"stan-rebuild-baseline": "phpstan.phar analyse ./src/ --generate-baseline",
"cs-check": "phpcs -n -p ./src ./tests",
Expand Down
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
<!-- Add any additional test suites you want to run here -->
<testsuites>
<testsuite name="SearchFilter">
<directory>tests/TestCase/</directory>
<directory>./tests/</directory>
</testsuite>
</testsuites>

<!-- Setup fixture extension -->
<extensions>
<extension class="\Cake\TestSuite\Fixture\PHPUnitExtension"/>
<bootstrap class="Cake\TestSuite\Fixture\Extension\PHPUnitExtension"/>
</extensions>

<coverage>
Expand Down
26 changes: 26 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0"?>
<psalm
allowStringToStandInForClass="true"
errorLevel="4"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>

<issueHandlers>
<LessSpecificReturnType errorLevel="info" />
<RedundantConditionGivenDocblockType errorLevel="info" />
<DocblockTypeContradiction errorLevel="info" />
<NoInterfaceProperties errorLevel="info" />
<ImplementedReturnTypeMismatch errorLevel="info" />
<MoreSpecificImplementedParamType errorLevel="info" />
</issueHandlers>

</psalm>
4 changes: 2 additions & 2 deletions src/Filter/AbstractFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function setProperties(array $properties): self
* @param mixed $value The value of the property.
* @return self
*/
public function setProperty(string $name, $value): self
public function setProperty(string $name, mixed $value): self
{
$this->properties[$name] = $value;

Expand All @@ -114,7 +114,7 @@ public function setProperty(string $name, $value): self
*
* @return mixed
*/
public function getCriterion()
public function getCriterion(): mixed
{
return $this->criterion;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Filter/Exception/MissingFilterException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ class MissingFilterException extends CakeException
/**
* @inheritDoc
*/
protected $_messageTemplate = 'Filter class %s could not be found.';
protected string $_messageTemplate = 'Filter class %s could not be found.';
}
13 changes: 7 additions & 6 deletions src/Filter/FilterCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@

use ArrayIterator;
use Countable;
use Exception;
use IteratorAggregate;
use Traversable;
use function Cake\I18n\__;

/**
* FilterCollection class
*
* This class represents a collection of filters and provides methods to manage and interact with them.
*
* @implements \IteratorAggregate<string, \CakeDC\SearchFilter\Filter\FilterInterface>
*/
class FilterCollection implements IteratorAggregate, Countable
{
Expand Down Expand Up @@ -49,10 +50,10 @@ public function __construct(array $filters = [])
* @return $this
* @throws \InvalidArgumentException
*/
public function add($alias, FilterInterface $filter)
public function add(string $alias, FilterInterface $filter)
{
if ($this->has($alias)) {
throw new \Exception(__('Filter %s already registered', $alias));
throw new Exception(__('Filter %s already registered', $alias));
}
$this->filters[$alias] = $filter;
$filter->setAlias($alias);
Expand Down Expand Up @@ -92,7 +93,6 @@ public function remove(string $name)
* Implementation of IteratorAggregate.
*
* @return \Traversable
* @psalm-return \Traversable<string, \CakeDC\SearchFilter\Filter\FilterInterface>
*/
public function getIterator(): Traversable
{
Expand All @@ -115,6 +115,7 @@ public function count(): int
* Get the list of available filter names.
*
* @return array<string> Filter names
* @psalm-return list<int|string>
*/
public function keys(): array
{
Expand All @@ -137,7 +138,7 @@ public function has(string $alias): bool
*
* @return array<string,mixed> The view configuration.
*/
public function getViewConfig()
public function getViewConfig(): array
{
$result = [];
$filters = $this->filters;
Expand All @@ -157,7 +158,7 @@ public function getViewConfig()
*
* @return array<string,mixed> The criteria.
*/
public function getCriteria()
public function getCriteria(): array
{
$result = [];
foreach ($this->filters as $alias => $filter) {
Expand Down
4 changes: 2 additions & 2 deletions src/Filter/FilterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ public function setProperties(array $properties): self;
* @param mixed $value The value of the property.
* @return self
*/
public function setProperty(string $name, $value): self;
public function setProperty(string $name, mixed $value): self;

/**
* Get the criterion of the filter.
*
* @return mixed
*/
public function getCriterion();
public function getCriterion(): mixed;

/**
* Set the criterion of the filter.
Expand Down
10 changes: 2 additions & 8 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Manager
*
* @var \CakeDC\SearchFilter\Model\Filter\Criterion\CriteriaBuilder
*/
protected $_criteriaBuilder;
protected CriteriaBuilder $_criteriaBuilder;

/**
* A configuration array for filters to be loaded.
Expand Down Expand Up @@ -101,6 +101,7 @@ public function __construct(ServerRequest $request, ?array $filters = null)
}

$this->loadFilters();
$this->_criteriaBuilder = new CriteriaBuilder();
}

/**
Expand Down Expand Up @@ -128,10 +129,6 @@ public function filters(): FilterRegistry
*/
public function criterion(): CriteriaBuilder
{
if ($this->_criteriaBuilder === null) {
$this->_criteriaBuilder = new CriteriaBuilder();
}

return $this->_criteriaBuilder;
}

Expand Down Expand Up @@ -269,15 +266,13 @@ public function appendFromSchema(FilterCollection $collection, Table $table, arr
public function buildCriterion(string $column, string $type, Table $table, array $options = []): ?BaseCriterion
{
$alias = $table->getAlias();
$criterion = null;
if (in_array($type, ['string', 'text'])) {
return $this->criterion()->string($alias . '.' . $column);
} elseif (in_array($type, ['date'])) {
return $this->criterion()->date($alias . '.' . $column);
} elseif (in_array($type, ['time', 'timestamp', 'datetime'])) {
return $this->criterion()->datetime($alias . '.' . $column);
} elseif (($type === 'integer' || $type == 'uuid') && (substr($column, -3) === '_id')) {
$autocompleteField = null;
$assocName = Inflector::pluralize(Inflector::camelize(substr($column, 0, -3)));
if ($table->associations()->has($assocName)) {
$assoc = $table->associations()->get($assocName);
Expand Down Expand Up @@ -320,7 +315,6 @@ protected function buildFilter(string $column, string $type, Table $table, array
} elseif (in_array($type, ['time', 'timestamp', 'datetime'])) {
$filter = $this->filters()->new('datetime');
} elseif (($type === 'integer' || $type == 'uuid') && (substr($column, -3) === '_id')) {
$autocompleteField = null;
$assocName = Inflector::pluralize(Inflector::camelize(substr($column, 0, -3)));

/** @var \CakeDC\SearchFilter\Filter\LookupFilter $lookupFilter */
Expand Down
11 changes: 6 additions & 5 deletions src/Model/Filter/CriteriaFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

namespace CakeDC\SearchFilter\Model\Filter;

use Cake\ORM\Query;
use Cake\ORM\Query\SelectQuery;
use PlumSearch\Model\Filter\AbstractFilter;
use PlumSearch\Model\Filter\Exception\MissingFilterException;
use PlumSearch\Model\FilterRegistry;
use function Cake\I18n\__;

/**
* Class CriteriaFilter
Expand Down Expand Up @@ -44,13 +45,13 @@ public function __construct(FilterRegistry $registry, array $config = [])
/**
* Returns query with applied filter
*
* @param \Cake\ORM\Query<\Cake\Datasource\EntityInterface> $query Query.
* @param \Cake\ORM\Query\SelectQuery<\Cake\Datasource\EntityInterface> $query Query.
* @param string $field Field name.
* @param array<string, mixed> $value Field value.
* @param mixed $value Field value.
* @param array<string, mixed> $data Filters values.
* @return \Cake\ORM\Query<\Cake\Datasource\EntityInterface>
* @return \Cake\ORM\Query\SelectQuery<\Cake\Datasource\EntityInterface>
*/
protected function _buildQuery(Query $query, string $field, $value, array $data = []): Query
protected function _buildQuery(SelectQuery $query, string $field, $value, array $data = []): SelectQuery
{
$criteria = $this->getConfig('criteria');
foreach ($value as $name => $values) {
Expand Down
12 changes: 6 additions & 6 deletions src/Model/Filter/Criterion/AndCriterion.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace CakeDC\SearchFilter\Model\Filter\Criterion;

use Cake\Database\Expression\QueryExpression;
use Cake\ORM\Query;
use Cake\ORM\Query\SelectQuery;

class AndCriterion extends BaseCriterion
{
Expand All @@ -31,14 +31,14 @@ public function __construct(array $criteria)
/**
* Finder method
*
* @param \Cake\ORM\Query<\Cake\Datasource\EntityInterface> $query
* @param \Cake\ORM\Query\SelectQuery<\Cake\Datasource\EntityInterface> $query
* @param string $condition
* @param array<string, mixed> $values
* @param array<string, mixed> $criteria
* @param array<string, mixed> $options
* @return \Cake\ORM\Query<\Cake\Datasource\EntityInterface>
* @return \Cake\ORM\Query\SelectQuery<\Cake\Datasource\EntityInterface>
*/
public function __invoke(Query $query, string $condition, array $values, array $criteria, array $options): Query
public function __invoke(SelectQuery $query, string $condition, array $values, array $criteria, array $options): SelectQuery
{
$filters = $this->buildFilter($condition, $values, $criteria, $options);
if (!empty($filters)) {
Expand All @@ -53,7 +53,7 @@ public function __invoke(Query $query, string $condition, array $values, array $
/**
* @inheritDoc
*/
public function buildFilter(string $condition, array $values, array $criteria, array $options = [])
public function buildFilter(string $condition, array $values, array $criteria, array $options = []): array|callable|null
{
$filters = [];
foreach ($this->criteria as $criterion) {
Expand All @@ -73,7 +73,7 @@ public function buildFilter(string $condition, array $values, array $criteria, a
* @param string $condition
* @return bool
*/
public function isApplicable($value, string $condition): bool
public function isApplicable(mixed $value, string $condition): bool
{
$result = false;
foreach ($this->criteria as $c) {
Expand Down
27 changes: 14 additions & 13 deletions src/Model/Filter/Criterion/BaseCriterion.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

namespace CakeDC\SearchFilter\Model\Filter\Criterion;

use Cake\Database\ExpressionInterface;
use Cake\Database\Expression\ComparisonExpression;
use Cake\Database\Expression\QueryExpression;
use Cake\I18n\FrozenDate;
use Cake\I18n\FrozenTime;
use Cake\I18n\Date;
use Cake\I18n\DateTime;
use Cake\Utility\Hash;
use CakeDC\SearchFilter\Filter\AbstractFilter;

Expand All @@ -20,16 +21,16 @@ abstract class BaseCriterion implements CriterionInterface
/**
* Field name used for searching
*
* @var string
* @var string|ExpressionInterface
*/
protected $field;
protected string|ExpressionInterface $field;

/**
* Table name used for field condition
*
* @var string
*/
protected $alias;
protected string $alias;

/**
* Extract single value or array by value name
Expand All @@ -39,7 +40,7 @@ abstract class BaseCriterion implements CriterionInterface
* @param array<mixed> $values
* @return mixed
*/
public function getValues(string $fieldName, string $condition, array $values)
public function getValues(string $fieldName, string $condition, array $values): mixed
{
if (in_array($condition, [AbstractFilter::COND_IN, AbstractFilter::COND_NOT_IN]) && Hash::numeric(array_keys($values))) {
return Hash::extract($values, '{n}.' . $fieldName);
Expand All @@ -60,7 +61,7 @@ public function getValues(string $fieldName, string $condition, array $values)
* @param array<string, mixed> $options
* @return array<int, \Cake\Database\Expression\QueryExpression>|array<string, mixed>|callable|null A callable that can be used to modify a QueryExpression, or null if not applicable.
*/
abstract public function buildFilter(string $condition, array $values, array $criteria, array $options = []);
abstract public function buildFilter(string $condition, array $values, array $criteria, array $options = []): array|callable|null;

/**
* Checks if value applicable for criterion filtering.
Expand All @@ -69,7 +70,7 @@ abstract public function buildFilter(string $condition, array $values, array $cr
* @param string $condition
* @return bool
*/
abstract public function isApplicable($value, string $condition): bool;
abstract public function isApplicable(mixed $value, string $condition): bool;

/**
* Build like style filter.
Expand All @@ -80,7 +81,7 @@ abstract public function isApplicable($value, string $condition): bool;
* @param array<string, mixed> $options
* @return callable
*/
public function buildQueryByCondition($field, string $condition, $value, array $options = [])
public function buildQueryByCondition(string|QueryExpression $field, string $condition, mixed $value, array $options = []): callable
{
$type = null;
if (isset($options['type'])) {
Expand All @@ -98,9 +99,9 @@ public function buildQueryByCondition($field, string $condition, $value, array $
}
if ($condition == AbstractFilter::COND_EQ) {
return function (QueryExpression $expr) use ($field, $value, $type): QueryExpression {
if ($value instanceof FrozenDate) {
if ($value instanceof Date) {
$value = $value->format('Y-m-d');
} elseif ($value instanceof FrozenTime) {
} elseif ($value instanceof DateTime) {
$value = $value->format('Y-m-d H:i');
}

Expand All @@ -117,9 +118,9 @@ public function buildQueryByCondition($field, string $condition, $value, array $
])
) {
return function (QueryExpression $expr) use ($field, $value, $type, $condition): QueryExpression {
if ($value instanceof FrozenDate) {
if ($value instanceof Date) {
$value = $value->format('Y-m-d');
} elseif ($value instanceof FrozenTime) {
} elseif ($value instanceof DateTime) {
$value = $value->format('Y-m-d H:i');
}

Expand Down
Loading

0 comments on commit 3b5cbd9

Please sign in to comment.