Skip to content

Commit

Permalink
Refactor validator implementation and exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
r3oath committed Oct 6, 2015
1 parent 83bfd38 commit a568825
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 44 deletions.
12 changes: 7 additions & 5 deletions src/Concrete/Data/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
namespace R\Hive\Concrete\Data;

use Illuminate\Contracts\Validation\Factory as ValidationFactory;
use R\Hive\Concrete\Exceptions\ValidatorCreateRulesNotSuppliedException;
use R\Hive\Concrete\Exceptions\ValidatorUpdateRulesNotSuppliedException;
use R\Hive\Concrete\Exceptions\ValidatorRulesNotSuppliedException;
use R\Hive\Contracts\Data\Validator as ValidatorContract;

class Validator implements ValidatorContract
Expand All @@ -20,7 +19,7 @@ public function __construct(ValidationFactory $factory)

public function getCreateRules()
{
throw new ValidatorCreateRulesNotSuppliedException($this);
throw new ValidatorRulesNotSuppliedException($this, 'create');
}

public function getErrors()
Expand All @@ -30,15 +29,15 @@ public function getErrors()

public function getUpdateRules()
{
throw new ValidatorUpdateRulesNotSuppliedException($this);
throw new ValidatorRulesNotSuppliedException($this, 'update');
}

public function hasErrors()
{
return $this->errors !== null;
}

public function isUpdate()
public function markAsUpdate()
{
$this->update = true;
return $this;
Expand All @@ -54,6 +53,9 @@ public function validate($attributes = [])
$this->errors = $validator->errors();
}

// Reset the update flag back to it's default value.
$this->update = false;

return $this;
}
}

This file was deleted.

25 changes: 25 additions & 0 deletions src/Concrete/Exceptions/ValidatorRulesNotSuppliedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace R\Hive\Concrete\Exceptions;

use R\Hive\Concrete\Exceptions\DomainException;
use R\Hive\Contracts\Data\Validator as ValidatorContract;

class ValidatorRulesNotSuppliedException extends DomainException
{
public $rules;
public $name;

public function __construct(ValidatorContract $validator, $rules)
{
$this->name = get_class($validator);
$this->rules = $rules;

$message = sprintf(
'Validator [%s] has no supplied %s rules.',
$this->name,
$this->rules
);
parent::__construct($message);
}
}

This file was deleted.

6 changes: 3 additions & 3 deletions src/Contracts/Data/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public function getCreateRules();
public function getUpdateRules();

/**
* Whether the validation to occur is for an update.
* @return boolean
* Marks this validation request as an update.
* @return mixed
*/
public function isUpdate();
public function markAsUpdate();
}
10 changes: 6 additions & 4 deletions tests/Data/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function ($mock) use ($validator) {
$mock->shouldReceive('getUpdateRules')->atLeast()->once()->andReturn([]);
});

$dummy_validator->isUpdate()->validate(['foo' => 'bar']);
$dummy_validator->markAsUpdate()->validate(['foo' => 'bar']);

$this->assertEquals(false, $dummy_validator->hasErrors());
}
Expand Down Expand Up @@ -86,7 +86,8 @@ function ($mock) use ($validator) {
}

/**
* @expectedException R\Hive\Concrete\Exceptions\ValidatorCreateRulesNotSuppliedException
* @expectedException R\Hive\Concrete\Exceptions\ValidatorRulesNotSuppliedException
* @expectedExceptionMessageRegExp #create.*#
*/
public function testValidatorCreateRulesNotSuppliedException()
{
Expand All @@ -98,14 +99,15 @@ public function testValidatorCreateRulesNotSuppliedException()
}

/**
* @expectedException R\Hive\Concrete\Exceptions\ValidatorUpdateRulesNotSuppliedException
* @expectedException R\Hive\Concrete\Exceptions\ValidatorRulesNotSuppliedException
* * @expectedExceptionMessageRegExp #update.*#
*/
public function testValidatorUpdateRulesNotSuppliedException()
{
$validator = m::mock('Illuminate\Contracts\Validation\Validator');
$factory = m::mock('Illuminate\Contracts\Validation\Factory');

$dummy_validator = new Validator($factory);
$dummy_validator->isUpdate()->validate(['foo' => 'bar']);
$dummy_validator->markAsUpdate()->validate(['foo' => 'bar']);
}
}

0 comments on commit a568825

Please sign in to comment.