/** * Serialize Node instances to an array * * @param NodeInterface $root Root node * @param Relations $relations List of child edges * * @return array */ private function serializeNode(NodeInterface $root, Relations $relations) { // get list of child edges $childs = $relations->filter(function (EdgeInterface $edge) use($root) { return $edge->getSource()->equals($root); }); // get list of serialized child nodes $childs = array_map(function (EdgeInterface $edge) use($relations) { return $this->serializeNode($edge->getTarget(), $relations); }, $childs->getRelations()); return $this->raw($root, $childs); }
/** * Serialize relations collection as a list of relations * * @param Relations $object * * @return array */ public function serialize($object) { return array_map(function (EdgeInterface $edge) { return $this->raw($edge); }, $object->getRelations()); }
/** * Test case 4: node with children, * one of the children has nested child, * nested child has common parents: * - for the first parent nested child have own child node * - for the second parent nested child have only name property * * @covers Serializer::serialize */ public function testSerializeNestedChildrenWithCommonParent() { $factory = $this->getFactoryMock(); $relations = new Relations(); $relations->addRelation($factory->createChildEdge('node 1', 'node 1.1')); $relations->addRelation($factory->createParentEdge('node 1.1', 'node 1')); $relations->addRelation($factory->createChildEdge('node 1', 'node 1.2')); $relations->addRelation($factory->createParentEdge('node 1.2', 'node 1')); $relations->addRelation($factory->createChildEdge('node 1', 'node 1.3')); $relations->addRelation($factory->createParentEdge('node 1.3', 'node 1')); $relations->addRelation($factory->createSiblingEdge('node 1.1', 'node 1.2')); $relations->addRelation($factory->createSiblingEdge('node 1.1', 'node 1.3')); $relations->addRelation($factory->createSiblingEdge('node 1.2', 'node 1.1')); $relations->addRelation($factory->createSiblingEdge('node 1.2', 'node 1.3')); $relations->addRelation($factory->createSiblingEdge('node 1.3', 'node 1.1')); $relations->addRelation($factory->createSiblingEdge('node 1.3', 'node 1.2')); $relations->addRelation($factory->createChildEdge('node 1.1', 'node 1.1.1')); $relations->addRelation($factory->createParentEdge('node 1.1.1', 'node 1.1')); $relations->addRelation($factory->createChildEdge('node 1.1.1', 'node 1.1.1.1')); $relations->addRelation($factory->createParentEdge('node 1.1.1.1', 'node 1.1.1')); $relations->addRelation($factory->createChildEdge('node 1.3', 'node 1.1.1')); $relations->addRelation($factory->createParentEdge('node 1.1.1', 'node 1.3')); $relations = $this->getSerializer()->serialize($relations, Context::AS_TREE); $this->assertEquals(['org_id' => 1, 'org_name' => 'node 1', 'daughters' => [['org_id' => 2, 'org_name' => 'node 1.1', 'daughters' => [['org_id' => 3, 'org_name' => 'node 1.1.1', 'daughters' => [['org_id' => 4, 'org_name' => 'node 1.1.1.1']]]]], ['org_id' => 5, 'org_name' => 'node 1.2'], ['org_id' => 7, 'org_name' => 'node 1.3', 'daughters' => [['org_id' => 3, 'org_name' => 'node 1.1.1', 'daughters' => [['org_id' => 4, 'org_name' => 'node 1.1.1.1']]]]]]], $relations); }