Last active
September 22, 2017 14:46
-
-
Save leonardorifeli/9c12f94b109cb7859ca9 to your computer and use it in GitHub Desktop.
Working with annotations using the doctrine annotation reader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "working-annotation", | |
"license": "MIT", | |
"type": "project", | |
"description": "Using annotations", | |
"require": | |
{ | |
"doctrine/common": "*" | |
}, | |
"authors": [ | |
{ | |
"name": "Leonardo Rifeli", | |
"email": "leonardorifeli@gmail.com", | |
"homepage": "http://leonardorifeli.com", | |
"role": "Back-end Developer" | |
} | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @PeopleAnnotation(description="Get all information about a people", type="class") | |
*/ | |
class People { | |
/** | |
* @PeopleAnnotation(description="Use to people name", type="attribute") | |
*/ | |
private $name; | |
/** | |
* @PeopleAnnotation(description="Use to people birth date", type="attribute") | |
*/ | |
private $birthDate; | |
/** | |
* @PeopleAnnotation(description="Get people name", type="method") | |
*/ | |
public function getName() { | |
return $this->name; | |
} | |
/** | |
* @PeopleAnnotation(description="Get people birth date", type="method") | |
*/ | |
public function getBirthDate() { | |
return $this->birthDate; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @Annotation | |
*/ | |
class PeopleAnnotation { | |
public $description; | |
public $type; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'vendor/autoload.php'; | |
require_once 'PeopleAnnotation.php'; | |
require_once 'People.php'; | |
use Doctrine\Common\Annotations\AnnotationReader; | |
$annotationReader = new AnnotationReader(); | |
$reflectionClass = new ReflectionClass('People'); | |
$classAnnotations = $annotationReader->getClassAnnotations($reflectionClass); | |
echo "CLASS ANNOTATIONS:"; | |
var_dump($classAnnotations); | |
echo '<hr/>'; | |
$people = new People(); | |
$reflectionObject = new ReflectionObject($people); | |
$objectAnnotations = $annotationReader->getClassAnnotations($reflectionObject); | |
echo "OBJECT ANNOTATIONS:"; | |
var_dump($objectAnnotations); | |
echo '<hr/>'; | |
$reflectionProperty = new ReflectionProperty('People', 'name'); | |
$propertyAnnotations = $annotationReader->getPropertyAnnotations($reflectionProperty); | |
echo "PROPERTY ANNOTATION NAME:"; | |
var_dump($propertyAnnotations); | |
echo '<hr/>'; | |
$reflectionProperty = new ReflectionProperty('People', 'birthDate'); | |
$propertyAnnotations = $annotationReader->getPropertyAnnotations($reflectionProperty); | |
echo "PROPERTY ANNOTATION BIRTH DATE"; | |
var_dump($propertyAnnotations); | |
echo '<hr/>'; | |
$reflectionMethod = new ReflectionMethod('People', 'getName'); | |
$methodAnnotations = $annotationReader->getMethodAnnotations($reflectionMethod); | |
echo "Method ANNOTATIONS getName:"; | |
var_dump($propertyAnnotations); | |
echo '<hr/>'; | |
$reflectionMethod = new ReflectionMethod('People', 'getBirthDate'); | |
$methodAnnotations = $annotationReader->getMethodAnnotations($reflectionMethod); | |
echo "Method ANNOTATIONS getBirthDate: "; | |
var_dump($propertyAnnotations); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CLASS ANNOTATIONS: | |
array (size=1) | |
0 => | |
object(PeopleAnnotation)[11] | |
public 'description' => string 'Get all information about a people' (length=34) | |
public 'type' => string 'class' (length=5) | |
OBJECT ANNOTATIONS: | |
array (size=1) | |
0 => | |
object(PeopleAnnotation)[15] | |
public 'description' => string 'Get all information about a people' (length=34) | |
public 'type' => string 'class' (length=5) | |
PROPERTY ANNOTATION NAME: | |
array (size=1) | |
0 => | |
object(PeopleAnnotation)[18] | |
public 'description' => string 'Use to people name' (length=18) | |
public 'type' => string 'attribute' (length=9) | |
PROPERTY ANNOTATION BIRTH DATE | |
array (size=1) | |
0 => | |
object(PeopleAnnotation)[19] | |
public 'description' => string 'Use to people birth date' (length=24) | |
public 'type' => string 'attribute' (length=9) | |
Method ANNOTATIONS getName: | |
array (size=1) | |
0 => | |
object(PeopleAnnotation)[19] | |
public 'description' => string 'Use to people birth date' (length=24) | |
public 'type' => string 'attribute' (length=9) | |
Method ANNOTATIONS getBirthDate: | |
array (size=1) | |
0 => | |
object(PeopleAnnotation)[19] | |
public 'description' => string 'Use to people birth date' (length=24) | |
public 'type' => string 'attribute' (length=9) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment