Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 59
StatsTotalIt
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 8
756.00
0.00% covered (danger)
0.00%
0 / 59
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 5
 arrayAdd
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 3
 dataAdd
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 6
 get_stat
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 7
 get_statHeirarchy
0.00% covered (danger)
0.00%
0 / 1
30.00
0.00% covered (danger)
0.00%
0 / 11
 get_basicStats
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 7
 get_basicStatsFlat
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 9
 get_basicStatsHeirarchy
0.00% covered (danger)
0.00%
0 / 1
30.00
0.00% covered (danger)
0.00%
0 / 11
<?php
namespace ia\Math;
/**
 * Register basic running statics for values in $valuesKeyedBy consolidating/totalizing by $consolidateByKeys
 *
 * @requires ia\Math\Stats.php
 *
 * @author Raul Santos
 * @license MIT
 * @copyright 2017
 * @version 1.0.3
 */
class StatsTotalIt {
    protected $decimals;
    protected $exceptions; // ia\Math\Stats::MISSING_*, ia\Math\Stats::NA_*
    const SEPARATOR = "\t"; // private
    protected $consolidateByKeys; //['key1','key2']
    protected $valuesKeyedBy; //['val_to_sum_1', 'val_to_sum_2']
    protected $stats = [];
    /**
     *
     * @param array $consolidateByKeys ['totalsBy_1','subtotalsBy_2',...]
     * @param array $valuesKeyedBy Keys for values to generate totals & subtotals ['value_1',...]
     * @param integer $decimals 2
     * @param integer $exceptions How to manage missing, null, N/A & not numeric. ia\Math\Stats::MISSING_ ia\Math\Stats::NA_ default Asume set to 0.00
     * @return void
     */
    public function __construct($consolidateByKeys, $valuesKeyedBy, $decimals = 2, $exceptions = 8) {
        $this->consolidateByKeys = array_flip($consolidateByKeys);
        $this->valuesKeyedBy = $valuesKeyedBy;
        $this->decimals = $decimals;
        $this->exceptions = $exceptions;
    }
/////////////////
//
/////////////////
    /**
     * Add Data from an array of datapoints
     *
     * @param array $arrayOfDataPoints [['totalsBy_1','totalsBy_2',...,'value_1',...], ...]
     * @return void
     */
    public function arrayAdd($arrayOfDataPoints) {
        foreach($arrayOfDataPoints as $dataPoint) {
            $this->dataAdd($dataPoint);
        }
    }
    /**
     * Add one Data point to the dataset
     *
     * @param array $dataPoint ['totalsBy_1','totalsBy_2',...,'value_1',...]
     * @return void
     */
    public function dataAdd($dataPoint) {
        $keyHash = implode(self::SEPARATOR, array_intersect_key($dataPoint, $this->consolidateByKeys));
        foreach($this->valuesKeyedBy as $key) {
            if(!isset($this->stats[$keyHash][$key])) {
                $this->stats[$keyHash][$key] = new Stats($this->decimals, $this->exceptions);
            }
            $this->stats[$keyHash][$key]->push(isset($dataPoint[$key]) ? $dataPoint[$key] : null);
        }
    }
/////////////////
//
/////////////////
    /**
     * Get desired statistic by stat name ia\Math\Stats:STAT_*
     *
     * @param string $statName ia\Math\Stats:STAT_* min,avg,max,variance,stdDev, skewness, kurtosis, coefficient of variation (cv)
     * @return array ["totalsBy_1\ttotalsBy_2"=>['totalsBy_1', 'totalsBy_2', ..., 'values_1']=>statValue, ...]
     */
    public function get_stat($statName = 'sum') {
        $ret = [];
        foreach($this->stats as $hashKey => $values) {
            $put = explode(self::SEPARATOR, $hashKey);
            foreach($values as $key => $stats) {
                $put[$key] = $stats->stats()[$statName];
            }
            $ret[$hashKey] = $put;
        }
        return $ret;
    }
    /**
     * Get desired statistic by stat name ia\Math\Stats:STAT_* in heirarchy
     *
     * @param string $statName ia\Math\Stats:STAT_* min,avg,max,variance,stdDev, skewness, kurtosis, coefficient of variation (cv)
     * @return @return array ['totalsBy_1'=>['totalsBy_2'=>'values_1']=>statNameValue, ...]
     */
    public function get_statHeirarchy($statName = 'sum') {
        $ret = [];
        foreach($this->stats as $hashKey => $values) {
            $arr = &$ret;
            $keys = explode(self::SEPARATOR, $hashKey);
            foreach($keys as $x) {
                if(!isset($arr[$x])) {
                    $arr[$x] = [];
                }
                $arr = &$arr[$x];
            }
            foreach($values as $key => $stats) {
                $arr[$key] = $stats->stats()[$statName];
            }
        }
        return $ret;
    }
    /**
     * All basic stats by total/subtotal as sub-array by value key
     *
     * @return array [ "totalsBy_1\ttotalsBy_2"=>['totalsBy_1', 'totalsBy_2', ..., 'values_1'=>['min'=>1,'avg'=>2,...] ...], ]
     */
    public function get_basicStats() {
        $ret = [];
        foreach($this->stats as $hashKey => $values) {
            $put = explode(self::SEPARATOR, $hashKey);
            foreach($values as $key => $stats) {
                $put[$key] = $stats->stats();
            }
            $ret[$hashKey] = $put;
        }
        return $ret;
    }
    /**
     * Get all statistics as STATNAME_value_1=>1
     *
     * @return array ["totalsBy_1\ttotalsBy_2"=>['totalsBy_1', 'totalsBy_2', ..., 'MIN_values_1'=>1, 'MIN_avg_1'=>1, ...],... ]
     */
    public function get_basicStatsFlat() {
        $ret = [];
        foreach($this->stats as $hashKey => $values) {
            $put = explode(self::SEPARATOR, $hashKey);
            foreach($values as $key => $stats) {
                $stats = $stats->stats();
                foreach($stats as $name=>$value) {
                    $put[$key."_".$name] = $value;
                }
            }
            $ret[$hashKey] = $put;
        }
        return $ret;
    }
    /**
     *
     * @return array ['totalsBy_1'=>['totalsBy_2'=>'values_1']=>[min=>1,avg=>2,...]]
     */
    public function get_basicStatsHeirarchy() {
        $ret = [];
        foreach($this->stats as $hashKey => $values) {
            $arr = &$ret;
            $keys = explode(self::SEPARATOR, $hashKey);
            foreach($keys as $x) {
                if(!isset($arr[$x])) {
                    $arr[$x] = [];
                }
                $arr = &$arr[$x];
            }
            foreach($values as $key => $stats) {
                $arr[$key] = $stats->stats();
            }
        }
        return $ret;
    }
}