Skip to content

Commit

Permalink
Merge pull request #59 from rakit/58-fix-unexpected-get-valid-data
Browse files Browse the repository at this point in the history
Fixes #58
  • Loading branch information
emsifa authored Nov 3, 2018
2 parents 4c9261b + b39e98c commit b989676
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 28 deletions.
7 changes: 6 additions & 1 deletion src/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ public function isArrayAttribute()
return count($this->getKeyIndexes()) > 0;
}

public function isUsingDotNotation()
{
return strpos($this->getKey(), '.') !== false;
}

public function resolveSiblingKey($key)
{
$indexes = $this->getKeyIndexes();
Expand Down Expand Up @@ -165,4 +170,4 @@ public function getAlias()
return $this->alias;
}

}
}
32 changes: 31 additions & 1 deletion src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,36 @@ public static function arraySet(&$target, $key, $value, $overwrite = true)
return $target;
}


/**
* Unset an item on an array or object using dot notation.
*
* @param mixed $target
* @param string|array $key
* @return mixed
*/
public static function arrayUnset(&$target, $key)
{
if (!is_array($target)) {
return $target;
}

$segments = is_array($key) ? $key : explode('.', $key);
$segment = array_shift($segments);

if ($segment == '*') {
$target = [];
} elseif ($segments) {
if (array_key_exists($segment, $target)) {
static::arrayUnset($target[$segment], $segments);
}
} elseif (array_key_exists($segment, $target)) {
unset($target[$segment]);
}

return $target;
}

/**
* Get snake_case format from given string
*
Expand All @@ -171,7 +201,7 @@ public static function snakeCase($value, $delimiter = '_')
$value = preg_replace('/\s+/u', '', ucwords($value));
$value = strtolower(preg_replace('/(.)(?=[A-Z])/u', '$1'.$delimiter, $value));
}

return $value;
}

Expand Down
30 changes: 16 additions & 14 deletions src/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Validation
protected $aliases = [];

protected $messageSeparator = ':';

protected $validData = [];
protected $invalidData = [];

Expand Down Expand Up @@ -72,7 +72,7 @@ protected function validateAttribute(Attribute $attribute)
}

$attributeKey = $attribute->getKey();
$rules = $attribute->getRules();
$rules = $attribute->getRules();

$value = $this->getValue($attributeKey);
$isEmptyValue = $this->isEmptyValue($value);
Expand All @@ -92,7 +92,7 @@ protected function validateAttribute(Attribute $attribute)
if ($isEmptyValue AND $this->ruleIsOptional($attribute, $ruleValidator)) {
continue;
}

if (!$valid) {
$isValid = false;
$this->addError($attribute, $value, $ruleValidator);
Expand Down Expand Up @@ -254,8 +254,8 @@ protected function isEmptyValue($value)

protected function ruleIsOptional(Attribute $attribute, Rule $rule)
{
return false === $attribute->isRequired() AND
false === $rule->isImplicit() AND
return false === $attribute->isRequired() AND
false === $rule->isImplicit() AND
false === $rule instanceof Required;
}

Expand Down Expand Up @@ -288,14 +288,14 @@ protected function resolveMessage(Attribute $attribute, $value, Rule $validator)
];

if ($primaryAttribute) {
// insert primaryAttribute keys
// insert primaryAttribute keys
// $message_keys = [
// $attributeKey.$this->messageSeparator.$ruleKey,
// >> here [1] <<
// $attributeKey,
// >> and here [3] <<
// $ruleKey
// ];
// ];
$primaryAttributeKey = $primaryAttribute->getKey();
array_splice($message_keys, 1, 0, $primaryAttributeKey.$this->messageSeparator.$ruleKey);
array_splice($message_keys, 3, 0, $primaryAttributeKey);
Expand Down Expand Up @@ -359,7 +359,7 @@ protected function resolveRules($rules)
foreach($rules as $i => $rule) {
if (empty($rule)) continue;
$params = [];

if (is_string($rule)) {
list($rulename, $params) = $this->parseRule($rule);
$validator = call_user_func_array($validatorFactory, array_merge([$rulename], $params));
Expand Down Expand Up @@ -446,7 +446,7 @@ protected function resolveInputAttributes(array $inputs)
$resolvedInputs = [];
foreach($inputs as $key => $rules) {
$exp = explode(':', $key);

if (count($exp) > 1) {
// set attribute alias
$this->aliases[$exp[0]] = $exp[1];
Expand All @@ -457,16 +457,17 @@ protected function resolveInputAttributes(array $inputs)

return $resolvedInputs;
}

public function getValidatedData() {
return array_merge($this->validData, $this->invalidData);
}

protected function setValidData(Attribute $attribute, $value)
{
$key = $attribute->getKey();
if ($attribute->isArrayAttribute()) {
Helper::arraySet($this->validData, $key, $value);
if ($attribute->isArrayAttribute() || $attribute->isUsingDotNotation()) {
Helper::arraySet($this->validData, $key, $value);
Helper::arrayUnset($this->invalidData, $key);
} else {
$this->validData[$key] = $value;
}
Expand All @@ -480,8 +481,9 @@ public function getValidData()
protected function setInvalidData(Attribute $attribute, $value)
{
$key = $attribute->getKey();
if ($attribute->isArrayAttribute()) {
Helper::arraySet($this->invalidData, $key, $value);
if ($attribute->isArrayAttribute() || $attribute->isUsingDotNotation()) {
Helper::arraySet($this->invalidData, $key, $value);
Helper::arrayUnset($this->validData, $key);
} else {
$this->invalidData[$key] = $value;
}
Expand Down
34 changes: 32 additions & 2 deletions tests/HelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testArrayGet()
$this->assertEquals(Helper::arrayGet($array, 'foo.bar'), $array['foo']['bar']);
$this->assertEquals(Helper::arrayGet($array, 'foo.bar.baz'), $array['foo']['bar']['baz']);
$this->assertEquals(Helper::arrayGet($array, 'one.two.three'), 123);

$this->assertNull(Helper::arrayGet($array, 'foo.bar.baz.qux'));
$this->assertNull(Helper::arrayGet($array, 'one.two'));
}
Expand Down Expand Up @@ -89,7 +89,7 @@ public function testArraySet()

Helper::arraySet($array, 'comments.*.id', null, false);
Helper::arraySet($array, 'comments.*.x.y', 1, false);

$this->assertEquals($array, [
'comments' => [
['id' => null, 'text' => 'foo', 'x' => ['y' => 1]],
Expand All @@ -99,4 +99,34 @@ public function testArraySet()
]);
}

public function testArrayUnset()
{
$array = [
'users' => [
'one' => 'user_one',
'two' => 'user_two',
],
'stuffs' => [1, 'two', ['three'], null, false, true],
'message' => "lorem ipsum",
];

Helper::arrayUnset($array, 'users.one');
$this->assertEquals($array, [
'users' => [
'two' => 'user_two',
],
'stuffs' => [1, 'two', ['three'], null, false, true],
'message' => "lorem ipsum",
]);

Helper::arrayUnset($array, 'stuffs.*');
$this->assertEquals($array, [
'users' => [
'two' => 'user_two',
],
'stuffs' => [],
'message' => "lorem ipsum",
]);
}

}
52 changes: 42 additions & 10 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function testRequiredUploadedFile()
$validation = $this->validator->validate([
'file' => $empty_file
], [
'file' => 'required|uploaded_file'
'file' => 'required|uploaded_file'
]);

$errors = $validation->errors();
Expand All @@ -97,20 +97,20 @@ public function testOptionalUploadedFile()
$validation = $this->validator->validate([
'file' => $empty_file
], [
'file' => 'uploaded_file'
'file' => 'uploaded_file'
]);
$this->assertTrue($validation->passes());
}

/**
* @dataProvider getSamplesMissingKeyFromUploadedFileValue
*/
*/
public function testMissingKeyUploadedFile($uploaded_file)
{
$validation = $this->validator->validate([
'file' => $uploaded_file
], [
'file' => 'required|uploaded_file'
'file' => 'required|uploaded_file'
]);

$errors = $validation->errors();
Expand Down Expand Up @@ -335,7 +335,7 @@ public function testAfterRule()

$this->assertFalse($validator2->passes());
}

public function testNewValidationRuleCanBeAdded()
{

Expand Down Expand Up @@ -735,7 +735,7 @@ public function testUsingDefaults()
'is_enabled' => '1',
'is_published' => 'invalid-value'
]);

// Getting only valid data
$validData = $validation->getValidData();
$this->assertEquals($validData, [
Expand Down Expand Up @@ -914,13 +914,22 @@ public function testGetValidData()
'something',
'[email protected]'
],
'stuffs' => [
'one' => '1',
'two' => '2',
'three' => 'three',
],
'thing' => 'exists',
], [
'thing' => 'required',
'items.*.product_id' => 'required|numeric',
'emails.*' => 'required|email',
'items.*.qty' => 'required|numeric',
'something' => 'default:on|required|in:on,off'
'something' => 'default:on|required|in:on,off',
'stuffs' => 'required|array',
'stuffs.one' => 'required|numeric',
'stuffs.two' => 'required|numeric',
'stuffs.three' => 'required|numeric',
]);

$validData = $validation->getValidData();
Expand All @@ -936,8 +945,15 @@ public function testGetValidData()
2 => '[email protected]'
],
'thing' => 'exists',
'something' => 'on'
'something' => 'on',
'stuffs' => [
'one' => '1',
'two' => '2',
]
], $validData);

$stuffs = $validData['stuffs'];
$this->assertFalse(isset($stuffs['three']));
}

public function testGetInvalidData()
Expand All @@ -954,13 +970,22 @@ public function testGetInvalidData()
'something',
'[email protected]'
],
'stuffs' => [
'one' => '1',
'two' => '2',
'three' => 'three',
],
'thing' => 'exists',
], [
'thing' => 'required',
'items.*.product_id' => 'required|numeric',
'emails.*' => 'required|email',
'items.*.qty' => 'required|numeric',
'something' => 'required|in:on,off'
'something' => 'required|in:on,off',
'stuffs' => 'required|array',
'stuffs.one' => 'numeric',
'stuffs.two' => 'numeric',
'stuffs.three' => 'numeric',
]);

$invalidData = $validation->getInvalidData();
Expand All @@ -974,8 +999,15 @@ public function testGetInvalidData()
'emails' => [
1 => 'something'
],
'something' => null
'something' => null,
'stuffs' => [
'three' => 'three',
]
], $invalidData);

$stuffs = $invalidData['stuffs'];
$this->assertFalse(isset($stuffs['one']));
$this->assertFalse(isset($stuffs['two']));
}

}

0 comments on commit b989676

Please sign in to comment.