Skip to content

Commit 22d10d4

Browse files
GromNaNdunglas
authored andcommitted
Replace const with property in state provider examples
"New expressions are not supported in this context\”
1 parent 4461006 commit 22d10d4

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

core/state-providers.md

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,23 @@ use ApiPlatform\State\ProviderInterface;
5858
*/
5959
final class BlogPostProvider implements ProviderInterface
6060
{
61-
private const DATA = [
62-
'ab' => new BlogPost('ab'),
63-
'cd' => new BlogPost('cd'),
64-
];
61+
private array $data;
62+
63+
public function __construct() {
64+
$this->data = [
65+
'ab' => new BlogPost('ab'),
66+
'cd' => new BlogPost('cd'),
67+
];
68+
}
6569

6670
public function provide(Operation $operation, array $uriVariables = [], array $context = []): BlogPost|null
6771
{
68-
return self::DATA[$uriVariables['id']] ?? null;
72+
return $this->data[$uriVariables['id']] ?? null;
6973
}
7074
}
7175
```
7276

73-
For the example, we store the list of our blog posts in an associative array (the `BlogPostProvider::DATA` constant).
77+
For the example, we store the list of our blog posts in an associative array `$data`.
7478

7579
As this operation expects a `BlogPost`, the `provide` methods return the instance of the `BlogPost` corresponding to the ID passed in the URL. If the ID doesn't exist in the associative array, `provide()` returns `null`. API Platform will automatically generate a 404 response if the provider returns `null`.
7680

@@ -110,18 +114,15 @@ use ApiPlatform\Metadata\CollectionOperationInterface;
110114
*/
111115
final class BlogPostProvider implements ProviderInterface
112116
{
113-
private const DATA = [
114-
'ab' => new BlogPost('ab'),
115-
'cd' => new BlogPost('cd'),
116-
];
117+
private array $data;
117118

118119
public function provide(Operation $operation, array $uriVariables = [], array $context = []): iterable|BlogPost|null
119120
{
120121
if ($operation instanceof CollectionOperationInterface) {
121-
return self::DATA;
122+
return $this->data;
122123
}
123124

124-
return self::DATA[$uriVariables['id']] ?? null;
125+
return $this->data[$uriVariables['id']] ?? null;
125126
}
126127
}
127128
```
@@ -169,19 +170,16 @@ use ApiPlatform\State\ProviderInterface;
169170
*/
170171
final class BlogPostProvider implements ProviderInterface
171172
{
172-
private const DATA = [
173-
'ab' => new BlogPost('ab'),
174-
'cd' => new BlogPost('cd'),
175-
];
173+
private array $data;
176174

177175
public function provide(Operation $operation, array $uriVariables = [], array $context = []): BlogPost|null
178176
{
179-
return self::DATA[$uriVariables['id']] ?? null;
177+
return $this->data[$uriVariables['id']] ?? null;
180178
}
181179
}
182180
```
183181

184-
For the example, we store the list of our blog posts in an associative array (the `BlogPostProvider::DATA` constant).
182+
For the example, we store the list of our blog posts in an associative array `$data`.
185183

186184
As this operation expects a `BlogPost`, the `provide` methods return the instance of the `BlogPost` corresponding to the ID passed in the URL. If the ID doesn't exist in the associative array, `provide()` returns `null`. API Platform will automatically generate a 404 response if the provider returns `null`.
187185

@@ -221,18 +219,15 @@ use ApiPlatform\Metadata\CollectionOperationInterface;
221219
*/
222220
final class BlogPostProvider implements ProviderInterface
223221
{
224-
private const DATA = [
225-
'ab' => new BlogPost('ab'),
226-
'cd' => new BlogPost('cd'),
227-
];
222+
private array $data;
228223

229224
public function provide(Operation $operation, array $uriVariables = [], array $context = []): iterable|BlogPost|null
230225
{
231226
if ($operation instanceof CollectionOperationInterface) {
232-
return self::DATA;
227+
return $this->data;
233228
}
234229

235-
return self::DATA[$uriVariables['id']] ?? null;
230+
return $this->data[$uriVariables['id']] ?? null;
236231
}
237232
}
238233
```

0 commit comments

Comments
 (0)