Skip to content

Commit

Permalink
Complete overhaul of unique processing
Browse files Browse the repository at this point in the history
Refs #152
  • Loading branch information
daftspunk committed Dec 19, 2015
1 parent 2e9d938 commit 077f3e9
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 23 deletions.
55 changes: 38 additions & 17 deletions src/Database/Traits/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,9 @@ protected function processValidationRules($rules)
/*
* Normalize rulesets
*/
if (!is_array($ruleParts))
if (!is_array($ruleParts)) {
$ruleParts = explode('|', $ruleParts);
}

/*
* Analyse each rule individually
Expand All @@ -272,22 +273,7 @@ protected function processValidationRules($rules)
* Remove primary key unique validation rule if the model already exists
*/
if (starts_with($rulePart, 'unique') && $this->exists) {
$ruleParts[$key] = 'unique:'.$this->getTable().','.$field.','.$this->getKey().','.$this->getKeyName();

$parameters = explode(',', $rulePart);
// If there were more than three parameters, add them
// back to the rule (idColumn and where statements)
if(count($parameters) > 3) {
$idColumnAndWheres = array_slice($parameters, 3);
$ruleParts[$key] .= ',' . implode(',', $idColumnAndWheres);

$parameters = explode(',', $rulePart);
// If there were more than three parameters, add them
// back to the rule (idColumn and where statements)
if(count($parameters) > 3) {
$idColumnAndWheres = array_slice($parameters, 3);
$ruleParts[$key] .= ',' . implode(',', $idColumnAndWheres);
}
$ruleParts[$key] = $this->processValidationUniqueRule($rulePart, $field);
}
/*
* Look for required:create and required:update rules
Expand All @@ -306,6 +292,41 @@ protected function processValidationRules($rules)
return $rules;
}

/**
* Rebuilds the unique validation rule to force for the existing ID
* @param string $definition
* @param string $fieldName
* @return string
*/
protected function processValidationUniqueRule($definition, $fieldName)
{
list(
$table,
$column,
$key,
$keyName,
$whereColumn,
$whereValue
) = array_pad(explode(',', $definition), 6, null);

$table = 'unique:' . $this->getTable();
$column = $column ?: $fieldName;
$key = $keyName ? $this->$keyName : $this->getKey();
$keyName = $keyName ?: $this->getKeyName();

$params = [$table, $column, $key, $keyName];

if ($whereColumn) {
$params[] = $whereColumn;
}

if ($whereValue) {
$params[] = $whereValue;
}

return implode(',', $params);
}

/**
* Determines if an attribute is required based on the validation rules.
* @param string $attribute
Expand Down
70 changes: 64 additions & 6 deletions tests/Database/Traits/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,83 @@ class ValidationTest extends TestCase

public $exists;

public $id = 20;

public function testUnique()
{
$rules = ['column' => 'unique:table,column,key,id,where,5'];
$processedRules = ['column' => ['unique:table,column,key,id,where,5']];
/*
* Basic usage of unique rule
*/
$rules = ['email' => 'unique:users'];

$this->exists = true;
$this->assertEquals([
'email' => ['unique:users,email,7,the_id']
], $this->processValidationRules($rules));

$this->exists = false;
$this->assertEquals([
'email' => ['unique:users']
], $this->processValidationRules($rules));

/*
* Specifying a custom column name
*/
$rules = ['email' => 'unique:users,email_address'];

$this->exists = true;
$this->assertEquals([
'email' => ['unique:users,email_address,7,the_id']
], $this->processValidationRules($rules));

$this->exists = false;
$this->assertEquals($processedRules, $this->processValidationRules($rules));
$this->assertEquals([
'email' => ['unique:users,email_address']
], $this->processValidationRules($rules));

/*
* Forcing a unique rule to ignore a given ID
*/
$rules = ['email' => 'unique:users,email_address,10'];

$this->exists = true;
$this->assertEquals($processedRules, $this->processValidationRules($rules));
$this->assertEquals([
'email' => ['unique:users,email_address,7,the_id']
], $this->processValidationRules($rules));

$this->exists = false;
$this->assertEquals([
'email' => ['unique:users,email_address,10']
], $this->processValidationRules($rules));

/*
* Adding additional where clauses
*/
$rules = ['email' => 'unique:users,email_address,NULL,id,account_id,1'];

$this->exists = true;
$this->assertEquals([
'email' => ['unique:users,email_address,20,id,account_id,1']
], $this->processValidationRules($rules));

$this->exists = false;
$this->assertEquals([
'email' => ['unique:users,email_address,NULL,id,account_id,1']
], $this->processValidationRules($rules));
}

protected function getTable()
{
return 'table';
return 'users';
}

protected function getKey()
{
return 'key';
return 7;
}

protected function getKeyName()
{
return 'the_id';
}
}

0 comments on commit 077f3e9

Please sign in to comment.