|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Utility\Traits; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class NameWorksTrait |
|
7
|
|
|
* @package Nip\Utility\Traits |
|
8
|
|
|
*/ |
|
9
|
|
|
trait NameWorksTrait |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var null|boolean |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $className = null; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var null|array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $classNameParts = null; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var null|boolean |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $classFirstName = null; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var null|string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $namespacePath = null; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var null|boolean |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $isNamespaced = null; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
3 |
|
public function getClassName() |
|
41
|
|
|
{ |
|
42
|
3 |
|
if ($this->className === null) { |
|
43
|
3 |
|
$this->setClassName($this->generateClassName()); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
3 |
|
return $this->className; |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param bool|null $className |
|
51
|
|
|
*/ |
|
52
|
3 |
|
public function setClassName($className) |
|
53
|
|
|
{ |
|
54
|
3 |
|
$this->className = $className; |
|
55
|
3 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
3 |
|
protected function generateClassName() |
|
61
|
|
|
{ |
|
62
|
3 |
|
return get_class($this); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return mixed|null |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function getNamespaceParentFolder() |
|
69
|
|
|
{ |
|
70
|
1 |
|
if (!$this->isNamespaced()) { |
|
71
|
|
|
return null; |
|
72
|
|
|
} |
|
73
|
1 |
|
$parts = $this->getClassNameParts(); |
|
74
|
1 |
|
array_pop($parts); |
|
75
|
1 |
|
return end($parts); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
2 |
|
public function isNamespaced() |
|
82
|
|
|
{ |
|
83
|
2 |
|
if ($this->isNamespaced === null) { |
|
84
|
2 |
|
$class = $this->getClassName(); |
|
85
|
|
|
|
|
86
|
2 |
|
$this->isNamespaced = strpos($class, '\\') !== false; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
2 |
|
return $this->isNamespaced; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return array|null |
|
94
|
|
|
*/ |
|
95
|
1 |
|
public function getClassNameParts() |
|
96
|
|
|
{ |
|
97
|
1 |
|
if ($this->classNameParts === null) { |
|
98
|
1 |
|
$this->initClassNameParts(); |
|
99
|
|
|
} |
|
100
|
1 |
|
return $this->classNameParts; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param array|null $classNameParts |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public function setClassNameParts($classNameParts) |
|
107
|
|
|
{ |
|
108
|
1 |
|
$this->classNameParts = $classNameParts; |
|
109
|
1 |
|
} |
|
110
|
|
|
|
|
111
|
1 |
|
protected function initClassNameParts() |
|
112
|
|
|
{ |
|
113
|
1 |
|
$class = $this->getClassName(); |
|
114
|
1 |
|
$parts = explode('\\', $class); |
|
115
|
1 |
|
$this->setClassNameParts($parts); |
|
116
|
1 |
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return null|string |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getNamespacePath() |
|
122
|
|
|
{ |
|
123
|
|
|
if ($this->namespacePath === null) { |
|
124
|
|
|
$this->initNamespacePath(); |
|
125
|
|
|
} |
|
126
|
|
|
return $this->namespacePath; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
protected function initNamespacePath() |
|
130
|
|
|
{ |
|
131
|
|
|
$this->namespacePath = ''; |
|
132
|
|
|
if ($this->isNamespaced()) { |
|
133
|
|
|
$parts = $this->getClassNameParts(); |
|
134
|
|
|
array_pop($parts); |
|
135
|
|
|
$this->namespacePath = implode('\\', $parts); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return bool|null |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getClassFirstName() |
|
143
|
|
|
{ |
|
144
|
|
|
if ($this->classFirstName === null) { |
|
145
|
|
|
$this->initClassFirstName(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $this->classFirstName; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
protected function initClassFirstName() |
|
152
|
|
|
{ |
|
153
|
|
|
$parts = $this->getClassNameParts(); |
|
154
|
|
|
$this->classFirstName = array_pop($parts); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|