Completed
Pull Request — master (#255)
by Anton
05:56
created

Row::beforeInsert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 9.4285
ccs 6
cts 6
cp 1
crap 1
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link https://github.com/bluzphp/skeleton
5
 */
6
7
/**
8
 * @namespace
9
 */
10
namespace Application\Options;
11
12
use Bluz\Validator\Traits\Validator;
13
use Bluz\Validator\Validator as v;
14
15
/**
16
 * Options Row
17
 *
18
 * @property string $namespace
19
 * @property string $key
20
 * @property string $value
21
 * @property string $description
22
 * @property string $created
23
 * @property string $updated
24
 *
25
 * @category Application
26
 * @package  Options
27
 */
28
class Row extends \Bluz\Db\Row
29
{
30
    use Validator;
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 2
    protected function afterRead()
36
    {
37 2
        $this->value = unserialize($this->value);
38 2
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 1
    protected function beforeSave()
44
    {
45 1
        $this->value = serialize($this->value);
46
47 1
        $this->addValidator(
48 1
            'namespace',
49 1
            v::required()->slug()
50
        );
51 1
        $this->addValidator(
52 1
            'key',
53 1
            v::required()->slug()
54
        );
55 1
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 1
    protected function beforeInsert()
61
    {
62
        // unique validator
63 1
        $this->addValidator(
64 1
            'key',
65 1
            v::callback(function () {
66
                return !$this->getTable()->findRowWhere(['key' => $this->key, 'namespace' => $this->namespace]);
67 1
            })->setError('Key name "{{input}}" already exists')
68
        );
69 1
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    protected function beforeUpdate()
75
    {
76
        $this->updated = gmdate('Y-m-d H:i:s');
77
    }
78
}
79