setValueMin($valueMin); $this->setValueMax($valueMax); } /** * @inheritDoc */ public function parseValue (string $val) { if (!$this->isInt($val)) { throw new RangeException('La valeur n\'est pas un entier valide : ' . $val); } $int = (int)$val; if ($this->hasValueMin()) { if ($int < $this->getValueMin()) { throw new RangeException('La valeur est inférieure au minimum (' . $this->getValueMin() . ') :' . $int); } } if ($this->hasValueMax()) { if ($int > $this->getValueMax()) { throw new RangeException('La valeur est supérieur au maximum (' . $this->getValueMax() . ') : ' . $int); } } return $int; } /** * Vérifie si la valeur est bien un nombre entier ? * * @return bool Est-ce que la valeur est bien un nombre entier ? */ protected function isInt ($val): bool { if (!is_numeric($val)) { return false; } if (floor($val) != $val) { return false; } return true; } /** * @inheritDoc */ public function getValueDescription (): string { return ($this->hasValueMin() ? $this->getValueMin() . ' <= ' : '') . 'integer' . ($this->hasValueMax() ? ' <= ' . $this->getValueMax() : ''); } /** * La valeur minimumm autorisée * * @return int|null La valeur minimum. * * @see $valueMin */ public function getValueMin (): ?int { return $this->valueMin; } /** * Définit la valeur minimum autorisée. * * @param int|null $valueMin La valeur minimum. * * @return $this */ public function setValueMin (?int $valueMin = null): self { if (!is_null($valueMin)) { if (!$this->isInt($valueMin)) { throw new InvalidArgumentException('Le minimum n\'est pas un entier ou null : ' . $valueMin); } } $this->valueMin = $valueMin; return $this; } /** * Est-ce qu'il existe une limite minimum ? * * @return boolean True s'il existe une limite, sinon False */ public function hasValueMin (): bool { return !is_null($this->getValueMin()); } /** * La valeur maximum autorisée * * @return int|null La valeur maximum. * * @see $valueMax */ public function getValueMax (): ?int { return $this->valueMax; } /** * Définit la valeur maximum autorisée. * * @param int|null $valueMax La valeur maximum. * * @return $this */ public function setValueMax (?int $valueMax = null): self { if (!is_null($valueMax)) { if (!$this->isInt($valueMax)) { throw new InvalidArgumentException('Le maximum n\'est pas un entier ou null : ' . $valueMax); } } $this->valueMax = $valueMax; return $this; } /** * Est-ce qu'il existe une limite maximum ? * * @return boolean True s'il existe une limite, sinon False */ public function hasValueMax (): bool { return !is_null($this->getValueMax()); } }