<?php

namespace Planaday\Utility\DataFilters\Validators;

class IsHexColor implements Validator {
	protected string $message;

	public function __construct(?string $message = null) {
		$this->message = $message ?? 'did not provide a valid hex color';
	}

	/**
	 * @param $data
	 *
	 * @return string|true
	 */
	public function validate( $data ): string|bool {
		if ($data === null) return true;
		if (is_string($data) && preg_match( '/^#(([a-f0-9]{3}){1,2}|([a-f0-9]{4}){1,2})$/i', $data )) return true;

		return $this->message;
	}

	public function getName(): string {
		return "is_hex_color";
	}
}