lukaszmakuch / aggregator
Makes generating statistics database agnostic and flexible.
Requires
- lukaszmakuch/class-based-registry: ^0.0.2
- lukaszmakuch/property-setter: ^0.2.0
- lukaszmakuch/text-generator: ^0.2.0
Requires (Dev)
- phpdocumentor/phpdocumentor: 2.*
- phpmd/phpmd: @stable
- phpunit/phpunit: 5.4.*
- squizlabs/php_codesniffer: 2.*
README
Aggregator
Makes generating statistics database agnostic and flexible.
This document briefly describes the library. For more details, check unit tests.
Why is it interesting?
A simple aggregator
$aggregator = new GroupingAggregator( new AgeReader(), new ListAggregator(new NameReader(), ", ") );
given
$aggregator->aggregate(new Cat(['name' => 'Henry', 'age' => 5])); $aggregator->aggregate(new Cat(['name' => 'Mruczek', 'age' => 2])); $aggregator->aggregate(new Cat(['name' => 'Meow', 'age' => 2])); $aggregator->aggregate(new Cat(['name' => 'Bob', 'age' => 2])); $aggregator->aggregate(new Cat(['name' => 'Tim', 'age' => 5]));
gives
{ "type": "group", "label": "grouped by age", "data": [{ "type": "subjects_with_common_properties", "label": "age 5", "data": { "type": "list", "label": "list", "data": "Henry, Tim" } }, { "type": "subjects_with_common_properties", "label": "age 2", "data": { "type": "list", "label": "list", "data": "Mruczek, Meow, Bob" } }] }
About the library
Built-in aggregators
There are few basic aggregators built-in
Counter
lukaszmakuch\Aggregator\Impl\Counter\Counter
Counts all given subjects.
Limit
lukaszmakuch\Aggregator\Impl\Limit\Limit
Limits the number of aggregated subjects. It is visible for visitors.
TransparentLimit
lukaszmakuch\Aggregator\Impl\Limit\TransparentLimit
Limits the number of aggregated subjects. It is invisible for visitors.
Percentage
lukaszmakuch\Aggregator\Impl\Percentage\Percentage
Calculates a percentage value of subjects that meet some requirement.
ListAggregator
lukaszmakuch\Aggregator\Impl\ListAggregator\ListAggregator
Generates a list of text representations of subjects separated with some given delimiter.
PropertyList
lukaszmakuch\Aggregator\Impl\PropertyList\PropertyList
List of properties of aggregated subjects. Uses a PropertyReader to read them.
Filter
lukaszmakuch\Aggregator\Impl\Filter\Filter
Takes into account only those subjects that meet the given requirement.
GroupingAggregator
lukaszmakuch\Aggregator\Impl\GroupingAggregator\GroupingAggregator
Groups subjects by some property.
HierarchicalAggregator
lukaszmakuch\Aggregator\Impl\HierarchicalAggregator\HierarchicalAggregator
Supports parent-children relationships.
ProjectionAggregator
lukaszmakuch\Aggregator\Impl\Projection\ProjectionAggregator
Before passing subjects to some other aggregator, gets a a different of them.
ConditionalAggregator
lukaszmakuch\Aggregator\Impl\ConditionalAggregator\ConditionalAggregator
Equals one of two given aggregators, depending on the given PredicateAggregator.
Container
lukaszmakuch\Aggregator\Impl\Container\Container
Holds many actual aggregators and passes to them all what's passed to it.
WithCustomLabel
lukaszmakuch\Aggregator\LabelGenerator\WithCustomLabel
Decorates some aggregator with a label generated by the provided label generator.
LabelGenerator
An instance of lukaszmakuch\TextGenerator\TextGenerator is used by an AggregatorVisitor to generate labels for aggregators.
LabelGeneratorBuilder
Because of the complex nature of possible labels, there's a builder that makes it easier to get a generator that fits your needs.
$labelGeneratingVisitor = (new DefaultLabelGeneratorBuilder()) ->registerDependency( PropertyToTextConverterUser::class, (new ClassBasedTextGeneratorProxy())->registerActualGenerator( Age::class, new AgeToTextConverter() ) ) ->registerLabelGeneratorPrototype( CustomAggregator::class, new CustomAggregatorLabelGenerator() ) ->build() ;
ScalarPresenter
Generates a representation of the given aggregator as a scalar value or an array of scalar values.
ScalarPresenterBuilder
Because of the complex nature of possible aggregator composites, there's a builder that makes it easier to get a scalar presenter that fits your needs.
$presentingVisitor = (new DefaultScalarPresenterBuilder()) ->registerDependency( LabelingVisitorUser::class, $labelingVisitor ) ->registerExtension(new ExtensionImpl( CustomAggregator::class, new CustomAggregatorPresenter(), "some_custom_aggregator" )) ->build();
XmlPresenter
Generates an XML representation of a given aggregator as a String.
XmlPresenterBuilder
Because of the complex nature of possible aggregator composites, there's a builder that makes it easier to get an xml presenter that fits your needs.
$xmlPresenter = (new DefaultXmlPresenterBuilder()) ->registerDependency( LabelingVisitorUser::class, $labelingVisitor ) ->registerActualPresenter( CustomAggregator::class, new CustomAggregatorXmlPresenter(), ) ->build();
How to install
$ composer require lukaszmakuch/aggregator