/** * Find price separator for the quantile * * @param int $quantileNumber should be from 1 to n-1 where n is number of intervals * @return array|null */ protected function _findPriceSeparator($quantileNumber) { if ($quantileNumber < 1 || $quantileNumber >= $this->getIntervalsNumber()) { return null; } $prices = array(); $quantileInterval = $this->_getQuantileInterval($quantileNumber); $intervalPricesCount = $quantileInterval[1] - $quantileInterval[0] + 1; $offset = $quantileInterval[0]; if (!is_null($this->_lastPriceLimiter[0])) { $offset -= $this->_lastPriceLimiter[0]; } if ($offset < 0) { $intervalPricesCount += $offset; $prices = array_slice($this->_prices, $this->_lastPriceLimiter[0] + $offset - $this->_quantileInterval[0], -$offset); $offset = 0; } $lowerPrice = $this->_lastPriceLimiter[1]; if (!is_null($this->_lowerLimit)) { $lowerPrice = max($lowerPrice, $this->_lowerLimit); } if ($intervalPricesCount >= 0) { $prices = array_merge($prices, $this->_pricesModel->loadPrices($intervalPricesCount + 1, $offset, $lowerPrice, $this->_upperLimit)); } $lastPrice = $prices[$intervalPricesCount - 1]; $bestRoundPrice = array(); if ($lastPrice == $prices[0]) { if ($quantileNumber == 1 && $offset) { $additionalPrices = $this->_pricesModel->loadPreviousPrices($lastPrice, $quantileInterval[0], $this->_lowerLimit); if ($additionalPrices) { $quantileInterval[0] -= count($additionalPrices); $prices = array_merge($additionalPrices, $prices); $bestRoundPrice = $this->_findRoundPrice($prices[0] + Mage_Catalog_Model_Resource_Layer_Filter_Price::MIN_POSSIBLE_PRICE / 10, $lastPrice, false); } } if ($quantileNumber == $this->getIntervalsNumber() - 1) { $pricesCount = count($prices); if ($prices[$pricesCount - 1] > $lastPrice) { $additionalPrices = array($prices[$pricesCount - 1]); } else { $additionalPrices = $this->_pricesModel->loadNextPrices($lastPrice, $this->_count - $quantileInterval[0] - count($prices), $this->_upperLimit); } if ($additionalPrices) { $quantileInterval[1] = $quantileInterval[0] + count($prices) - 1; if ($prices[$pricesCount - 1] <= $lastPrice) { $quantileInterval[1] += count($additionalPrices); $prices = array_merge($prices, $additionalPrices); } $upperBestRoundPrice = $this->_findRoundPrice($lastPrice + Mage_Catalog_Model_Resource_Layer_Filter_Price::MIN_POSSIBLE_PRICE / 10, $prices[count($prices) - 1], false); $this->_mergeRoundPrices($bestRoundPrice, $upperBestRoundPrice); } } } else { $bestRoundPrice = $this->_findRoundPrice($prices[0] + Mage_Catalog_Model_Resource_Layer_Filter_Price::MIN_POSSIBLE_PRICE / 10, $lastPrice); } $this->_quantileInterval = $quantileInterval; $this->_prices = $prices; if (empty($bestRoundPrice)) { $this->_skippedQuantilesUpperLimits[$quantileNumber] = $quantileInterval[1]; return $bestRoundPrice; } $pricesCount = count($prices); if ($prices[$pricesCount - 1] > $lastPrice) { $this->_lastPriceLimiter = array($quantileInterval[0] + $pricesCount - 1, $prices[$pricesCount - 1]); } ksort($bestRoundPrice, SORT_NUMERIC); foreach ($bestRoundPrice as $index => &$bestRoundPriceValues) { if (empty($bestRoundPriceValues)) { unset($bestRoundPrice[$index]); } else { sort($bestRoundPriceValues); } } return array_reverse($bestRoundPrice); }