Skip to content

Commit

Permalink
Sanitize only passed data (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
norbybaru authored and William committed Apr 25, 2019
1 parent d978b8f commit ab12ea9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,15 @@ public function sanitize()
$sanitized = $this->data;

foreach ($this->rules as $attr => $rules) {
$value = Arr::get($this->data, $attr);
if (Arr::has($this->data, $attr)) {
$value = Arr::get($this->data, $attr);

foreach ($rules as $rule) {
$value = $this->applyFilter($rule['name'], $value, $rule['options']);
}
foreach ($rules as $rule) {
$value = $this->applyFilter($rule['name'], $value, $rule['options']);
}

Arr::set($sanitized, $attr, $value);
Arr::set($sanitized, $attr, $value);
}
}

return $sanitized;
Expand Down
18 changes: 18 additions & 0 deletions tests/SanitizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,22 @@ public function it_throws_exception_if_non_existing_filter()
];
$data = $this->sanitize($data, $rules);
}

public function test_it_should_only_sanitize_passed_data()
{
$data = [
'title' => ' Hello WoRlD '
];

$rules = [
'title' => 'trim',
'name' => 'trim|escape'
];

$data = $this->sanitize($data, $rules);

$this->assertArrayNotHasKey('name', $data);
$this->assertArrayHasKey('title', $data);
$this->assertEquals(1, count($data));
}
}

0 comments on commit ab12ea9

Please sign in to comment.