From ba557d97373b4edab2014001395ea6c8a1c0e95e Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Fri, 14 Jun 2024 20:01:22 +0400 Subject: [PATCH] + `\DDTools\Base\Base::getClassName`: The new public static method. Gets data about a class name (see README). --- README.md | 32 +++++++++++++++++++ src/Base/Base.php | 81 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) diff --git a/README.md b/README.md index b96745f..2891ab8 100644 --- a/README.md +++ b/README.md @@ -886,6 +886,38 @@ It is convenient to inherit your classes from this. You can see an example of how it works in the [(MODX)EvolutionCMS.snippets.ddGetDocumentField](https://code.divandesign.ru/modx/ddgetdocumentfield) code. +#### `\DDTools\Base\Base::getClassName()` + +Gets data about a class name. + + +##### Returns + +* `$result` + * Description: Class name data. + * Valid values: `stdClass` + +* `$result->full` + * Description: Full class name including namespace, e. g.: `'\\ddSendFeedback\\Sender\\Email\\Sender'`. + * Valid values: `string` + +* `$result->nameShort` + * Description: Short class name, e. g.: `'Sender'`. + * Valid values: `string` + +* `$result->namespaceFull` + * Description: Namespace, e. g.: `'\\ddSendFeedback\\Sender\\Email'`. + * Valid values: `string` + +* `$result->namespaceShort` + * Description: Last namespace item, e. g.: `'Email'`. + * Valid values: `string` + +* `$result->namespacePrefix` + * Description: Namespace prefix, e. g.: `'\\ddSendFeedback\\Sender'`. + * Valid values: `string` + + #### `\DDTools\Base\Base::setExistingProps($props)` Sets existing object properties. diff --git a/src/Base/Base.php b/src/Base/Base.php index 8f60c05..df5b35b 100644 --- a/src/Base/Base.php +++ b/src/Base/Base.php @@ -2,6 +2,87 @@ namespace DDTools\Base; abstract class Base { + /** + * @var $ddClassNames {\stdClass} — Storage of all class names. + * @var $ddClassNames->{$className} {\stdClass} + * @var $ddClassNames->{$className}->full {string} — Full class name including namespace, e. g.: '\\ddSendFeedback\\Sender\\Email\\Sender'. + * @var $ddClassNames->{$className}->nameShort {string} — Short class name, e. g.: 'Sender'. + * @var $ddClassNames->{$className}->namespaceFull {string} — Namespace, e. g.: '\\ddSendFeedback\\Sender\\Email'. + * @var $ddClassNames->{$className}->namespaceShort {string} — Last namespace item, e. g.: 'Email'. + * @var $ddClassNames->{$className}->namespacePrefix {string} — Namespace prefix, e. g.: '\\ddSendFeedback\\Sender'. + */ + private static $ddClassNames = null; + + /** + * getClassName + * @version 1.0 (2024-06-14) + * + * @see README.md + * + * @return $result {stdClass} + */ + public static function getClassName(): \stdClass { + $full = get_called_class(); + + //Init + if (is_null(self::$ddClassNames)){ + self::$ddClassNames = new \stdClass(); + } + + //If not defined before for this child class + if ( + !property_exists( + self::$ddClassNames, + $full + ) + ){ + static::$ddClassNames->{$full} = (object) [ + 'full' => '', + 'nameShort' => '', + 'namespaceFull' => '', + 'namespaceShort' => '', + 'namespacePrefix' => '', + ]; + + static::$ddClassNames->{$full}->full = $full; + + $fullArray = explode( + '\\', + static::$ddClassNames->{$full}->full + ); + + static::$ddClassNames->{$full}->full = '\\' . static::$ddClassNames->{$full}->full; + //Extract short class name + static::$ddClassNames->{$full}->nameShort = array_pop($fullArray); + + //If namespace exists + if (count($fullArray) > 0){ + static::$ddClassNames->{$full}->namespaceFull = + '\\' + . implode( + '\\', + $fullArray + ) + ; + //Extract namespace + static::$ddClassNames->{$full}->namespaceShort = array_pop($fullArray); + + //If neamespace prefix exists + if (count($fullArray) > 0){ + static::$ddClassNames->{$full}->namespacePrefix = + '\\' + . implode( + '\\', + $fullArray + ) + ; + } + } + } + + return static::$ddClassNames->{$full}; + } + /** * setExistingProps * @version 1.4 (2022-01-08)