<h1>Price Range Test</h1>
<p>
The purpose of this class is to create dynamic price ranges (minimum and maximum value ranges)
according to the grouping of prices passed through an array.
</p>
<p>
The return of the class is a list of intervals (minimum / maximum) with the
amount of items that are within each range.
</p>
<?php
$array = array(10, 50, 50, 520, 140, 551, 199, 601, 601, 650, 681, 350, 250, 851, 871, 871, 900, 990, 999, 1001, 1130, 1149, 351, 1300, 4460);
?>
<p>
The array of prices contains:
<?php
foreach($array as $item)
{
echo $item . ', ';
}
?>
</p>
<p>
The result is:
<?php
$t = new PriceRange($array);
echo $t->show();
?>
</p>
<?php
class PriceRange {
//Based on code by Emrah Toy
private $prices;
private $lastElement;
private $highestPrice;
private $minimumPrice;
private $maxPRoductInRange;
private $rangeChart;
private $chart;
function __construct($array_of_prices) {
$this->prices = $array_of_prices;
$this->lastElement= end($this->prices);
$this->highestPrice = round($this->lastElement, -2);
$this->minimumPrice = $this->prices[0];
$this->maxPRoductInRange = 5;
$this->rangeChart = array();
$this->chart = array();
$this->calculateRange();
}
private function calculateRange() {
$this->makeRange($this->minimumPrice,$this->highestPrice);
$count = count($this->rangeChart);
for($a = 0; $a < $count; $a++) {
if(isset($this->rangeChart[$a + 1])) {
$min = $this->rangeChart[$a];
$max = $this->rangeChart[$a + 1];
$result = $this->countItems($min,$max);
if($result[0] > $this->maxPRoductInRange) {
$this->makeRange($min,$max);
$this->calculateRange();
}
}
}
}
private function makeRange($min=0,$max) {
$middleOfRange=($max+$min)/2;
$this->rangeChart[]=$min;
$this->rangeChart[]=$middleOfRange;
$this->rangeChart[]=$max;
$this->rangeChart = array_unique($this->rangeChart);
sort($this->rangeChart, SORT_NUMERIC );
}
private function countItems($min,$max) {
$count=0;
$rest=0;
foreach($this->prices as $price) {
if($price >= $min && $price < $max) {
$count++;
}
else {
$rest++;
}
}
$this->chart[$min] = $count;
return array($count,$rest);
}
function show() {
$minPrices = array_keys($this->chart);
$count = count($minPrices);
$line = '';
for($a = 0; $a < $count; $a++) {
$line .= '<br>';
$line .= $minPrices[$a];
$line .= isset($minPrices[$a+1]) ? ' - ' . $minPrices[$a+1] : '+';
$line .= '('. $this->chart[$minPrices[$a]] .')';
}
return $line;
}
}
?>
I was browsing the dlass given above. I am less stuck with classes terminology, but more confused with general logic.
Since functions declared are intertwined. I get stcuked in understanding of two functions to finally understand the whole class logic:
#1
private function calculateRange() {
$this->makeRange($this->minimumPrice,$this->highestPrice);
$count = count($this->rangeChart);
for($a = 0; $a < $count; $a++) {
if(isset($this->rangeChart[$a + 1])) {
$min = $this->rangeChart[$a];
$max = $this->rangeChart[$a + 1];
$result = $this->countItems($min,$max);
if($result[0] > $this->maxPRoductInRange) {
$this->makeRange($min,$max);
$this->calculateRange();
}
}
}
}
and this one →
function show() {
$minPrices = array_keys($this->chart);
$count = count($minPrices);
$line = '';
for($a = 0; $a < $count; $a++) {
$line .= '<br>';
$line .= $minPrices[$a];
$line .= isset($minPrices[$a+1]) ? ' - ' . $minPrices[$a+1] : '+';
$line .= '('. $this->chart[$minPrices[$a]] .')';
}
return $line;
}