mirror of
https://github.com/parsecsv/parsecsv-for-php.git
synced 2026-02-19 08:36:39 +00:00
init implementation of abstract enum class
This commit is contained in:
40
src/enums/AbstractEnum.php
Normal file
40
src/enums/AbstractEnum.php
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
namespace ParseCsv\enums;
|
||||||
|
|
||||||
|
use ReflectionClass;
|
||||||
|
|
||||||
|
abstract class AbstractEnum {
|
||||||
|
/**
|
||||||
|
* Creates a new value of some type
|
||||||
|
*
|
||||||
|
* @param mixed $value
|
||||||
|
*
|
||||||
|
* @throws \UnexpectedValueException if incompatible type is given.
|
||||||
|
*/
|
||||||
|
public function __construct($value)
|
||||||
|
{
|
||||||
|
if (!$this->isValid($value)) {
|
||||||
|
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . get_called_class());
|
||||||
|
}
|
||||||
|
$this->value = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getConstants(){
|
||||||
|
$class = get_called_class();
|
||||||
|
$reflection = new \ReflectionClass($class);
|
||||||
|
|
||||||
|
return $reflection->getConstants();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if enum value is valid
|
||||||
|
*
|
||||||
|
* @param $value
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function isValid($value)
|
||||||
|
{
|
||||||
|
return in_array($value, static::getConstants(), true);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,7 +9,7 @@ namespace ParseCsv\enums;
|
|||||||
*
|
*
|
||||||
* todo: needs a basic parent enum class for error handling.
|
* todo: needs a basic parent enum class for error handling.
|
||||||
*/
|
*/
|
||||||
class DatatypeEnum {
|
class DatatypeEnum extends AbstractEnum {
|
||||||
|
|
||||||
const __DEFAULT = self::TYPE_STRING;
|
const __DEFAULT = self::TYPE_STRING;
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: sgorzaly
|
|
||||||
* Date: 22.02.18
|
|
||||||
* Time: 20:24
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace ParseCsv\enums;
|
namespace ParseCsv\enums;
|
||||||
|
|
||||||
|
|
||||||
class SortEnum {
|
class SortEnum extends AbstractEnum {
|
||||||
const SORT_TYPE_REGULAR = SORT_REGULAR;
|
const SORT_TYPE_REGULAR = SORT_REGULAR;
|
||||||
|
|
||||||
const SORT_TYPE_NUMERIC = SORT_NUMERIC;
|
const SORT_TYPE_NUMERIC = SORT_NUMERIC;
|
||||||
|
|||||||
Reference in New Issue
Block a user