Skip to content

Commit

Permalink
Fix wrong first element exctracting
Browse files Browse the repository at this point in the history
  • Loading branch information
dzubchik committed Jul 28, 2017
1 parent dfd264a commit 70baaa7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
32 changes: 26 additions & 6 deletions src/ContainerAwareManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class ContainerAwareManager extends Manager implements ContainerAwareInterface
* Main method to kick this all off. Make a resource then pass it over, and use toArray()
*
* @param ResourceInterface $resource
* @param string $scopeIdentifier
* @param Scope $parentScopeInstance
* @param string $scopeIdentifier
* @param Scope $parentScopeInstance
*
* @return Scope
*/
Expand Down Expand Up @@ -66,13 +66,33 @@ private function resolveTransformer(ResourceInterface $resource)
if ($resource instanceof Item) {
$instance = ClassUtils::getRealClass(get_class($resource->getData()));
} elseif ($resource instanceof Collection) {
$instance = ClassUtils::getRealClass(get_class($resource->getData()[0]));
$data = $resource->getData();

if (!is_array($data) && !($data instanceof \Traversable)) {
throw new \InvalidArgumentException(
sprintf('Expected array or array iterator. Given %s', gettype($data))
);
}

$data = ($data instanceof \IteratorAggregate) ? $data->getIterator() : $data;

$element = is_array($data) ? array_values($data)[0] : $data->current();

if (!is_object($element)) {
throw new \InvalidArgumentException(
sprintf('Element expected to be object. Given %s', gettype($element))
);
}

$instance = ClassUtils::getRealClass(get_class($element));
} else {
return;
}

/** @var ResolverInterface $resolver */
foreach ($serviceRegistry->all() as $resolver) {
/** @var ResolverInterface[] $resolvers */
$resolvers = $serviceRegistry->all();

foreach ($resolvers as $resolver) {
if ($resolver instanceof ContainerAwareInterface) {
$resolver->setContainer($this->container);
}
Expand All @@ -84,4 +104,4 @@ private function resolveTransformer(ResourceInterface $resource)
}
}
}
}
}
3 changes: 3 additions & 0 deletions src/PaymaxiFractalBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
*/
class PaymaxiFractalBundle extends Bundle
{
/**
* @param ContainerBuilder $container
*/
public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new PrioritizedRegistryPass('fractal.transformer.resolvers'));
Expand Down

0 comments on commit 70baaa7

Please sign in to comment.