1--TEST-- 2Doctrine-like attributes usage 3--FILE-- 4<?php 5 6namespace Doctrine\ORM\Attributes { 7 class Annotation { public $values; public function construct() { $this->values = func_get_args(); } } 8 class Entity extends Annotation {} 9 class Id extends Annotation {} 10 class Column extends Annotation { const UNIQUE = 'unique'; const T_INTEGER = 'integer'; } 11 class GeneratedValue extends Annotation {} 12 class JoinTable extends Annotation {} 13 class ManyToMany extends Annotation {} 14 class JoinColumn extends Annotation { const UNIQUE = 'unique'; } 15 class InverseJoinColumn extends Annotation {} 16} 17 18namespace Symfony\Component\Validator\Constraints { 19 class Annotation { public $values; public function construct() { $this->values = func_get_args(); } } 20 class Email extends Annotation {} 21 class Range extends Annotation {} 22} 23 24namespace { 25use Doctrine\ORM\Attributes as ORM; 26use Symfony\Component\Validator\Constraints as Assert; 27 28#[ORM\Entity] 29/** @ORM\Entity */ 30class User 31{ 32 /** @ORM\Id @ORM\Column(type="integer"*) @ORM\GeneratedValue */ 33 #[ORM\Id] 34 #[ORM\Column("integer")] 35 #[ORM\GeneratedValue] 36 private $id; 37 38 /** 39 * @ORM\Column(type="string", unique=true) 40 * @Assert\Email(message="The email '{{ value }}' is not a valid email.") 41 */ 42 #[ORM\Column("string", ORM\Column::UNIQUE)] 43 #[Assert\Email(array("message" => "The email '{{ value }}' is not a valid email."))] 44 private $email; 45 46 /** 47 * @ORM\Column(type="integer") 48 * @Assert\Range( 49 * min = 120, 50 * max = 180, 51 * minMessage = "You must be at least {{ limit }}cm tall to enter", 52 * maxMessage = "You cannot be taller than {{ limit }}cm to enter" 53 * ) 54 */ 55 #[Assert\Range(["min" => 120, "max" => 180, "minMessage" => "You must be at least {{ limit }}cm tall to enter"])] 56 #[ORM\Column(ORM\Column::T_INTEGER)] 57 protected $height; 58 59 /** 60 * @ORM\ManyToMany(targetEntity="Phonenumber") 61 * @ORM\JoinTable(name="users_phonenumbers", 62 * joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}, 63 * inverseJoinColumns={@ORM\JoinColumn(name="phonenumber_id", referencedColumnName="id", unique=true)} 64 * ) 65 */ 66 #[ORM\ManyToMany(Phonenumber::class)] 67 #[ORM\JoinTable("users_phonenumbers")] 68 #[ORM\JoinColumn("user_id", "id")] 69 #[ORM\InverseJoinColumn("phonenumber_id", "id", ORM\JoinColumn::UNIQUE)] 70 private $phonenumbers; 71} 72 73$class = new ReflectionClass(User::class); 74$attributes = $class->getAttributes(); 75 76foreach ($attributes as $attribute) { 77 var_dump($attribute->getName(), $attribute->getArguments()); 78} 79 80foreach ($class->getProperties() as $property) { 81 $attributes = $property->getAttributes(); 82 83 foreach ($attributes as $attribute) { 84 var_dump($attribute->getName(), $attribute->getArguments()); 85 } 86} 87} 88?> 89--EXPECT-- 90string(30) "Doctrine\ORM\Attributes\Entity" 91array(0) { 92} 93string(26) "Doctrine\ORM\Attributes\Id" 94array(0) { 95} 96string(30) "Doctrine\ORM\Attributes\Column" 97array(1) { 98 [0]=> 99 string(7) "integer" 100} 101string(38) "Doctrine\ORM\Attributes\GeneratedValue" 102array(0) { 103} 104string(30) "Doctrine\ORM\Attributes\Column" 105array(2) { 106 [0]=> 107 string(6) "string" 108 [1]=> 109 string(6) "unique" 110} 111string(45) "Symfony\Component\Validator\Constraints\Email" 112array(1) { 113 [0]=> 114 array(1) { 115 ["message"]=> 116 string(45) "The email '{{ value }}' is not a valid email." 117 } 118} 119string(45) "Symfony\Component\Validator\Constraints\Range" 120array(1) { 121 [0]=> 122 array(3) { 123 ["min"]=> 124 int(120) 125 ["max"]=> 126 int(180) 127 ["minMessage"]=> 128 string(48) "You must be at least {{ limit }}cm tall to enter" 129 } 130} 131string(30) "Doctrine\ORM\Attributes\Column" 132array(1) { 133 [0]=> 134 string(7) "integer" 135} 136string(34) "Doctrine\ORM\Attributes\ManyToMany" 137array(1) { 138 [0]=> 139 string(11) "Phonenumber" 140} 141string(33) "Doctrine\ORM\Attributes\JoinTable" 142array(1) { 143 [0]=> 144 string(18) "users_phonenumbers" 145} 146string(34) "Doctrine\ORM\Attributes\JoinColumn" 147array(2) { 148 [0]=> 149 string(7) "user_id" 150 [1]=> 151 string(2) "id" 152} 153string(41) "Doctrine\ORM\Attributes\InverseJoinColumn" 154array(3) { 155 [0]=> 156 string(14) "phonenumber_id" 157 [1]=> 158 string(2) "id" 159 [2]=> 160 string(6) "unique" 161} 162