Automattic\WooCommerce\Internal\PushNotifications\Validators

PushTokenValidator::validate_originprivate staticWC 10.6.0

Validates origin.

Method of the class: PushTokenValidator{}

No Hooks.

Returns

true|false|WP_Error.

Usage

$result = PushTokenValidator::validate_origin( $value, ?array $context );
$value(mixed) (required)
The value to validate.
?array $context
.
Default: array()

Changelog

Since 10.6.0 Introduced.

PushTokenValidator::validate_origin() code WC 10.8.1

private static function validate_origin( $value, ?array $context = array() ) {
	if ( is_null( $value ) ) {
		return new WP_Error( self::ERROR_CODE, 'Origin is required.' );
	}

	if ( ! is_string( $value ) ) {
		return new WP_Error( self::ERROR_CODE, 'Origin must be a string.' );
	}

	$value = trim( $value );

	if ( '' === $value ) {
		return new WP_Error( self::ERROR_CODE, 'Origin cannot be empty.' );
	}

	if ( ! in_array( $value, PushToken::ORIGINS, true ) ) {
		return new WP_Error(
			self::ERROR_CODE,
			sprintf( 'Origin must be one of: %s.', implode( ', ', PushToken::ORIGINS ) )
		);
	}

	return true;
}