Completed
Push — master ( 8a83ac...f28038 )
by Gabriel
04:28
created

SerializableRecord::serialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Nip\Records\Traits\Serializable;
4
5
/**
6
 * Trait SerializableRecord
7
 * @package Nip\Records\Traits\Serializable
8
 */
9
trait SerializableRecord
10
{
11
    /**
12
     * @return string
13
     */
14 1
    public function serialize()
15
    {
16 1
        $properties = $this->__sleep();
17 1
        $data = [];
18 1
        foreach ($properties as $property) {
19 1
            $data[$property] = $this->{$property};
20
        }
21 1
        return serialize($data);
22
    }
23
24
    /**
25
     * @param $data
26
     */
27 1
    public function unserialize($data)
28
    {
29 1
        $data = unserialize($data);
30 1
        if (!is_array($data)) {
31
            return;
32
        }
33 1
        foreach ($data as $property => $value) {
34 1
            $this->{$property} = $value;
35
        }
36 1
    }
37
38
    /**
39
     * @return array
40
     */
41 1
    public function __sleep()
42
    {
43 1
        return ['_data'];
44
    }
45
46
    public function __wakeup()
47
    {
48
    }
49
}