1: <?php
2:
3: namespace Charcoal\Validator;
4:
5: // Local namespace dependencies
6: use \Charcoal\Validator\ValidatorInterface as ValidatorInterface;
7:
8: /**
9: * A full default implementation, as trait, of the ValidatableInterface.
10: *
11: * There is one additional abstract method: `create_validator()`
12: */
13: trait ValidatableTrait
14: {
15: /**
16: * In-objet copy of the `ValidatorInterface` validator object
17: * @var ValidatorInterface $validator
18: */
19: protected $validator;
20:
21: /**
22: * @param ValidatorInterface $validator The validator object to use for validation.
23: * @return ValidatableInterface Chainable
24: */
25: public function setValidator(ValidatorInterface $validator)
26: {
27: $this->validator = $validator;
28: return $this;
29: }
30:
31: /**
32: * @return ValidatorInterface
33: */
34: public function validator()
35: {
36: if ($this->validator === null) {
37: $this->validator = $this->createValidator();
38: }
39: return $this->validator;
40: }
41:
42: /**
43: * @return ValidatorInterface
44: */
45: abstract protected function createValidator();
46:
47: /**
48: * @param ValidatorInterface $v Optional. A custom validator object to use for validation. If null, use object's.
49: * @return boolean
50: */
51: public function validate(ValidatorInterface &$v = null)
52: {
53: if ($v !== null) {
54: $this->setValidator($v);
55: }
56:
57: return $this->validator()->validate();
58: }
59: }
60: