Completed
Push — 2.0 ( d6e995...0a3164 )
by Peter
09:53 queued 13s
created

BaseSpecification::resolveContext()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 4
nc 3
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\Specification;
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
22
/**
23
 * Extend this abstract class if you want to build a new spec with your domain logic.
24
 */
25
abstract class BaseSpecification implements Specification
26
{
27
    /**
28
     * @var string|null
29
     */
30
    private $context;
31
32
    /**
33
     * @param string|null $context
34
     */
35
    public function __construct(?string $context = null)
36
    {
37
        $this->context = $context;
38
    }
39
40
    /**
41
     * @param QueryBuilder $qb
42
     * @param string       $context
43
     *
44
     * @return string
45
     */
46
    public function getFilter(QueryBuilder $qb, string $context): string
47
    {
48
        $spec = $this->getSpec();
49
50
        if ($spec instanceof Filter) {
51
            return $spec->getFilter($qb, $this->getContext($context));
52
        }
53
54
        return '';
55
    }
56
57
    /**
58
     * @param QueryBuilder $qb
59
     * @param string       $context
60
     */
61
    public function modify(QueryBuilder $qb, string $context): void
62
    {
63
        $spec = $this->getSpec();
64
65
        if ($spec instanceof QueryModifier) {
66
            $spec->modify($qb, $this->getContext($context));
67
        }
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function filterCollection(iterable $collection, ?string $context = null): iterable
74
    {
75
        $spec = $this->getSpec();
76
77
        if ($spec instanceof Satisfiable) {
78
            return $spec->filterCollection($collection, $this->resolveContext($context));
79
        }
80
81
        return $collection;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function isSatisfiedBy($candidate, ?string $context = null): bool
88
    {
89
        $spec = $this->getSpec();
90
91
        if ($spec instanceof Satisfiable) {
92
            return $spec->isSatisfiedBy($candidate, $this->resolveContext($context));
93
        }
94
95
        return true;
96
    }
97
98
    /**
99
     * Return all the specifications.
100
     *
101
     * @return Filter|QueryModifier
102
     */
103
    abstract protected function getSpec();
104
105
    /**
106
     * @param string $context
107
     *
108
     * @return string
109
     */
110
    private function getContext(string $context): string
111
    {
112
        if (null !== $this->context) {
113
            return sprintf('%s.%s', $context, $this->context);
114
        }
115
116
        return $context;
117
    }
118
119
    /**
120
     * @param string $context
121
     *
122
     * @return string
123
     */
124
    protected function getNestedContext(string $context): string
125
    {
126
        if (null !== $this->context) {
127
            return sprintf('%s.%s', $this->context, $context);
128
        }
129
130
        return $context;
131
    }
132
133
    /**
134
     * @param string|null $context
135
     *
136
     * @return string|null
137
     */
138
    private function resolveContext(?string $context): ?string
139
    {
140
        if (null !== $this->context && null !== $context) {
141
            return sprintf('%s.%s', $context, $this->context);
142
        }
143
144
        if (null !== $this->context) {
145
            return $this->context;
146
        }
147
148
        return $context;
149
    }
150
}
151