PHP code example of egorov / yii2-annotations

1. Go to this page and download the library: Download egorov/yii2-annotations library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

egorov / yii2-annotations example snippets

`
/**
 * @Annotation
 *
 * Some Annotation using a constructor
 */
class Bar
{
    private $foo;

    public function __construct(array $values)
    {
        $this->foo = $values['foo'];
    }
}

/**
 * @Annotation
 *
 * Some Annotation without a constructor
 */
class Foo
{
    public $bar;
}
`
/**
 * @Annotation
 * @Target({"METHOD","PROPERTY"})
 */
class Bar
{
    /** @var mixed */
    public $mixed;

    /** @var boolean */
    public $boolean;

    /** @var bool */
    public $bool;

    /** @var float */
    public $float;

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

    /** @var integer */
    public $integer;

    /** @var array */
    public $array;

    /** @var SomeAnnotationClass */
    public $annotation;

    /** @var array<integer> */
    public $arrayOfIntegers;

    /** @var array<SomeAnnotationClass> */
    public $arrayOfAnnotations;
}

/**
 * @Annotation
 * @Target({"METHOD","PROPERTY"})
 * @Attributes({
 *   @Attribute("stringProperty", type = "string"),
 *   @Attribute("annotProperty",  type = "SomeAnnotationClass"),
 * })
 */
class Foo
{
    public function __construct(array $values)
    {
        $this->stringProperty = $values['stringProperty'];
        $this->annotProperty = $values['annotProperty'];
    }

    // some code
}
`

/**
 * @Annotation
 * @Target({"CLASS"})
 */
class ServiceDescription
{
    /** @var string */
    private $description;

    /** @var array<ServiceMethod> */
    private $methods;

    public function __construct(array $values)
    {
        $this->description = $values['description'] ?? [];
        $this->methods = $values['methods'] ?? [];
    }
}
`

/**
 * @Annotation
 * @Target({"CLASS", "ANNOTATION"})
 */
class ServiceMethod
{
    /** @var string */
    private $name;

    /** @var string */
    private $description;

    public function __construct(array $values)
    {
        $this->name = $values['name'];
        $this->description = $values['description'];
    }
}
`

...
$reader = Yii::$app->annotations->getReader();
...
`

...
$reader = Yii::$app->annotations->getParser([
    'annotationName' => AnnotationClass::class
]);
...