Automattic\WooCommerce\Internal\PushNotifications\Validators

PushTokenValidator::validate_device_uuidprivate staticWC 10.6.0

Validates device UUID.

Method of the class: PushTokenValidator{}

No Hooks.

Returns

true|false|WP_Error.

Usage

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

Changelog

Since 10.6.0 Introduced.

PushTokenValidator::validate_device_uuid() code WC 10.8.1

private static function validate_device_uuid( $value, ?array $context = array() ) {
	/**
	 * We may or may not have platform; if we don't have it, we can skip the
	 * platform-specific checks and allow the platform validation to trigger
	 * the failure.
	 */
	$maybe_platform = $context['platform'] ?? null;

	if (
		PushToken::PLATFORM_APPLE === $maybe_platform
		|| PushToken::PLATFORM_ANDROID === $maybe_platform
	) {
		/**
		 * The browser platform doesn't use a device UUID, so we don't need
		 * to check truthiness or format unless the platform is not browser.
		 */
		if ( is_null( $value ) ) {
			return new WP_Error( self::ERROR_CODE, 'Device UUID is required.' );
		}

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

		$value = trim( $value );

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

		if ( ! preg_match( self::DEVICE_UUID_FORMAT, $value ) ) {
			return new WP_Error( self::ERROR_CODE, 'Device UUID is an invalid format.' );
		}
	}

	if (
		is_string( $value )
		&& strlen( $value ) > self::DEVICE_UUID_MAXIMUM_LENGTH ) {
		/**
		 * Check maximum length for all device UUIDs sent, regardless of
		 * platform. We don't know for sure the value is a string as the
		 * check above isn't guaranteed to have run, so ensure it is a
		 * string before evaluating this validation rule.
		 */
		return new WP_Error(
			self::ERROR_CODE,
			sprintf( 'Device UUID exceeds maximum length of %s.', self::DEVICE_UUID_MAXIMUM_LENGTH )
		);
	}

	return true;
}