Skip to content

Instantly share code, notes, and snippets.

@ikeogu
Created February 16, 2022 19:59
Show Gist options
  • Save ikeogu/de1be4e79c1689b7571731bf7d596dc4 to your computer and use it in GitHub Desktop.
Save ikeogu/de1be4e79c1689b7571731bf7d596dc4 to your computer and use it in GitHub Desktop.
Qualified asseement
class PlantQueryFilterTest extends TestCase {
function createSmallData() {
return [
[
'id' => 11,
'scientific_name' => 'Phacelia scopulina (A. Nelson) J.T. Howell var. scopulina',
'common_name' => 'Debeque Phacelia',
'family' => 'Hydrophyllaceae'
],
[
'id' => 12,
'scientific_name' => 'Pogonatum urnigerum (Hedw.) P. Beauv.',
'common_name' => 'Pogonatum Moss',
'family' => 'Polytrichaceae'
],
[
'id' => 13,
'scientific_name' => 'Phacelia infundibuliformis Torr.',
'common_name' => 'Rio Grande Phacelia',
'family' => 'Hydrophyllaceae'
],
[
'id' => 14,
'scientific_name' => 'Campylium halleri (Hedw.) Lindb.',
'common_name' => 'Haller\'s Campylium Moss',
'family' => 'Amblystegiaceae'
]
];
}
public function testBasicFunctionality() {
$data = $this->createSmallData();
$actual = query_filter("moss", "common_name", $data);
$expected = [
[
'id' => 12,
'scientific_name' => 'Pogonatum urnigerum (Hedw.) P. Beauv.',
'common_name' => 'Pogonatum Moss',
'family' => 'Polytrichaceae'
],
[
'id' => 14,
'scientific_name' => 'Campylium halleri (Hedw.) Lindb.',
'common_name' => 'Haller\'s Campylium Moss',
'family' => 'Amblystegiaceae'
]
];
$this->assertEquals(
$expected, $actual,
"Expected\n\n" . var_export($actual, true) .
"\n\nto equal\n\n" .
var_export($expected, true) . "\n\n"
);
}
}
solution
function query_filter($query, $field, $data) {
$arr = array_filter(
$data, function ($e) use ($query, $field) {
return !is_bool(stripos($e[$field], $query));
}
);
usort($arr,function($a,$b){
return $a['id'] <=>$b['id'];
});
return $arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment