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