UpdateCommand::isEmpty()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * Spiral, Core Components
4
 *
5
 * @author Wolfy-J
6
 */
7
8
namespace Spiral\ORM\Commands;
9
10
use Spiral\Database\Entities\Table;
11
use Spiral\ORM\Commands\Traits\ContextTrait;
12
use Spiral\ORM\Commands\Traits\PrimaryTrait;
13
use Spiral\ORM\Commands\Traits\WhereTrait;
14
use Spiral\ORM\ContextualCommandInterface;
15
16
/**
17
 * Update data CAN be modified by parent commands using context.
18
 *
19
 * This is conditional command, it would not be executed when no fields are given!
20
 */
21
class UpdateCommand extends TableCommand implements ContextualCommandInterface
22
{
23
    use ContextTrait, PrimaryTrait, WhereTrait;
24
25
    /**
26
     * Columns to be updated.
27
     *
28
     * @var array
29
     */
30
    private $values = [];
31
32
    /**
33
     * UpdateCommand constructor.
34
     *
35
     * @param Table $table
36
     * @param array $where
37
     * @param array $values
38
     * @param mixed $primaryKey
39
     */
40
    public function __construct(Table $table, array $where, array $values = [], $primaryKey = null)
41
    {
42
        parent::__construct($table);
43
        $this->where = $where;
44
        $this->values = $values;
45
        $this->primaryKey = $primaryKey;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getDriver()
52
    {
53
        if (empty($this->context) && empty($this->values)) {
54
            //Nothing to do
55
            return null;
56
        }
57
58
        return $this->table->getDatabase()->getDriver();
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function isEmpty(): bool
65
    {
66
        return empty($this->values) && empty($this->context);
67
    }
68
69
    /**
70
     * Update values, context not included.
71
     *
72
     * @return array
73
     */
74
    public function getValues(): array
75
    {
76
        return $this->values;
77
    }
78
79
    /**
80
     * Inserting data into associated table.
81
     */
82
    public function execute()
83
    {
84
        if (!$this->isEmpty()) {
85
            $this->table->update($this->context + $this->values, $this->where)->run();
86
        }
87
88
        parent::execute();
89
    }
90
}