You are here

class crumbs_Container_WeightMap in Crumbs, the Breadcrumbs suite 7.2

Can determine a weight for a rule key based on wildcard weights.

E.g. if the weight settings are

  • = 0

crumbs.* = 4 crumbs.nodeParent.* = 5 and then we are looking for a weight for crumbs.nodeParent.page then the weight map will return 5, because crumbs.nodeParent.* is the best matching wildcard.

Hierarchy

Expanded class hierarchy of crumbs_Container_WeightMap

File

lib/Container/WeightMap.php, line 15

View source
class crumbs_Container_WeightMap extends crumbs_Container_WildcardData {

  /**
   * @var array
   */
  private $localWeightMaps = array();

  /**
   * @param array $data
   *   Weights with wildcards, as saved in the configuration form.
   */
  function __construct(array $data) {
    asort($data);
    parent::__construct($data);
  }

  /**
   * Get the smallest weight in range.
   *
   * @return int
   *   The smallest weight..
   */
  function smallestValue() {
    foreach ($this->data as $value) {
      if ($value !== FALSE) {
        return $value;
      }
    }
    return FALSE;
  }

  /**
   * Gets a local weight map with a prefix.
   * E.g. if the config contains a weight setting "crumbs.nodeParent.* = 5",
   * then in a local weight map with prefix "crumbs", this will be available as
   * "nodeParent.* = 5".
   *
   * @param string $prefix
   *   The prefix.
   *
   * @return self
   *   The local weight map.
   *
   * @see crumbs_Container_WildcardData::prefixedContainer()
   */
  function localWeightMap($prefix) {
    if (!isset($this->localWeightMaps[$prefix])) {
      $data = $this
        ->buildPrefixedData($prefix);
      $this->localWeightMaps[$prefix] = new self($data);
    }
    return $this->localWeightMaps[$prefix];
  }

  /**
   * @param true[] $candidates
   *   Format: $[$candidateKey] = true
   *
   * @return mixed[][]
   *   Format: $[0|1][$candidateKey] = $weight|false
   */
  function sortCandidateKeys($candidates) {
    $buckets = array();
    $disabledCandidates = array();
    foreach ($candidates as $key => $cTrue) {
      $weight = $this
        ->valueAtKey($key);
      if (FALSE !== $weight) {
        $buckets[$weight][$key] = $weight;
      }
      else {
        $disabledCandidates[$key] = FALSE;
      }
    }
    ksort($buckets);
    $sorted = array();
    foreach ($buckets as $bucket) {
      $sorted += $bucket;
    }
    return array(
      $sorted,
      $disabledCandidates,
    );
  }

  /**
   * @param mixed[] $candidates
   *   Format: $[$candidateKey] = $candidateValue
   *
   * @return string|null
   *   The candidate key with the smallest weight, or NULL if none found.
   */
  function findBestCandidateKey(array $candidates) {
    $bestKey = NULL;
    $bestWeight = NULL;
    $this
      ->findBetterCandidateKey($bestKey, $bestWeight, $candidates);
    return $bestKey;
  }

  /**
   * Check if the candidates contain a key with better weight than the one
   * given.
   *
   * @param string|null $bestKey
   * @param int|null $bestWeight
   * @param mixed[] $candidates
   *   Format: $[$candidateKey] = $candidateValue
   *
   * @return string|null
   *   The candidate key with the smallest weight, or NULL if none found.
   */
  function findBetterCandidateKey(&$bestKey, &$bestWeight, array $candidates) {
    foreach ($candidates as $key => $value) {
      $weight = $this
        ->valueAtKey($key);
      if ($weight === FALSE) {
        continue;
      }
      if (!isset($bestWeight) || $weight < $bestWeight) {
        $bestWeight = $weight;
        $bestKey = $key;
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
crumbs_Container_WeightMap::$localWeightMaps private property
crumbs_Container_WeightMap::findBestCandidateKey function
crumbs_Container_WeightMap::findBetterCandidateKey function Check if the candidates contain a key with better weight than the one given.
crumbs_Container_WeightMap::localWeightMap function Gets a local weight map with a prefix. E.g. if the config contains a weight setting "crumbs.nodeParent.* = 5", then in a local weight map with prefix "crumbs", this will be available as "nodeParent.* = 5".
crumbs_Container_WeightMap::smallestValue function Get the smallest weight in range.
crumbs_Container_WeightMap::sortCandidateKeys function
crumbs_Container_WeightMap::__construct function Overrides crumbs_Container_WildcardData::__construct
crumbs_Container_WildcardData::$data protected property
crumbs_Container_WildcardData::$fallback protected property
crumbs_Container_WildcardData::buildPrefixedData protected function Helper: Actually build the prefixed container.
crumbs_Container_WildcardData::getAll function Determine the values for the key and all wildcard parents.
crumbs_Container_WildcardData::getAllMerged function If the values are arrays, then this one merges the array values for the key and all wildcard parents.
crumbs_Container_WildcardData::valueAtKey function Determine the value for the rule specified by the key.
crumbs_Container_WildcardData::wildcardValue protected function Helper: Resolve wildcards..