/** * @dataProvider sourceEntityClassNameProvider */ public function testSourceEntityClassName($sourceName, $expected) { // Arrange $a = new NodeType(); $a->setName($sourceName); // Assert $this->assertEquals($expected, $a->getSourceEntityClassName()); }
/** * @param array $data * @param RZ\Roadiz\Core\Entities\NodeType $type * @param RZ\Roadiz\Core\Entities\Translation $translation * * @return RZ\Roadiz\Core\Entities\Node */ protected function createNode($data, NodeType $type, Translation $translation) { $node = new Node($type); $node->setNodeName($data['nodeName']); $this->getService('em')->persist($node); $sourceClass = "GeneratedNodeSources\\" . $type->getSourceEntityClassName(); $source = new $sourceClass($node, $translation); $source->setTitle($data['nodeName']); $this->getService('em')->persist($source); $this->getService('em')->flush(); return $node; }
protected function doTranstype(Node $node, NodeType $nodeType) { /* * Get an association between old fields and new fields * to find data that can be transfered during transtyping. */ $fieldAssociations = []; $oldFields = $node->getNodeType()->getFields(); $er = $this->getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\NodeTypeField'); foreach ($oldFields as $oldField) { $matchingField = $er->findOneBy(['nodeType' => $nodeType, 'name' => $oldField->getName(), 'type' => $oldField->getType()]); if (null !== $matchingField) { $fieldAssociations[] = [$oldField, $matchingField]; } } foreach ($node->getNodeSources() as $existingSource) { $sourceClass = "GeneratedNodeSources\\" . $nodeType->getSourceEntityClassName(); $source = new $sourceClass($node, $existingSource->getTranslation()); $source->setTitle($existingSource->getTitle()); foreach ($fieldAssociations as $fields) { if (!$fields[0]->isVirtual()) { /* * Copy simple data from source to another */ $setter = $fields[0]->getSetterName(); $getter = $fields[0]->getGetterName(); $source->{$setter}($existingSource->{$getter}()); } elseif ($fields[0]->getType() === NodeTypeField::DOCUMENTS_T) { /* * Copy documents. */ $documents = $this->getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\Document')->findByNodeSourceAndField($existingSource, $fields[0]); foreach ($documents as $document) { $nsDoc = new NodesSourcesDocuments($source, $document, $fields[1]); $source->getDocumentsByFields()->add($nsDoc); } } } // First plan old source deletion. $this->getService('em')->remove($existingSource); $this->getService('em')->flush(); $this->getService('em')->persist($source); foreach ($source->getDocumentsByFields() as $nsDoc) { $this->getService('em')->persist($nsDoc); } $this->getService('em')->flush(); } $node->setNodeType($nodeType); $this->getService('em')->flush(); }
/** * Generate a node with a unique name. * * @param NodeType $nodeType * @param Translation $translation * @param Node|null $parent * @param Tag|null $tag * @param boolean $pushToTop * * @return RZ\Roadiz\Core\Entities\NodesSources */ public function generate(NodeType $nodeType, Translation $translation, Node $parent = null, Tag $tag = null, $pushToTop = false) { $name = $nodeType->getDisplayName() . " " . uniqid(); $node = new Node($nodeType); $node->setParent($parent); $node->setNodeName($name); if (null !== $tag) { $node->addTag($tag); } $this->entityManager->persist($node); if ($pushToTop) { $node->setPosition(0.5); } $sourceClass = "GeneratedNodeSources\\" . $nodeType->getSourceEntityClassName(); $source = new $sourceClass($node, $translation); $source->setTitle($name); $this->entityManager->persist($source); $this->entityManager->flush(); return $source; }
/** * @param InputInterface $input * @param OutputInterface $output * @param NodeType $type * @param Translation $translation * * @return string */ private function executeNodeCreation(InputInterface $input, OutputInterface $output, NodeType $type, Translation $translation) { $nodeName = $input->getArgument('node-name'); $node = new Node($type); $node->setNodeName($nodeName); $this->entityManager->persist($node); // Source $sourceClass = "GeneratedNodeSources\\" . $type->getSourceEntityClassName(); $source = new $sourceClass($node, $translation); $fields = $type->getFields(); foreach ($fields as $field) { if (!$field->isVirtual()) { $question = new Question('<question>[Field ' . $field->getLabel() . ']</question> : ', null); $fValue = $this->questionHelper->ask($input, $output, $question); $setterName = $field->getSetterName(); $source->{$setterName}($fValue); } } $this->entityManager->persist($source); $this->entityManager->flush(); $text = '<info>Node “' . $nodeName . '” created…</info>' . PHP_EOL; return $text; }
/** * {@inheritDoc} * * @param string $string * @param RZ\Roadiz\Core\Entities\NodeType $type * * @return RZ\Roadiz\Core\Entities\NodeSource */ public function deserializeWithNodeType($string, NodeType $type) { $fields = $type->getFields(); /* * Create source default values */ $sourceDefaults = ["title", "meta_title", "meta_keywords", "meta_description"]; foreach ($fields as $field) { if (!$field->isVirtual()) { $sourceDefaults[] = $field->getName(); } } $encoder = new JsonEncoder(); $nameConverter = new CamelCaseToSnakeCaseNameConverter($sourceDefaults); $normalizer = new GetSetMethodNormalizer(null, $nameConverter); $serializer = new Serializer([$normalizer], [$encoder]); $node = $serializer->deserialize($string, NodeType::getGeneratedEntitiesNamespace() . '\\' . $type->getSourceEntityClassName(), 'json'); return $node; }