/** * Creates a subdivision object from the provided definitions. * * @param int $code The subdivision code. * @param array $definitions The subdivision definitions. * * @return Subdivision */ protected function createSubdivisionFromDefinitions($code, array $definitions) { if (!isset($definitions['subdivisions'][$code])) { // No matching definition found. return null; } $definition = $definitions['subdivisions'][$code]; // The 'parents' key is omitted when it contains just the country code. $definitions += ['parents' => [$definitions['country_code']]]; $parents = $definitions['parents']; // Load the parent, if known. $definition['parent'] = null; if (count($parents) > 1) { $grandparents = $parents; $parentId = array_pop($grandparents); $parentGroup = $this->buildGroup($grandparents); if (!isset($this->parents[$parentGroup][$parentId])) { $this->parents[$parentGroup][$parentId] = $this->get($parentId, $grandparents); } $definition['parent'] = $this->parents[$parentGroup][$parentId]; } // Prepare children. if (!empty($definition['has_children'])) { $childrenParents = array_merge($parents, [$code]); $children = new LazySubdivisionCollection($childrenParents); $children->setRepository($this); $definition['children'] = $children; } return new Subdivision($definition); }
/** * @covers ::getRepository * @covers ::setRepository */ public function testRepository() { $subdivisionRepository = $this->getMockBuilder('CommerceGuys\\Addressing\\Subdivision\\SubdivisionRepository')->disableOriginalConstructor()->getMock(); $this->collection->setRepository($subdivisionRepository); $this->assertSame($subdivisionRepository, $this->collection->getRepository()); }