Skip to content

Backport #1014 (also includes #907) #1097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ parameters:

# False positives
- '#Parameter \#2 \$dqlPart of method Doctrine\\ORM\\QueryBuilder::add\(\) expects Doctrine\\ORM\\Query\\Expr\\Base, Doctrine\\ORM\\Query\\Expr\\Join\[\] given#' # Fixed in Doctrine's master
- '#Call to an undefined method Doctrine\\Common\\Persistence\\ObjectManager::getConnection\(\)#'
- '#Parameter \#1 \$callable of static method Doctrine\\Common\\Annotations\\AnnotationRegistry::registerLoader\(\) expects callable, mixed\[\] given#'
58 changes: 3 additions & 55 deletions src/Bridge/Doctrine/Orm/ItemDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@

use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryResultItemExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\IdentifierManagerTrait;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator;
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\Exception\PropertyNotFoundException;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use ApiPlatform\Core\Exception\RuntimeException;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\QueryBuilder;

Expand All @@ -35,9 +34,9 @@
*/
class ItemDataProvider implements ItemDataProviderInterface
{
use IdentifierManagerTrait;

private $managerRegistry;
private $propertyNameCollectionFactory;
private $propertyMetadataFactory;
private $itemExtensions;

/**
Expand Down Expand Up @@ -116,55 +115,4 @@ private function addWhereForIdentifiers(array $identifiers, QueryBuilder $queryB
$queryBuilder->setParameter($placeholder, $value);
}
}

/**
* Transform and check the identifier, composite or not.
*
* @param int|string $id
* @param ObjectManager $manager
* @param string $resourceClass
*
* @throws PropertyNotFoundException
*
* @return array
*/
private function normalizeIdentifiers($id, ObjectManager $manager, string $resourceClass): array
{
$identifierValues = [$id];
$doctrineMetadataIdentifier = $manager->getClassMetadata($resourceClass)->getIdentifier();

if (count($doctrineMetadataIdentifier) >= 2) {
$identifiers = explode(';', $id);
$identifiersMap = [];

// first transform identifiers to a proper key/value array
foreach ($identifiers as $identifier) {
$keyValue = explode('=', $identifier);
$identifiersMap[$keyValue[0]] = $keyValue[1];
}
}

$identifiers = [];
$i = 0;

foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);

$identifier = $propertyMetadata->isIdentifier();
if (null === $identifier || false === $identifier) {
continue;
}

$identifier = !isset($identifiersMap) ? $identifierValues[$i] ?? null : $identifiersMap[$propertyName] ?? null;

if (null === $identifier) {
throw new PropertyNotFoundException(sprintf('Invalid identifier "%s", "%s" has not been found.', $id, $propertyName));
}

$identifiers[$propertyName] = $identifier;
++$i;
}

return $identifiers;
}
}
85 changes: 85 additions & 0 deletions src/Bridge/Doctrine/Orm/Util/IdentifierManagerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Util;

use ApiPlatform\Core\Exception\PropertyNotFoundException;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\DBAL\Types\Type as DBALType;
use Doctrine\ORM\EntityManagerInterface;

/**
* @internal
*/
trait IdentifierManagerTrait
{
private $propertyNameCollectionFactory;
private $propertyMetadataFactory;

/**
* Transform and check the identifier, composite or not.
*
* @param int|string $id
* @param ObjectManager $manager
* @param string $resourceClass
*
* @throws PropertyNotFoundException
*
* @return array
*/
private function normalizeIdentifiers($id, ObjectManager $manager, string $resourceClass): array
{
$identifierValues = [$id];
$doctrineClassMetadata = $manager->getClassMetadata($resourceClass);
$doctrineIdentifierFields = $doctrineClassMetadata->getIdentifier();
$isOrm = interface_exists(EntityManagerInterface::class) && $manager instanceof EntityManagerInterface;
$platform = $isOrm ? $manager->getConnection()->getDatabasePlatform() : null;
Copy link
Member

@soyuka soyuka May 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

phpstan isn't happy with this :| We added the following in phpstan.neon on master:

        - '#Call to an undefined method Doctrine\\Common\\Persistence\\ObjectManager::getConnection\(\)#'


if (count($doctrineIdentifierFields) > 1) {
$identifiersMap = [];

// first transform identifiers to a proper key/value array
foreach (explode(';', $id) as $identifier) {
$identifierPair = explode('=', $identifier);
$identifiersMap[$identifierPair[0]] = $identifierPair[1];
}
}

$identifiers = [];
$i = 0;

foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);

if (!$propertyMetadata->isIdentifier()) {
continue;
}

$identifier = !isset($identifiersMap) ? $identifierValues[$i] ?? null : $identifiersMap[$propertyName] ?? null;
if (null === $identifier) {
throw new PropertyNotFoundException(sprintf('Invalid identifier "%s", "%s" has not been found.', $id, $propertyName));
}

$doctrineTypeName = $doctrineClassMetadata->getTypeOfField($propertyName);

if ($isOrm && null !== $doctrineTypeName && DBALType::hasType($doctrineTypeName)) {
$identifier = DBALType::getType($doctrineTypeName)->convertToPHPValue($identifier, $platform);
}

$identifiers[$propertyName] = $identifier;
++$i;
}

return $identifiers;
}
}
Loading