LookupBuilder::buildJoin()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Tsukasa\QueryBuilder\LookupBuilder;
4
5
use Tsukasa\QueryBuilder\Exception\QBException;
6
use Tsukasa\QueryBuilder\Interfaces\QueryBuilderInterface;
7
8
class LookupBuilder extends Base
9
{
10
    public function parseLookup(QueryBuilderInterface $queryBuilder, $rawLookup, $value)
11
    {
12
        $nodesCount = substr_count($rawLookup, $this->separator);
13
        $lookupNodes = explode($this->separator, $rawLookup);
14
15
        switch (substr_count($rawLookup, $this->separator)) {
16
            case 0:
17
                $rawLookup = $this->fetchColumnName($rawLookup);
18
                return [$this->default, $rawLookup, $value];
19
            case 1:
20
                if ($this->hasLookup(end($lookupNodes))) {
21
                    list($column, $lookup) = explode($this->separator, $rawLookup);
22
                    if ($this->hasLookup($lookup) === false) {
23
                        throw new QBException('Unknown lookup:' . $lookup);
24
                    }
25
                    $column = $this->fetchColumnName($column);
26
                    return [$lookup, $column, $value];
27
                }
28
                break;
29
            default:
30
                if ($nodesCount > 1) {
31
                    if ($this->columnCallback === null) {
32
                        throw new QBException('Unknown lookup: ' . $rawLookup);
33
                    }
34
35
                    return $this->runCallback($queryBuilder, explode($this->separator, $rawLookup), $value);
36
                }
37
        }
38
39
        return $this->runCallback($queryBuilder, $lookupNodes, $value);
40
    }
41
42
    public function buildJoin(QueryBuilderInterface $queryBuilder, $lookup)
43
    {
44
        if (substr_count($lookup, $this->getSeparator()) > 0) {
45
            return $this->runJoinCallback($queryBuilder, explode($this->getSeparator(), $lookup));
46
        }
47
48
        return false;
49
    }
50
51
    public function parse(QueryBuilderInterface $queryBuilder, array $where)
52
    {
53
        $conditions = [];
54
        foreach ($where as $lookup => $value) {
55
            /**
56
             * Parse new QOr([[username => 1], [username => 2]])
57
             */
58
            if (is_numeric($lookup) && is_array($value)) {
59
                $lookup = key($value);
60
                $value = array_shift($value);
61
            }
62
            $conditions[] = $this->parseLookup($queryBuilder, $lookup, $value);
63
        }
64
        return $conditions;
65
    }
66
}