Enum component enables developers to define strict enumerated types based on standard PHP classes. Usage of this component is a little bit verbose but it allows you to get rid of constant validation of every enum field in your entities. This implementation is pretty much similar to all those you can find out there (including SplEnum) but pretends to be more accurate.
This is a fork from startuplabs/enum
which is not maintained anymore.
<?php
namespace Acme\Example;
use Pulyaevskiy\Enum;
/**
* @method static Shape triangle()
* @method static Shape square()
* @method static Shape pentagon()
* @method static Shape hexagon()
*/
class Shape extends Enum
{
const TRIANGLE = 'triangle';
const SQUARE = 'square';
const PENTAGON = 'pentagon';
const HEXAGON = 'hexagon';
}
<?php
namespace Acme\Example;
class Model
{
/** @var Shape */
private $shape;
public function setShape(Shape $value)
{
// Here it is guaranteed that the $value has already been validated
$this->shape = $value;
}
}
<?php
namespace Acme\Example;
class Application
{
public function changeShapeToSquare(Model $model)
{
// Enum class has __callStatic method that checks if there is constant with name of called function
// and then create instance of Shape class with this value
$shape = Shape::square();
$model->setShape($shape);
}
}
- Alexey Tihomirov
- Anatoly Pulyaevskiy
This library is under MIT license. Please see the complete license in the LICENSE file provided with the library source code.