Functions
9 months ago
.htaccess
9 months ago
ConstantUtilityTrait.php
9 months ago
index.html
9 months ago
web.config
9 months ago
ConstantUtilityTrait.php
45 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace phpseclib3\Common; |
| 6 | |
| 7 | use phpseclib3\Exception\InvalidArgumentException; |
| 8 | |
| 9 | /** |
| 10 | * @internal |
| 11 | */ |
| 12 | trait ConstantUtilityTrait |
| 13 | { |
| 14 | /** @var string[]|null */ |
| 15 | private static $valueToConstantNameMap = null; |
| 16 | |
| 17 | /** |
| 18 | * @param string|int $value |
| 19 | */ |
| 20 | public static function findConstantNameByValue($value): ?string |
| 21 | { |
| 22 | if (!self::$valueToConstantNameMap) { |
| 23 | $reflectionClass = new \ReflectionClass(static::class); |
| 24 | $constantNameToValueMap = $reflectionClass->getConstants(); |
| 25 | self::$valueToConstantNameMap = array_flip($constantNameToValueMap); |
| 26 | } |
| 27 | if (isset(self::$valueToConstantNameMap[$value])) { |
| 28 | return self::$valueToConstantNameMap[$value]; |
| 29 | } |
| 30 | return null; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @param string|int $value |
| 35 | */ |
| 36 | public static function getConstantNameByValue($value): string |
| 37 | { |
| 38 | $constantName = static::findConstantNameByValue($value); |
| 39 | if ($constantName === null) { |
| 40 | throw new InvalidArgumentException(sprintf('"%s" does not have constant with value "%s".', static::class, $value)); |
| 41 | } |
| 42 | return $constantName; |
| 43 | } |
| 44 | } |
| 45 |