Completed
Push — 2.0 ( 0357a9...3fe951 )
by Peter
08:22 queued 10s
created

BaseSpecification::getAlias()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of the Happyr Doctrine Specification package.
6
 *
7
 * (c) Tobias Nyholm <[email protected]>
8
 *     Kacper Gunia <[email protected]>
9
 *     Peter Gribanov <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Happyr\DoctrineSpecification;
16
17
use Doctrine\ORM\QueryBuilder;
18
use Happyr\DoctrineSpecification\Filter\Filter;
19
use Happyr\DoctrineSpecification\Filter\Satisfiable;
20
use Happyr\DoctrineSpecification\Query\QueryModifier;
21
use Happyr\DoctrineSpecification\Specification\Specification;
22
23
/**
24
 * Extend this abstract class if you want to build a new spec with your domain logic.
25
 */
26
abstract class BaseSpecification implements Specification
27
{
28
    /**
29
     * @var string|null
30
     */
31
    private $context;
32
33
    /**
34
     * @param string|null $context
35
     */
36
    public function __construct($context = null)
37
    {
38
        $this->context = $context;
39
    }
40
41
    /**
42
     * @param QueryBuilder $qb
43
     * @param string       $context
44
     *
45
     * @return string
46
     */
47
    public function getFilter(QueryBuilder $qb, string $context): string
48
    {
49
        $spec = $this->getSpec();
50
51
        if ($spec instanceof Filter) {
52
            return $spec->getFilter($qb, $this->getContext($context));
53
        }
54
55
        return '';
56
    }
57
58
    /**
59
     * @param QueryBuilder $qb
60
     * @param string       $context
61
     */
62
    public function modify(QueryBuilder $qb, string $context): void
63
    {
64
        $spec = $this->getSpec();
65
66
        if ($spec instanceof QueryModifier) {
67
            $spec->modify($qb, $this->getContext($context));
68
        }
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function filterCollection(iterable $collection): iterable
75
    {
76
        $spec = $this->getSpec();
77
78
        if ($spec instanceof Satisfiable) {
79
            return $spec->filterCollection($collection);
80
        }
81
82
        return $collection;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function isSatisfiedBy($candidate): bool
89
    {
90
        $spec = $this->getSpec();
91
92
        if ($spec instanceof Satisfiable) {
93
            return $spec->isSatisfiedBy($candidate);
94
        }
95
96
        return true;
97
    }
98
99
    /**
100
     * Return all the specifications.
101
     *
102
     * @return Filter|QueryModifier
103
     */
104
    abstract protected function getSpec();
105
106
    /**
107
     * @param string $context
108
     *
109
     * @return string
110
     */
111
    private function getContext(string $context): string
112
    {
113
        if (null !== $this->context) {
114
            return sprintf('%s.%s', $context, $this->context);
115
        }
116
117
        return $context;
118
    }
119
120
    /**
121
     * @param string $context
122
     *
123
     * @return string
124
     */
125
    protected function getNestedContext(string $context): string
126
    {
127
        if (null !== $this->context) {
128
            return sprintf('%s.%s', $this->context, $context);
129
        }
130
131
        return $context;
132
    }
133
}
134