|
11 | 11 | * Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Attributes\Models\Simple
|
12 | 12 | *
|
13 | 13 | * @property integer $id
|
14 |
| - * @property-read mixed $lowercase_name |
| 14 | + * @property int $diverging_type_hinted_get_and_set |
15 | 15 | * @property string|null $name
|
16 |
| - * @property-read string $uppercase_name |
| 16 | + * @property-read mixed $non_type_hinted_get |
| 17 | + * @property mixed $non_type_hinted_get_and_set |
| 18 | + * @property-write mixed $non_type_hinted_set |
| 19 | + * @property-write mixed $parameterless_set |
| 20 | + * @property-read string|null $type_hinted_get |
| 21 | + * @property string|null $type_hinted_get_and_set |
| 22 | + * @property-write string|null $type_hinted_set |
17 | 23 | * @method static \Illuminate\Database\Eloquent\Builder|Simple newModelQuery()
|
18 | 24 | * @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery()
|
19 | 25 | * @method static \Illuminate\Database\Eloquent\Builder|Simple query()
|
|
22 | 28 | */
|
23 | 29 | class Simple extends Model
|
24 | 30 | {
|
| 31 | + // With a backed property |
25 | 32 | protected function name(): Attribute
|
26 | 33 | {
|
27 | 34 | return new Attribute(
|
28 | 35 | function (?string $name): ?string {
|
29 | 36 | return $name;
|
30 | 37 | },
|
31 | 38 | function (?string $name): ?string {
|
32 |
| - return $name === null ? null : ucfirst($name); |
| 39 | + return $name; |
| 40 | + } |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + // Without backed properties |
| 45 | + |
| 46 | + protected function typeHintedGetAndSet(): Attribute |
| 47 | + { |
| 48 | + return new Attribute( |
| 49 | + function (): ?string { |
| 50 | + return $this->name; |
| 51 | + }, |
| 52 | + function (?string $name) { |
| 53 | + $this->name = $name; |
33 | 54 | }
|
34 | 55 | );
|
35 | 56 | }
|
36 | 57 |
|
37 |
| - public function uppercaseName(): Attribute |
| 58 | + protected function divergingTypeHintedGetAndSet(): Attribute |
38 | 59 | {
|
39 |
| - return Attribute::get(function (): string { |
40 |
| - return strtoupper($this->name); |
| 60 | + return new Attribute( |
| 61 | + function (): int { |
| 62 | + return strlen($this->name); |
| 63 | + }, |
| 64 | + function (?string $name) { |
| 65 | + $this->name = $name; |
| 66 | + } |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + protected function typeHintedGet(): Attribute |
| 71 | + { |
| 72 | + return Attribute::get(function (): ?string { |
| 73 | + return $this->name; |
41 | 74 | });
|
42 | 75 | }
|
43 | 76 |
|
44 |
| - public function lowercaseName(): Attribute |
| 77 | + protected function typeHintedSet(): Attribute |
| 78 | + { |
| 79 | + return Attribute::set(function (?string $name) { |
| 80 | + $this->name = $name; |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + protected function nonTypeHintedGetAndSet(): Attribute |
| 85 | + { |
| 86 | + return new Attribute( |
| 87 | + function () { |
| 88 | + return $this->name; |
| 89 | + }, |
| 90 | + function ($name) { |
| 91 | + $this->name = $name; |
| 92 | + } |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + protected function nonTypeHintedGet(): Attribute |
45 | 97 | {
|
46 | 98 | return Attribute::get(function () {
|
47 |
| - return strtolower($this->name); |
| 99 | + return $this->name; |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + protected function nonTypeHintedSet(): Attribute |
| 104 | + { |
| 105 | + return Attribute::set(function ($name) { |
| 106 | + $this->name = $name; |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + protected function parameterlessSet(): Attribute |
| 111 | + { |
| 112 | + return Attribute::set(function () { |
| 113 | + $this->name = null; |
48 | 114 | });
|
49 | 115 | }
|
50 | 116 |
|
|
0 commit comments