Skip to content

Commit

Permalink
+ \DDTools\Base\Base::getClassName: The new public static method. G…
Browse files Browse the repository at this point in the history
…ets data about a class name (see README).
  • Loading branch information
Ronef committed Jun 14, 2024
1 parent b9dcb0a commit ba557d9
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
81 changes: 81 additions & 0 deletions src/Base/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ba557d9

Please sign in to comment.