| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Components; |
| 4 |
|
| 5 |
use Elementor\Core\Utils\Api\Parse_Result; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly. |
| 9 |
} |
| 10 |
|
| 11 |
class Components_Parser { |
| 12 |
const MIN_NAME_LENGTH = 2; |
| 13 |
const MAX_NAME_LENGTH = 50; |
| 14 |
|
| 15 |
public static function make() { |
| 16 |
return new static(); |
| 17 |
} |
| 18 |
|
| 19 |
public function parse_name( $name, $existing_components_names ): Parse_Result { |
| 20 |
$result = Parse_Result::make(); |
| 21 |
|
| 22 |
$sanitized = trim( sanitize_text_field( $name ) ); |
| 23 |
|
| 24 |
if ( strlen( $sanitized ) < self::MIN_NAME_LENGTH ) { |
| 25 |
$result->errors()->add( 'name', 'component_name_too_short_min_' . self::MIN_NAME_LENGTH ); |
| 26 |
} |
| 27 |
|
| 28 |
if ( strlen( $sanitized ) > self::MAX_NAME_LENGTH ) { |
| 29 |
$result->errors()->add( 'name', 'component_name_too_long_max_' . self::MAX_NAME_LENGTH ); |
| 30 |
} |
| 31 |
|
| 32 |
if ( in_array( $sanitized, $existing_components_names, true ) ) { |
| 33 |
$result->errors()->add( 'name', 'duplicated_component_name' ); |
| 34 |
} |
| 35 |
|
| 36 |
return $result->wrap( $sanitized ); |
| 37 |
} |
| 38 |
} |
| 39 |
|