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
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://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@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    6.5.0
25 * @property   $where mixed
26 */
27abstract class AbstractPredicateClause extends AbstractClause
28{
29
30    /**
31     * WHERE predicate object
32     * @var ?Where
33     */
34    protected ?Where $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 ($this->where === null) {
45            $this->where = new Where($this);
46        }
47
48        if ($where !== null) {
49            if (is_string($where)) {
50                if ((stripos($where, ' AND ') !== false) || (stripos($where, ' OR ') !== false)) {
51                    $expressions = array_map('trim', preg_split(
52                        '/(AND|OR)/', $where, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY
53                    ));
54                    foreach ($expressions as $i => $expression) {
55                        if (isset($expressions[$i - 1]) && (strtoupper($expressions[$i - 1]) == 'AND')) {
56                            $this->where->and($expression);
57                        } else if (isset($expressions[$i - 1]) && (strtoupper($expressions[$i - 1]) == 'OR')) {
58                            $this->where->or($expression);
59                        } else if (($expression != 'AND') && ($expression != 'OR')) {
60                            $this->where->add($expression);
61                        }
62                    }
63                } else {
64                    $this->where->add($where);
65                }
66            } else if (is_array($where)) {
67                $this->where->addExpressions($where);
68            }
69        }
70
71        return $this;
72    }
73
74    /**
75     * Access the WHERE clause with AND
76     *
77     * @param  mixed $where
78     * @return AbstractPredicateClause
79     */
80    public function andWhere(mixed $where = null): AbstractPredicateClause
81    {
82        if ($this->where === null) {
83            $this->where = new Where($this);
84        }
85
86        if ($where !== null) {
87            if (is_string($where)) {
88                $this->where->and($where);
89            } else if (is_array($where)) {
90                foreach ($where as $w) {
91                    $this->where->and($w);
92                }
93            }
94        }
95
96        return $this;
97    }
98
99    /**
100     * Access the WHERE clause with OR
101     *
102     * @param  mixed $where
103     * @return AbstractPredicateClause
104     */
105    public function orWhere(mixed $where = null): AbstractPredicateClause
106    {
107        if ($this->where === null) {
108            $this->where = new Where($this);
109        }
110
111        if ($where !== null) {
112            if (is_string($where)) {
113                $this->where->or($where);
114            } else if (is_array($where)) {
115                foreach ($where as $w) {
116                    $this->where->or($w);
117                }
118            }
119        }
120
121        return $this;
122    }
123
124}