Skip to content

Commit 38fa026

Browse files
committed
fixes for php 7
1 parent f980613 commit 38fa026

File tree

5 files changed

+39
-12
lines changed

5 files changed

+39
-12
lines changed

src/Api/IdentifiersExtractor.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ public function getIdentifiersFromItem($item, string $operationName = null, arra
6262
throw new RuntimeException('No identifier value found, did you forgot to persist the entity?');
6363
}
6464

65-
if (is_scalar($identifierValue) || $identifierValue instanceof \Stringable) {
65+
// TODO: php 8 remove method_exists
66+
if (is_scalar($identifierValue) || method_exists($identifierValue, '__toString') || $identifierValue instanceof \Stringable) {
6667
$identifiers[$parameterName] = (string) $identifierValue;
6768
continue;
6869
}
@@ -76,7 +77,7 @@ public function getIdentifiersFromItem($item, string $operationName = null, arra
7677
if (1 === \count($relatedIdentifiers)) {
7778
$identifierValue = $this->resolveIdentifierValue($identifierValue, $relatedResourceClass, current($relatedIdentifiers)[1]);
7879

79-
if (is_scalar($identifierValue) || $identifierValue instanceof \Stringable) {
80+
if (is_scalar($identifierValue) || method_exists($identifierValue, '__toString') || $identifierValue instanceof \Stringable) {
8081
$identifiers[$parameterName] = (string) $identifierValue;
8182
continue;
8283
}

src/Core/Bridge/Doctrine/EventListener/PublishMercureUpdatesListener.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use ApiPlatform\Core\Exception\InvalidArgumentException;
2121
use ApiPlatform\Core\Exception\RuntimeException;
2222
use ApiPlatform\Core\Util\ResourceClassInfoTrait;
23+
use ApiPlatform\Exception\OperationNotFoundException;
2324
use ApiPlatform\GraphQl\Subscription\MercureSubscriptionIriGeneratorInterface as GraphQlMercureSubscriptionIriGeneratorInterface;
2425
use ApiPlatform\GraphQl\Subscription\SubscriptionManagerInterface as GraphQlSubscriptionManagerInterface;
2526
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
@@ -172,7 +173,15 @@ private function storeObjectToPublish($object, string $property): void
172173
return;
173174
}
174175

175-
$options = $this->resourceMetadataFactory->create($resourceClass)->getAttribute('mercure', false);
176+
if ($this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
177+
try {
178+
$options = $this->resourceMetadataFactory->create($resourceClass)->getOperation()->getMercure();
179+
} catch (OperationNotFoundException $e) {
180+
return;
181+
}
182+
} else {
183+
$options = $this->resourceMetadataFactory->create($resourceClass)->getAttribute('mercure', false);
184+
}
176185

177186
if (\is_string($options)) {
178187
if (null === $this->expressionLanguage) {

src/Core/JsonLd/Serializer/ItemNormalizer.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,8 @@ public function normalize($object, $format = null, array $context = [])
8484
$context = $this->initContext($resourceClass, $context);
8585
$metadata = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
8686

87-
if (isset($context['operation'])) {
88-
if ($previousResourceClass !== $resourceClass) {
89-
unset($context['operation'], $context['operation_name']);
90-
}
87+
if (isset($context['operation']) && $previousResourceClass !== $resourceClass) {
88+
unset($context['operation'], $context['operation_name']);
9189
}
9290

9391
if ($this->iriConverter instanceof IriConverterInterface) {

tests/Fixtures/TestBundle/Entity/AttributeDefaultOperations.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,21 @@
2020
final class AttributeDefaultOperations
2121
{
2222
#[ApiProperty(identifier: true)]
23-
private int $identifier;
23+
private $identifier;
24+
private $name;
2425

25-
public function __construct(int $identifier, public string $name)
26+
public function __construct(int $identifier, string $name)
2627
{
2728
$this->identifier = $identifier;
29+
$this->name = $name;
2830
}
2931

3032
public function getIdentifier()
3133
{
3234
return $this->identifier;
3335
}
36+
37+
public function getName() {
38+
return $this->name;
39+
}
3440
}

tests/Fixtures/TestBundle/Entity/AttributeResource.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,30 @@
3030
final class AttributeResource
3131
{
3232
#[ApiProperty(identifier: true)]
33-
private int $identifier;
33+
private $identifier;
3434

35-
public ?Dummy $dummy = null;
35+
/**
36+
* @var ?Dummy
37+
*/
38+
public $dummy = null;
3639

37-
public function __construct(int $identifier, public string $name)
40+
/**
41+
* @var string
42+
*/
43+
public $name;
44+
45+
public function __construct(int $identifier, string $name)
3846
{
3947
$this->identifier = $identifier;
48+
$this->name = $name;
4049
}
4150

4251
public function getIdentifier()
4352
{
4453
return $this->identifier;
4554
}
55+
56+
public function getName() {
57+
return $this->name;
58+
}
4659
}

0 commit comments

Comments
 (0)