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