LessThanOrEqualFilter::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright (C) 2020-2025 Iain Cambridge
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by
10
 * the Free Software Foundation, either version 2.1 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
namespace Parthenon\Athena\Filters;
23
24
use Doctrine\ORM\Query;
25
use Doctrine\ORM\QueryBuilder;
26
27
class LessThanOrEqualFilter implements DoctrineFilterInterface
28
{
29
    use QueryBuilderTrait;
30
31
    public const NAME = 'less_than_or_equal';
32
33
    protected string $fieldName;
34
35
    private $data;
36
37
    public function modifyQueryBuilder(QueryBuilder $queryBuilder)
38
    {
39
        if (!$this->data) {
40
            return;
41
        }
42
        [$alias, $fieldName] = $this->readyQueryBuilderForAliasAndFieldName($queryBuilder);
43
        $queryBuilder->andWhere($alias.'.'.$fieldName.' <= :'.$this->getSafeFieldName());
44
    }
45
46
    public function modifyQuery(Query $query)
47
    {
48
        if (!$this->data) {
49
            return;
50
        }
51
        $query->setParameter(':'.$this->getSafeFieldName(), $this->data);
52
    }
53
54
    public function getName(): string
55
    {
56
        return static::NAME;
57
    }
58
59
    public function setData($data): FilterInterface
60
    {
61
        $this->data = $data;
62
63
        return $this;
64
    }
65
66
    public function setFieldName(string $fieldName): FilterInterface
67
    {
68
        $this->fieldName = $fieldName;
69
70
        return $this;
71
    }
72
73
    public function getFieldName(): string
74
    {
75
        return $this->fieldName;
76
    }
77
78
    public function getHeaderName(): string
79
    {
80
        return ucwords(str_replace('_', ' ', $this->fieldName));
81
    }
82
83
    public function getData()
84
    {
85
        return $this->data;
86
    }
87
88
    public function hasData(): bool
89
    {
90
        return isset($this->data);
91
    }
92
}
93