Completed
Pull Request — master (#2)
by Pol
10:23
created

AbstractExceptionObject::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
nc 1
cc 1
nop 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace drupol\valuewrapper\Object\Exception;
6
7
use drupol\valuewrapper\Object\ObjectValue;
8
9
/**
10
 * Class AbstractExceptionObject
11
 */
12
abstract class AbstractExceptionObject extends ObjectValue
13
{
14
    /**
15
     * Throwable constructor.
16
     *
17
     * @param \Throwable $value
18
     */
19
    public function __construct(\Throwable $value)
20
    {
21
        parent::__construct($value);
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function hash(): string
28
    {
29
        /** @var \TypeError $value */
30
        $value = $this->get();
31
32
        $data = [
33
            'message' => $value->getMessage(),
34
            'code' => $value->getCode(),
35
            'class' => $this->class(),
36
        ];
37
38
        return $this->doHash(implode('', $data));
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function serialize()
45
    {
46
        return \serialize([
47
            'value' => [
48
                'message' => $this->get()->getMessage(),
49
                'code' => $this->get()->getCode(),
50
                'class' => $this->class(),
51
            ],
52
        ]);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function unserialize($serialized)
59
    {
60
        $unserialize = \unserialize($serialized);
61
62
        $reflection = new \ReflectionClass($unserialize['value']['class']);
63
64
        $this->set(
65
            ($reflection->newInstanceArgs([$unserialize['value']['message'], $unserialize['value']['code']]))
66
        );
67
    }
68
}
69