Issues (15)

src/structures/Structure.php (3 issues)

Labels
Severity
1
<?php
2
3
namespace vakata\asn1\structures;
4
5
use \vakata\asn1\Decoder;
0 ignored issues
show
The type \vakata\asn1\Decoder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use \vakata\asn1\LazyDecoder;
0 ignored issues
show
The type \vakata\asn1\LazyDecoder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use \vakata\asn1\Reader;
0 ignored issues
show
The type \vakata\asn1\Reader was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
abstract class Structure
10
{
11
    protected $reader;
12
13
    /**
14
     * Create an instance by passing in an instantiated reader.
15
     *
16
     * @param Reader $reader
17
     */
18
    public function __construct(Reader $reader)
19
    {
20
        $this->reader = $reader;
21
    }
22
    /**
23
     * Create an instance from a string.
24
     *
25
     * @param string $data
26
     * @return Structure
27
     */
28
    public static function fromString($data)
29
    {
30
        if (preg_match('(^[\-]+BEGIN.*?\n)i', $data)) {
31
            $data = base64_decode(preg_replace('(^[\-]+(BEGIN|END).*$)im', '', $data));
32
        }
33
        return new static(Reader::fromString($data));
34
    }
35
    /**
36
     * Create an instance from a file
37
     *
38
     * @param string $path the path to the file to parse
39
     * @return Structure
40
     */
41
    public static function fromFile($path)
42
    {
43
        $reader = Reader::fromFile($path);
44
        $data = $reader->bytes(1024);
45
        if (preg_match('(^[\-]+BEGIN.*?\n)i', $data)) {
46
            return static::fromString(
47
                base64_decode(preg_replace('(^[\-]+(BEGIN|END).*$)im', '', file_get_contents($path)))
48
            );
49
        }
50
        return new static(Reader::fromFile($path));
51
    }
52
    /**
53
     * Output the raw ASN1 structure of the data.
54
     *
55
     * @return array
56
     */
57
    public function structure(bool $lazy = false)
58
    {
59
        $this->reader->seek(0);
60
        $decoder = $lazy ? (new LazyDecoder($this->reader)) : (new Decoder($this->reader));
61
        return $decoder->structure();
62
    }
63
    /**
64
     * Get the mapped or values only view of the parsed data.
65
     *
66
     * @param boolean $valuesOnly should only values be returned or the map be used - defaults to `false` - use a map
67
     * @return mixed
68
     */
69
    public function toArray(bool $valuesOnly = false, bool $lazy = false)
70
    {
71
        $this->reader->seek(0);
72
        $decoder = $lazy ? (new LazyDecoder($this->reader)) : (new Decoder($this->reader));
73
        return $valuesOnly ? $decoder->values() : $decoder->map(static::map());
74
    }
75
    public function __toString()
76
    {
77
        $iterator = new \RecursiveIteratorIterator(
78
            new \RecursiveArrayIterator($this->toArray()),
79
            \RecursiveIteratorIterator::LEAVES_ONLY
80
        );
81
        $result = [];
82
        foreach ($iterator as $k => $v) {
83
            if (strlen($v)) {
84
                $result[] = str_repeat(' ', $iterator->getDepth()) . $v;
85
            }
86
        }
87
        return implode("\r\n", $result);
88
    }
89
    public function getReader()
90
    {
91
        return $this->reader;
92
    }
93
    abstract protected static function map();
94
}
95