Completed
Push — master ( b4b898...f81b53 )
by Woody
11s
created

src/Query/UpdateQuery.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
2
declare(strict_types=1);
3
4
namespace Latitude\QueryBuilder\Query;
5
6
use Latitude\QueryBuilder\ExpressionInterface;
7
use Latitude\QueryBuilder\StatementInterface;
8
9
use function Latitude\QueryBuilder\express;
10
use function Latitude\QueryBuilder\identify;
11
use function Latitude\QueryBuilder\listing;
12
use function Latitude\QueryBuilder\param;
13
14
class UpdateQuery extends AbstractQuery
15
{
16
    use Capability\HasWhere;
17
18
    /** @var StatementInterface */
19
    protected $table;
20
21
    /** @var StatementInterface */
22
    protected $set;
23
24 4
    public function table($table): self
25
    {
26 4
        $this->table = identify($table);
27 4
        return $this;
28
    }
29
30 3
    public function set(array $map): self
31
    {
32 3
        $this->set = listing(array_map(
33 3
            function ($key, $value): StatementInterface {
34 3
                return express('%s = %s', identify($key), param($value));
35 3
            },
36 3
            array_keys($map),
37 3
            $map
38
        ));
39 3
        return $this;
40
    }
41
42 4 View Code Duplication
    public function asExpression(): ExpressionInterface
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    {
44 4
        $query = $this->startExpression();
45 4
        $query = $this->applyTable($query);
46 4
        $query = $this->applySet($query);
47 4
        $query = $this->applyWhere($query);
48
49 4
        return $query;
50
    }
51
52 4
    protected function startExpression(): ExpressionInterface
53
    {
54 4
        return express('UPDATE');
55
    }
56
57 4
    protected function applyTable(ExpressionInterface $query): ExpressionInterface
58
    {
59 4
        return $this->table ? $query->append('%s', $this->table) : $query;
60
    }
61
62 4
    protected function applySet(ExpressionInterface $query): ExpressionInterface
63
    {
64 4
        return $this->set ? $query->append('SET %s', $this->set): $query;
65
    }
66
}
67