| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Core\Utils\Api; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
class Parse_Errors { |
| 10 |
|
| 11 |
/** |
| 12 |
* @var array<array{key: string, error: string}> |
| 13 |
*/ |
| 14 |
private array $errors = []; |
| 15 |
|
| 16 |
public static function make() { |
| 17 |
return new static(); |
| 18 |
} |
| 19 |
|
| 20 |
public function add( string $key, string $error ): self { |
| 21 |
$this->errors[] = [ |
| 22 |
'key' => $key, |
| 23 |
'error' => $error, |
| 24 |
]; |
| 25 |
|
| 26 |
return $this; |
| 27 |
} |
| 28 |
|
| 29 |
public function is_empty(): bool { |
| 30 |
return empty( $this->errors ); |
| 31 |
} |
| 32 |
|
| 33 |
public function all(): array { |
| 34 |
return $this->errors; |
| 35 |
} |
| 36 |
|
| 37 |
public function to_string(): string { |
| 38 |
$errors = []; |
| 39 |
|
| 40 |
foreach ( $this->errors as $error ) { |
| 41 |
$errors[] = $error['key'] . ': ' . $error['error']; |
| 42 |
} |
| 43 |
|
| 44 |
return implode( ', ', $errors ); |
| 45 |
} |
| 46 |
|
| 47 |
public function merge( Parse_Errors $errors, ?string $prefix = null ): self { |
| 48 |
foreach ( $errors->all() as $error ) { |
| 49 |
$new_key = $prefix ? "{$prefix}.{$error['key']}" : $error['key']; |
| 50 |
|
| 51 |
$this->add( $new_key, $error['error'] ); |
| 52 |
} |
| 53 |
|
| 54 |
return $this; |
| 55 |
} |
| 56 |
} |
| 57 |
|