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