Skip to content
This repository was archived by the owner on Aug 24, 2019. It is now read-only.

Latest commit

 

History

History
95 lines (76 loc) · 1.73 KB

File metadata and controls

95 lines (76 loc) · 1.73 KB

Annotation

Docblock Annotations parser functional

Requirements

  • PHP >= 7.0

Installation

Run the following command in the root directory of your web project:

composer require aengine/annotation

Usage

Custom Enum annotation

/**
 * @AEngine\Annotations\Annotation\Target('PROPERTY')
 */
class Enum extends \AEngine\Annotations\Annotation
{
    protected $literal;

    public $message;

    public function __construct($literal)
    {
        $this->literal = $literal;
    }

    public function getMessage() {
        return $this->message;
    }

    public function validate($value)
    {
        return in_array(strtolower($value), $this->literal);
    }
}

Example check function

function check($obj)
{
    $arc = new \AEngine\Annotations\AnnotatedReflectionClass($obj);

    foreach ($arc->getProperties() as $property) {
        $annotation = $property->getAnnotation(Enum::class);

        if ($annotation) {
            if (!$annotation->validate($property->getValue($obj))) {
                throw new RuntimeException(sprintf($annotation->getMessage(), $property->getName()));
            }
        }
    }

    return true;
}

Entity example

class Car
{
    /**
     * @Enum({'bmw', 'opel', 'lada'}, message="Value in "%s" is not available")
     *
     * @var string
     */
    public $brand;

    /**
     * @var string
     */
    public $model;
}

Create object and check

$car = new Car();
$car->brand = 'BMW';
$car->model = 'X5';

if (check($car) === true) {
    // other code
}

Contributing

Please see CONTRIBUTING for details.

License

The package annotation is licensed under the MIT license. See License File for more information.