|
1 | 1 | Collector |
2 | 2 | ========= |
3 | 3 |
|
4 | | -phpReport has a very powerful feature to aggregate values and increment counters. |
5 | | -Each value and counter is implemented as a calculator object. These objects |
6 | | -will be assigned to one collector. |
| 4 | +A collector class is designed to hold and manage multiple items. An item can |
| 5 | +be an other collector or an calculator object. |
7 | 6 |
|
8 | | -phpReport instantiates the following collectors. |
| 7 | +The main responsibilty of an collector is to call methods in assigned items. |
| 8 | +To make sure that the cumputation of calculated values works correct calculator |
| 9 | +objects must be registered to the **total** collector or to one of his child collectors. |
9 | 10 |
|
10 | | -.. note:: |
11 | | - All entities assigned to one of those collectors are cumulated to |
12 | | - higher group levels. |
| 11 | +The collector class has the ArrayAccess interface and the magic __get method |
| 12 | +implemented which allows a broad range of access options. |
13 | 13 |
|
| 14 | +The visibility of the $items array is public. This allows maximum speed when accessing |
| 15 | +an item and gives the opportunity to apply all php array methods. |
14 | 16 |
|
15 | | -Row counter collector |
16 | | ----------------------- |
17 | 17 |
|
18 | | -The row counter collector is named **rc**. For each data dimension one CalculatorXS |
19 | | -object will be instantiated. |
| 18 | +Add items |
| 19 | +--------- |
20 | 20 |
|
21 | | -Group counter collector |
22 | | ------------------------ |
| 21 | +Items will be added (or registered) to an collector usually by calling the calculate() |
| 22 | +or sheet() methods of the **phpReport** class. |
23 | 23 |
|
24 | | -The group counter collector is named **gc**. For each defined group one CalculatorXS |
25 | | -object will be instantiated. |
| 24 | +You may can also add items by the addItem() method. This is especially desired when |
| 25 | +you want to group multiple item objects. |
26 | 26 |
|
27 | | -Total collector |
28 | | ---------------- |
| 27 | +Adding items to a sheet collector is hidden and will be |
| 28 | +internally handled based on key values of the add() method. |
| 29 | + |
| 30 | + |
| 31 | +Alternate item keys |
| 32 | +------------------- |
| 33 | +Items are stored in the $item array indexed by the name given when calling the |
| 34 | +addItem() method. You can also apply an alternate key to allow accessing an item |
| 35 | +by the alternate key as well. |
29 | 36 |
|
30 | | -The total collector is named **total**. The collector is used to hold all calculator |
31 | | -objects instantiated by the aggregate() method and the sheet objects instantiated |
32 | | -by the sheet() method. |
| 37 | +The setAltKeys() method will set many alternate keys while the setAltKey() |
| 38 | +method one alternate key. |
33 | 39 |
|
34 | | -You can assign further collectors, sheets or calculators to this total collector |
35 | | -or any other collector in this tree. |
36 | | -So it's possible to build a hirachichal structure of aggregated values. |
| 40 | +The group conter collecor uses alternate keys to allow accessing the counters |
| 41 | +by the group level and by the group name. |
37 | 42 |
|
38 | 43 |
|
39 | | -Accessing values |
40 | | ----------------- |
| 44 | +Get items |
| 45 | +--------- |
41 | 46 |
|
42 | | -There are multiple options to access a calculater object. The long version looks |
43 | | -like |
| 47 | +To access an item you can use the getItems() method for all items or the getItem() |
| 48 | +method to get one item. |
44 | 49 |
|
45 | | -$this->rep->*collectorName*->items[*itemName*]. |
| 50 | +The magic __get() and the array access get methods calls the getItem() method. |
| 51 | +The item will returned when it exist either by the item array key or by the alternate |
| 52 | +key. |
46 | 53 |
|
47 | 54 | .. code-block:: php |
48 | 55 |
|
49 | | - $rep = $this->report; |
50 | | - $rep->t->*name*; |
51 | | - $rep->t['*name*']; |
52 | | - $rep->rc; |
53 | | - $rep->rc->{0}; |
54 | | - $rep->rc->[0]; |
55 | | - |
56 | | -.. include:: rowCounter.rst |
57 | | -.. include:: groupCounter.rst |
| 56 | + $this->rep = new phpReport($this); |
| 57 | + $this->rep->compute('sales'); |
| 58 | + // All of the following statements will return the same item. |
| 59 | + $item = $this->report->total->getItem('sales'); |
| 60 | + $item = $this->report->total->items['sales']); |
| 61 | + $item = $this->report->total->sales; |
| 62 | + $item = $this->report->total['sales']; |
| 63 | + |
| 64 | +
|
| 65 | +Aggregate methods |
| 66 | +----------------- |
58 | 67 |
|
59 | | -Range of values |
| 68 | +All aggregate methods implemented in the calculator classes have their counterpart |
| 69 | +it the collector classes. |
| 70 | + |
| 71 | +The collector aggregate methods calls the same methods for each assigned item. |
| 72 | +Returned results will be returned either as an array indexed by the item key or |
| 73 | +as an scalar aggregated value. |
| 74 | + |
| 75 | + |
| 76 | +Subset of items |
60 | 77 | --------------- |
61 | | -To apply aggregate funcions only to a subset of collected items use the range method. |
| 78 | + |
| 79 | +To apply aggregate methods only on some items you can build a subset by calling the |
| 80 | +following filter methods. Each of them will return a cloned collector object with |
| 81 | +just the filtered items. |
| 82 | + |
| 83 | +To use the cloned collector multiple times hold the reference to the cloned collector in |
| 84 | +a variable. |
| 85 | + |
| 86 | +.. php:method:: range(...$ranges): AbstractCollecotor |
| 87 | +
|
| 88 | + Extract ranges of items. |
| 89 | + |
| 90 | + Returns ranges of items located between start and end keys. |
| 91 | + |
| 92 | + When a range is an array value1 is the start and value2 the end key. |
| 93 | + When one of the keys don't exist the value of the altKey will be used instead. |
| 94 | + When the items still doesn't exist an error will be thrown. |
| 95 | + |
| 96 | + If start key equals Null the range begins at the first item. |
| 97 | + When the end key equals Null the range ends at the last item. |
| 98 | + |
| 99 | + When a range is not an array then the item with the corresponding key or |
| 100 | + altKey is returned if it exist. If this doesn't exist php raise a notice. |
| 101 | + |
| 102 | + Item keys are preserved. Sort order within ranges are preserved. Ranges |
| 103 | + are returned in given order. When items belong to multiple ranges only |
| 104 | + the first occurence will returned. |
| 105 | + |
| 106 | + :param array|int|string[] $ranges Ranges or item keys for items to be filtered. |
| 107 | + :return AbstractCollector Cloned collector with items in ranges. |
| 108 | + :throws InvalidArgumentException When start or end item doesn't exist. |
| 109 | + |
| 110 | +.. php:method:: between(...$ranges): AbstractCollecotor |
| 111 | +
|
| 112 | + Filters items where key is between values. |
| 113 | + |
| 114 | + Iterates over each collector item. If a range is an array and the item key |
| 115 | + is between value1 and value2 of this range (inclusive) the item is returned. |
| 116 | + |
| 117 | + If the range isn't an the item with the corresponding key is returned. |
| 118 | + |
| 119 | + If a range matches the key of a named range then the named range value will |
| 120 | + be used to filter the items. |
| 121 | + |
| 122 | + Item keys and sort order are preserved. |
| 123 | + |
| 124 | + :param array|int|string[]: $ranges Ranges or item keys for items to be filtered. |
| 125 | + :returns: AbstractCollector Cloned collector with items in ranges. |
| 126 | + |
| 127 | +.. php:method:: filter(callable $callable): |
| 128 | +
|
| 129 | + Filters items using a callback function. |
| 130 | + Iterates over each item in the array passing key and value to the callback |
| 131 | + function. If the callback function returns TRUE, the current item is returned |
| 132 | + into the cloned collector. Item keys are preserved. |
| 133 | + |
| 134 | + :param callable $callable: The callback function to use. |
| 135 | + :returns: AbstractCollector Clone of current collector with filtered items |
| 136 | + |
| 137 | +.. php:method:: cmd(callable $command, ...$params): AbstractCollector |
| 138 | +
|
| 139 | + Alter item collection by executing a php array command. |
| 140 | + |
| 141 | + :param callable $command: Any php array command which accepts an |
| 142 | + array as the first parameter. |
| 143 | + :param mixed[] $params: Additional parameters passed to the php command. |
| 144 | + :returns: AbstractCollector Clone of current collector with applied command |
| 145 | + on the items array. |
0 commit comments