Skip to content
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

Support for 'include' context - only include given properties #73

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
91 changes: 91 additions & 0 deletions src/Model/Context.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

namespace BowlOfSoup\NormalizerBundle\Model;

class Context
{
/** @var string|null */
private $group;

/** @var string[] */
private $includes = [];

/** @var int|null */
private $maxDepth;

public function getGroup(): ?string
{
return $this->group;
}

/**
* @return $this
*/
public function setGroup(?string $group): self
{
$this->group = $group;
return $this;
}

public function getIncludes(): array
{
return $this->includes;
}

/**
* @return $this
*/
public function setIncludes(array $includes): self
{
$this->includes = $includes;

return $this;
}

/**
* @return $this
*/
public function setIncludesFromString(string $includes): self
{
$this->setIncludes(explode(',', $includes));

return $this;
}

/**
* @return $this
*/
public function addInclude(string $include): self
{
$this->includes[] = $include;

return $this;
}

public function hasInclude(string $assertion): bool
{
return in_array($assertion, $this->includes);
}

public function hasIncludes(): bool
{
return count($this->includes) > 0;
}

public function getMaxDepth(): ?int
{
return $this->maxDepth;
}

/**
* @return $this
*/
public function setMaxDepth(int $maxDepth): self
{
$this->maxDepth = $maxDepth;

return $this;
}
}
70 changes: 65 additions & 5 deletions src/Service/Normalize/AbstractNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
use BowlOfSoup\NormalizerBundle\Annotation\Normalize;
use BowlOfSoup\NormalizerBundle\Annotation\Translate;
use BowlOfSoup\NormalizerBundle\Exception\BosNormalizerException;
use BowlOfSoup\NormalizerBundle\Model\Context;
use BowlOfSoup\NormalizerBundle\Model\ObjectCache;
use BowlOfSoup\NormalizerBundle\Service\Extractor\AnnotationExtractor;
use BowlOfSoup\NormalizerBundle\Service\Extractor\ClassExtractor;
use BowlOfSoup\NormalizerBundle\Service\Normalizer;
use BowlOfSoup\NormalizerBundle\Service\ObjectHelper;
use Doctrine\Common\Collections\Collection;
use Symfony\Contracts\Translation\TranslatorInterface;

abstract class AbstractNormalizer
{
protected const TYPE_DATETIME = 'datetime';
protected const TYPE_OBJECT = 'object';
protected const TYPE_COLLECTION = 'collection';

/** @var \BowlOfSoup\NormalizerBundle\Service\Normalizer|null */
protected $sharedNormalizer = null;

Expand All @@ -32,6 +36,12 @@ abstract class AbstractNormalizer
/** @var string|null */
protected $group = null;

/** @var \BowlOfSoup\NormalizerBundle\Model\Context */
protected $context;

/** @var array */
protected $currentPath = [];

/** @var int|null */
protected $maxDepth = null;

Expand All @@ -56,6 +66,7 @@ public function __construct(

public function cleanUp(): void
{
$this->currentPath = [];
$this->maxDepth = null;
}

Expand All @@ -75,6 +86,26 @@ protected function hasMaxDepth(): bool
return null !== $this->maxDepth && ($this->processedDepth + 1) > $this->maxDepth;
}

/**
* This function deals with processing $context, which due to compatibility can be either a group or a context model.
* Refactor this in the next major version.
*
* @param \BowlOfSoup\NormalizerBundle\Model\Context|string|null $context
*/
protected function handleContext($context): void
{
if (is_string($context)) {
// Group has been given instead of context. Set group on context.
$context = (new Context())->setGroup($context);
} elseif (!$context instanceof Context) {
// No context has been given, instantiate empty context.
$context = new Context();
}

$this->group = $context->getGroup();
$this->context = $context;
}

/**
* @throws \BowlOfSoup\NormalizerBundle\Exception\BosNormalizerException
*
Expand Down Expand Up @@ -138,7 +169,7 @@ protected function normalizeReferencedObject(object $object, object $parentObjec
$objectName = get_class($object);

if (is_object($object) && !$this->isCircularReference($object, $objectName)) {
$normalizedConstruct = $this->sharedNormalizer->normalizeObject($object, $this->group);
$normalizedConstruct = $this->sharedNormalizer->normalizeObject($object, $this->context);

if (empty($normalizedConstruct)) {
return null;
Expand Down Expand Up @@ -194,10 +225,11 @@ protected function normalizeReferencedCollection($propertyValue, Normalize $prop
$propertyAnnotation
);
} else {
$normalizedObject = $this->sharedNormalizer->normalizeObject($collectionItem, $this->group);
$normalizedObject = $this->sharedNormalizer->normalizeObject($collectionItem, $this->context);
$normalizedCollection[] = (!empty($normalizedObject) ? $normalizedObject : null);
}
--$this->processedDepth;

}

return $normalizedCollection;
Expand Down Expand Up @@ -226,15 +258,15 @@ protected function handleCallbackResult($propertyValue, Normalize $propertyAnnot
$allObjects = false;
continue;
}
$normalizedCollection[] = $this->sharedNormalizer->normalizeObject($item, $this->group);
$normalizedCollection[] = $this->sharedNormalizer->normalizeObject($item, $this->context);
}
if (empty($normalizedCollection) && !$allObjects) {
return $propertyValue;
}

return $normalizedCollection;
} elseif (is_object($propertyValue)) {
return $this->sharedNormalizer->normalizeObject($propertyValue, $this->group);
return $this->sharedNormalizer->normalizeObject($propertyValue, $this->context);
}

return $propertyValue;
Expand Down Expand Up @@ -303,6 +335,34 @@ protected function storeNormalizedConstructForObject(string $constructName, stri
$this->nameAndClassStore[$baseObjectName]->set($constructName, $object);
}

protected function decreaseCurrentPath(): void
{
if (is_array($this->currentPath) && count($this->currentPath) > 1) {
array_pop($this->currentPath);
} else {
$this->currentPath = [];
}
}

protected function canCurrentPathBeIncluded(?string $pathType): bool
{
if ($pathType !== static::TYPE_OBJECT && $pathType !== static::TYPE_COLLECTION) {
return true;
}

if ($this->context->getMaxDepth() === (count($this->currentPath) - 1)) {
return false;
}

if (!$this->context->hasIncludes()) {
return true;
}

$pathThatIsBeingNormalized = implode('.', $this->currentPath);

return $this->context->hasInclude($pathThatIsBeingNormalized);
}

private function isCircularReference(object $object, string $objectName): bool
{
$objectIdentifier = ObjectHelper::getObjectIdentifier($object);
Expand Down
33 changes: 22 additions & 11 deletions src/Service/Normalize/MethodNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,21 @@ public function __construct(
}

/**
* @param \BowlOfSoup\NormalizerBundle\Model\Context|string|null $context
*
* @throws \BowlOfSoup\NormalizerBundle\Exception\BosNormalizerException
* @throws \ReflectionException
*/
public function normalize(
Normalizer $sharedNormalizer,
ObjectBag $objectBag,
?string $group
$context
): array {
$object = $objectBag->getObject();
$objectName = $objectBag->getObjectName();

$this->sharedNormalizer = $sharedNormalizer;
$this->group = $group;

$this->handleContext($context);
$this->processedDepthObjects[$objectName] = $this->processedDepth;

$normalizedMethods = [];
Expand Down Expand Up @@ -104,6 +105,19 @@ private function normalizeMethod(
continue;
}

$annotationName = $methodAnnotation->getName();
if (!empty($annotationName)) {
$methodName = $methodAnnotation->getName();
}

// Add to current path, like a breadcrumb where we are when normalizing.
$this->currentPath[] = $methodName;
if (!$this->canCurrentPathBeIncluded($methodAnnotation->getType())) {
$this->decreaseCurrentPath();

continue;
}

if ($methodAnnotation->hasType()) {
$methodValue = $this->getValueForMethodWithType(
$object,
Expand All @@ -120,17 +134,14 @@ private function normalizeMethod(
}
}

$annotationName = $methodAnnotation->getName();
if (!empty($annotationName)) {
$methodName = $methodAnnotation->getName();
}

$methodValue = (is_array($methodValue) && empty($methodValue) ? null : $methodValue);
if (null !== $translationAnnotation) {
$methodValue = $this->translateValue($methodValue, $translationAnnotation);
}

$normalizedProperties[$methodName] = $methodValue;

$this->decreaseCurrentPath();
}

return $normalizedProperties;
Expand All @@ -156,11 +167,11 @@ private function getValueForMethodWithType(
$newMethodValue = null;
$annotationMethodType = strtolower($annotationMethodType);

if ('datetime' === $annotationMethodType) {
if (static::TYPE_DATETIME === $annotationMethodType) {
$newMethodValue = $this->getValueForMethodWithDateTime($object, $method, $methodAnnotation);
} elseif ('object' === $annotationMethodType) {
} elseif (static::TYPE_OBJECT === $annotationMethodType) {
$newMethodValue = $this->getValueForMethodWithTypeObject($object, $method, $methodValue, $methodAnnotation);
} elseif ('collection' === $annotationMethodType) {
} elseif (static::TYPE_COLLECTION === $annotationMethodType) {
$newMethodValue = $this->normalizeReferencedCollection($methodValue, $methodAnnotation);
}

Expand Down
32 changes: 22 additions & 10 deletions src/Service/Normalize/PropertyNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,22 @@ public function __construct(
}

/**
* @param \BowlOfSoup\NormalizerBundle\Model\Context|string|null $context
*
* @throws \BowlOfSoup\NormalizerBundle\Exception\BosNormalizerException
* @throws \ReflectionException
*/
public function normalize(
Normalizer $sharedNormalizer,
ObjectBag $objectBag,
?string $group
$context
): array {
$object = $objectBag->getObject();
$objectName = $objectBag->getObjectName();
$objectIdentifier = $objectBag->getObjectIdentifier();

$this->sharedNormalizer = $sharedNormalizer;
$this->group = $group;
$this->handleContext($context);
$this->nameAndClassStore[$objectIdentifier] = new Store();

$normalizedProperties = [];
Expand Down Expand Up @@ -123,6 +125,19 @@ private function normalizeProperty(
continue;
}

$annotationName = $propertyAnnotation->getName();
if (!empty($annotationName)) {
$propertyName = $propertyAnnotation->getName();
}

// Add to current path, like a breadcrumb where we are when normalizing.
$this->currentPath[] = $propertyName;
if (!$this->canCurrentPathBeIncluded($propertyAnnotation->getType())) {
$this->decreaseCurrentPath();

continue;
}

if ($propertyAnnotation->hasType()) {
$propertyValue = $this->getValueForPropertyWithType(
$object,
Expand All @@ -142,17 +157,14 @@ private function normalizeProperty(
}
}

$annotationName = $propertyAnnotation->getName();
if (!empty($annotationName)) {
$propertyName = $propertyAnnotation->getName();
}

$propertyValue = (is_array($propertyValue) && empty($propertyValue) ? null : $propertyValue);
if (null !== $translationAnnotation) {
$propertyValue = $this->translateValue($propertyValue, $translationAnnotation);
}

$normalizedProperties[$propertyName] = $propertyValue;

$this->decreaseCurrentPath();
}

return $normalizedProperties;
Expand All @@ -178,11 +190,11 @@ private function getValueForPropertyWithType(
$newPropertyValue = null;
$annotationPropertyType = strtolower($annotationPropertyType);

if ('datetime' === $annotationPropertyType) {
if (static::TYPE_DATETIME === $annotationPropertyType) {
$newPropertyValue = $this->getValueForPropertyWithDateTime($object, $property, $propertyAnnotation);
} elseif ('object' === $annotationPropertyType) {
} elseif (static::TYPE_OBJECT === $annotationPropertyType) {
$newPropertyValue = $this->getValueForPropertyWithTypeObject($object, $propertyValue, $propertyAnnotation);
} elseif ('collection' === $annotationPropertyType) {
} elseif (static::TYPE_COLLECTION === $annotationPropertyType) {
$newPropertyValue = $this->normalizeReferencedCollection($propertyValue, $propertyAnnotation);
}

Expand Down
Loading
Loading