mirror of
https://github.com/parsecsv/parsecsv-for-php.git
synced 2026-02-19 08:36:39 +00:00
Merge remote-tracking branch 'itexia/refactoring-constants'
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.
|
||||
*/
|
||||
class DatatypeEnum {
|
||||
class DatatypeEnum extends AbstractEnum {
|
||||
|
||||
const __DEFAULT = self::TYPE_STRING;
|
||||
|
||||
|
||||
28
src/enums/SortEnum.php
Normal file
28
src/enums/SortEnum.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
namespace ParseCsv\enums;
|
||||
|
||||
|
||||
class SortEnum extends AbstractEnum {
|
||||
const __DEFAULT = self::SORT_TYPE_REGULAR;
|
||||
|
||||
const SORT_TYPE_REGULAR = 'regular';
|
||||
|
||||
const SORT_TYPE_NUMERIC = 'numeric';
|
||||
|
||||
const SORT_TYPE_STRING = 'string';
|
||||
|
||||
private static $sorting = array(
|
||||
self::SORT_TYPE_REGULAR => SORT_REGULAR,
|
||||
self::SORT_TYPE_STRING => SORT_STRING,
|
||||
self::SORT_TYPE_NUMERIC => SORT_NUMERIC
|
||||
);
|
||||
|
||||
public static function getSorting($type){
|
||||
if (array_key_exists($type, self::$sorting)){
|
||||
return self::$sorting[$type];
|
||||
}
|
||||
|
||||
return self::$sorting[self::__DEFAULT];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user