- array_unshift: documentation ( source)
- preg_match_all: documentation ( source)
<?php
declare(strict_types=1);
namespace fema\enums;
class DocEnum extends Enum {
private static $enums = null;
private function __construct() {
}
/**
* @inheritDoc
*/
public static function getAll() : array {
if (static::$enums === null) {
$last = new \ReflectionClass(static::class);
/** @var \ReflectionClass[] $classes */
$classes = [$last];
while ($last->getParentClass()->getName() !== self::class) {
//Put on top of array
array_unshift($classes, $last = new \ReflectionClass($last->getParentClass()->getName()));
}
$enums = [];
$ordinal = 0;
foreach ($classes as $class) {
$doc = $class->getDocComment();
$matches = [];
if (preg_match_all('/^\\s*\\*\\s*@method\\s+static\\s+([A-Z]+)\\(\\)\\s*$/m', $doc, $matches) > 0) {
foreach ($matches[1] as $name) {
$enum = new DocEnum();
$enum->name = $name;
$enum->ordinal = $ordinal++;
$enums[] = $enum;
}
}
}
static::$enums = $enums;
}
return static::$enums;
}
}