Automattic\WooCommerce\Internal\PushNotifications\Validators

PushTokenValidator::validate_platformprivate staticWC 10.6.0

Validates platform.

Method of the class: PushTokenValidator{}

No Hooks.

Returns

true|false|WP_Error.

Usage

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

Changelog

Since 10.6.0 Introduced.

PushTokenValidator::validate_platform() code WC 10.8.1

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

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

	$value = trim( $value );

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

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

	return true;
}