You are here

class EntityShareUtility in Entity Share 8.3

Same name and namespace in other branches
  1. 8.2 src/EntityShareUtility.php \Drupal\entity_share\EntityShareUtility

Contains helper methods for Entity Share.

Hierarchy

Expanded class hierarchy of EntityShareUtility

20 files declare their use of EntityShareUtility
AuthenticationTestBase.php in modules/entity_share_client/tests/src/Functional/AuthenticationTestBase.php
BlockFieldBlockContentImporter.php in modules/entity_share_client/src/Plugin/EntityShareClient/Processor/BlockFieldBlockContentImporter.php
ChangedTime.php in modules/entity_share_client/src/Plugin/EntityShareClient/Processor/ChangedTime.php
DiffController.php in modules/entity_share_diff/src/Controller/DiffController.php
EmbeddedEntityImporter.php in modules/entity_share_client/src/Plugin/EntityShareClient/Processor/EmbeddedEntityImporter.php

... See full list

File

src/EntityShareUtility.php, line 12

Namespace

Drupal\entity_share
View source
class EntityShareUtility {

  /**
   * Uniformize JSON data in case of single value.
   *
   * @param array $data
   *   The JSON data.
   *
   * @return array
   *   An array of data.
   */
  public static function prepareData(array $data) {
    if (self::isNumericArray($data)) {
      return $data;
    }
    else {
      return [
        $data,
      ];
    }
  }

  /**
   * Check if a array is numeric.
   *
   * @param array $array
   *   The array to check.
   *
   * @return bool
   *   TRUE if the array is numeric. FALSE in case of associative array.
   */
  public static function isNumericArray(array $array) {
    foreach (array_keys($array) as $a) {
      if (!is_int($a)) {
        return FALSE;
      }
    }
    return TRUE;
  }

  /**
   * Converts any expected "changed" time value into integer timestamp.
   *
   * Needed mostly for converting times coming from Remotes.
   *
   * @param string|int $changed_time
   *   The timestamp or formatted date of "changed" date.
   *
   * @return int
   *   The timestamp of "changed" date.
   */
  public static function convertChangedTime($changed_time) {
    $entity_changed_time = 0;

    // If the website is using backward compatible timestamps output.
    // @see https://www.drupal.org/node/2859657.
    // The value is cast in integer for
    // https://www.drupal.org/node/2837696.
    if (is_numeric($changed_time)) {
      $entity_changed_time = (int) $changed_time;
    }
    else {
      $changed_datetime = \DateTime::createFromFormat(\DateTime::RFC3339, $changed_time);
      if ($changed_datetime) {
        $entity_changed_time = $changed_datetime
          ->getTimestamp();
      }
    }
    return $entity_changed_time;
  }

  /**
   * Alters the JSON:API URL by applying filtering by UUID's.
   *
   * @param string $url
   *   URL to request.
   * @param string[] $uuids
   *   Array of entity UUID's.
   *
   * @return string
   *   The URL with UUID filter.
   */
  public static function prepareUuidsFilteredUrl(string $url, array $uuids) {
    $parsed_url = UrlHelper::parse($url);
    $query = $parsed_url['query'];
    $query['filter']['uuid-filter'] = [
      'condition' => [
        'path' => 'id',
        'operator' => 'IN',
        'value' => $uuids,
      ],
    ];
    $query = UrlHelper::buildQuery($query);
    return $parsed_url['path'] . '?' . $query;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityShareUtility::convertChangedTime public static function Converts any expected "changed" time value into integer timestamp.
EntityShareUtility::isNumericArray public static function Check if a array is numeric.
EntityShareUtility::prepareData public static function Uniformize JSON data in case of single value.
EntityShareUtility::prepareUuidsFilteredUrl public static function Alters the JSON:API URL by applying filtering by UUID's.