You are here

final class UrlData in Commerce Core 8.2

Encodes and decodes array data in a URL-safe way.

Hierarchy

Expanded class hierarchy of UrlData

4 files declare their use of UrlData
TaxNumberController.php in modules/tax/src/Controller/TaxNumberController.php
TaxNumberDefaultFormatter.php in modules/tax/src/Plugin/Field/FieldFormatter/TaxNumberDefaultFormatter.php
TaxNumberTest.php in modules/tax/tests/src/FunctionalJavascript/TaxNumberTest.php
UrlDataTest.php in tests/src/Unit/UrlDataTest.php

File

src/UrlData.php, line 8

Namespace

Drupal\commerce
View source
final class UrlData {

  /**
   * Encodes the given data.
   *
   * @param array $data
   *   The data.
   *
   * @return string
   *   The encoded data.
   */
  public static function encode(array $data) {
    $data = json_encode($data);

    // URL-safe Base64 encoding (base64url).
    $data = str_replace([
      '+',
      '/',
      '=',
    ], [
      '-',
      '_',
      '',
    ], base64_encode($data));
    return $data;
  }

  /**
   * Decodes the given data.
   *
   * @param string $data
   *   The encoded data.
   *
   * @return array|false
   *   The decoded data, or FALSE if decoding failed.
   */
  public static function decode($data) {
    $data = base64_decode(str_replace([
      '-',
      '_',
    ], [
      '+',
      '/',
    ], $data));
    if ($data) {
      $data = json_decode($data, TRUE);
    }
    return is_array($data) ? $data : FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UrlData::decode public static function Decodes the given data.
UrlData::encode public static function Encodes the given data.