Skip to content

Commit 8e2c0e8

Browse files
authored
Add a function to find inclusive path (#20)
* Add a function to find inclusive path Fixes #19 * Fixed code smell issues on test file
1 parent a5f8bb8 commit 8e2c0e8

4 files changed

Lines changed: 47 additions & 11 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"test": [
3636
"phpunit --debug tests/TestDataObject",
3737
"phpunit --debug tests/TestDataPath",
38+
"phpunit --debug tests/TestDataFilters",
3839
"phpunit --debug tests/TestValidation"
3940
],
4041
"phpstan": "phpstan analyse src tests",

src/Data/DataFilters.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,23 @@ public function __construct(array $options)
5757
public function filter(string $path, $data)
5858
{
5959
if (!isset($this->options[$path])) {
60-
// TODO: #19 find inclusive path
61-
// $path = $this->findInclusivePaths($path, $this->options);
60+
$path = $this->findInclusivePaths($path, $this->options);
6261

63-
return $data;
62+
if (empty($path)) {
63+
return $data;
64+
}
6465
}
6566

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

6869
if (is_array($data)) {
69-
return array_filter($data, function ($item) use ($filters) {
70-
return $this->filterValue($item, $filters);
70+
array_walk_recursive($data, function (&$item, $key) use ($filters, $path) {
71+
if (!is_array($item) && substr($path, strrpos($path, '/') + 1) === $key) {
72+
$item = $this->filterValue($item, $filters);
73+
}
7174
});
75+
76+
return $data;
7277
}
7378

7479
return $this->filterValue($data, $filters);
@@ -84,11 +89,11 @@ public function filter(string $path, $data)
8489
*/
8590
private function filterValue($value, $filters)
8691
{
87-
$filterType = $filters['type'] ?? 'string';
88-
$filterArgs = $filters['args'] ?? [];
92+
$isFiltered = false;
93+
$filterType = $filters['type'] ?? 'string';
94+
$filterArgs = $filters['args'] ?? [];
8995
$filterCallback = $filters['callback'] ?? null;
90-
91-
$value = $this->castType($value, $filterType);
96+
$value = $this->castType($value, $filterType);
9297

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

102-
return call_user_func_array($filterCallback, $args);
107+
$isFiltered = ! call_user_func_array($filterCallback, $args);
103108
}
104109

105-
return $value;
110+
return $isFiltered ? false : $value;
106111
}
107112
}

src/Helpers/DataParsers.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,30 @@ public function findPaths(string $path, array $data, ?Closure $closure = null):
6565

6666
return $paths;
6767
}
68+
69+
/**
70+
* Finds the most inclusive path in the options array
71+
*
72+
* @param string $path
73+
* @param array $options
74+
*
75+
* @return string
76+
*/
77+
protected function findInclusivePaths(string $path, array $options): string
78+
{
79+
foreach ($options as $optionPath => $value) {
80+
$asterixIndex = 0;
81+
82+
while ($asterixPos = strpos($optionPath, '*', $asterixIndex)) {
83+
$asterixIndex = $asterixPos + 1;
84+
$path = substr_replace($path, '*', $asterixPos, strpos($path, '/', $asterixPos) - $asterixPos);
85+
}
86+
87+
if ($path === $optionPath) {
88+
return $optionPath;
89+
}
90+
}
91+
92+
return '';
93+
}
6894
}

tests/TestDataFilters.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,9 @@ public function testDataFilter()
8181
);
8282

8383
$this->assertFalse($test_value_age['persons'][0]['age']);
84+
$this->assertSame(34, $test_value_age['persons'][2]['age']);
85+
86+
print_r($test_value_name);
87+
print_r($test_value_age);
8488
}
8589
}

0 commit comments

Comments
 (0)