<?php

namespace HelpGent\App\Support\Element\Fields;

defined( 'ABSPATH' ) || exit;

use HelpGent\App\Support\Element\Element;
use HelpGent\App\Support\Element\Summary\Pagination;
use HelpGent\WpMVC\RequestValidator\Validator;
use WP_REST_Request;

class LongText extends Element {

    use Pagination;

    public static function get_key(): string {
        return 'long_text';
    }

    public function validate( WP_REST_Request $request, Validator $validator, array $element ) {
        self::request_validator( $request, $validator, $element );
    }

    public static function request_validator( WP_REST_Request $request, Validator $validator, array $element ) {
        $attributes = $element['attributes'];

        $rules = [];

        if ( '1' === $attributes['required'] ) {
            $rules[] = 'required';
        }

        $rules[] = 'string';

        if ( '1' === $attributes['is_max_length'] ) {
            $max     = $attributes['max_length'];
            $rules[] = "max:{$max}";
        }

        $validator->validate(
            [
                $element['element_type'] => implode( '|', $rules ),
            ]
        );
    }
}