symfony - PHP: Check whether an integer is in a certain interval or not -
i working on achievement system small online game. achievement entity has 4 attributes:
- id
- earnedby
- earnedon
- progress
the last 1 supposed percentage, meaning number between 0 , 100. in order make sure no numbers greater 100 or smaller 0 saved in database, setter-method looks followed (i using symfony2 / doctrine orm):
public function setprogress($progress) { $this->progress = max(min($progress, 100), 0); return $this; }
the important line here max(min($progress, 100), 0)
.
it works totally fine, wanted ask, if there built-in function in php doing thing, , if doing okay (concerning good-developing style)
you should consider adding constraints on entity the validation component of symfony2.
use symfony\component\validator\constraints assert; class achievement { /** * @assert\range(min=0, max=100) */ protected progress; }
the validator service called automatically when example validating forms, can call manually getting validator service, example in controller.
$achievement = new achievement(); $errors = $this->get('validator')->validate($achievement);
Comments
Post a Comment