Answer the question
In order to leave comments, you need to log in
ValueObject field validation?
There is an entity User and VO Token.
User->Token Token
has 2, 3 fields.
Token {
public string $token;
public DateTime $validTill;
public string $ip;
}
abstract class AbstractDateValidityVO extends AbstractVO {
use StringHelperTrait;
protected ?DateTimeImmutable $createdAt;
protected ?DateTimeImmutable $expireAt;
/**
* @throws Exception
*/
public function __construct() {
$this->createdAt = new DateTimeImmutable();
// use minimal time for token among all tokens
$defaultMinutes = 5;
$this->expireAt = (new DateTimeImmutable())->add(new DateInterval(sprintf('PT%dM', $defaultMinutes)));
}
public function value(): object {
return (object)get_object_vars($this);
}
final public function getCreatedAt(): ?DateTimeImmutable {
return $this->createdAt;
}
final public function isValid(): bool {
if (null === $this->getValidTill()) {
return true;
}
return new DateTimeImmutable() < $this->getValidTill();
}
final public function getValidTill(): ?DateTimeImmutable {
return $this->expireAt;
}
}
Answer the question
In order to leave comments, you need to log in
If I understand you correctly, then this is not validation. These are business rules. Business rules are checked in the service/handler/entity/VO and if the condition is not met an exception is thrown.
The check is placed where there is all the necessary data. If the VO has all the data to check, you can put the check there. You are talking about a request, then we visit such a check in the Handler.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question