'', 'slug' => '', 'description' => null, 'is_active' => true, 'sort_order' => 0 ], $current, $input ); if ( ! is_string( $data['name'] ) || '' === trim( sanitize_text_field( $data['name'] ) ) ) { self::invalid( 'name', __( 'Name is required.', 'abece-booking' ) ); } $data['name'] = sanitize_text_field( $data['name'] ); if ( mb_strlen( $data['name'] ) > 150 ) { self::invalid( 'name', __( 'Name must not exceed 150 characters.', 'abece-booking' ) ); } if ( ! is_string( $data['slug'] ) ) { self::invalid( 'slug', __( 'Slug must be text.', 'abece-booking' ) ); } $data['slug'] = sanitize_title( '' === trim( $data['slug'] ) ? $data['name'] : $data['slug'] ); if ( '' === $data['slug'] || strlen( $data['slug'] ) > 150 ) { self::invalid( 'slug', __( 'Slug is required and must not exceed 150 characters.', 'abece-booking' ) ); } if ( null !== $data['description'] && ! is_string( $data['description'] ) ) { self::invalid( 'description', __( 'Description must be text.', 'abece-booking' ) ); } $data['description'] = null === $data['description'] ? null : sanitize_textarea_field( $data['description'] ); $data['is_active'] = self::boolean( $data['is_active'], 'is_active' ); $data['sort_order'] = self::integer( $data['sort_order'], 'sort_order', -2147483648, 2147483647 ); return $data; } public static function boolean( mixed $value, string $field ): bool { if ( ! is_bool( $value ) ) { self::invalid( $field, __( 'A boolean value is required.', 'abece-booking' ) ); } return $value; } public static function integer( mixed $value, string $field, int $min, int $max, bool $nullable = false ): ?int { if ( null === $value && $nullable ) { return null; } if ( ! is_int( $value ) || $value < $min || $value > $max ) { self::invalid( $field, __( 'An integer within the allowed range is required.', 'abece-booking' ) ); } return $value; } public static function choice( mixed $value, string $field, array $allowed ): string { if ( ! is_string( $value ) || ! in_array( $value, $allowed, true ) ) { self::invalid( $field, __( 'The selected value is not allowed.', 'abece-booking' ) ); } return $value; } public static function invalid( string $field, string $message ): never { throw new CatalogException( 'abece_booking_invalid_field', $message, 400, $field ); } }