Completed
Push — master ( f49652...503636 )
by Anton
12s
created

Row::afterRead()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
ccs 3
cts 3
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 1
        );
51 1
        $this->addValidator(
52 1
            'key',
53 1
            v::required()->slug()
54 1
        );
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 1
        );
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