Decoupling Validation Messages

With some minor changes to our existing implementation, we're able to decouple the validation messages from the validator:

class LocationValidationHandler implements ValidationHandler
{
public function handleCityNotFoundInCountry();

public function handleInvalidPostcodeForCity();
}

class LocationValidator
{
private $location;
private $validationHandler;

public function __construct(
Location $location,
LocationValidationHandler $validationHandler
) {
$this->location = $location;
$this->validationHandler = $validationHandler;
}

public function validate()
{
if (!$this->location->country()->hasCity(
$this->location->city()
)) {
$this->validationHandler->handleCityNotFoundInCountry();
}

if (! $this->location->city()->isPostcodeValid(
$this->location->postcode()
)) {
$this->validationHandler->handleInvalidPostcodeForCity();
}
}
}

We also need to change the signature of the validation method to the following:

class Location
{
// ...

public function validate(
LocationValidationHandler $validationHandler
) {
$validator = new LocationValidator($this, $validationHandler);
$validator->validate();
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.137.240