BasicEnumAbstract.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom;
  3. abstract class BasicEnumAbstract {
  4. private static $constCacheArray = NULL;
  5. private static function getConstants() {
  6. if (self::$constCacheArray == NULL) {
  7. self::$constCacheArray = array();
  8. }
  9. $calledClass = get_called_class();
  10. if (!array_key_exists($calledClass, self::$constCacheArray)) {
  11. $reflect = new \ReflectionClass($calledClass);
  12. self::$constCacheArray[$calledClass] = $reflect->getConstants();
  13. }
  14. return self::$constCacheArray[$calledClass];
  15. }
  16. public static function isValidName($name, $strict = false) {
  17. $constants = self::getConstants();
  18. if ($strict) {
  19. return array_key_exists($name, $constants);
  20. }
  21. $keys = array_map('strtolower', array_keys($constants));
  22. return in_array(strtolower($name), $keys);
  23. }
  24. public static function isValidValue($value) {
  25. $values = array_values(self::getConstants());
  26. return in_array($value, $values, $strict = false); //strict true?
  27. }
  28. }