<?php

namespace Planaday\Utility\DataFilters\Validators;

class IsEmail implements Validator {
	protected string $message;

	public function __construct(?string $message = null) {
		$this->message = $message ?? 'supplied an invalid email address';
	}

	/**
	 * @param mixed $data
	 *
	 * @return string|true
	 */
	public function validate($data): string|bool {
		if (empty($data)) {
			return true;
		}

		if ( !filter_var( $data, FILTER_VALIDATE_EMAIL ) ) {
			return $this->message;
		}

		return true;
	}

	public function getName(): string {
		return 'is_email';
	}
}