Skip to content

Commit cca5ad0

Browse files
authored
Merge pull request #66 from lmc-eu/feature/update-dev-dependencies
Update readme and fix codestyle
2 parents 1fafc8a + 199a0e7 commit cca5ad0

27 files changed

+89
-93
lines changed

README.md

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -400,19 +400,18 @@ Result:
400400
```
401401
402402
## Functions in filters
403-
With function you can handle all kinds of situations, which might be problematic with just a simple filters like `eq`, etc.
404-
405-
Let's see how to work with functions and what is required to do. We will show it right on the example.
403+
With function you can handle all kinds of problems, which might be problematic with just a simple filters like `eq`, etc.
406404

407405
### Example for `fullName` function
406+
Let's see how to work with functions and what is required to do. We will show it right on the example.
408407

409408
#### Expected api
410409
```http request
411410
GET http://host/endpoint?fullName=(Jon,Snow)
412411
```
413-
☝️ _this shows what we want to offer to our consumers. It's easy and explicit enough._
412+
☝️ example above shows what we want to offer to our consumers. It's easy and explicit enough.
414413

415-
It may even hide some inner differences, for example with simple filters, database column must have same name as the field in the filter, but with function, we can change it.
414+
It may even hide some inner differences, for example with simple filters, database column must have same name as field in filter, but with function, we can change it.
416415

417416
Let's say that in database we have something like:
418417
```fs
@@ -432,15 +431,15 @@ $apiFilter->declareFunction(
432431
'fullName',
433432
[
434433
new ParameterDefinition('firstName', 'eq', 'first_name'), // parameter name and field name are different, so we need to define it
435-
'lastname`, // parameter name and field name are the same and we use the implicit `eq` filter, so it is defined simply
434+
'lastname`, // parameter name and field name are the same and we use the implicit `eq` filter, so it is defined simply
436435
]
437436
);
438437
```
439-
Method `declareFunction` will create a function with filters based on parameters.
440-
_There is also [registerFunction](#register-and-execute-function) method, which allows you to pass any function you want. This may be useful when you don't need filter functionality at all or have some custom logic/storage, etc._
438+
Method `declareFunction` will create a function with filters based on parameters.
439+
_There is also `registerFunction` method, which allows you to pass any function you want. This may be useful when you dont need filter functionality at all or have some custom storage, etc._
441440

442441
#### Parsing and applying filters
443-
Now when request with `?fullName=(Jon,Snow)` comes, `ApiFilter` can parse it to:
442+
Now when request with `?fullName=(Jon,Snow)` come, `ApiFilter` can parse it to:
444443
```php
445444
// in service/controller/...
446445
$sql = 'SELECT * FROM person';
@@ -473,11 +472,7 @@ $filters = $apiFilter->parseFilters($request->query->all());
473472
// ]
474473

475474
$appliedSql = $apiFilter->applyFilters($filters, $sql);
476-
// SELECT *
477-
// FROM person
478-
// WHERE
479-
// first_name = :firstName_function_parameter AND
480-
// lastname = :lastname_function_parameter
475+
// SELECT * FROM person WHERE first_name = :firstName_function_parameter AND lastname = :lastname_function_parameter
481476

482477
$preparedValues = $apiFilter->getPreparedValues($filters, $sql);
483478
// [
@@ -493,36 +488,36 @@ All examples below results the same. We have that many options, so we can allow
493488
### Explicit function call
494489
GET http://host/endpoint?fullName=(Jon,Snow)
495490
496-
### Explicit function call with values
491+
### Explicit function call with values
497492
GET http://host/endpoint?function=fullName&firstName=Jon&lastname=Snow
498493
499494
### Implicit function call by values
500495
GET http://host/endpoint?firstName=Jon&lastname=Snow
501496
502-
### Explicit function call by tuple
497+
### Explicit function call by tuple
503498
GET http://host/endpoint?(function,firstName,surname)=(fullName, Jon, Snow)
504499
505500
### Implicit function call by tuple
506501
GET http://host/endpoint?(firstName,surname)=(Jon, Snow)
507502
508-
### Explicit function call by filter parameter
503+
### Explicit function call by filter parameter
509504
GET http://host/endpoint?filter[]=(fullName,Jon,Snow)
510505
```
511506

512507
### Function Parameters Definition
513508
To `declare` or `register` function, you have to define its parameters. There are many ways/needs to do it.
514509

515510
#### Defined as string
516-
This is the easiest way to do it. You just define a parameter(s) name.
517-
518-
```php
519-
$apiFilter->declareFunction('fullName', ['firstName', 'surname']);
520-
```
511+
This is the easiest way to do it. You just define a name.
521512

522513
It means:
523514
- you want `eq` filter (_or `IN` for array_) and the column name and parameter name are the same
524515
- the value for this parameter is mandatory
525516

517+
```php
518+
$apiFilter->declareFunction('fullName', ['firstName', 'surname']);
519+
```
520+
526521
#### Defined as array
527522
This allows you to pass more options for a paramater.
528523

@@ -533,25 +528,24 @@ $apiFilter->declareFunction('fullName', [['firstName'], ['surname']]);
533528
```
534529

535530
##### More than one item
531+
It means
532+
- `firstName` parameter uses `eq` filter, has `first_name` column in storage and is mandatory
533+
- `surname` parameter uses `eq` filter, has `lastname` column in storage and its value is `Snow` (_which will always be used and no value can override it_)
536534
```php
537535
$apiFilter->declareFunction('fullName', [
538536
['firstName', 'eq', 'first_name'],
539537
['surname', 'eq', 'lastname', 'Snow']
540538
]);
541539
```
542540

543-
It means
544-
- `firstName` parameter uses `eq` filter, has `first_name` column in a storage and is mandatory
545-
- `surname` parameter uses `eq` filter, has `lastname` column in a storage and its value is `Snow` (_which will always be used and no value can override it_)
546-
547541
#### Defined as object
548-
This allows you to pass same options as with the array, but explicitly defined in the object. (_It even has some special constructor methods to simplify special needs._)
542+
This allows you to pass same options as with the array, but explicitly defined object. (_It even has some special constructor methods to simplify creation_)
549543
```php
550544
$apiFilter->declareFunction('fullName', [
551545
new ParameterDefinition('firstName', 'eq', 'first_name'),
552546
new ParameterDefinition('surname', 'eq', 'lastname, new Value('Snow'))
553547
]);
554-
```
548+
```
555549

556550
#### Combinations
557551
All options can be combined to best suite the parameter.
@@ -585,7 +579,7 @@ $apiFilter->registerFunction(
585579
function (\PDO $client, FunctionParameter $query): \PDOStatement {
586580
return $client->query($query->getValue()->getValue());
587581
}
588-
)
582+
);
589583

590584
// in service/controller/...
591585
$statement = $apiFilter->executeFunction('sql', $queryParameters, $client); // \PDOStatement

composer.json

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@
2222
"doctrine/orm": "To allow applying filters directly to QueryBuilder"
2323
},
2424
"require-dev": {
25-
"doctrine/orm": "^2.6",
26-
"lmc/coding-standard": "^1.1",
27-
"mockery/mockery": "^1.1",
28-
"php-coveralls/php-coveralls": "^2.1",
29-
"phpstan/phpstan-beberlei-assert": "^0.10.0",
30-
"phpstan/phpstan-mockery": "^0.10.2",
31-
"phpstan/phpstan-shim": "^0.10",
32-
"phpunit/phpunit": "^7.0"
25+
"doctrine/orm": "^2.7",
26+
"lmc/coding-standard": "^2.0",
27+
"mockery/mockery": "^1.3",
28+
"php-coveralls/php-coveralls": "^2.2",
29+
"phpstan/extension-installer": "^1.0",
30+
"phpstan/phpstan": "^0.12.23",
31+
"phpstan/phpstan-beberlei-assert": "^0.12.2",
32+
"phpstan/phpstan-mockery": "^0.12.5",
33+
"phpunit/phpunit": "^7.5"
3334
},
3435
"config": {
3536
"sort-packages": true
@@ -46,7 +47,7 @@
4647
"phpstan": "vendor/bin/phpstan analyze ./src ./tests -c phpstan.neon --ansi --level 7",
4748
"tests": "vendor/bin/phpunit",
4849
"tests-ci": "mkdir -p reports && php -dxdebug.coverage_enable=1 vendor/bin/phpunit -c phpunit.xml.dist",
49-
"cs": "vendor/bin/ecs check -vv --ansi src/ tests/",
50-
"fix": "vendor/bin/ecs check -vv --ansi --clear-cache --fix src/ tests/"
50+
"cs": "vendor/bin/ecs check --ansi src/ tests/",
51+
"fix": "vendor/bin/ecs check --ansi --clear-cache --fix src/ tests/"
5152
}
5253
}

easy-coding-standard.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
imports:
2-
- { resource: '%vendor_dir%/lmc/coding-standard/easy-coding-standard.yaml' }
2+
- { resource: 'vendor/lmc/coding-standard/easy-coding-standard.yaml' }
33

44
parameters:
55
skip:

phpstan.neon

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
includes:
2-
- vendor/phpstan/phpstan-beberlei-assert/extension.neon
3-
- vendor/phpstan/phpstan-mockery/extension.neon
1+
parameters:
2+
checkMissingIterableValueType: false

src/ApiFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public function registerApplicator(ApplicatorInterface $applicator, int $priorit
209209
* @param array $parameters names or definition of needed parameters (parameters will be passed to function in given order)
210210
* @throws ApiFilterExceptionInterface
211211
*/
212-
public function declareFunction(string $functionName, array $parameters)
212+
public function declareFunction(string $functionName, array $parameters): self
213213
{
214214
$parameters = $this->functionCreator->normalizeParameters($parameters);
215215

src/Assertion.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Assertion extends BaseAssertion
1616
* @param mixed $value
1717
* @param string|callable|null $message
1818
*/
19-
public static function isTuple($value, $message = null, ?string $propertyPath = null)
19+
public static function isTuple($value, $message = null, ?string $propertyPath = null): bool
2020
{
2121
if (self::isTupleValue($value)) {
2222
return true;
@@ -30,6 +30,7 @@ public static function isTuple($value, $message = null, ?string $propertyPath =
3030
throw static::createException($value, $message, 0, $propertyPath);
3131
}
3232

33+
/** @param mixed $value */
3334
private static function isTupleValue($value): bool
3435
{
3536
return $value instanceof ITuple || (is_string($value) && mb_substr($value, 0, 1) === '(');

src/Exception/InvalidArgumentException.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ class InvalidArgumentException extends \InvalidArgumentException implements ApiF
1313
/** @var array */
1414
private $constraints;
1515

16+
/**
17+
* @param mixed $value
18+
*/
1619
public function __construct(
1720
string $message,
1821
int $code = null,

src/Exception/TupleException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ class TupleException extends InvalidArgumentException implements TupleExceptionI
88
{
99
public static function forBaseTupleException(TupleExceptionInterface $e): self
1010
{
11-
return new static($e->getMessage(), $e->getCode(), null, null, [], $e);
11+
return new self($e->getMessage(), $e->getCode(), null, null, [], $e);
1212
}
1313
}

src/Exception/UnknownFilterException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class UnknownFilterException extends InvalidArgumentException
88
{
99
public static function forFilterWithColumnAndValue(string $filter, string $column, Value $value): self
1010
{
11-
return new static(
11+
return new self(
1212
sprintf(
1313
'Filter "%s" is not implemented. For column "%s" with value "%s".',
1414
$filter,

src/Exception/UnsupportedFilterException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ class UnsupportedFilterException extends InvalidArgumentException
88
{
99
public static function forFilter(FilterInterface $filter): self
1010
{
11-
return new static(sprintf('Unsupported filter given "%s".', get_class($filter)));
11+
return new self(sprintf('Unsupported filter given "%s".', get_class($filter)));
1212
}
1313
}

src/Exception/UnsupportedFilterableException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static function forFilterable(Filterable $filterable): self
1010
{
1111
$filterableValue = $filterable->getValue();
1212

13-
return new static(
13+
return new self(
1414
sprintf(
1515
'Unsupported filterable of type "%s".',
1616
is_object($filterableValue)

src/Filters/Filters.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
class Filters implements FiltersInterface
1717
{
18-
/** @var IList|FilterInterface[] */
18+
/** @var IList<FilterInterface> */
1919
private $filters;
2020

2121
/** @param FilterInterface[] $filters */
@@ -27,7 +27,10 @@ public static function from(array $filters): FiltersInterface
2727
/** @param FilterInterface[] $filters */
2828
public function __construct(array $filters = [])
2929
{
30-
$this->filters = ListCollection::fromT(FilterInterface::class, $filters);
30+
/** @var IList<FilterInterface> $filterCollection */
31+
$filterCollection = ListCollection::fromT(FilterInterface::class, $filters);
32+
33+
$this->filters = $filterCollection;
3134
}
3235

3336
/**

src/Service/FunctionCreator.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ public function normalizeParameters(array $parameters): IMap
4444
}
4545

4646
/**
47-
* @see FunctionCreator::normalizeParameters()
48-
*
4947
* @param ParameterDefinition[]|IMap $normalizedParameters IMap<string, Parameter>
48+
* @see FunctionCreator::normalizeParameters()
5049
*/
5150
public function getParameterNames(IMap $normalizedParameters): array
5251
{
@@ -59,9 +58,8 @@ public function getParameterNames(IMap $normalizedParameters): array
5958
}
6059

6160
/**
62-
* @see FunctionCreator::normalizeParameters()
63-
*
6461
* @param ParameterDefinition[]|IMap $normalizedParameters IMap<string, Parameter>
62+
* @see FunctionCreator::normalizeParameters()
6563
*/
6664
public function createByParameters(FilterApplicator $applicator, IMap $normalizedParameters): callable
6765
{
@@ -75,6 +73,9 @@ public function createByParameters(FilterApplicator $applicator, IMap $normalize
7573
};
7674
}
7775

76+
/**
77+
* @param mixed $parameter
78+
*/
7879
private function assertParameter($parameter): void
7980
{
8081
Assertion::true(
@@ -126,10 +127,9 @@ private function getParameterByDefinition(array $parameters, string $name): Func
126127
}
127128

128129
/**
129-
* @see FunctionCreator::normalizeParameters()
130-
*
131130
* @param ParameterDefinition[]|IMap $normalizedParameters IMap<string, Parameter>
132131
* @return ParameterDefinition[]
132+
* @see FunctionCreator::normalizeParameters()
133133
*/
134134
public function getParameterDefinitions(IMap $normalizedParameters): array
135135
{

src/Service/Parser/AbstractParser.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public function __construct(FilterFactory $filterFactory)
1717
$this->filterFactory = $filterFactory;
1818
}
1919

20+
/**
21+
* @param string|array $value
22+
*/
2023
protected function isTuple($value): bool
2124
{
2225
return is_string($value) && mb_substr($value, 0, 1) === '(';
@@ -40,6 +43,9 @@ protected function isColumnWithFilter(string $column): bool
4043
return mb_strpos($column, '[') !== false || mb_strpos($column, ']') !== false;
4144
}
4245

46+
/**
47+
* @param mixed $value
48+
*/
4349
protected function createFilter(string $column, string $filter, $value): FilterInterface
4450
{
4551
return $this->filterFactory->createFilter($column, $filter, new Value($value));

src/Service/Parser/FunctionParser/AbstractFunctionParser.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ final public function supports(string $rawColumn, $rawValue): bool
4747
return $this->supportsParameters($this->assertQueryParameters(), $rawColumn, $rawValue);
4848
}
4949

50+
/**
51+
* @param string|array $rawValue Raw value from query parameters
52+
*/
5053
abstract protected function supportsParameters(array $queryParameters, string $rawColumn, $rawValue): bool;
5154

5255
private function assertQueryParameters(): array

src/Service/Parser/FunctionParser/ExplicitFunctionDefinitionParser.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,11 @@
77

88
class ExplicitFunctionDefinitionParser extends AbstractFunctionParser
99
{
10-
/**
11-
* @param string|array $rawValue Raw value from query parameters
12-
*/
1310
protected function supportsParameters(array $queryParameters, string $rawColumn, $rawValue): bool
1411
{
1512
return !$this->isTuple($rawColumn) && $this->functions->isFunctionRegistered($rawColumn);
1613
}
1714

18-
/**
19-
* @param string|array $rawValue Raw value from query parameters
20-
*/
2115
protected function parseParameters(array $queryParameters, string $rawColumn, $rawValue): iterable
2216
{
2317
if (!$this->functions->isFunctionRegistered($rawColumn)) {
@@ -41,6 +35,9 @@ protected function parseParameters(array $queryParameters, string $rawColumn, $r
4135
}
4236
}
4337

38+
/**
39+
* @param string|array $rawValue Raw value from query parameters
40+
*/
4441
protected function assertSingleStringValue($rawValue): void
4542
{
4643
Assertion::false(

src/Service/Parser/FunctionParser/FunctionInFilterParameterParser.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ protected function supportsParameters(array $queryParameters, string $rawColumn,
1717
return array_key_exists(Column::FILTER, $queryParameters);
1818
}
1919

20-
/**
21-
* @param string|array $rawValue Raw value from query parameters
22-
*/
2320
protected function parseParameters(array $queryParameters, string $rawColumn, $rawValue): iterable
2421
{
2522
if ($this->isColumnParsed(Column::FILTER)) {

0 commit comments

Comments
 (0)