Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
50.00% |
2 / 4 |
CRAP | |
81.82% |
9 / 11 |
Enum | |
0.00% |
0 / 1 |
|
50.00% |
2 / 4 |
5.15 | |
81.82% |
9 / 11 |
__construct | |
0.00% |
0 / 1 |
2.02 | |
83.33% |
5 / 6 |
|||
__callStatic | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
getValue | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
__toString | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 1 |
<?php | |
namespace Puyo\DataStructure; | |
/** | |
* Enumlator | |
*/ | |
abstract class Enum | |
{ | |
private $scalar; | |
public function __construct($value) { | |
$ref = new \ReflectionObject($this); | |
$constans = $ref->getConstants(); | |
if(!in_array($value, $constans, true)) { | |
throw new \InvalidArgumentException(); | |
} | |
$this->scalar = $value; | |
} | |
/** | |
* $className::constName() でインスタンスを作れるように | |
* @param string $label | |
* @param array $args | |
* @return self | |
*/ | |
final public static function __callStatic($label, $args) { | |
$className = get_called_class(); | |
$const = constant("${className}::${label}"); | |
return new $className($const); | |
} | |
final public function getValue() { | |
return $this->scalar; | |
} | |
final public function __toString() { | |
return (string) $this->scalar; | |
} | |
} |