Skip to content

Commit

Permalink
Replace const with property in state provider examples
Browse files Browse the repository at this point in the history
"New expressions are not supported in this context\”
  • Loading branch information
GromNaN authored and dunglas committed Dec 7, 2024
1 parent 4461006 commit 22d10d4
Showing 1 changed file with 19 additions and 24 deletions.
43 changes: 19 additions & 24 deletions core/state-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,23 @@ use ApiPlatform\State\ProviderInterface;
*/
final class BlogPostProvider implements ProviderInterface
{
private const DATA = [
'ab' => new BlogPost('ab'),
'cd' => new BlogPost('cd'),
];
private array $data;

public function __construct() {
$this->data = [
'ab' => new BlogPost('ab'),
'cd' => new BlogPost('cd'),
];
}

public function provide(Operation $operation, array $uriVariables = [], array $context = []): BlogPost|null
{
return self::DATA[$uriVariables['id']] ?? null;
return $this->data[$uriVariables['id']] ?? null;
}
}
```

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

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`.

Expand Down Expand Up @@ -110,18 +114,15 @@ use ApiPlatform\Metadata\CollectionOperationInterface;
*/
final class BlogPostProvider implements ProviderInterface
{
private const DATA = [
'ab' => new BlogPost('ab'),
'cd' => new BlogPost('cd'),
];
private array $data;

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

return self::DATA[$uriVariables['id']] ?? null;
return $this->data[$uriVariables['id']] ?? null;
}
}
```
Expand Down Expand Up @@ -169,19 +170,16 @@ use ApiPlatform\State\ProviderInterface;
*/
final class BlogPostProvider implements ProviderInterface
{
private const DATA = [
'ab' => new BlogPost('ab'),
'cd' => new BlogPost('cd'),
];
private array $data;

public function provide(Operation $operation, array $uriVariables = [], array $context = []): BlogPost|null
{
return self::DATA[$uriVariables['id']] ?? null;
return $this->data[$uriVariables['id']] ?? null;
}
}
```

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

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`.

Expand Down Expand Up @@ -221,18 +219,15 @@ use ApiPlatform\Metadata\CollectionOperationInterface;
*/
final class BlogPostProvider implements ProviderInterface
{
private const DATA = [
'ab' => new BlogPost('ab'),
'cd' => new BlogPost('cd'),
];
private array $data;

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

return self::DATA[$uriVariables['id']] ?? null;
return $this->data[$uriVariables['id']] ?? null;
}
}
```
Expand Down

0 comments on commit 22d10d4

Please sign in to comment.