Completed
Push — master ( 72342f...ed922a )
by Nikola
06:37
created

ClassMetadata::getAst()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/*
3
 * This file is part of the Abstract builder package, an RunOpenCode project.
4
 *
5
 * (c) 2017 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\AbstractBuilder\Ast;
11
12
use RunOpenCode\AbstractBuilder\Exception\InvalidArgumentException;
13
14
/**
15
 * Class ClassMetadata
16
 *
17
 * @package RunOpenCode\AbstractBuilder\Ast
18
 */
19
class ClassMetadata
20
{
21
    /**
22
     * @var array
23
     */
24
    private $ast;
25
26
    /**
27
     * @var string
28
     */
29
    private $namespace;
30
31
    /**
32
     * @var string
33
     */
34
    private $class;
35
36
    /**
37
     * @var string
38
     */
39
    private $fqcn;
40
41
    /**
42
     * @var string
43
     */
44
    private $filename;
45
46
    /**
47
     * @var bool
48
     */
49
    private $final;
50
51
    /**
52
     * @var bool
53
     */
54
    private $abstract;
55
56
    /**
57
     * ClassMetadata constructor.
58
     *
59
     * @param array $ast
60
     * @param string $namespace
61
     * @param string $class
62
     * @param null|string $filename
63
     *
64
     * @throws \RunOpenCode\AbstractBuilder\Exception\InvalidArgumentException
65
     */
66
    public function __construct(array $ast, $namespace, $class, $filename = null, $final = false, $abstract = false)
67
    {
68
        $this->ast = $ast;
69
        $this->namespace = trim($namespace, '\\');
70
        $this->class = trim($class, '\\');
71
        $this->filename = $filename;
72
        $this->final = $final;
73
        $this->abstract = $abstract;
74
75
        $this->fqcn = '\\';
76
77
        if ($this->namespace) {
78
            $this->fqcn .= $this->namespace.'\\';
79
        }
80
81
        foreach (explode('\\', ltrim($this->fqcn, '\\')) as $part) {
82
83
            if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $part)) {
84
                throw new InvalidArgumentException(sprintf('Provided full qualified class name "%s" is not valid PHP class name.', $this->fqcn));
85
            }
86
        }
87
    }
88
89
    /**
90
     * @return array
91
     */
92
    public function getAst()
93
    {
94
        return $this->ast;
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    public function getNamespace()
101
    {
102
        return $this->namespace;
103
    }
104
105
    /**
106
     * @return string
107
     */
108
    public function getClass()
109
    {
110
        return $this->class;
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    public function getFqcn()
117
    {
118
        return $this->fqcn;
119
    }
120
121
    /**
122
     * @return string
123
     */
124
    public function getFilename()
125
    {
126
        return $this->filename;
127
    }
128
129
    /**
130
     * @return bool
131
     */
132
    public function isFinal()
133
    {
134
        return $this->final;
135
    }
136
137
    /**
138
     * @return bool
139
     */
140
    public function isAbstract()
141
    {
142
        return $this->abstract;
143
    }
144
145
    /**
146
     * @return bool
147
     */
148
    public function isDefined()
149
    {
150
        return class_exists($this->getFqcn(), true);
151
    }
152
153
    /**
154
     * Initialize new, non-existing class.
155
     *
156
     * @param string $fqcn
157
     *
158
     * @return ClassMetadata|static $this
159
     */
160
    public static function create($fqcn)
161
    {
162
        $parts = explode('\\', trim($fqcn, '\\'));
163
        $class = array_pop($parts);
164
        $namespace = implode('\\', $parts);
165
166
        return new static([], $namespace, $class);
167
    }
168
169
    /**
170
     * Clones original metadata object, with possible values overwrite
171
     *
172
     * @param ClassMetadata $original
173
     * @param array $overwrite
174
     *
175
     * @return ClassMetadata|static $this
176
     */
177
    public static function clone(ClassMetadata $original, array $overwrite = [])
178
    {
179
        $data = [
180
            'ast' => $original->getAst(),
181
            'namespace' => $original->getNamespace(),
182
            'class' => $original->getClass(),
183
            'filename' => $original->getFilename()
184
        ];
185
186
        $data = array_merge($data, $overwrite);
187
188
        return (new \ReflectionClass(static::class))->newInstanceArgs(array_values($data));
189
    }
190
}
191