Automattic\WooCommerce\Internal\PushNotifications\Validators

PushTokenValidator::validate_device_localeprivate staticWC 10.6.0

Validates device locale.

Method of the class: PushTokenValidator{}

No Hooks.

Returns

true|false|WP_Error.

Usage

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

Changelog

Since 10.6.0 Introduced.

PushTokenValidator::validate_device_locale() code WC 10.8.1

private static function validate_device_locale( $value, ?array $context = array() ) {
	if ( ! isset( $value ) ) {
		return new WP_Error( self::ERROR_CODE, 'Device locale is required.' );
	}

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

	$value = trim( $value );

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

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

	return true;
}