Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractPredicateClause
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
3 / 3
26
100.00% covered (success)
100.00%
1 / 1
 where
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
14
 andWhere
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
6
 orWhere
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <nick@popphp.org>
8 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Db\Sql;
16
17/**
18 * Abstract clause class
19 *
20 * @category   Pop
21 * @package    Pop\Db
22 * @author     Nick Sagona, III <nick@popphp.org>
23 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
24 * @license    https://www.popphp.org/license     New BSD License
25 * @version    7.0.0
26 *
27 * @property-read ?PredicateSet $where WHERE predicate object (lazily created by subclasses)
28 */
29abstract class AbstractPredicateClause extends AbstractClause
30{
31
32    /**
33     * WHERE predicate object
34     * @var ?PredicateSet
35     */
36    protected ?PredicateSet $wherePredicate = null;
37
38    /**
39     * Access the WHERE clause
40     *
41     * @param  mixed $where
42     * @return AbstractPredicateClause
43     */
44    public function where(mixed $where = null): AbstractPredicateClause
45    {
46        if ($where instanceof PredicateSet) {
47            $this->wherePredicate = $where;
48        } else {
49            if ($this->wherePredicate === null) {
50                $this->wherePredicate = new Where($this);
51            }
52
53            if ($where !== null) {
54                if (is_string($where)) {
55                    $tokens = Parser\Keyword::split($where);
56                    if (count($tokens) > 1) {
57                        foreach ($tokens as $i => $token) {
58                            if (($i > 0) && (($tokens[$i - 1] === 'AND') || ($tokens[$i - 1] === 'OR'))) {
59                                if ($tokens[$i - 1] === 'AND') {
60                                    $this->wherePredicate->and($token);
61                                } else {
62                                    $this->wherePredicate->or($token);
63                                }
64                            } else if (($token !== 'AND') && ($token !== 'OR')) {
65                                $this->wherePredicate->add($token);
66                            }
67                        }
68                    } else {
69                        $this->wherePredicate->add($where);
70                    }
71                } else if (is_array($where)) {
72                    $this->wherePredicate->addExpressions($where);
73                }
74            }
75        }
76
77        return $this;
78    }
79
80    /**
81     * Access the WHERE clause with AND
82     *
83     * @param  mixed $where
84     * @return AbstractPredicateClause
85     */
86    public function andWhere(mixed $where = null): AbstractPredicateClause
87    {
88        if ($this->wherePredicate === null) {
89            $this->wherePredicate = new Where($this);
90        }
91
92        if ($where !== null) {
93            if (is_string($where)) {
94                $this->wherePredicate->and($where);
95            } else if (is_array($where)) {
96                foreach ($where as $w) {
97                    $this->wherePredicate->and($w);
98                }
99            }
100        }
101
102        return $this;
103    }
104
105    /**
106     * Access the WHERE clause with OR
107     *
108     * @param  mixed $where
109     * @return AbstractPredicateClause
110     */
111    public function orWhere(mixed $where = null): AbstractPredicateClause
112    {
113        if ($this->wherePredicate === null) {
114            $this->wherePredicate = new Where($this);
115        }
116
117        if ($where !== null) {
118            if (is_string($where)) {
119                $this->wherePredicate->or($where);
120            } else if (is_array($where)) {
121                foreach ($where as $w) {
122                    $this->wherePredicate->or($w);
123                }
124            }
125        }
126
127        return $this;
128    }
129
130}