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

Add a function to find inclusive path #20

Merged
merged 2 commits into from
Aug 25, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading