| Total Complexity | 7 |
| Total Lines | 84 |
| Duplicated Lines | 0 % |
| Coverage | 95% |
| Changes | 0 | ||
| 1 | <?php |
||
| 8 | class SanitizeSpec extends AbstractSpec |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Value to use when inserting |
||
| 12 | * |
||
| 13 | * @var mixed |
||
| 14 | */ |
||
| 15 | protected $blankValue; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Failure mode for the rule. |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $failureMode = 'HARD_FAILURE'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Invokes the rule the spec is configured for. |
||
| 26 | * |
||
| 27 | * @param object $subject |
||
| 28 | * |
||
| 29 | * @return bool |
||
| 30 | */ |
||
| 31 | 24 | public function __invoke($subject): bool |
|
| 32 | { |
||
| 33 | 24 | if (!$this->subjectFieldIsBlank($subject)) { |
|
| 34 | 9 | return parent::__invoke($subject); |
|
| 35 | } |
||
| 36 | |||
| 37 | 15 | if (!$this->allowBlanks) { |
|
| 38 | 9 | return false; |
|
| 39 | } |
||
| 40 | |||
| 41 | 6 | $subject->{$this->field} = $this->blankValue; |
|
| 42 | |||
| 43 | 6 | return true; |
|
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Set the Sanitize rule to be used. |
||
| 48 | * |
||
| 49 | * @param SanitizeRuleInterface $rule |
||
| 50 | * |
||
| 51 | * @return SanitizeSpec |
||
| 52 | */ |
||
| 53 | 21 | public function to(SanitizeRuleInterface $rule): self |
|
| 54 | { |
||
| 55 | 21 | $this->rule = $rule; |
|
| 56 | 21 | $this->ruleClass = get_class($rule); |
|
| 57 | |||
| 58 | 21 | return $this; |
|
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Sets the blank value to use. |
||
| 63 | * |
||
| 64 | * Defaults to null. |
||
| 65 | * |
||
| 66 | * @param $blankValue |
||
| 67 | * |
||
| 68 | * @return SanitizeSpec |
||
| 69 | */ |
||
| 70 | 9 | public function usingBlank($blankValue = null): self |
|
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Returns the default failure message for this rule specification. |
||
| 80 | * |
||
| 81 | * @return string |
||
| 82 | */ |
||
| 83 | 6 | protected function getDefaultMessage(): string |
|
| 92 | } |
||
| 93 | } |
||
| 94 |