1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Brend Wanders <b.wanders@utwente.nl>
5 */
6// must be run within Dokuwiki
7if(!defined('DOKU_INC')) die('Meh.');
8
9/**
10 * The summation aggregator.
11 */
12class plugin_strata_aggregate_sum extends plugin_strata_aggregate {
13    function aggregate($values, $hint = null) {
14        if($hint == 'strict') {
15            return array_reduce($values, function(&$state, $item) {
16                if(is_numeric($item)) {
17                    $state[0] += $item;
18                } else {
19                    $state[] = $item;
20                }
21                return $state;
22            }, array(0));
23        } else {
24            return array(array_sum($values));
25        }
26    }
27
28    function getInfo() {
29        return array(
30            'desc'=>'Sums up all items. Any item that does not have a clear numeric value (i.e. starts with a number) is counted as 0. If the \'strict\' hint is used, values that are not strictly numeric (i.e. contains only a number) are left intact.',
31            'hint'=>'\'strict\' to leave non-numeric values',
32            'tags'=>array('numeric')
33        );
34    }
35}
36