/** * @param Zone $zone * * @return array|Elect[] * * @throws NoElectsForZoneException * @throws DBALException */ public function getElectsFromZone(Zone $zone) { $datas = $this->manager->getDBResults('@Api/sql/elects/getElectsFromZone.sql.twig', ['zone' => $zone], new NoElectsForZoneException($zone)); foreach ($datas as &$data) { $data = ArraysUtils::toMultiDimensional($data); $data['this_zone_mandate'] = []; $data['other_mandates'] = []; } $datas = ArraysUtils::mergeBy($datas, 'id', 'all_mandates'); foreach ($datas as &$data) { $data['this_zone_mandate'] = array_values(array_filter($data['all_mandates'], function ($mandate) { return intval($mandate['is_current_zone_mandate']) == 1; }))[0]; $data['other_mandates'] = array_filter($data['all_mandates'], function ($mandate) { return intval($mandate['is_current_zone_mandate']) == 0; }); unset($data['all_mandates']); } $elects = []; foreach ($datas as $data) { $newElect = $this->hydrator->hydrateElect($data['id'], $data['first_name'], $data['last_name'], $data['slug'], $data['sex'], is_null($data['birth_date']) ? null : new \DateTime($data['birth_date']), $this->hydrator->hydrateSocioProfessionalCategory($data['socio_professional_category']['id'], $data['socio_professional_category']['name'], $data['socio_professional_category']['slug'], $data['socio_professional_category']['family'])); $newElect->setZoneMandate($this->hydrator->hydrateMandate($data['this_zone_mandate']['id'], $data['this_zone_mandate']['name'], $data['this_zone_mandate']['wikipedia'], $this->hydrator->hydrateZoneType($data['this_zone_mandate']['zone_type']['id'], $data['this_zone_mandate']['zone_type']['name'], $data['this_zone_mandate']['zone_type']['slug'], $data['this_zone_mandate']['zone_type']['level']), null, $zone)); foreach ($data['other_mandates'] as $otherMandate) { $newElect->addMandate($this->hydrator->hydrateMandate($otherMandate['id'], $otherMandate['name'], $otherMandate['wikipedia'], $this->hydrator->hydrateZoneType($otherMandate['zone_type']['id'], $otherMandate['zone_type']['name'], $otherMandate['zone_type']['slug'], $otherMandate['zone_type']['level']), null, $this->hydrator->hydrateZone($otherMandate['id'], $this->hydrator->hydrateCountry($data['country']['id'], $data['country']['name'], $data['country']['iso_code_2_letters']), $this->hydrator->hydrateZoneType($otherMandate['zone_type']['id'], $otherMandate['zone_type']['name'], $otherMandate['zone_type']['slug'], $otherMandate['zone_type']['level'], $this->hydrator->hydrateCountry($data['country']['id'], $data['country']['name'], $data['country']['iso_code_2_letters'])), $otherMandate['zone']['ref'], $otherMandate['zone']['ref_official'], $otherMandate['zone']['name'], $otherMandate['zone']['slug'], $otherMandate['zone']['population'], $otherMandate['zone']['wikipedia'], !is_null($otherMandate['zone']['date_from']) ? new \DateTime($otherMandate['zone']['date_from']) : null, !is_null($otherMandate['zone']['date_until']) ? new \DateTime($otherMandate['zone']['date_until']) : null, $otherMandate['zone']['shape_polygon'], $otherMandate['zone']['shape_multipolygon']))); } $elects[] = $newElect; } return $elects; }
/** * Test merge by. * * @see ArrayUtils::mergeBy */ public function testMergeBy() { $array = [['id' => 1, 'otherKey' => 'otherValue', 'arrayKey' => ['key1' => 'value1', 'key2' => 'value2'], 'skill' => 'php'], ['id' => 1, 'otherKey' => 'otherValue', 'arrayKey' => ['key1' => 'value1', 'key2' => 'value2'], 'skill' => 'symfony']]; $merged = ArraysUtils::mergeBy($array, 'id', 'skill')[0]; $this->assertArrayHasKey('id', $merged); $this->assertArrayHasKey('otherKey', $merged); $this->assertArrayHasKey('arrayKey', $merged); $this->assertArrayHasKey('skills', $merged); $this->assertArrayNotHasKey('skill', $merged); $this->assertArraySubset(['php', 'symfony'], $merged['skills']); }