bingo-soft /
jabe
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Jabe\Authorization; |
||
| 4 | |||
| 5 | trait PermissionTrait |
||
| 6 | { |
||
| 7 | private $name; |
||
| 8 | private $id; |
||
| 9 | |||
| 10 | public function getName(): string |
||
| 11 | { |
||
| 12 | return $this->name; |
||
| 13 | } |
||
| 14 | |||
| 15 | public function getValue(): int |
||
| 16 | { |
||
| 17 | return $this->id; |
||
| 18 | } |
||
| 19 | |||
| 20 | public static function forName(string $name): PermissionInterface |
||
| 21 | { |
||
| 22 | $names = explode("_", strtolower($name)); |
||
| 23 | $func = $names[0]; |
||
| 24 | if (count($names) > 1) { |
||
| 25 | for ($i = 1; $i < count($names); $i += 1) { |
||
|
0 ignored issues
–
show
|
|||
| 26 | $func .= ucfirst($names[$i]); |
||
| 27 | } |
||
| 28 | } |
||
| 29 | |||
| 30 | return self::$func(); |
||
| 31 | } |
||
| 32 | |||
| 33 | public static function values(): array |
||
| 34 | { |
||
| 35 | $reflection = new \ReflectionClass(__CLASS__); |
||
| 36 | $methods = $reflection->getMethods(\ReflectionMethod::IS_STATIC); |
||
| 37 | $arr = []; |
||
| 38 | |||
| 39 | foreach ($methods as $method) { |
||
| 40 | if ($method->getName() !== "values" && $method->getName() !== "forName" && $method->getName() !== "resources") { |
||
| 41 | $arr[] = $method->invoke(null); |
||
| 42 | } |
||
| 43 | } |
||
| 44 | return $arr; |
||
| 45 | } |
||
| 46 | } |
||
| 47 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: