Browse Source

NEW: added new abstract class System::Enum

Giancarlo 7 years ago
parent
commit
cccf748298
2 changed files with 56 additions and 2 deletions
  1. 2 2
      package.json
  2. 54 0
      src/OpenMind/System/System.php

+ 2 - 2
package.json

@@ -1,6 +1,6 @@
 {
-	"name": "OM-Link",
-	"version": "1.9.31",
+	"name": "omlink",
+	"version": "1.9.32",
 	"codename": "Barricade",
 	"private": true,
 	"description": "OM-Link Engine",

+ 54 - 0
src/OpenMind/System/System.php

@@ -12,6 +12,60 @@ use Cache;
 use DB\SQL;
 use PDO;
 use PDOException;
+use ReflectionClass;
+
+/**
+ * Class Enum
+ * ---
+ * Implement basic ENUMERATORS, support auto numbering of integer default number and custom start number
+ *
+ * It's possible to get enum name by add suffix "ToString" to enum name:<br/>
+ * >$enum->START<br/>
+ * $enum->STARTToString (return 'START')<br/>
+ *
+ * @package OpenMind\System
+ */
+abstract class Enum
+{
+	public function __construct()
+	{
+		$counter = 0;
+		foreach((new ReflectionClass($this))->getProperties() as $property) {
+			$propertyValue = $property->getValue($this);
+
+			switch (gettype($propertyValue)) {
+				case 'integer':
+					$counter = $propertyValue + 1;
+					break;
+				case 'NULL':
+					$propertyValue = $counter++;
+					break;
+			}
+			$property->setValue($this, $propertyValue);
+			$this->{"{$property->name}ToString"} = $property->name;
+		}
+	}
+
+	/**
+	 * Get the variable name as string by its value
+	 *
+	 * @param $value
+	 * @return string|null the variable name
+	 */
+	public function getNameByValue($value): ?string
+	{
+		try {
+			foreach((new ReflectionClass($this))->getProperties() as $property) {
+				if ($property->getValue($this) === $value) {
+					return $property->name;
+				}
+			}
+		} catch (\ReflectionException $e) {
+			return $e->getMessage();
+		}
+		return null;
+	}
+}
 
 class System extends PluginStrategy
 {