| 1 |
<?php |
| 2 |
namespace Give\DonationForms\Rules; |
| 3 |
|
| 4 |
use Closure; |
| 5 |
use Give\Vendors\StellarWP\Validation\Contracts\Sanitizer; |
| 6 |
use Give\Vendors\StellarWP\Validation\Contracts\ValidatesOnFrontEnd; |
| 7 |
use Give\Vendors\StellarWP\Validation\Contracts\ValidationRule; |
| 8 |
|
| 9 |
class ArrayRule implements ValidationRule, ValidatesOnFrontEnd, Sanitizer |
| 10 |
{ |
| 11 |
/** |
| 12 |
* @var boolean |
| 13 |
*/ |
| 14 |
private $sanitizeAsString; |
| 15 |
|
| 16 |
public function __construct($useSanitizer = false) |
| 17 |
{ |
| 18 |
$this->sanitizeAsString = $useSanitizer; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* @since 3.0.0 |
| 23 |
*/ |
| 24 |
public static function id(): string |
| 25 |
{ |
| 26 |
return 'array'; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* @since 3.0.0 |
| 31 |
*/ |
| 32 |
public static function fromString(string $options = null): ValidationRule |
| 33 |
{ |
| 34 |
return new self(); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* @since 3.0.0 |
| 39 |
*/ |
| 40 |
public function __invoke($value, Closure $fail, string $key, array $values) |
| 41 |
{ |
| 42 |
if (!empty($value) && !is_array($value)) { |
| 43 |
$fail(sprintf(__('%s must be an array', 'give'), '{field}')); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @since 3.0.0 |
| 49 |
*/ |
| 50 |
public function sanitize($value): string |
| 51 |
{ |
| 52 |
return $this->sanitizeAsString ? implode(' | ', $value) : $value; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* @since 3.0.0 |
| 57 |
*/ |
| 58 |
public function serializeOption() |
| 59 |
{ |
| 60 |
return null; |
| 61 |
} |
| 62 |
} |
| 63 |
|