-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathClazz.php
More file actions
69 lines (56 loc) · 1.71 KB
/
Copy pathClazz.php
File metadata and controls
69 lines (56 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
declare(strict_types=1);
namespace Mihaeu\TestGenerator;
use PhpParser\Node\Stmt\Class_;
class Clazz
{
/** @var string */
private $class;
/** @var string */
private $namespacedName;
/** @var string */
private $namespace;
public function __construct(string $class, string $namespacedName, string $namespace)
{
$this->class = $class;
$this->namespacedName = $namespacedName;
$this->namespace = $namespace;
}
public function clazz(): string
{
return $this->class;
}
public static function fromClassNode(Class_ $classNode) : Clazz
{
$namespaceParts = $classNode->namespacedName->parts;
$namespace = count($namespaceParts)
? implode('\\', array_slice($namespaceParts, 0, -1))
: '';
return new Clazz(
(string) $classNode->name,
implode('\\', $namespaceParts),
$namespace
);
}
public static function fromFullyQualifiedNameString(string $fqn) : Clazz
{
self::assertNameIsValidPhpIdentifier($fqn);
$parts = explode('\\', $fqn);
$namespace = implode('\\', array_slice($parts, 0, -1));
return new Clazz($parts[count($parts) - 1], $fqn, $namespace);
}
private static function assertNameIsValidPhpIdentifier(string $fqn): void
{
if (!preg_match('/^[a-zA-Z_\x7f-\xff][\\a-zA-Z0-9_\x7f-\xff]*$/', $fqn)) {
throw new InvalidFullyQualifiedNameException($fqn);
}
}
public function toArray() : array
{
return [
'class' => $this->class,
'namespacedName' => $this->namespacedName,
'namespace' => $this->namespace,
];
}
}