Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
Update
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
4 / 4
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 __toString
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 statement
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare(strict_types=1);
4
5namespace Projom\Storage\SQL\Statement;
6
7use Stringable;
8
9use Projom\Storage\SQL\Component\Filter;
10use Projom\Storage\SQL\Component\Join;
11use Projom\Storage\SQL\Component\Set;
12use Projom\Storage\SQL\Component\Table;
13use Projom\Storage\SQL\Statement\StatementInterface;
14use Projom\Storage\SQL\QueryObject;
15use Projom\Storage\SQL\Util;
16
17class Update implements StatementInterface, Stringable
18{
19    private readonly Table $table;
20    private readonly Set $set;
21    private readonly Join $join;
22    private readonly Filter $filter;
23
24    public function __construct(QueryObject $queryUpdate)
25    {
26        $this->table = Table::create($queryUpdate->collections);
27        $this->set = Set::create($queryUpdate->fieldsWithValues);
28        $this->join = Join::create($queryUpdate->joins);
29        $this->filter = Filter::create($queryUpdate->filters);
30    }
31
32    public function __toString(): string
33    {
34        [$statement, $params] = $this->statement();
35        return $statement;
36    }
37
38    public static function create(QueryObject $queryUpdate): Update
39    {
40        return new Update($queryUpdate);
41    }
42
43    public function statement(): array
44    {
45        $queryParts[] = "UPDATE {$this->table} SET {$this->set}";
46
47        if (!$this->join->empty())
48            $queryParts[] = "{$this->join}";
49
50        if (!$this->filter->empty())
51            $queryParts[] = "WHERE {$this->filter}";
52
53        $query = Util::join($queryParts, ' ');
54        $params = ($this->set->params() + $this->filter->params()) ?: null;
55
56        return [
57            $query,
58            $params
59        ];
60    }
61}