nalabdou/algebra-symfony

Symfony 7 bundle for algebra-php DI integration, Doctrine adapter.

Maintainers

Package info

github.com/nalabdou/algebra-symfony

Type:symfony-bundle

pkg:composer/nalabdou/algebra-symfony

Statistics

Installs: 3

Dependents: 0

Suggesters: 1

Stars: 0

Open Issues: 0

1.1.0 2026-03-17 00:10 UTC

This package is auto-updated.

Last update: 2026-03-22 00:01:03 UTC


README

PHP Symfony License: MIT

Symfony 7 bundle for algebra-php — the pure PHP relational algebra engine.

Installation

composer require nalabdou/algebra-symfony

Symfony Flex registers the bundle automatically. Without Flex:

// config/bundles.php
return [
    Nalabdou\Algebra\Symfony\AlgebraBundle::class => ['all' => true],
];

What the bundle provides

Feature Details
DI services algebra.factory, algebra.evaluator, algebra.aggregates
Custom aggregates #[AsAggregate] attribute — zero config, auto-registration
Custom adapters #[AsAlgebraAdapter(priority: N)] — auto-injected into CollectionFactory
Doctrine adapters Auto-detected when doctrine/orm / doctrine/collections installed

Twig filters (|algebra_where, |algebra_orderby, ...) are provided by the separate nalabdou/algebra-twig [Comming soon] package.

Quick start

Algebra::from() — static, works as documented

use Nalabdou\Algebra\Algebra;

$result = Algebra::from($orders)
    ->where("item['status'] == 'paid'")
    ->groupBy('region')
    ->aggregate(['revenue' => 'sum(amount)', 'orders' => 'count(*)'])
    ->orderBy('revenue', 'desc')
    ->toArray();

Custom aggregates registered via #[AsAggregate] are available here after the first request (the AlgebraBootstrapListener re-registers them into Algebra's singleton registry after calling Algebra::reset()).

Custom aggregates

// src/Aggregate/GeomeanAggregate.php
use Nalabdou\Algebra\Contract\AggregateInterface;
use Nalabdou\Algebra\Symfony\Attribute\AsAggregate;

#[AsAggregate]
final class GeomeanAggregate implements AggregateInterface
{
    public function name(): string { return 'geomean'; }

    public function compute(array $values): mixed
    {
        return empty($values) ? null : array_product($values) ** (1 / count($values));
    }
}

That's it — no services.yaml, no Algebra::aggregates()->register(). The bundle discovers it via #[AsAggregate] and auto-registers it.

// Now available everywhere
Algebra::from($products)
    ->groupBy('category')
    ->aggregate(['geoMeanPrice' => 'geomean(price)'])
    ->toArray();

Custom adapters

// src/Adapter/CsvFileAdapter.php
use Nalabdou\Algebra\Contract\AdapterInterface;
use Nalabdou\Algebra\Symfony\Attribute\AsAlgebraAdapter;

#[AsAlgebraAdapter(priority: 50)]
final class CsvFileAdapter implements AdapterInterface
{
    public function supports(mixed $input): bool
    {
        return is_string($input) && str_ends_with($input, '.csv');
    }

    public function toArray(mixed $input): array { /* read CSV */ }
}

The adapter is auto-injected into the algebra.adapter service. Use :

$result = Algebra::from('/data/orders.csv')
    ->where("item['status'] == 'paid'")
    ->toArray();

Doctrine adapters

Automatically registered when the relevant packages are installed:

Package Input type accepted
doctrine/collections ArrayCollection, PersistentCollection
doctrine/orm QueryBuilder
composer require doctrine/orm
composer require doctrine/collections
// Doctrine QueryBuilder
$result = Algebra::from(
    $em->createQueryBuilder()->select('o')->from(Order::class, 'o')
)
->where("item['status'] == 'paid'")
->toArray();

// Doctrine Collection (e.g. from a OneToMany relation)
$result = Algebra::from($user->getOrders())
    ->orderBy('amount', 'desc')
    ->toArray();

Configuration

# config/packages/algebra.yaml
algebra:
    strict_mode: true   # throw on invalid expressions (default: true)

Injectable services

Service ID Class Public
algebra.factory CollectionFactory
algebra.evaluator ExpressionEvaluator
algebra.aggregates AggregateRegistry
algebra.adapter_registry AdapterRegistry

Requirements

Version
PHP ≥ 8.2
nalabdou/algebra-php ^1.0
symfony/framework-bundle ^7.0

Optional: doctrine/orm ^3.0, doctrine/collections ^2.0