Completed
Pull Request — develop (#127)
by
unknown
11:07
created

Serialize::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Noodlehaus\Parser;
4
5
use Noodlehaus\Exception\ParseException;
6
7
/**
8
 * Class Serialize
9
 *
10
 * @package Config
11
 */
12
class Serialize implements ParserInterface
13
{
14
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function parseFile($filename)
19
    {
20
        $data = file_get_contents($filename);
21
22
        return (array) $this->parse($data, $filename);
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function parseString($config)
29
    {
30
        return (array) $this->parse($config);
31
    }
32
33
34
    /**
35
     * Completes parsing of JSON data
36
     *
37
     * @param  string  $data
38
     * @param  string $filename
39
     * @return array|null
40
     *
41
     * @throws ParseException If there is an error parsing the serialized data
42
     */
43
    protected function parse($data = null, $filename = null)
44
    {
45
        $serializedData = @unserialize($data);
46
        if($serializedData === false){
47
48
            $error = [
49
                'message' => $php_errormsg,
50
                'type'    => 'unserialize error',
51
                'file'    => $filename,
52
            ];
53
54
            throw new ParseException($error);
55
        }
56
57
        return $serializedData;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public static function getSupportedExtensions()
64
    {
65
        return ['txt'];
66
    }
67
}
68