Passed
Push — master ( b2bdac...3a7f22 )
by Ondřej
03:22
created

ConstrainedRelationDefinition::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 6
nc 2
nop 3
1
<?php
2
namespace Ivory\Query;
3
4
use Ivory\Lang\Sql\ISqlPredicate;
5
use Ivory\Lang\SqlPattern\SqlPattern;
6
use Ivory\Type\ITypeDictionary;
7
8
class ConstrainedRelationDefinition extends RelationDefinition implements IRelationDefinition
9
{
10
    private $baseRelDef;
11
    private $cond;
12
    private $args;
13
14
    /**
15
     * @param IRelationDefinition $relationDefinition definition of relation to constrain
16
     * @param ISqlPredicate|SqlPattern|string $cond
17
     * @param array $args
18
     */
19
    public function __construct(IRelationDefinition $relationDefinition, $cond, ...$args)
20
    {
21
        if ($cond instanceof ISqlPredicate && $args) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $args of type array[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
22
            throw new \InvalidArgumentException('$args not supported for ' . ISqlPredicate::class . ' condition');
23
        }
24
25
26
        $this->baseRelDef = $relationDefinition;
27
        $this->cond = $cond;
28
        $this->args = $args;
29
    }
30
31
    public function toSql(ITypeDictionary $typeDictionary): string
32
    {
33
        $relSql = $this->baseRelDef->toSql($typeDictionary);
34
35
        if ($this->cond instanceof ISqlPredicate) {
36
            $condSql = $this->cond->getSql();
37
        } else {
38
            $condDef = SqlRelationDefinition::fromPattern($this->cond, ...$this->args);
39
            $condSql = $condDef->toSql($typeDictionary);
40
        }
41
42
        return "SELECT *\nFROM (\n$relSql\n) t\nWHERE $condSql";
43
    }
44
}
45