'extra_up_offpeak', 'base_down_offpeak'=>'extra_down_offpeak', 'base_up_peak'=>'extra_up_peak', 'base_down_peak'=>'extra_down_peak', ]); // Map the NULL relationships - and where traffic gets applied if NULL $merge = collect([ 'extra_up_offpeak'=>'base_down_offpeak', 'extra_down_offpeak'=>'base_down_peak', 'extra_up_peak'=>'base_down_peak', 'extra_down_peak'=>'base_down_peak', ]); if (is_null($config)) $config = collect($config); // If config is null, use the configuration from this Model if (! $config->count()) { // Base Config foreach ($map->keys() as $k) { $config->put($k,$this->{$k}); } // Excess Config foreach ($map->values() as $k) { $config->put($k,$this->{$k}); } // Shaped or Charge $config->put('shaped',$this->extra_shaped); $config->put('charged',$this->extra_charged); // Metric - used to round down data in $data. $config->put('metric',$this->metric); } $result = collect(); // If data is empty, we'll report on allowance, otherwise we'll report on consumption $report = $data ? FALSE : TRUE; // Work out if we charge each period foreach ($map as $k => $v) { // Anything NULL is not counted if (is_null($config->get($k))) continue; $x = $report ? $config->get($k) : ($config->get($k)-Arr::get($data,$k,0)); if ($ceil) $x = (int)ceil($x); // Non-NULL entries are counted as is if (! is_null($config->get($v))) { // Existing value for this item to be added $value = $result->has($k) ? $result->get($k) : 0; $result->put($k,$value+$x); // NULL entries are merged into another key } else { // New Key for this item $key = $merge->get($v); // Existing value for this item to be added $value = $result->has($key) ? $result->get($key) : 0; // Any value in the existing key, add it too. if ($k !== $key AND $result->has($k)) { $value += $result->get($k); $result->forget($k); } $result->put($key,$value+$x); } } if ($config->has('metric') AND $config->get('metric')) $result->transform(function($item) use ($config,$ceil) { return $ceil ? (int)ceil($item/$config->get('metric')) : $item/$config->get('metric'); }); return $result; } public function getNameAttribute() { return $this->speed; } }