Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
Update
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
5 / 5
12
100.00% covered (success)
100.00%
1 / 1
 set
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 values
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 render
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
5
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __get
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
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 * Update 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 */
27class Update extends AbstractPredicateClause
28{
29
30    /**
31     * Set a value
32     *
33     * @param  string $name
34     * @param  mixed  $value
35     * @return Update
36     */
37    public function set(string $name, mixed $value): Update
38    {
39        $this->addValue($value, $name);
40        return $this;
41    }
42
43    /**
44     * Set a value
45     *
46     * @param  array $values
47     * @return Update
48     */
49    public function values(array $values): Update
50    {
51        $this->setValues($values);
52        return $this;
53    }
54
55    /**
56     * Render the UPDATE statement
57     *
58     * @return string
59     */
60    public function render(): string
61    {
62        // Start building the UPDATE statement
63        $sql = 'UPDATE ' . $this->quoteId($this->table) . ' SET ';
64        $set = [];
65
66        $paramCount = 1;
67        $dbType = $this->getDbType();
68
69        foreach ($this->values as $column => $value) {
70            $colValue = (str_contains($column, '.')) ?
71                substr($column, (strpos($column, '.') + 1)) : $column;
72
73            $val = ($value === null) ? 'NULL' : $this->quote($value);
74            $set[] = $this->quoteId($column) .' = ' . $val;
75        }
76
77        $sql .= implode(', ', $set);
78
79        // Build any WHERE clauses
80        if ($this->wherePredicate !== null) {
81            $sql .= ' WHERE ' . $this->wherePredicate;
82        }
83
84        return $sql;
85    }
86
87    /**
88     * Render the UPDATE statement
89     *
90     * @return string
91     */
92    public function __toString(): string
93    {
94        return $this->render();
95    }
96
97    /**
98     * Magic method to access $where property
99     *
100     * @param  string $name
101     * @throws Exception
102     * @return mixed
103     */
104    public function __get(string $name): mixed
105    {
106        switch (strtolower($name)) {
107            case 'where':
108                if ($this->wherePredicate === null) {
109                    $this->wherePredicate = new Where($this);
110                }
111                return $this->wherePredicate;
112            default:
113                throw new Exception('Not a valid property for this object.');
114        }
115    }
116
117}