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

excludeIf should work with @Content too #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 8 additions & 1 deletion Factory/ContentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,19 @@ public function createRelationsContent(ClassMetadataInterface $classMetadata, $o
{
$relationsContent = new \SplObjectStorage();

/**
* @var RelationMetadataInterface $relationMetadata
*/
foreach ($classMetadata->getRelations() as $relationMetadata) {
if (null === $relationMetadata->getContent()) {
continue;
}

$relationsContent->attach($relationMetadata, $this->getContent($relationMetadata, $object));
if (!$this->parametersFactory->createExclude($object, $relationMetadata->getExcludeIf())
&& $content = $this->getContent($relationMetadata, $object)
) {
$relationsContent->attach($relationMetadata, $content);
}
}

return $relationsContent->count() === 0 ? null : $relationsContent;
Expand Down
32 changes: 4 additions & 28 deletions Factory/LinkFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

use Symfony\Component\PropertyAccess\PropertyPath;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;

use FSC\HateoasBundle\Model\Link;
use FSC\HateoasBundle\Metadata\MetadataFactoryInterface;
use FSC\HateoasBundle\Metadata\ClassMetadataInterface;
Expand All @@ -15,19 +12,16 @@

class LinkFactory extends AbstractLinkFactory implements LinkFactoryInterface
{
protected $propertyAccessor;
protected $metadataFactory;
protected $parametersFactory;

public function __construct(MetadataFactoryInterface $metadataFactory,
ParametersFactoryInterface $parametersFactory,
RelationUrlGenerator $relationUrlGenerator,
PropertyAccessorInterface $propertyAccessor,
$forceAbsolute = true
) {
parent::__construct($relationUrlGenerator, $forceAbsolute);

$this->propertyAccessor = $propertyAccessor;
$this->metadataFactory = $metadataFactory;
$this->parametersFactory = $parametersFactory;
}
Expand All @@ -38,7 +32,8 @@ public function createLinks($object)
return;
}

if (null === ($classMetadata = $this->metadataFactory->getMetadata($object))) {
$classMetadata = $this->metadataFactory->getMetadata($object);
if (null === $classMetadata) {
return;
}

Expand All @@ -53,8 +48,8 @@ public function createLinksFromMetadata(ClassMetadataInterface $classMetadata, $
* @var RelationMetadataInterface $relationMetadata
*/
foreach ($classMetadata->getRelations() as $relationMetadata) {
if (!$this->shouldExcludeLink($relationMetadata, $object) &&
$link = $this->createLinkFromMetadata($relationMetadata, $object)
if (!$this->parametersFactory->createExclude($object, $relationMetadata->getExcludeIf())
&& $link = $this->createLinkFromMetadata($relationMetadata, $object)
) {
$links[] = $link;
}
Expand All @@ -63,25 +58,6 @@ public function createLinksFromMetadata(ClassMetadataInterface $classMetadata, $
return $links;
}

protected function shouldExcludeLink(RelationMetadataInterface $relationMetadata, $object)
{
if (!$fields = $relationMetadata->getExcludeIf()) {
return false;
}

foreach ($fields as $field => $valueToExclude) {
$field = trim($field, '.');
$propertyPath = new PropertyPath($field);
$value = $this->propertyAccessor->getValue($object, $propertyPath);

if ($valueToExclude === $value) {
return true;
}
}

return false;
}

public function createLinkFromMetadata(RelationMetadataInterface $relationMetadata, $object)
{
if (null !== $relationMetadata->getUrl()) {
Expand Down
27 changes: 26 additions & 1 deletion Factory/ParametersFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public function createParameters($data, $parameters)
$propertyAccessor = $this->propertyAccessor;
array_walk($parameters, function (&$value, $key) use ($data, $self, $propertyAccessor) {
if (is_string($value) && in_array(substr($value, 0, 1), array('.', '['))) {
$propertyPath = new PropertyPath(preg_replace('/^\./', '', $value));
$value = ltrim($value, '.');
$propertyPath = new PropertyPath($value);
$value = $propertyAccessor->getValue($data, $propertyPath);
} elseif ('@' === $value) {
$value = $data;
Expand All @@ -37,4 +38,28 @@ public function createParameters($data, $parameters)

return $parameters;
}

/**
* {@inheritdoc}
*/
public function createExclude($object, $excludeIf)
Copy link

Choose a reason for hiding this comment

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

The naming create* looks really weird for a method returning a boolean

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as the using of factory. I know.

Unfortunately I don't find another proper endpoint (furthermore PropertyFactory have PropertyPath injection already). It would be wrong if MetadataFactory returns NULL when excludeIf conditions is true. And Metadata object is just set of setters/getters and don't contain logic. Visitor Design Pattern would be redundant here also.

So I need some advice.

Copy link

Choose a reason for hiding this comment

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

@marapper IMO, you need a new object for this instead of trying to fit it in an existing interface where it does not belong

{
if ( !$excludeIf ) {
return false;
}

foreach ($excludeIf as $field => $valueToExclude) {
$field = ltrim($field, '.');
$propertyPath = new PropertyPath($field);

$value = $this->propertyAccessor->getValue($object, $propertyPath);

if ($valueToExclude === $value) {
return true;
}
}

return false;
}

}
14 changes: 14 additions & 0 deletions Factory/ParametersFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,19 @@

interface ParametersFactoryInterface
{
/**
* @param object $data
* @param array $parameters
*
* @return mixed
*/
public function createParameters($data, $parameters);

/**
* @param object $object
* @param array $excludeIf
*
* @return boolean
*/
public function createExclude($object, $excludeIf);
Copy link

Choose a reason for hiding this comment

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

This does not really belong to the ParametersFactory IMO. It is not about creating parameters. It is a totally different responsibility

}
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ class User
}
```

## Conditionally excluding links
## Conditionally excluding links and embedded relations

You can add conditions on relations that will determine whether the link should be excluded or not. For example:

Expand All @@ -694,4 +694,19 @@ relations:
parameters: { id: .parent.id }
exclude_if:
".parent": ~
```
```

It is also worked with embedded relations:

```php
/**
* @Rest\Relation(
* "parent",
* embed = @Rest\Content( property = ".childs" ),
* excludeIf = { ".showChilds" = null }
* )
*/
class Post
{
}
```
1 change: 0 additions & 1 deletion Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ services:
- @fsc_hateoas.metadata.factory
- @fsc_hateoas.factory.parameters
- @fsc_hateoas.routing.relation_url_generator
- @fsc_hateoas.property_accessor

fsc_hateoas.factory.parameters:
class: %fsc_hateoas.factory.parameters.class%
Expand Down