Completed
Push — master ( 5bff48...0eeb04 )
by Oscar
02:44
created

Update::marks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace SimpleCrud\Queries\Mysql;
4
5
use SimpleCrud\Queries\BaseQuery;
6
use SimpleCrud\Queries\SelectionTrait;
7
use SimpleCrud\Entity;
8
use PDOStatement;
9
10
/**
11
 * Manages a database update query.
12
 */
13
class Update extends BaseQuery
14
{
15
    use SelectionTrait;
16
17
    protected $data = [];
18
19
    /**
20
     * Set the data to update.
21
     *
22
     * @param array $data
23
     *
24
     * @return self
25
     */
26
    public function data(array $data)
27
    {
28
        $this->data = $this->entity->prepareDataToDatabase($data, false);
29
30
        return $this;
31
    }
32
33
    /**
34
     * Adds new marks to the query.
35
     *
36
     * @param array $marks
37
     *
38
     * @return self
39
     */
40
    public function marks(array $marks)
41
    {
42
        $this->marks += $marks;
43
44
        return $this;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 View Code Duplication
    public function __invoke()
0 ignored issues
show
Duplication introduced by
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...
51
    {
52
        $marks = $this->marks;
53
54
        foreach ($this->data as $field => $value) {
55
            $marks[":__{$field}"] = $value;
56
        }
57
58
        return $this->entity->getDb()->execute((string) $this, $marks);
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function __toString()
65
    {
66
        $query = "UPDATE `{$this->entity->name}`";
67
        $query .= ' SET '.static::buildFields(array_keys($this->data));
68
69
        $query .= $this->whereToString();
70
        $query .= $this->limitToString();
71
72
        return $query;
73
    }
74
75
    /**
76
     * Generates the data part of a UPDATE query.
77
     *
78
     * @param array $fields
79
     *
80
     * @return string
81
     */
82 View Code Duplication
    protected static function buildFields(array $fields)
0 ignored issues
show
Duplication introduced by
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...
83
    {
84
        $query = [];
85
86
        foreach ($fields as $field) {
87
            $query[] = "`{$field}` = :__{$field}";
88
        }
89
90
        return implode(', ', $query);
91
    }
92
}
93