Skip to content

Commit

Permalink
Refactor namespace and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kusaasira committed Oct 21, 2022
1 parent 3bfc79b commit e00bd6f
Show file tree
Hide file tree
Showing 20 changed files with 228 additions and 191 deletions.
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
129 changes: 19 additions & 110 deletions src/Uganda/Geo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,130 +2,39 @@

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);
}
return json_encode(end($properties));
}

/**
/**
* Retrieve count
* @param array $properties
*/
public function count(){
$properties = get_object_vars($this);
if (is_array($properties) || is_object($properties)){
foreach($properties as $p){
foreach($p as $x){
return number_format($x['count']);
}
}
}
public function count() {
$properties = get_object_vars($this);
if (is_array($properties) || is_object($properties)) {
foreach ($properties as $p) {
foreach ($p as $x) {
return number_format($x['count']);
}
}
}
}



}

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.
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

0 comments on commit e00bd6f

Please sign in to comment.