<?php
abstract class WorkUnitDataType {
//==============================================
// consts / vars
//==============================================
const NoneDataType = 0;
const BoolDataType = 1;
const StringDataType = 2;
const ArrayDataType = 3;
const JsonDataType = 4;
const UrlDataType = 5;
const FileDataType = 6;
private $value;
private $name;
//==============================================
// construct
//==============================================
protected function __construct() {
$this->setByName(get_class($this));
}
protected function setByValue($value) {
if (is_int($value)) {
$this->value = $value;
$this->name = WorkUnitDataType::getNameFromConst($value);
}
}
protected function setByName($name) {
if (is_string($name)) {
$this->value =
$this->name = $name;
}
}
//==============================================
// accessing
//==============================================
public function toString() {
return $this->name;
}
public function toValue() {
return $this->value;
}
//==============================================
// convenience
//==============================================
public static function None() { return new NoneDataType();}
public static function Bool() { return new BoolDataType();}
public static function String() { return new StringDataType();}
public static function Array() { return new ArrayDataType();}
public static function Json() { return new JsonDataType();}
public static function Url() { return new UrlDataType();}
public static function File() { return new FileDataType();}
//==============================================
// utility
//==============================================
public static function getNameFromConst($const) {
$class = new ReflectionClass('WorkUnitDataType');
$constants = $class->getConstants();
foreach ($constants as $name => $value) {
if ($value == $const) {
return $name;
}
}
return Null;
}
public static function getValueFromName($text) {
$class = new ReflectionClass('WorkUnitDataType');
$constants = $class->getConstants();
foreach ($constants as $name => $value) {
if ($name == $text) {
return $value;
}
}
return -1;
}
}
class NoneDataType extends WorkUnitDataType {}
class BoolDataType extends WorkUnitDataType {}
class StringDataType extends WorkUnitDataType {}
class ArrayDataType extends WorkUnitDataType {}
class JsonDataType extends WorkUnitDataType {}
class UrlDataType extends WorkUnitDataType {}
class FileDataType extends WorkUnitDataType {}
$a = WorkUnitDataType::None();
var_dump($a);
- Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- object(NoneDataType)#1 (2) {
["value":"WorkUnitDataType":private]=>
string(12) "NoneDataType"
["name":"WorkUnitDataType":private]=>
string(12) "NoneDataType"
}
- Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.28
- Parse error: syntax error, unexpected 'Array' (T_ARRAY), expecting identifier (T_STRING) in /in/Cc20d on line 63
Process exited with code 255.
preferences:
188.05 ms | 402 KiB | 240 Q