Skip to content

Commit

Permalink
Add a function to find inclusive path (#20)
Browse files Browse the repository at this point in the history
* Add a function to find inclusive path
Fixes #19

* Fixed code smell issues on test file
  • Loading branch information
uuur86 authored Aug 25, 2024
1 parent a5f8bb8 commit 8e2c0e8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"test": [
"phpunit --debug tests/TestDataObject",
"phpunit --debug tests/TestDataPath",
"phpunit --debug tests/TestDataFilters",
"phpunit --debug tests/TestValidation"
],
"phpstan": "phpstan analyse src tests",
Expand Down
27 changes: 16 additions & 11 deletions src/Data/DataFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,23 @@ public function __construct(array $options)
public function filter(string $path, $data)
{
if (!isset($this->options[$path])) {
// TODO: #19 find inclusive path
// $path = $this->findInclusivePaths($path, $this->options);
$path = $this->findInclusivePaths($path, $this->options);

return $data;
if (empty($path)) {
return $data;
}
}

$filters = $this->options[$path];

if (is_array($data)) {
return array_filter($data, function ($item) use ($filters) {
return $this->filterValue($item, $filters);
array_walk_recursive($data, function (&$item, $key) use ($filters, $path) {
if (!is_array($item) && substr($path, strrpos($path, '/') + 1) === $key) {
$item = $this->filterValue($item, $filters);
}
});

return $data;
}

return $this->filterValue($data, $filters);
Expand All @@ -84,11 +89,11 @@ public function filter(string $path, $data)
*/
private function filterValue($value, $filters)
{
$filterType = $filters['type'] ?? 'string';
$filterArgs = $filters['args'] ?? [];
$isFiltered = false;
$filterType = $filters['type'] ?? 'string';
$filterArgs = $filters['args'] ?? [];
$filterCallback = $filters['callback'] ?? null;

$value = $this->castType($value, $filterType);
$value = $this->castType($value, $filterType);

if (is_callable($filterCallback) && $filterCallback instanceof Closure) {
$args = [$value];
Expand All @@ -99,9 +104,9 @@ private function filterValue($value, $filters)
$args[] = $filterArgs;
}

return call_user_func_array($filterCallback, $args);
$isFiltered = ! call_user_func_array($filterCallback, $args);
}

return $value;
return $isFiltered ? false : $value;
}
}
26 changes: 26 additions & 0 deletions src/Helpers/DataParsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,30 @@ public function findPaths(string $path, array $data, ?Closure $closure = null):

return $paths;
}

/**
* Finds the most inclusive path in the options array
*
* @param string $path
* @param array $options
*
* @return string
*/
protected function findInclusivePaths(string $path, array $options): string
{
foreach ($options as $optionPath => $value) {
$asterixIndex = 0;

while ($asterixPos = strpos($optionPath, '*', $asterixIndex)) {
$asterixIndex = $asterixPos + 1;
$path = substr_replace($path, '*', $asterixPos, strpos($path, '/', $asterixPos) - $asterixPos);
}

if ($path === $optionPath) {
return $optionPath;
}
}

return '';
}
}
4 changes: 4 additions & 0 deletions tests/TestDataFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,9 @@ public function testDataFilter()
);

$this->assertFalse($test_value_age['persons'][0]['age']);
$this->assertSame(34, $test_value_age['persons'][2]['age']);

print_r($test_value_name);
print_r($test_value_age);
}
}

0 comments on commit 8e2c0e8

Please sign in to comment.