You are here

public static function InstantArticleContentEntityNormalizer::sortComponents in Facebook Instant Articles 3.x

Same name and namespace in other branches
  1. 8.2 src/Normalizer/InstantArticleContentEntityNormalizer.php \Drupal\fb_instant_articles\Normalizer\InstantArticleContentEntityNormalizer::sortComponents()

Sorts a structured array by region then by weight elements.

Parameters

array $a: First item for comparison. The compared items should be associative arrays that include a 'region' element and optionally include a 'weight' element. For items without a 'weight' element, a default value of 0 will be used.

array $b: Second item for comparison.

Return value

int The comparison result for uasort().

File

src/Normalizer/InstantArticleContentEntityNormalizer.php, line 421

Class

InstantArticleContentEntityNormalizer
Facebook Instant Articles content entity normalizer.

Namespace

Drupal\fb_instant_articles\Normalizer

Code

public static function sortComponents(array $a, array $b) {
  $regions = [
    'header' => 0,
    'content' => 1,
    'footer' => 2,
  ];
  $a_region = $a['region'];
  $b_region = $b['region'];
  $a_weight = isset($a['weight']) ? $a['weight'] : 0;
  $b_weight = isset($b['weight']) ? $b['weight'] : 0;

  // Element $a's region comes before element $b.
  if ($regions[$a_region] < $regions[$b_region]) {
    return -1;
  }
  elseif ($regions[$a_region] > $regions[$b_region]) {
    return 1;
  }
  else {
    if ($a_weight == $b_weight) {
      return 0;
    }
    return $a_weight < $b_weight ? -1 : 1;
  }
}