*/ class Valid implements Validator { private Config $config; public function __construct(Config $config) { $this->config = $config; } /** * @param User $entity */ public function validate(Entity $entity, string $field, Data $data): ?Failure { $value = $entity->getUserName(); if ($value === null) { return null; } /** @var ?string $regExp */ $regExp = $this->config->get('userNameRegularExpression'); if (!$regExp) { throw new RuntimeException("No `userNameRegularExpression` in config."); } if (strpos($value, ' ') !== false) { return Failure::create(); } if (preg_replace("/{$regExp}/", '_', $value) !== $value) { return Failure::create(); } return null; } }