Enum.php
104 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Copyright (c) Microsoft Corporation. All Rights Reserved. |
| 4 | * Licensed under the MIT License. See License in the project root |
| 5 | * for license information. |
| 6 | * |
| 7 | * Enum File |
| 8 | * PHP version 7 |
| 9 | * |
| 10 | * @category Library |
| 11 | * @package Microsoft.Graph |
| 12 | * @copyright 2016 Microsoft Corporation |
| 13 | * @license https://opensource.org/licenses/MIT MIT License |
| 14 | * @version GIT: 0.1.0 |
| 15 | * @link https://graph.microsoft.io/ |
| 16 | */ |
| 17 | namespace Microsoft\Graph\Core; |
| 18 | |
| 19 | use Microsoft\Graph\Exception\GraphException; |
| 20 | |
| 21 | /** |
| 22 | * Class Enum |
| 23 | * |
| 24 | * @category Library |
| 25 | * @package Microsoft.Graph |
| 26 | * @license https://opensource.org/licenses/MIT MIT License |
| 27 | * @link https://graph.microsoft.io/ |
| 28 | */ |
| 29 | abstract class Enum |
| 30 | { |
| 31 | private static $constants = []; |
| 32 | /** |
| 33 | * The value of the enum |
| 34 | * |
| 35 | * @var string |
| 36 | */ |
| 37 | private $_value; |
| 38 | |
| 39 | /** |
| 40 | * Create a new enum |
| 41 | * |
| 42 | * @param string $value The value of the enum |
| 43 | * |
| 44 | * @throws GraphException if enum value is invalid |
| 45 | */ |
| 46 | public function __construct($value) |
| 47 | { |
| 48 | if (!self::has($value)) { |
| 49 | throw new GraphException("Invalid enum value $value"); |
| 50 | } |
| 51 | $this->_value = $value; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Check if the enum has the given value |
| 56 | * |
| 57 | * @param string $value |
| 58 | * @return bool the enum has the value |
| 59 | */ |
| 60 | public function has($value) |
| 61 | { |
| 62 | return in_array($value, self::toArray(), true); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Check if the enum is defined |
| 67 | * |
| 68 | * @param string $value the value of the enum |
| 69 | * |
| 70 | * @return bool True if the value is defined |
| 71 | */ |
| 72 | public function is($value) |
| 73 | { |
| 74 | return $this->_value === $value; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Create a new class for the enum in question |
| 79 | * |
| 80 | * @return mixed |
| 81 | * @throws \ReflectionException |
| 82 | */ |
| 83 | public function toArray() |
| 84 | { |
| 85 | $class = get_called_class(); |
| 86 | |
| 87 | if (!(array_key_exists($class, self::$constants))) |
| 88 | { |
| 89 | $reflectionObj = new \ReflectionClass($class); |
| 90 | self::$constants[$class] = $reflectionObj->getConstants(); |
| 91 | } |
| 92 | return self::$constants[$class]; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get the value of the enum |
| 97 | * |
| 98 | * @return string value of the enum |
| 99 | */ |
| 100 | public function value() |
| 101 | { |
| 102 | return $this->_value; |
| 103 | } |
| 104 | } |