| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets; |
| 4 |
|
| 5 |
use Elementor\Core\Utils\Collection; |
| 6 |
use Elementor\Modules\AtomicWidgets\PropTypes\Prop_Type; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
class Settings_Validator { |
| 13 |
|
| 14 |
private array $schema; |
| 15 |
|
| 16 |
public function __construct( array $schema ) { |
| 17 |
$this->schema = $schema; |
| 18 |
} |
| 19 |
|
| 20 |
public static function make( array $schema ): self { |
| 21 |
return new static( $schema ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* @param array $settings |
| 26 |
* |
| 27 |
* @return array{ |
| 28 |
* 0: bool, |
| 29 |
* 1: array<string, mixed>, |
| 30 |
* 2: array<string> |
| 31 |
* } |
| 32 |
*/ |
| 33 |
public function validate( array $settings ): array { |
| 34 |
$validated = []; |
| 35 |
$errors = []; |
| 36 |
|
| 37 |
foreach ( $settings as $key => $value ) { |
| 38 |
$prop_type = $this->schema[ $key ] ?? null; |
| 39 |
|
| 40 |
if ( ! ( $prop_type instanceof Prop_Type ) ) { |
| 41 |
continue; |
| 42 |
} |
| 43 |
|
| 44 |
try { |
| 45 |
$prop_type->validate_with_additional( $value ); |
| 46 |
} catch ( \Exception $e ) { |
| 47 |
$errors[] = $key; |
| 48 |
continue; |
| 49 |
} |
| 50 |
|
| 51 |
$validated[ $key ] = $value; |
| 52 |
} |
| 53 |
|
| 54 |
$is_valid = empty( $errors ); |
| 55 |
|
| 56 |
return [ |
| 57 |
$is_valid, |
| 58 |
$validated, |
| 59 |
$errors, |
| 60 |
]; |
| 61 |
} |
| 62 |
} |
| 63 |
|