Passed
Push — master ( 4c916a...c716e1 )
by compolom
01:44
created

src/Parts/Where.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php declare(strict_types=1);
2
3
namespace Compolomus\LSQLQueryBuilder\Parts;
4
5
use Compolomus\LSQLQueryBuilder\System\{
6
    Conditions,
7
    Traits\Helper,
8
    Traits\Caller
9
};
10
11
class Where
12
{
13
    use Caller, Helper;
14
15
    private $whereType;
16
17
    private $whereTypes = [
18
        'and',
19
        'or'
20
    ];
21
22
    private $counter = 0;
23
24
    private $conditions;
25
26
    public function where(array $where = [], string $type = 'and'): Where
27
    {
28
        if (!in_array(strtolower($type), $this->whereTypes)) {
29
            throw new \InvalidArgumentException('DIE |WHERE construct|');
30
        }
31
        $this->whereType[$this->counter] = $type;
32
        $this->conditions[$this->counter] = new Conditions;
33
        if (count($where)) {
34
            foreach ($where as $condition) {
35
                list($field, $cond, $value) = $condition;
36
                $this->add($field, $cond, $value);
37
            }
38
        }
39
        $this->counter++;
40
        return $this;
41
    }
42
43
    public function condition(): Conditions
44
    {
45
        return end($this->conditions);
46
    }
47
48
    public function __construct(array $where = [], string $type = 'and')
49
    {
50
        $this->where($where, $type);
51
    }
52
53
    /**
54
     * @param string $field
55
     * @param string $cond
56
     * @param mixed $value
57
     * @return $this
58
     */
59
    public function add(string $field, string $cond, $value): Where
60
    {
61
        $this->condition()->add($field, $cond, $value);
62
        return $this;
63
    }
64
65
    public function result(): string
66
    {
67
        $array = [];
68
        $placeholders = [];
69
        foreach ($this->conditions as $key => $condition) {
70
            $placeholders += $condition->placeholders()->get();
71
            $array[] = '(' . $this->concatWhere($condition->conditions(), $this->whereType[$key]) . ')';
72
        }
73
        $this->addPlaceholders($placeholders);
0 ignored issues
show
Documentation Bug introduced by
The method addPlaceholders does not exist on object<Compolomus\LSQLQueryBuilder\Parts\Where>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
74
        return 'WHERE ' . $this->concatWhere($array, 'and');
75
    }
76
}
77