Created
December 23, 2013 13:58
-
-
Save StanAngeloff/8097526 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* (c) PSP UK Group Ltd. <[email protected]> | |
* | |
* For the full copyright and license information, | |
* please view the LICENSE file that was distributed with this source code. | |
*/ | |
namespace Psp\Bundle\SystemConnectorBridgeBundle\Serializer\Exclusion; | |
use Doctrine\Common\Persistence\ObjectManager; | |
use JMS\Serializer\Context; | |
use JMS\Serializer\Exclusion\ExclusionStrategyInterface; | |
use JMS\Serializer\Metadata\ClassMetadata; | |
use JMS\Serializer\Metadata\PropertyMetadata; | |
class DoctrineAssociationExclusionStrategy implements ExclusionStrategyInterface | |
{ | |
/** | |
* @var ObjectManager | |
*/ | |
private $manager; | |
/** | |
* Initialize a new instance of DoctrineAssociationExclusionStrategy. | |
* | |
* @param ObjectManager $manager | |
*/ | |
public function __construct(ObjectManager $manager) | |
{ | |
$this->setManager($manager); | |
} | |
# {{{ ExclusionStrategyInterface | |
/** | |
* {@inheritdoc} | |
*/ | |
public function shouldSkipClass(ClassMetadata $metadata, Context $context) | |
{ | |
return false; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function shouldSkipProperty(PropertyMetadata $property, Context $context) | |
{ | |
$depth = $context->getDepth(); | |
# If we are processing a property on the main class, always accept. | |
if ($depth < 2) { | |
return false; | |
} | |
# Find the most recent entity class name and accept only if property is an identifier field. | |
$metadataStack = $context->getMetadataStack(); | |
$entityClassName = null; | |
foreach ($metadataStack as $metadata) { | |
if ($metadata instanceof ClassMetadata) { | |
$entityClassName = $metadata->name; | |
break; | |
} | |
} | |
# If we did not find an entity, accept property. | |
if ($entityClassName === null) { | |
return false; | |
} | |
try { | |
$entityMetadata = $this->manager->getClassMetadata($entityClassName); | |
} catch (\Exception $exception) { | |
return false; | |
} | |
$identifierNames = $entityMetadata->getIdentifierFieldNames(); | |
return ( ! in_array($property->name, $identifierNames)); | |
} | |
# }}} | |
# {{{ Getters/Setters | |
/** | |
* Get the ObjectManager instance associated with this object. | |
* | |
* @return ObjectManager | |
*/ | |
public function getManager() | |
{ | |
return $this->manager; | |
} | |
/** | |
* Set the ObjectManager instance associated with this object. | |
* | |
* @param ObjectManager $manager | |
* | |
* @return void | |
*/ | |
public function setManager(ObjectManager $manager) | |
{ | |
$this->manager = $manager; | |
} | |
# }}} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment