Skip to content

Commit

Permalink
Merge pull request #2 from sagautam5/dev
Browse files Browse the repository at this point in the history
Basic search and recursive search on each entity
  • Loading branch information
sagautam5 authored Jan 20, 2021
2 parents f53a374 + 1c0d137 commit b1f235f
Show file tree
Hide file tree
Showing 19 changed files with 329 additions and 101 deletions.
4 changes: 3 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ Before submitting a pull request:
```sh
composer update
```
- Create and test the feature/issue and make sure it doesn't affect codebase by running full tests by executing following console command.
- Create and test the feature/issue and make sure it doesn't affect codebase by running full tests by executing following console command for two different environment variables.

Set APP_LANG to np and en respectively and run following command for each value of language.
```sh
vendor/bin/phpunit
```
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 27 additions & 1 deletion docs/usage/Category.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,30 @@ Once you initiate category entity, you can retrieve variety of data.

```php
['id', 'name', 'short_code']
```
```

5. Recursive search with multiple set of key value data

```php
<?php

use Sagautam5\LocalStateNepal\Entities\Category;

$category = new Category();
$params = [
['key' => 'name', 'value' => 'some name', 'exact' => false],
['key' => 'id', 'value' => 'some id', 'exact' => true]
];
$categoryDetails = $category->recursiveSearch($params);

// if you have already filtered category data in structured format, then you can pass the data in recursive search
$data = $category->search('name', 'some name', false);

$categoryDetails = $category->search($params, $data);
```

List of options for parameter key:

```php
['id', 'name', 'short_code']
```
28 changes: 27 additions & 1 deletion docs/usage/District.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,30 @@ Once you initiate district entity, you can retrieve variety of data.

```php
['id', 'province_id', 'name', 'area_sq_km', 'website', 'headquarter'];
```
```
8. Recursive search with multiple set of key value data

```php
<?php

use Sagautam5\LocalStateNepal\Entities\District;

$district = new District();
$params = [
['key' => 'headquarter', 'value' => 'some name', 'exact' => true],
['key' => 'id', 'value' => 'some id', 'exact' => true]
];
$districtDetails = $district->recursiveSearch($params);

// if you have already filtered district data in structured format, then you can pass the data in recursive search
$data = $district->search('name', 'some name', false);

$districtDetails = $district->search($params, $data);
```
List of options for parameter key:

```php
['id', 'province_id', 'name', 'area_sq_km', 'website', 'headquarter'];
```
28 changes: 27 additions & 1 deletion docs/usage/Municipality.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,30 @@ Once you initiate municipality entity, you can retrieve variety of data.

```php
['id', 'district_id', 'category_id', 'name', 'area_sq_km', 'website', 'wards']
```
```

9. Recursive search with multiple set of key value data

```php
<?php

use Sagautam5\LocalStateNepal\Entities\Municipality;

$municipality = new Municipality();
$params = [
['key' => 'municipality_id', 'value' => 'some id', 'exact' => true],
['key' => 'wards', 'value' => 'some ward', 'exact' => true]
];
$municipalityDetails = $municipality->recursiveSearch($params);

// if you have already filtered municipality data in structured format, then you can pass the data in recursive search
$data = $municipality->search('name', 'some name', false);

$municipalityDetails = $municipality->search($params, $data);
```

List of options for parameter key:

```php
['id', 'district_id', 'category_id', 'name', 'area_sq_km', 'website', 'wards'];
```
28 changes: 27 additions & 1 deletion docs/usage/Province.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,30 @@ Once you initiate province entity, you can retrieve variety of data.

```php
['id', 'name', 'area_sq_km', 'website', 'headquarter'];
```
```

8. Recursive search with multiple set of key value data

```php
<?php

use Sagautam5\LocalStateNepal\Entities\Province;

$province = new Province();
$params = [
['key' => 'name', 'value' => 'some name', 'exact' => false],
['key' => 'headquarter', 'value' => 'some name', 'exact' => true]
];
$provinceDetails = $province->recursiveSearch($params);

// if you have already filtered province data in structured format, then you can pass the data in recursive search
$data = $province->search('name', 'some name', false);

$provinceDetails = $province->search($params, $data);
```

List of options for parameter key:

```php
['id', 'name', 'area_sq_km', 'website', 'headquarter'];
```
11 changes: 6 additions & 5 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src/</directory>
</include>
</coverage>

<php>
<env name="APP_ENV" value="testing"/>
<env name="APP_NAME" value="LOCAL_STATES_NEPAL"/>
<env name="APP_LANG" value="np"/>
</php>
</phpunit>
47 changes: 47 additions & 0 deletions src/Entities/BaseEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php


namespace Sagautam5\LocalStateNepal\Entities;


class BaseEntity
{
/**
* @var
*/
protected $items;

/**
* Filter data by key value pair
*
* @param $key
* @param $value
* @param $data
* @param bool $exact
* @return array
*/
protected function filter($key, $value, $data, $exact = false)
{
return array_values(array_filter($data, function ($item) use ($key, $value, $exact) {
return ($exact ? ($item->$key == $value ? true:false) :(is_int(strpos($item->$key, $value)) ? true:false));
}));
}

/**
* Recursive Search Data
*
* @param $params
* @param array $data
* @return array|mixed
*/
public function recursiveSearch($params, $data = [])
{
$data = $data ? $data : $this->items;

$param = count($params) ? array_pop($params):null;

$data = $param ? $this->filter($param['key'], $param['value'], $data, $param['exact']):$data;

return ($param && $data) ? $this->recursiveSearch($params, $data):$data;
}
}
24 changes: 9 additions & 15 deletions src/Entities/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@
* Class Category
* @package Sagautam5\LocalStateNepal\Entities
*/
class Category
class Category extends BaseEntity
{
/**
* @var
*/
private $categories;

/**
* @var string
*/
Expand All @@ -32,7 +27,7 @@ public function __construct($lang = 'en')
$this->lang = $lang;

$loader = new CategoriesLoader($this->lang);
$this->categories = $loader->categories();
$this->items = $loader->categories();
}catch (LoadingException $exception){
throw $exception;
}
Expand All @@ -45,7 +40,7 @@ public function __construct($lang = 'en')
*/
public function allCategories()
{
return $this->categories;
return $this->items;
}

/**
Expand All @@ -56,9 +51,9 @@ public function allCategories()
*/
public function find($id)
{
$key = (array_search($id, array_column($this->categories, 'id')));
$key = (array_search($id, array_column($this->items, 'id')));

return is_int($key) ? $this->categories[$key]:null;
return is_int($key) ? $this->items[$key]:null;
}

/**
Expand All @@ -69,9 +64,9 @@ public function find($id)
*/
public function findByShortCode($short_code)
{
$key = (array_search($short_code, array_column($this->categories, 'short_code')));
$key = (array_search($short_code, array_column($this->items, 'short_code')));

return is_int($key) ? $this->categories[$key]:null;
return is_int($key) ? $this->items[$key]:null;
}

/**
Expand All @@ -85,8 +80,7 @@ public function findByShortCode($short_code)
public function search($key, $value, $exact = false)
{
$categories = $this->allCategories();
return array_filter($categories, function ($item) use ($key, $value, $exact) {
return $exact ? ($item->$key == $value ? true:false) :(is_int(strpos($item->$key, $value)) ? true:false);
});

return $this->filter($key, $value, $categories, $exact);
}
}
Loading

0 comments on commit b1f235f

Please sign in to comment.