commerce_amws-8.x-1.x-dev/src/Utilities.php

src/Utilities.php
<?php

namespace Drupal\commerce_amws;

use Drupal\commerce_price\Price;

/**
 * Wrapper class for utility functions.
 */
class Utilities {

  /**
   * Merges two or more arrays recursively.
   *
   * Similarly to `array_merge`, it merges the elements of two or more arrays
   * together so that the values of one are appended to the end of the previous
   * one.
   *
   * If the input arrays have the same string keys, then the later value
   * for that key will overwrite the previous one.
   *
   * If, however, the arrays contain numeric keys, the later value will not
   * overwrite the original value, but will be appended. Values in the input
   * array with numeric keys will be renumbered with incrementing keys starting
   * from zero in the result array.
   *
   * If the input arrays contain array values, they will be merged recursively
   * with the rules described.
   *
   * @param array $array1
   *   The first array to merge.
   * @param array $array2
   *   The second array to merge.
   *
   * @return array
   *   The resulting array.
   */
  public static function arrayMergeRecursive(array $array1, array $array2) {
    // Get all input arrays. We still use arguments in the function declaration
    // so that we get type casting and minimum number of arguments without
    // writing additional code.
    $arrays = func_get_args();

    // No  need to loop over the first array, just copy it.
    $merged = array_shift($arrays);

    while ($arrays) {
      $array = array_shift($arrays);
      if (!is_array($array)) {
        throw new \InvalidArgumentException(
          sprintf(
            'All arguments must be arrays for merging them recursively, "%s" given.',
            json_encode($array)
          )
        );
      }

      // Loop over all array items. If we have a string key and array values in
      // both arrays, merge the values recursively. Otherwise, append the value
      // if the key is integer or replace the value if the key is string.
      foreach ($array as $key => $value) {
        if (is_string($key)) {
          if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
            $merged[$key] = self::arrayMergeRecursive($merged[$key], $value);
            continue;
          }

          $merged[$key] = $value;
        }
        else {
          $merged[] = $value;
        }
      }
    }

    return $merged;
  }

  /**
   * Converts an Amazon MWS price object to a Drupal Price object.
   *
   * Since we convert Model objects from the Selling Partner API PHP SDK to
   * `\stdClass` objects as required by the Entity Synchronization module, we
   * expect Amazon MWS price objects to be `\stdClass` objects as well.
   *
   * @param object $amws_price
   *   The Amazon MWS price.
   *
   * @return \Drupal\commerce_price\Price
   *   The Drupal price object.
   */
  public static function amwsPriceToDrupalPrice(\stdClass $amws_price) {
    return new Price($amws_price->Amount, $amws_price->CurrencyCode);
  }

  /**
   * Validates that an array has a value in exactly one of the given keys.
   *
   * NULL values are considered not set. An option could be provided for
   * checking for existence i.e. use `array_key_exists` instead of `isset`.
   *
   * This method is provided for facilitating argument validation in API
   * clients. We therefore throw an `\InvalidArgumentException` instead of
   * return a `TRUE` or `FALSE` result. If we find the need to use it elsewhere
   * we could provide an option to return the result instead of throwing an
   * exception.
   *
   * @param array $array
   *   The array to validate.
   * @param array $keys
   *   The keys to check.
   *
   * @throws \InvalidArgumentException
   *   If zero or more than one array elements exist with a non-NULL value in
   *   any of the given keys.
   */
  public static function validateArrayKeysExactlyOneOf(
    array $array,
    array $keys
  ) {
    $count = 0;
    foreach ($keys as $key) {
      if (isset($array[$key])) {
        $count++;
      }
    }

    if ($count === 1) {
      return;
    }

    throw new \InvalidArgumentException(sprintf(
      'Exactly one of the following array elements must be provided, %s given: %s.',
      $count,
      implode(', ', $keys)
    ));
  }

  /**
   * Converts a Unix timestamp to an ISO 8601 date as expected by the SP-API.
   *
   * @param int $timestamp
   *   The Unix timestamp to convert.
   *
   * @return string
   *   The time in ISO 8601 format and in UTC timezone.
   *
   * @I Validate that the input is a Unix timestamp
   *    type     : bug
   *    priority : normal
   *    labels   : validation
   */
  public static function timestampToIso8601($timestamp) {
    $datetime = new \DateTime('now', new \DateTimeZone('UTC'));
    $datetime->setTimestamp($timestamp);

    return $datetime->format('Y-m-d\TH:i:s') . 'Z';
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc