Adding validation on address validFrom date (unique by person)

This commit is contained in:
Marc Ducobu
2016-04-22 16:28:00 +02:00
parent 6fcf5944a0
commit e283c32984
4 changed files with 182 additions and 47 deletions

View File

@@ -669,6 +669,44 @@ class Person implements HasCenterInterface {
}
}
/**
* Return true if the person has two addresses with the
* same validFrom date (in format 'Y-m-d')
*/
public function hasTwoAdressWithSameValidFromDate()
{
$validYMDDates = array();
foreach ($this->addresses as $ad) {
$validDate = $ad->getValidFrom()->format('Y-m-d');
if (in_array($validDate, $validYMDDates)) {
return true;
}
$validYMDDates[] = $validDate;
}
return false;
}
/**
* Validation callback that checks if the addresses are valid (do not have
* two addresses with the same validFrom date)
*
* This method add violation errors.
*/
public function isAddressesValid(ExecutionContextInterface $context)
{
if ($this->hasTwoAdressWithSameValidFromDate()) {
$context->addViolationAt(
'addresses',
'Two addresses has the same validFrom date',
array()
);
}
}
const ERROR_PERIODS_ARE_COLLAPSING = 1; // when two different periods
// have days in commun
const ERROR_ADDIND_PERIOD_AFTER_AN_OPEN_PERIOD = 2; // where there exist
@@ -713,4 +751,4 @@ class Person implements HasCenterInterface {
return true;
}
}
}