1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the Cache package. |
||
5 | * |
||
6 | * Copyright (c) Daniel González |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | * |
||
11 | * @author Daniel González <[email protected]> |
||
12 | * @author Arnold Daniels <[email protected]> |
||
13 | */ |
||
14 | |||
15 | declare(strict_types=1); |
||
16 | |||
17 | namespace Desarrolla2\Cache\Packer; |
||
18 | |||
19 | use Desarrolla2\Cache\Packer\PackerInterface; |
||
20 | use Desarrolla2\Cache\Exception\InvalidArgumentException; |
||
21 | |||
22 | /** |
||
23 | * Pack value through serialization |
||
24 | */ |
||
25 | class SerializePacker implements PackerInterface |
||
26 | { |
||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $options; |
||
31 | |||
32 | /** |
||
33 | * SerializePacker constructor |
||
34 | * |
||
35 | * @param array $options Any options to be provided to unserialize() |
||
36 | */ |
||
37 | 868 | public function __construct(array $options = ['allowed_classes' => true]) |
|
38 | { |
||
39 | 868 | $this->options = $options; |
|
40 | } |
||
41 | |||
42 | /** |
||
43 | * Get cache type (might be used as file extension) |
||
44 | * |
||
45 | * @return string |
||
46 | */ |
||
47 | 394 | public function getType() |
|
48 | { |
||
49 | 394 | return 'php.cache'; |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * Pack the value |
||
54 | * |
||
55 | * @param mixed $value |
||
56 | * @return string |
||
57 | */ |
||
58 | 634 | public function pack($value) |
|
59 | { |
||
60 | 634 | return serialize($value); |
|
61 | } |
||
62 | |||
63 | /** |
||
64 | * Unpack the value |
||
65 | * |
||
66 | * @param string $packed |
||
67 | * @return string |
||
68 | * @throws \UnexpectedValueException if he value can't be unpacked |
||
69 | */ |
||
70 | 292 | public function unpack($packed) |
|
71 | { |
||
72 | 292 | if (!is_string($packed)) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
73 | throw new InvalidArgumentException("packed value should be a string"); |
||
74 | } |
||
75 | |||
76 | 292 | return unserialize($packed, $this->options); |
|
77 | } |
||
78 | } |
||
79 |