Search code examples
phpsolid-principlessingle-responsibility-principle

How to design Validator class in regards to SRP?


Is it better to have a validator class which have methods such as validateUrl(), validateEmail(), validateInt(), etc ? Or, URLValidator class, EmailValidator class, and INTValidator class ?


Solution

  • Whilst I agree that this is primarily opinion based I think that you should have a single class per validator. Otherwise what is the single responsibility of your class? To do all the validation? As suggested you could start out with a Validator that did everything and move towards separate classes later or your validator could just be a facade for all validation with the specific validation in separate classes.

    As for the static question, that will likely depend on you need to stub those validators in your tests. In which case having them static will be a pain.