tmilos / value
Abstract value and enum objects
Installs: 965 011
Dependents: 3
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: >=5.6.0
Requires (Dev)
- phpunit/phpunit: ~4.8|~5.6
- satooshi/php-coveralls: ~1.0
This package is not auto-updated.
Last update: 2024-10-22 21:12:07 UTC
README
Abstract value and enum objects
Value objects
class IntValue extends AbstractValue { public static function isValid($value) { return is_int($value); } } $x = new IntValue(10); // ok print $x->getValue(); // 10 $y = new IntValue(10); var_dump($x->equal($y)); // true $z = new IntValue('20'); // throws \UnexpectedValueException
Enum value object
All class defined constants are valid values, and magic method with same constant name can be called to instantiate Enum with that value.
class Gender extends AbstractEnum { const MALE = 'male'; const FEMALE = 'female'; private static $titles = [ self::MALE => 'gender.male', self::FEMALE => 'gender.female', ]; public function getTitle() { return self::$titles[$this->getValue()]; } } var_dump(Gender::all()); // ['male' => Gender() => 'female' => Gender() ] var_dump(Gender::values()); // [ 0 => 'male', 1 => 'female' ] $m = Gender::MALE(); print $m->getValue(); // male print $m->getTitle(); // gender.male var_dump(Gender::isValid('male')); // true var_dump(Gender::isValid('something')); // false