Automattic\WooCommerce\Internal\Checkout

DateFormatLimit{} │ final │ WC 1.0└─ Keyword

Compares date strings using the ajv-formats limit keywords.

No Hooks.

Usage

$DateFormatLimit = new DateFormatLimit();
// use class methods

Methods

  1. public __construct( string $keyword, $limit )
  2. public validate( ValidationContext $context, Schema $schema )

DateFormatLimit{} code WC 11.2.1

final class DateFormatLimit implements Keyword {

	use ErrorTrait;

	/**
	 * The format comparison keyword.
	 *
	 * @var string
	 */
	private $keyword;

	/**
	 * The date limit or a reference to its value.
	 *
	 * @var string|JsonPointer
	 */
	private $limit;

	/**
	 * Sets the comparison and its limit.
	 *
	 * @since 11.2.0
	 *
	 * @param string             $keyword The comparison keyword.
	 * @param string|JsonPointer $limit   The date limit or data reference.
	 */
	public function __construct( string $keyword, $limit ) {
		$this->keyword = $keyword;
		$this->limit   = $limit;
	}

	/**
	 * Checks the date against its limit without changing either value.
	 *
	 * @since 11.2.0
	 *
	 * @param ValidationContext $context The current document and value.
	 * @param Schema            $schema  The schema being evaluated.
	 * @return ValidationError|null The comparison error, if any.
	 */
	public function validate( ValidationContext $context, Schema $schema ): ?ValidationError {
		$limit = $this->limit instanceof JsonPointer
			? $this->limit->data( $context->rootData(), $context->currentDataPath(), $this )
			: $this->limit;

		// AJV skips an unresolved $data reference, but rejects one with the wrong type.
		if ( $this === $limit ) {
			return null;
		}

		if ( ! is_string( $limit ) ) {
			return $this->error( $schema, $context, $this->keyword, 'The date limit must be a string.' );
		}

		// ajv-formats does not compare dates when either string is empty.
		if ( '' === $limit || '' === $context->currentData() ) {
			return null;
		}

		$comparison = strcmp( $context->currentData(), $limit );
		switch ( $this->keyword ) {
			case 'formatMinimum':
				$valid = $comparison >= 0;
				break;
			case 'formatMaximum':
				$valid = $comparison <= 0;
				break;
			case 'formatExclusiveMinimum':
				$valid = $comparison > 0;
				break;
			case 'formatExclusiveMaximum':
				$valid = $comparison < 0;
				break;
			default:
				return $this->error( $schema, $context, $this->keyword, 'Unsupported date comparison keyword.' );
		}

		return $valid ? null : $this->error(
			$schema,
			$context,
			$this->keyword,
			'Date does not satisfy {keyword}: {limit}.',
			array(
				'keyword' => $this->keyword,
				'limit'   => $limit,
			)
		);
	}
}