Skip to content

Commit

Permalink
added ability to limit doctrine collections - #10
Browse files Browse the repository at this point in the history
  • Loading branch information
zhil committed May 15, 2017
1 parent 1abef57 commit 597cb5c
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
11 changes: 11 additions & 0 deletions doc/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,26 @@ Fixture Generation Contexts allow you to specify options which will affect a par
```php
<?php
use Trappar\AliceGenerator\FixtureGenerationContext;
use Trappar\AliceGenerator\ObjectHandler\CollectionHandler;

// this post will have 10 comments in fixtures
CollectionHandler::limitCollection($post,"comments",10);

// This will include only the Post
$fixtureGenerator->generateYaml(
$post,
FixtureGenerationContext::create()
->setMaximumRecursion(2)
->setMaximumCollectionChilds(5) // by default all collections would have 5 childs
->setEntityCollectionLimit('AppBundle\Entity\Comment',3) // by default posts would have 3 comments
);
```

Collection limit check priorities
* CollectionHandler::limitCollection - you can limit some particular collection
* ->setEntityCollectionLimit('AppBundle\Entity\Comment',3) - all Comment collections, which are not already limited with CollectionHandler::limitCollection
* ->setMaximumCollectionChilds(5) - all not limited collections

### Limiting Recursion

Since generating fixtures involves recursing through objects, you will often find yourself in a situation where generating fixtures yields quite a lot more information than you would like - this is just the nature of recursion! `FixtureGenerationContext` offers two solutions to this problem.
Expand Down
46 changes: 46 additions & 0 deletions src/Trappar/AliceGenerator/FixtureGenerationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ class FixtureGenerationContext
* @var int
*/
private $maximumRecursion = 5;

/**
* @var int
*/
private $maximumCollectionChilds; // TODO: set to null to leave current behaviour. Maybe, lets set some default value, like 10?
/**
* @var array
*/
private $entityCollectionLimits = [];

/**
* @var PersistedObjectConstraints
*/
Expand Down Expand Up @@ -143,4 +154,39 @@ public function setSortResults($sortResults)

return $this;
}

/**
* @return int
*/
public function getMaximumCollectionChilds()
{
return $this->maximumCollectionChilds;
}

/**
* @param int $maximumCollectionChilds
* @return self
*/
public function setMaximumCollectionChilds($maximumCollectionChilds)
{
$this->maximumCollectionChilds = $maximumCollectionChilds;
return $this;
}

public function getCollectionLimit($entityClassName)
{
if(isset($this->entityCollectionLimits[$entityClassName])) {
return $this->entityCollectionLimits[$entityClassName];
} elseif($this->maximumCollectionChilds) {
return $this->maximumCollectionChilds;
} else {
return false;
}
}

public function setEntityCollectionLimit($entityClassName,$limit)
{
$this->entityCollectionLimits[$entityClassName] = $limit;
return $this;
}
}
21 changes: 20 additions & 1 deletion src/Trappar/AliceGenerator/ObjectHandler/CollectionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,29 @@ public function handle(ValueContext $valueContext)
if (!is_a($collection = $valueContext->getValue(), 'Doctrine\Common\Collections\Collection')) {
return false;
}
$fixturesGenerationContext = $valueContext->getValueVisitor()->getFixtureGenerationContext();
if(is_a($collection, 'Doctrine\ORM\PersistentCollection') && ($collectionLimit = $fixturesGenerationContext->getCollectionLimit($collection->getTypeClass()->getName()))) {
$refCollection = $valueContext->getMetadata()->reflection;
$criteria = \Doctrine\Common\Collections\Criteria::create()->setMaxResults($collectionLimit);
$refCollection->setAccessible(true);
$limitedValue = $collection->matching($criteria);
$refCollection->setValue($valueContext->getContextObject(), $limitedValue);

$valueContext->setValue($collection->toArray());
$valueContext->setValue($limitedValue->toArray());
} else {
$valueContext->setValue($collection->toArray());
}
$valueContext->getValueVisitor()->visitArray($valueContext);

return true;
}

public static function limitCollection($entity, $collectionName, $limit)
{
$refMatchTeams = new \ReflectionProperty($entity,$collectionName);

$criteria = \Doctrine\Common\Collections\Criteria::create()->setMaxResults($limit);
$refMatchTeams->setAccessible(true);
$refMatchTeams->setValue($entity,$refMatchTeams->getValue($entity)->matching($criteria));
}
}
9 changes: 9 additions & 0 deletions src/Trappar/AliceGenerator/ValueVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,13 @@ private function handlePersistedObject($object, $reference)
return true;
}
}

/**
* @return FixtureGenerationContext
*/
public function getFixtureGenerationContext()
{
return $this->fixtureGenerationContext;
}

}

0 comments on commit 597cb5c

Please sign in to comment.