Passed
Pull Request — master (#2)
by Alex
02:48
created

AbstractFilter::createParamName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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