Passed
Pull Request — master (#5)
by Alex
02:11
created

AbstractFilter::validateFieldName()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 2
nop 3
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Query\Filter;
6
7
use Arp\LaminasDoctrine\Query\Exception\InvalidArgumentException;
8
use Arp\LaminasDoctrine\Query\Metadata\MetadataInterface;
9
use Arp\LaminasDoctrine\Query\QueryFilterManager;
10
11
/**
12
 * @author  Alex Patterson <[email protected]>
13
 * @package Arp\LaminasDoctrine\Query\Filter
14
 */
15
abstract class AbstractFilter implements FilterInterface
16
{
17
    /**
18
     * @var QueryFilterManager
19
     */
20
    protected QueryFilterManager $queryFilterManager;
21
22
    /**
23
     * @param QueryFilterManager $queryFilterManager
24
     */
25
    public function __construct(QueryFilterManager $queryFilterManager)
26
    {
27
        $this->queryFilterManager = $queryFilterManager;
28
    }
29
30
    /**
31
     * @param MetadataInterface $metadata
32
     * @param array             $criteria
33
     * @param string            $key
34
     *
35
     * @return string
36
     *
37
     * @throws InvalidArgumentException
38
     */
39
    protected function resolveFieldName(MetadataInterface $metadata, array $criteria, string $key = 'field'): string
40
    {
41
        if (empty($criteria[$key])) {
42
            throw new InvalidArgumentException(
43
                sprintf(
44
                    'The required \'%s\' criteria value is missing for filter \'%s\'',
45
                    $key,
46
                    static::class
47
                )
48
            );
49
        }
50
51
        if (!$metadata->hasField($criteria[$key]) && !$metadata->hasAssociation($criteria[$key])) {
52
            throw new InvalidArgumentException(
53
                sprintf(
54
                    'The field name \'%s\' is invalid for entity \'%s\'',
55
                    $criteria[$key],
56
                    $metadata->getName()
57
                )
58
            );
59
        }
60
61
        return $criteria[$key];
62
    }
63
64
    /**
65
     * @param MetadataInterface $metadata
66
     * @param string            $fieldName
67
     * @param string            $entityName
68
     *
69
     * @throws InvalidArgumentException
70
     */
71
    protected function validateFieldName(MetadataInterface $metadata, string $fieldName, string $entityName): void
72
    {
73
        if (!$metadata->hasField($fieldName) && !$metadata->hasAssociation($fieldName)) {
74
            throw new InvalidArgumentException(
75
                sprintf(
76
                    'Unable to apply query filter \'%s\': '
77
                    . 'The entity class \'%s\' has no field or association named \'%s\'',
78
                    static::class,
79
                    $entityName,
80
                    $fieldName
81
                )
82
            );
83
        }
84
    }
85
86
    /**
87
     * @param string $fieldName
88
     * @param array  $criteria
89
     *
90
     * @return mixed
91
     *
92
     * @throws InvalidArgumentException
93
     */
94
    protected function resolveValue(string $fieldName, array $criteria)
95
    {
96
        if (!array_key_exists('value', $criteria)) {
97
            throw new InvalidArgumentException(
98
                sprintf(
99
                    'The required \'value\' criteria value is missing for filter \'%s::%s\'',
100
                    static::class,
101
                    $fieldName
102
                )
103
            );
104
        }
105
        return $criteria['value'];
106
    }
107
108
    /**
109
     * @param MetadataInterface $metadata
110
     * @param string            $fieldName
111
     * @param mixed             $value
112
     * @param string|null       $format
113
     *
114
     * @return mixed
115
     *
116
     * @noinspection PhpUnusedParameterInspection
117
     */
118
    protected function formatValue(MetadataInterface $metadata, string $fieldName, $value, ?string $format = null)
0 ignored issues
show
Unused Code introduced by
The parameter $metadata is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

118
    protected function formatValue(/** @scrutinizer ignore-unused */ MetadataInterface $metadata, string $fieldName, $value, ?string $format = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $fieldName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

118
    protected function formatValue(MetadataInterface $metadata, /** @scrutinizer ignore-unused */ string $fieldName, $value, ?string $format = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $format is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

118
    protected function formatValue(MetadataInterface $metadata, string $fieldName, $value, /** @scrutinizer ignore-unused */ ?string $format = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
119
    {
120
        return $value;
121
    }
122
}
123