Completed
Push — master ( dfda1d...2f160c )
by Lars
04:17 queued 18s
created

SerializerIgbinary::setUnserializeOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\cache;
6
7
/**
8
 * SerializerIgbinary: serialize / unserialize
9
 */
10
class SerializerIgbinary implements iSerializer
11
{
12
    /**
13
     * @var bool
14
     */
15
    public static $_exists_igbinary;
16
17
    /**
18
     * @var array
19
     */
20
    private $unserialize_options;
21
22
    /**
23
     * SerializerIgbinary constructor.
24
     */
25 57
    public function __construct()
26
    {
27 57
        if (self::$_exists_igbinary === null) {
28 1
            self::$_exists_igbinary = (
29 1
                \function_exists('igbinary_serialize')
30
                &&
31 1
                \function_exists('igbinary_unserialize')
32
            );
33
        }
34 57
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 27
    public function serialize($value)
40
    {
41 27
        if (self::$_exists_igbinary === true) {
42
            /** @noinspection PhpUndefinedFunctionInspection */
43
            /** @noinspection PhpComposerExtensionStubsInspection */
44
            return \igbinary_serialize($value);
45
        }
46
47
        // fallback
48 27
        return \serialize($value);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 18
    public function unserialize($value)
55
    {
56 18
        if (self::$_exists_igbinary === true) {
57
            /** @noinspection PhpUndefinedFunctionInspection */
58
            /** @noinspection PhpComposerExtensionStubsInspection */
59
            return \igbinary_unserialize($value);
60
        }
61
62
        // fallback
63 18
        return \unserialize($value, $this->unserialize_options);
64
    }
65
66
    /**
67
     * @param array $options
68
     */
69 39
    public function setUnserializeOptions(array $options)
70
    {
71 39
        $this->unserialize_options = $options;
72 39
    }
73
}
74