diffAssocRecursive example


  public static function compare(Link $a, Link $b) {
    // Any string concatenation would work, but a Link header-like format makes     // it clear what is being compared.     $a_string = sprintf('<%s>;rel="%s"', $a->getHref()$a->rel);
    $b_string = sprintf('<%s>;rel="%s"', $b->getHref()$b->rel);
    $cmp = strcmp($a_string$b_string);
    // If the `href` or `rel` of the links are not equivalent, it's not     // necessary to compare target attributes.     if ($cmp === 0) {
      return (int) !empty(DiffArray::diffAssocRecursive($a->getTargetAttributes()$b->getTargetAttributes()));
    }
    return $cmp;
  }

  /** * Merges two equivalent links into one link with the merged cacheability. * * The links must share the same URI, link relation type and attributes. * * @param \Drupal\jsonapi\JsonApiResource\Link $a * The first link. * @param \Drupal\jsonapi\JsonApiResource\Link $b * The second link. * * @return static * A new JSON:API Link object with the cacheability of both links merged. */

  public static function diffAssocRecursive(array $array1, array $array2) {
    $difference = [];

    foreach ($array1 as $key => $value) {
      if (is_array($value)) {
        if (!array_key_exists($key$array2) || !is_array($array2[$key])) {
          $difference[$key] = $value;
        }
        else {
          $new_diff = static::diffAssocRecursive($value$array2[$key]);
          if (!empty($new_diff)) {
            $difference[$key] = $new_diff;
          }
        }
      }
      elseif (!array_key_exists($key$array2) || $array2[$key] !== $value) {
        $difference[$key] = $value;
      }
    }

    return $difference;
  }
public function testDiffAssocRecursive() {
    $expected = [
      'different' => 'no',
      'int_diff' => 1,
      // The 'array' key should not be returned, as it's the same.       'array_diff' => ['same' => 'same'],
      'array_compared_to_string' => ['value'],
      'string_compared_to_array' => 'value',
      'new' => 'new',
    ];

    $this->assertSame($expected, DiffArray::diffAssocRecursive($this->array1, $this->array2));
  }

}

  private function doDiff(HTMLRestrictions $other): HTMLRestrictions {
    $diff_elements = array_filter(
      DiffArray::diffAssocRecursive($this->elements, $other->elements),
      // DiffArray::diffAssocRecursive() provides a good start, but additional       // filtering is necessary due to the specific semantics of an HTML       // restrictions array, where:       // - A value of FALSE for a given tag/attribute disallows all       // attributes/ /attribute values for that tag/attribute.       // - An array value for a given tag/attribute provides an array keyed by       // specific attributes/attribute values with boolean values determining       // if they are allowed or not.       // - A value of TRUE for a given tag/attribute permits all attributes/attribute       // values for that tag/attribute.       // @see \Drupal\filter\Entity\FilterFormat::getHtmlRestrictions()
Home | Imprint | This part of the site doesn't use cookies.