Completed
Pull Request — master (#143)
by Alexander
05:04
created

Record::setFields()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * Yandex PHP Library
4
 *
5
 * @copyright NIX Solutions Ltd.
6
 * @link      https://github.com/nixsolutions/yandex-php-library
7
 */
8
9
/**
10
 * @namespace
11
 */
12
namespace Yandex\DataSync\Models\Database\Delta;
13
14
use Yandex\Common\Model;
15
use Yandex\DataSync\Models\Database\Delta\RecordFields;
16
17
/**
18
 * Class Record
19
 *
20
 * @category Yandex
21
 * @package  DataSync
22
 *
23
 * @author   Alexander Khaylo <[email protected]>
24
 * @created  02.03.16 11:35
25
 */
26
class Record extends Model
27
{
28
    protected $propNameMap = [
29
        'change_type'   => 'changeType',
30
        'collection_id' => 'collectionId',
31
        'record_id'     => 'recordId'
32
    ];
33
34
    protected $mappingClasses = [
35
        'changes' => 'Yandex\DataSync\Models\Database\Delta\RecordFields',
36
        'fields'  => 'Yandex\DataSync\Models\Database\Delta\RecordFields',
37
    ];
38
39
    /**
40
     * Adding a new record.
41
     * (only Database)
42
     */
43
    const CHANGE_TYPE_INSERT = 'insert';
44
    /**
45
     * Partial recording the change (change only the specified field, all existing fields of a record are saved).
46
     * (only Database)
47
     */
48
    const CHANGE_TYPE_UPDATE = 'update';
49
    /**
50
     * Complete change of recording (all existing fields are deleted).
51
     * Adding a new field or change existing values.
52
     * (Database & fields)
53
     */
54
    const CHANGE_TYPE_SET = 'set';
55
    /**
56
     * Deleting records or Deleting field.
57
     * (Database & fields)
58
     */
59
    const CHANGE_TYPE_DELETE = 'delete';
60
61
    /**
62
     * @var string
63
     */
64
    protected $changeType;
65
66
    /**
67
     * @var string
68
     */
69
    protected $collectionId;
70
71
    /**
72
     * @var string
73
     */
74
    protected $recordId;
75
76
    /**
77
     * @var RecordFields
78
     */
79
    protected $changes;
80
81
    /**
82
     * @var RecordFields
83
     */
84
    protected $fields;
85
86
    /**
87
     * The revision number after the latest changes to the current record.
88
     * @var string
89
     */
90
    protected $revision;
91
92
    /**
93
     * @return string
94
     */
95 1
    public function getChangeType()
96
    {
97 1
        return $this->changeType;
98
    }
99
100
    /**
101
     * @param string $changeType
102
     *
103
     * @return $this
104
     */
105 4
    public function setChangeType($changeType)
106
    {
107 4
        $this->changeType = $changeType;
108 4
        return $this;
109
    }
110
111
    /**
112
     * @return string
113
     */
114 2
    public function getCollectionId()
115
    {
116 2
        return $this->collectionId;
117
    }
118
119
    /**
120
     * @param string $collectionId
121
     *
122
     * @return $this
123
     */
124 4
    public function setCollectionId($collectionId)
125
    {
126 4
        $this->collectionId = $collectionId;
127 4
        return $this;
128
    }
129
130
    /**
131
     * @return RecordFields
132
     */
133 1
    public function getChanges()
134
    {
135 1
        return $this->changes;
136
    }
137
138
    /**
139
     * @param RecordFields|array $changes
140
     *
141
     * @return $this
142
     */
143 4
    public function setChanges($changes)
144
    {
145 4
        if ($changes instanceof RecordFields) {
146 1
            $this->changes = $changes;
147 4
        } elseif (is_array($changes)) {
148 4
            $recordFields = new RecordFields();
149 4
            foreach ($changes as $change) {
150 4
                $recordFields->add($change);
151 4
            }
152 4
            $this->changes = $recordFields;
153 4
        }
154 4
        return $this;
155
    }
156
157
    /**
158
     * @return RecordFields
159
     */
160 2
    public function getFields()
161
    {
162 2
        return $this->fields;
163
    }
164
165
    /**
166
     * @param RecordFields|array $fields
167
     *
168
     * @return $this
169
     */
170 2
    public function setFields($fields)
171
    {
172 2
        if ($fields instanceof RecordFields) {
173 1
            $this->fields = $fields;
174 1
        } else {
175 2
            if (is_array($fields)) {
176 2
                $fields       = new RecordFields($fields);
177 2
                $this->fields = $fields;
178 2
            }
179
        }
180 2
        return $this;
181
    }
182
183
    /**
184
     * @return string
185
     */
186 2
    public function getRecordId()
187
    {
188 2
        return $this->recordId;
189
    }
190
191
    /**
192
     * @param $recordId
193
     *
194
     * @return $this
195
     */
196 4
    public function setRecordId($recordId)
197
    {
198 4
        $this->recordId = $recordId;
199 4
        return $this;
200
    }
201
202
    /**
203
     * @return string
204
     */
205 1
    public function getRevision()
206
    {
207 1
        return $this->revision;
208
    }
209
210
    /**
211
     * @param string $revision
212
     */
213 1
    public function setRevision($revision)
214
    {
215 1
        $this->revision = $revision;
216 1
    }
217
}
218