<?php
/**
 * @package Hafenstudios\Wellenbrecher
 */

namespace Hafenstudios\Wellenbrecher\Rules;

use Hafenstudios\Wellenbrecher\Engine\Context;
use Hafenstudios\Wellenbrecher\Engine\RuleResult;

defined( 'ABSPATH' ) || exit;

/**
 * Der Inhalt besteht ausschliesslich aus einer URL (kein weiterer Text).
 */
class UrlOnlyRule extends AbstractRule {

	public const ID             = 'url_only';
	public const DEFAULT_POINTS = 15;

	public function __construct( int $points = self::DEFAULT_POINTS ) {
		parent::__construct( $points );
	}

	public function id(): string {
		return self::ID;
	}

	public function stage(): int {
		return 2;
	}

	public function evaluate( Context $context ): RuleResult {
		$content = trim( $context->field( 'content' ) );
		if ( '' === $content ) {
			return $this->miss();
		}
		return preg_match( '#^https?://\S+$#i', $content ) ? $this->hit() : $this->miss();
	}
}
