/** * Create a EntityList from a given array of entity data. * * @param array $entities_data * * @return EntityList */ protected function createEntityList(array $entities_data) { $type_map = []; foreach ($this->getOption(self::OPTION_REFERENCE_MODULES, []) as $type) { $type_map[$type->getEntityType()] = $type; } $collection = new EntityList(); ksort($entities_data); foreach ($entities_data as $entity_data) { if (!isset($entity_data[self::OBJECT_TYPE])) { $this->throwError('missing_doc_type', [], IncidentInterface::CRITICAL); continue; } $reference_type = $entity_data[self::OBJECT_TYPE]; unset($entity_data['@type']); if ($reference_type[0] !== '\\') { $reference_type = '\\' . $reference_type; } if (!isset($type_map[$reference_type])) { $this->throwError('invalid_doc_type', array('type' => @$entity_data[self::OBJECT_TYPE]), IncidentInterface::NOTICE); continue; } $reference_type = $type_map[$reference_type]; $collection->push($reference_type->createEntity($entity_data)); } return $collection; }
public function testCountShouldBeCorrectAfterAddingEntities() { $type = new ArticleType(); $test_entity = $type->createEntity(); $list = new EntityList([]); $list->push($test_entity); $list->push($test_entity); $entity1 = $list[0]; $entity2 = $list[1]; $this->assertEquals($test_entity, $entity1); $this->assertEquals($test_entity, $entity2); $this->assertCount(2, $list); $this->assertTrue(count($list) === 2); $this->assertTrue($list->getSize() === 2); }
/** * Create a EntityList from a given array of entity data. * * @param array $entities_data * * @return EntityList */ protected function createEntityList(array $entities_data, EntityList $list, EntityInterface $parent_entity = null) { $success = true; $embedded_entity_type_map = $this->getOption(self::OPTION_ENTITY_TYPES, new EntityTypeMap()); foreach ($entities_data as $embedded_entity_data) { if (!isset($embedded_entity_data[self::OBJECT_TYPE])) { $success = false; $this->throwError('missing_embed_prefix', [], IncidentInterface::CRITICAL); continue; } $embed_prefix = $embedded_entity_data[self::OBJECT_TYPE]; if (!$embedded_entity_type_map->hasKey($embed_prefix)) { //var_dump(array_keys($type_map), $trimmed_embed_type);exit; $success = false; $this->throwError('invalid_embed_prefix', ['type' => var_export($embedded_entity_data[self::OBJECT_TYPE], true)], IncidentInterface::CRITICAL); continue; } unset($embedded_entity_data['@type']); $embedded_type = $embedded_entity_type_map->getItem($embed_prefix); $list->push($embedded_type->createEntity($embedded_entity_data, $parent_entity)); } return $success; }
/** * Generates and adds fake data for a embed entities. * * @param EntityInterface $entity an instance of the entity to fill with fake data. * @param EmbeddedEntityListAttribute $attribute instance of the EmbeddedEntityListAttribute to fill with fake data. * @param array $options array of options to customize fake data creation. * * @return void */ protected function addEmbeddedEntityList(EntityInterface $entity, EmbeddedEntityListAttribute $attribute, array $options = array()) { $options_clone = $options; $entity_collection = new EntityList(); $embedded_type_map = $attribute->getEmbeddedEntityTypeMap(); $min_count = $attribute->getOption('min_count', 0); $max_count = $attribute->getOption('max_count', 3); $inline_mode = $attribute->getOption('inline_mode', false); if (true === $inline_mode) { $number_of_new_embed_entries = 1; } else { $number_of_new_embed_entries = $this->faker->numberBetween($min_count, $max_count); } // add new entities to collection for embed types for ($i = 0; $i < $number_of_new_embed_entries; $i++) { $embed_type = $this->faker->randomElement($embedded_type_map->getValues()); $new_entity = $this->createFakeEntity($embed_type, $options_clone, $entity); $entity_collection->addItem($new_entity); } $this->setValue($entity, $attribute, $entity_collection, $options); }