1 | <?php declare(strict_types=1); |
||
13 | |||
14 | /** |
||
15 | * Qualified name part |
||
16 | * |
||
17 | * For all models that have a name and namespace |
||
18 | * |
||
19 | * @author Thomas Gossmann |
||
20 | */ |
||
21 | trait QualifiedNamePart { |
||
22 | use NamePart; |
||
23 | |||
24 | /** @var string */ |
||
25 | private string $namespace = ''; |
||
|
|||
26 | 10 | ||
27 | 10 | /** |
|
28 | * Sets the namespace |
||
29 | 10 | * |
|
30 | * @param string $namespace |
||
31 | * |
||
32 | * @return $this |
||
33 | */ |
||
34 | public function setNamespace(string $namespace = ''): self { |
||
35 | $this->namespace = $namespace; |
||
36 | |||
37 | return $this; |
||
38 | } |
||
39 | 65 | ||
40 | 65 | /** |
|
41 | 45 | * In contrast to setName(), this method accepts the fully qualified name |
|
42 | * including the namespace. |
||
43 | * |
||
44 | 26 | * @param string $fullName |
|
45 | 6 | * |
|
46 | 6 | * @return $this |
|
47 | */ |
||
48 | 6 | public function setQualifiedName(string $fullName = ''): self { |
|
49 | $fullName = new Text($fullName); |
||
50 | |||
51 | 22 | if ($fullName->isEmpty()) { |
|
52 | 22 | return $this; |
|
53 | } |
||
54 | 22 | ||
55 | $this->name = $fullName->substring((int) $fullName->lastIndexOf('\\'))->trimStart('\\')->toString(); |
||
56 | $this->namespace = $fullName->trimEnd($this->name)->trim('\\')->toString(); |
||
57 | |||
58 | return $this; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | 19 | * Returns the namespace |
|
63 | 19 | * |
|
64 | * @return string |
||
65 | */ |
||
66 | public function getNamespace(): string { |
||
67 | return $this->namespace; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | 3 | * Returns the qualified name |
|
72 | 3 | * |
|
73 | 3 | * @return string |
|
74 | */ |
||
75 | public function getQualifiedName(): string { |
||
76 | 1 | if ($this->namespace) { |
|
77 | return $this->namespace . '\\' . $this->name; |
||
78 | } |
||
79 | |||
83 |