示例#1
0
 /**
  * Iterate collection and apply decorators based on action.
  *
  * @param \Percy\Entity\Collection $collection
  * @param integer                  $action
  *
  * @return boolean
  */
 protected function decorate(Collection $collection, $action)
 {
     foreach ($collection->getIterator() as $entity) {
         $decorators = $entity->getDecorators($action);
         array_walk($decorators, [$this, 'invokeDecorators'], $entity);
     }
 }
示例#2
0
 /**
  * Iterate collection and validate data.
  *
  * @param \Percy\Entity\Collection $collection
  *
  * @throws \Percy\Exception\ValidationException when first validation failure occurs
  *
  * @return boolean
  */
 public function validate(Collection $collection)
 {
     foreach ($collection->getIterator() as $entity) {
         if (is_null($entity->getValidator())) {
             continue;
         }
         $filter = $this->filter->newSubjectFilter($entity->getValidator());
         try {
             $data = $entity->getData([], false);
             foreach (array_keys($entity->getMapping()) as $property) {
                 $data[$property] = array_key_exists($property, $data) ? $data[$property] : null;
             }
             $filter($data);
         } catch (FilterFailed $e) {
             throw new ValidationException($e->getMessage());
         }
     }
     return true;
 }
示例#3
0
 /**
  * Asserts that a collection can add and iterate entities.
  */
 public function testCollectionAddsAndIteratesEntities()
 {
     $collection = new Collection();
     $arrayCollection = [];
     for ($i = 0; $i < 10; $i++) {
         $data = ['entity' => $i];
         $arrayCollection[] = $data;
         $entity = $this->createMock(EntityInterface::class);
         $entity->expects($this->any())->method('getDataSource')->will($this->returnValue('test'));
         $entity->expects($this->exactly(2))->method('getData')->will($this->returnValue($data));
         $entity->expects($this->exactly(1))->method('toArray')->will($this->returnValue($data));
         $collection->addEntity($entity);
         $this->assertCount($i + 1, $collection);
     }
     $i = 0;
     foreach ($collection->getIterator() as $entity) {
         $this->assertSame($entity->getData(), ['entity' => $i]);
         $i++;
     }
     $this->assertSame($arrayCollection, $collection->getData());
     $this->assertSame(['count' => 10, 'total' => 10, '_embedded' => ['test' => $arrayCollection]], $collection->toArray());
 }
示例#4
0
 /**
  * Return relationship bind conditional.
  *
  * @param \Percy\Entity\Collection $collection
  * @param string                   $relationship
  * @param string                   $key
  *
  * @return string
  */
 protected function getRelationshipBinds(Collection $collection, $relationship, $key)
 {
     $primaries = [];
     foreach ($collection->getIterator() as $entity) {
         if (!array_key_exists($relationship, $entity->getRelationshipMap())) {
             continue;
         }
         $primaries[] = $entity[$key];
     }
     return $primaries;
 }
示例#5
0
 /**
  * Iterate a collection with the correct callback.
  *
  * @param \Percy\Entity\Collection $collection
  * @param string                   $callable
  *
  * @return boolean
  */
 protected function collectionIterator(Collection $collection, $callable, array $scopes = [])
 {
     $this->dbal->beginTransaction();
     try {
         foreach ($collection->getIterator() as $entity) {
             call_user_func_array([$this, $callable], [$entity, $scopes]);
         }
     } catch (PDOException $e) {
         $this->dbal->rollBack();
         return false;
     }
     $this->dbal->commit();
     return true;
 }