Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Refactor #5

Merged
merged 1 commit into from
Oct 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ vendor/
build/
yarn.lock
node_modules/
pull_request.md
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":1,"defects":{"DistrictTest::testDistrictsDoesNotExist":4,"VillageTest::testVillageInNonExistentParish":4},"times":{"CountyTest::testUgandaHasCounties":0.037,"DistrictTest::testUgandaHasDistricts":0.046,"DistrictTest::testDistrictsDoesNotExist":0.065,"DistrictTest::testDistrictHasCounties":0.084,"DistrictTest::testDistrictHasSubCounties":0.185,"ParishTest::testUgandaHasParishes":0.133,"ParishTest::testParishInNonExistentSubCounty":0.184,"ParishTest::testDistrictHasParishes":0.469,"SubCountyTest::testUgandaHasSubCounties":0.054,"SubCountyTest::testSubCountiesInNonExistentCounty":0.058,"SubCountyTest::testDistrictHasSubCounties":0.113,"VillageTest::testUgandaHasVillages":0.603,"VillageTest::testVillageInNonExistentParish":0.965,"VillageTest::testDistrictHasVillages":4.698}}
7 changes: 7 additions & 0 deletions example.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

use Uganda\Geo;

/**
* Fetch all Villages in
* 1. Mukono district
* 2. Mukono Municipality
* 3. Goma Division
* 4. Nantabulirwa Ward
*/
$geo = new Geo();
$data = $geo
->districts('Mukono')
Expand Down
105 changes: 9 additions & 96 deletions src/Uganda/Geo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,107 +2,20 @@

namespace Uganda;

class Geo extends Helpers {
use Uganda\Util\Helpers;
use Uganda\Util\District;
use Uganda\Util\County;
use Uganda\Util\SubCounty;
use Uganda\Util\Parish;
use Uganda\Util\Village;

public function districts($district = null) {
$districts = parent::fetch('districts.json');

if ($district) {
$filtered = array_filter($districts, parent::filter('name',$district));

if (empty($filtered)) {
$this->_error['district'] = 'District ' . $district . ' does not exist.';
} else {
$this->_district = [...$filtered][0]['id'];
}
} else {
$this->_districts['districts'] = parent::format($districts);
}
return $this;
}

public function counties($county = null) {
$counties = parent::fetch('counties.json');
if ($county) {
$filtered = array_filter($counties, parent::filter('name', $county));
if (empty($filtered)) {
$this->_error['county'] = 'County ' . $county . ' does not exist.';
} else {
$this->_county = [...$filtered][0]['id'];
}
} else {
if (isset($this->_district)) {
$filtered = array_filter($counties, parent::filter('district', $this->_district));
$this->_counties['counties'] = parent::format([...$filtered]);
} else {
$this->_counties['counties'] = parent::format($counties);
}
}

return $this;
}

public function sub_counties($sub_county = null) {
$sub_counties = parent::fetch('sub_counties.json');
if ($sub_county) {
$filtered = array_filter($sub_counties, parent::filter('name', $sub_county));
if (empty($filtered)) {
$this->_error['sub_county'] = 'Sub county ' . $sub_county . ' does not exist.';
}
else {
$this->_sub_county = [...$filtered][0]['id'];
}
} else {
if (isset($this->_district) && isset($this->_county)) {
$filtered = array_filter($sub_counties, parent::filter('county', $this->_county));
$this->_sub_counties['sub_counties'] = parent::format([...$filtered]);
} else {
$this->_sub_counties['sub_counties'] = parent::format($sub_counties);
}
}

return $this;
}

public function parishes($parish = null) {
$parishes = parent::fetch('parishes.json');
if ($parish) {
$filtered = array_filter($parishes, parent::filter('name', $parish));
if (empty($filtered)) {
$this->_error['parish'] = 'Parish ' . $parish . ' does not exist.';
} else {
$this->_parish = [...$filtered][0]['id'];
}
} else {
if (isset($this->_district) && isset($this->_county) && isset($this->_sub_county)) {

$filtered = array_filter($parishes, parent::filter('subcounty', $this->_sub_county));
$this->_parishes['parishes'] = parent::format([...$filtered]);
} else {
$this->_parishes['parishes'] = parent::format($parishes);
}
}

return $this;
}

public function villages() {
$villages = parent::fetch('villages.json');

if (isset($this->_district) && isset($this->_county) && isset($this->_sub_county) && isset($this->_parish)) {
$filtered = array_filter($villages, parent::filter('parish', $this->_parish));
$this->_villages['villages'] = parent::format([...$filtered]);
} else {
$this->_villages['villages'] = parent::format($villages);
}

return $this;
}
class Geo {
use Helpers, District, County, SubCounty, Parish, Village;

public function all() {
$properties = get_object_vars($this);
header("Content-Type:application/json");

if (array_key_exists("_error", $properties)) {
$error['errors'] = $properties['_error'];
return json_encode($error);
Expand Down
27 changes: 27 additions & 0 deletions src/Uganda/Util/County.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Uganda\Util;

trait County {
use Helpers;
public function counties($county = null) {
$counties = $this->fetch('counties.json');
if ($county) {
$filtered = array_filter($counties, $this->filter('name', $county));
if (empty($filtered)) {
$this->_error['county'] = 'County ' . $county . ' does not exist.';
} else {
$this->_county = [...$filtered][0]['id'];
}
} else {
if (isset($this->_district)) {
$filtered = array_filter($counties, $this->filter('district', $this->_district));
$this->_counties['counties'] = $this->format([...$filtered]);
} else {
$this->_counties['counties'] = $this->format($counties);
}
}

return $this;
}
}
23 changes: 23 additions & 0 deletions src/Uganda/Util/District.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Uganda\Util;

trait District {
use Helpers;
public function districts($district = null) {
$districts = $this->fetch('districts.json');

if ($district) {
$filtered = array_filter($districts, $this->filter('name', $district));

if (empty($filtered)) {
$this->_error['district'] = 'District ' . $district . ' does not exist.';
} else {
$this->_district = [...$filtered][0]['id'];
}
} else {
$this->_districts['districts'] = $this->format($districts);
}
return $this;
}
}
11 changes: 6 additions & 5 deletions src/Uganda/Helpers.php β†’ src/Uganda/Util/Helpers.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php
namespace Uganda;

class Helpers {
namespace Uganda\Util;

trait Helpers {
public function fetch($file) {
$json = file_get_contents(dirname(__FILE__).'/geo/'.$file);
$json = file_get_contents(dirname(__FILE__) . '/geo/' . $file);
$data = json_decode($json, true);

return $data;
Expand All @@ -12,10 +13,10 @@ public function fetch($file) {
public function format($results) {
$info['count'] = (int) count($results);
$info['data'] = (array) $results;

return $info;
}

public function filter($name, $data) {
return function ($obj) use ($name, $data) {
if (isset($obj[$name])) {
Expand Down
28 changes: 28 additions & 0 deletions src/Uganda/Util/Parish.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Uganda\Util;

trait Parish {
use Helpers;
public function parishes($parish = null) {
$parishes = $this->fetch('parishes.json');
if ($parish) {
$filtered = array_filter($parishes, $this->filter('name', $parish));
if (empty($filtered)) {
$this->_error['parish'] = 'Parish ' . $parish . ' does not exist.';
} else {
$this->_parish = [...$filtered][0]['id'];
}
} else {
if (isset($this->_district) && isset($this->_county) && isset($this->_sub_county)) {

$filtered = array_filter($parishes, $this->filter('subcounty', $this->_sub_county));
$this->_parishes['parishes'] = $this->format([...$filtered]);
} else {
$this->_parishes['parishes'] = $this->format($parishes);
}
}

return $this;
}
}
27 changes: 27 additions & 0 deletions src/Uganda/Util/SubCounty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Uganda\Util;

trait SubCounty {
use Helpers;
public function sub_counties($sub_county = null) {
$sub_counties = $this->fetch('sub_counties.json');
if ($sub_county) {
$filtered = array_filter($sub_counties, $this->filter('name', $sub_county));
if (empty($filtered)) {
$this->_error['sub_county'] = 'Sub county ' . $sub_county . ' does not exist.';
} else {
$this->_sub_county = [...$filtered][0]['id'];
}
} else {
if (isset($this->_district) && isset($this->_county)) {
$filtered = array_filter($sub_counties, $this->filter('county', $this->_county));
$this->_sub_counties['sub_counties'] = $this->format([...$filtered]);
} else {
$this->_sub_counties['sub_counties'] = $this->format($sub_counties);
}
}

return $this;
}
}
19 changes: 19 additions & 0 deletions src/Uganda/Util/Village.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Uganda\Util;

trait Village {
use Helpers;
public function villages() {
$villages = $this->fetch('villages.json');

if (isset($this->_district) && isset($this->_county) && isset($this->_sub_county) && isset($this->_parish)) {
$filtered = array_filter($villages, $this->filter('parish', $this->_parish));
$this->_villages['villages'] = $this->format([...$filtered]);
} else {
$this->_villages['villages'] = $this->format($villages);
}

return $this;
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 3 additions & 4 deletions tests/unit/CountyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@

/**
* @covers \Uganda\Geo
*/
*/
final class CountyTest extends TestCase {

public function setUp() : void
{
public function setUp(): void {
$this->Uganda = new Geo();
$this->faker = Factory::create();
$this->faker->addProvider(new Address($this->faker));
Expand All @@ -20,7 +19,7 @@ public function setUp() : void
/**
* @runInSeparateProcess
* @covers \Uganda\Geo::counties
*/
*/
public function testUgandaHasCounties() {
$district_data = $this->Uganda->counties()->all();
$data = json_decode($district_data);
Expand Down
26 changes: 12 additions & 14 deletions tests/unit/DistrictTest.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
<?php

use Uganda\Geo;
use PHPUnit\Framework\TestCase;
use Faker\Factory;
use PHPUnit\Framework\TestCase;
use Uganda\Geo;
use \Faker\Provider\en_UG\Address;

/**
* @covers \Uganda\Geo
*/
*/
final class DistrictTest extends TestCase {

public function setUp() : void
{
public function setUp(): void {
$this->Uganda = new Geo();
$this->faker = Factory::create();
$this->faker->addProvider(new Address($this->faker));
Expand All @@ -25,28 +23,28 @@ public function setUp() : void
/**
* @runInSeparateProcess
* @covers \Uganda\Geo::districts
*/
*/
public function testUgandaHasDistricts() {
$district_data = $this->Uganda->districts()->all();
$data = json_decode($district_data);
$this->assertGreaterThanOrEqual(135, $data->districts->count);
}
}

/**
* @runInSeparateProcess
* @covers \Uganda\Geo::districts
*/
*/
public function testDistrictsDoesNotExist() {
$fake_district = $this->faker->name;
$district_data = $this->Uganda->districts($fake_district)->all();
$data = json_decode($district_data);
$this->assertEquals('District '.$fake_district.' does not exist.', $data->errors->district);
}
$this->assertEquals('District ' . $fake_district . ' does not exist.', $data->errors->district);
}

/**
* @runInSeparateProcess
* @covers \Uganda\Geo::districts
*/
*/
public function testDistrictHasCounties() {
$county_data = $this->Uganda->districts($this->district)->counties()->all();
$data = json_decode($county_data);
Expand All @@ -57,7 +55,7 @@ public function testDistrictHasCounties() {
* @runInSeparateProcess
* @covers \Uganda\Geo::districts
* @covers \Uganda\Geo::sub_counties
*/
*/
public function testDistrictHasSubCounties() {
$sub_county_data = $this->Uganda->districts($this->district)->counties($this->county)->sub_counties()->all();
$data = json_decode($sub_county_data);
Expand Down