Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

update annotations reference

  • Loading branch information...
commit 27d347d5da9a7e2679fe6cdc2ba0510d9d9fd03e 1 parent ecf1531
@FabioBatSilva FabioBatSilva authored
Showing with 133 additions and 1 deletion.
  1. +133 −1 en/reference/annotations.rst
View
134 en/reference/annotations.rst
@@ -297,7 +297,139 @@ in the AnnotationRegistry. Annotation classes have to contain a class-level docb
/** @Annotation */
class Bar
{
- public $foo;
+ //some code
+ }
+
+Inject annotation values
+------------------------
+
+The annotation parser check if we the annotation constructor has arguments,
+if so then we will pass the value array, otherwise will try to inject values into public properties directly:
+
+
+.. code-block :: php
+
+ <?php
+ namespace MyCompany\Annotations;
+
+ /**
+ * @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
+-----------------
+
+``@Target`` indicates the kinds of class element to which an annotation type is applicable.
+Then you could define one or more targets :
+
+- ``CLASS`` Allowed in the class docblock
+- ``PROPERTY`` Allowed in the property docblock
+- ``METHOD`` Allowed in the method docblock
+- ``ALL`` Allowed in the class, property and method docblock
+
+If the annotations is not allowed in the current context you got an ``AnnotationException``
+
+.. code-block :: php
+
+ <?php
+ namespace MyCompany\Annotations;
+
+ /**
+ * @Annotation
+ * @Target({"METHOD","PROPERTY"})
+ */
+ class Bar
+ {
+ //some code
+ }
+
+ /**
+ * @Annotation
+ * @Target("CLASS")
+ */
+ class Foo
+ {
+ //some code
+ }
+
+Attribute types
+---------------
+
+Annotation parser check the given parameters using the phpdoc annotation ``@var``,
+The data type could be validated using the ``@var`` annotation on the annotation properties
+or using the annotations ``@Attributes`` and ``@Attribute``.
+
+If the data type not match you got an ``AnnotationException``
+
+.. code-block :: php
+
+ <?php
+ namespace MyCompany\Annotations;
+
+ /**
+ * @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
}
Usage
Please sign in to comment.
Something went wrong with that request. Please try again.