Test Failed
Pull Request — master (#2)
by Alex
02:56
created

AbstractFilter::getOptions()   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 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\DoctrineQueryFilter\Filter;
6
7
use Arp\DoctrineQueryFilter\Filter\Exception\FilterException;
8
use Arp\DoctrineQueryFilter\Filter\Exception\InvalidArgumentException;
9
use Arp\DoctrineQueryFilter\Filter\Exception\TypecastException;
10
use Arp\DoctrineQueryFilter\Metadata\MetadataInterface;
11
use Arp\DoctrineQueryFilter\QueryFilterManagerInterface;
12
13
/**
14
 * @author  Alex Patterson <[email protected]>
15
 * @package Arp\DoctrineQueryFilter\Filter
16
 */
17
abstract class AbstractFilter implements FilterInterface
18
{
19
    /**
20
     * @var QueryFilterManagerInterface
21
     */
22
    protected QueryFilterManagerInterface $queryFilterManager;
23
24
    /**
25
     * @var TypecastInterface
26
     */
27
    protected TypecastInterface $typecaster;
28
29
    /**
30
     * @var array
31
     */
32
    protected array $options = [];
33
34
    /**
35
     * @param QueryFilterManagerInterface $queryFilterManager
36
     * @param TypecastInterface           $typecaster
37
     * @param array                       $options
38
     */
39
    public function __construct(
40
        QueryFilterManagerInterface $queryFilterManager,
41
        TypecastInterface $typecaster,
42
        array $options = []
43
    ) {
44
        $this->queryFilterManager = $queryFilterManager;
45
        $this->typecaster = $typecaster;
46
        $this->options = $options;
47
    }
48
49
    /**
50
     * @return array
51
     */
52
    public function getOptions(): array
53
    {
54
        return $this->options;
55
    }
56
57
    /**
58
     * Create a new unique parameter name
59
     *
60
     * @param string $prefix
61
     *
62
     * @return string
63
     */
64
    protected function createParamName(string $prefix = ''): string
65
    {
66
        return uniqid($prefix, false);
67
    }
68
69
    /**
70
     * @param MetadataInterface $metadata
71
     * @param array             $criteria
72
     * @param string            $key
73
     *
74
     * @return string
75
     *
76
     * @throws InvalidArgumentException
77
     */
78
    protected function resolveFieldName(MetadataInterface $metadata, array $criteria, string $key = 'field'): string
79
    {
80
        if (empty($criteria[$key])) {
81
            throw new InvalidArgumentException(
82
                sprintf(
83
                    'The required \'%s\' criteria value is missing for filter \'%s\'',
84
                    $key,
85
                    static::class
86
                )
87
            );
88
        }
89
90
        if (!$metadata->hasField($criteria[$key]) && !$metadata->hasAssociation($criteria[$key])) {
91
            throw new InvalidArgumentException(
92
                sprintf(
93
                    'Unable to apply query filter \'%s\': '
94
                    . 'The entity class \'%s\' has no field or association named \'%s\'',
95
                    static::class,
96
                    $metadata->getName(),
97
                    $criteria[$key]
98
                )
99
            );
100
        }
101
102
        return $criteria[$key];
103
    }
104
105
    /**
106
     * @param MetadataInterface $metadata
107
     * @param string            $fieldName
108
     * @param mixed             $value
109
     * @param string|null       $format
110
     *
111
     * @return mixed
112
     *
113
     * @noinspection PhpUnusedParameterInspection
114
     */
115
    protected function formatValue(
116
        MetadataInterface $metadata,
117
        string $fieldName,
118
        $value,
119
        ?string $type = null,
120
        array $options = []
121
    ) {
122
        try {
123
            return $this->typecaster->typecast($metadata, $fieldName, $value, $type, $options);
124
        } catch (TypecastException $e) {
125
            throw new FilterException(
126
                sprintf(
127
                    'Failed to format the value for field \'%s::%s\': %s',
128
                    $metadata->getName(),
129
                    $fieldName,
130
                    $e->getMessage()
131
                ),
132
                $e->getCode(),
133
                $e
134
            );
135
        }
136
    }
137
}
138