PHPackages                             utopia-php/query - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. utopia-php/query

ActiveLibrary[Framework](/categories/framework)

utopia-php/query
================

A simple library providing a query abstraction for filtering, ordering, and pagination

0.1.1(2mo ago)0476[1 PRs](https://github.com/utopia-php/query/pulls)MITPHPPHP &gt;=8.4

Since Mar 3Pushed 1mo agoCompare

[ Source](https://github.com/utopia-php/query)[ Packagist](https://packagist.org/packages/utopia-php/query)[ RSS](/packages/utopia-php-query/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (4)Used By (0)

Utopia Query
============

[](#utopia-query)

[![Tests](https://github.com/utopia-php/query/actions/workflows/tests.yml/badge.svg)](https://github.com/utopia-php/query/actions/workflows/tests.yml)[![Linter](https://github.com/utopia-php/query/actions/workflows/linter.yml/badge.svg)](https://github.com/utopia-php/query/actions/workflows/linter.yml)[![Static Analysis](https://github.com/utopia-php/query/actions/workflows/static-analysis.yml/badge.svg)](https://github.com/utopia-php/query/actions/workflows/static-analysis.yml)

A simple PHP library providing a query abstraction for filtering, ordering, and pagination. It offers a fluent, type-safe API for building queries that can be serialized to JSON and parsed back, making it easy to pass query definitions between client and server or between services.

Installation
------------

[](#installation)

```
composer require utopia-php/query
```

System Requirements
-------------------

[](#system-requirements)

- PHP 8.4+

Usage
-----

[](#usage)

```
use Utopia\Query\Query;
```

### Filter Queries

[](#filter-queries)

```
// Equality
$query = Query::equal('status', ['active', 'pending']);
$query = Query::notEqual('role', 'guest');

// Comparison
$query = Query::greaterThan('age', 18);
$query = Query::greaterThanEqual('score', 90);
$query = Query::lessThan('price', 100);
$query = Query::lessThanEqual('quantity', 0);

// Range
$query = Query::between('createdAt', '2024-01-01', '2024-12-31');
$query = Query::notBetween('priority', 1, 3);

// String matching
$query = Query::startsWith('email', 'admin');
$query = Query::endsWith('filename', '.pdf');
$query = Query::search('content', 'hello world');
$query = Query::regex('slug', '^[a-z0-9-]+$');

// Array / contains
$query = Query::contains('tags', ['php', 'utopia']);
$query = Query::containsAny('categories', ['news', 'blog']);
$query = Query::containsAll('permissions', ['read', 'write']);
$query = Query::notContains('labels', ['deprecated']);

// Null checks
$query = Query::isNull('deletedAt');
$query = Query::isNotNull('verifiedAt');

// Existence
$query = Query::exists(['name', 'email']);
$query = Query::notExists('legacyField');

// Date helpers
$query = Query::createdAfter('2024-01-01');
$query = Query::updatedBetween('2024-01-01', '2024-06-30');
```

### Ordering and Pagination

[](#ordering-and-pagination)

```
$query = Query::orderAsc('createdAt');
$query = Query::orderDesc('score');
$query = Query::orderRandom();

$query = Query::limit(25);
$query = Query::offset(50);

$query = Query::cursorAfter('doc_abc123');
$query = Query::cursorBefore('doc_xyz789');
```

### Logical Combinations

[](#logical-combinations)

```
$query = Query::and([
    Query::greaterThan('age', 18),
    Query::equal('status', ['active']),
]);

$query = Query::or([
    Query::equal('role', ['admin']),
    Query::equal('role', ['moderator']),
]);
```

### Spatial Queries

[](#spatial-queries)

```
$query = Query::distanceLessThan('location', [40.7128, -74.0060], 5000, meters: true);
$query = Query::distanceGreaterThan('location', [51.5074, -0.1278], 100);

$query = Query::intersects('area', [[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]);
$query = Query::overlaps('region', [[0, 0], [2, 0], [2, 2], [0, 2], [0, 0]]);
$query = Query::touches('boundary', [[0, 0], [1, 1]]);
$query = Query::crosses('path', [[0, 0], [5, 5]]);
```

### Vector Similarity

[](#vector-similarity)

```
$query = Query::vectorDot('embedding', [0.1, 0.2, 0.3, 0.4]);
$query = Query::vectorCosine('embedding', [0.1, 0.2, 0.3, 0.4]);
$query = Query::vectorEuclidean('embedding', [0.1, 0.2, 0.3, 0.4]);
```

### Selection

[](#selection)

```
$query = Query::select(['name', 'email', 'createdAt']);
```

### Serialization

[](#serialization)

Queries serialize to JSON and can be parsed back:

```
$query = Query::equal('status', ['active']);

// Serialize to JSON string
$json = $query->toString();
// '{"method":"equal","attribute":"status","values":["active"]}'

// Parse back from JSON string
$parsed = Query::parse($json);

// Parse multiple queries
$queries = Query::parseQueries([$json1, $json2, $json3]);
```

### Grouping Helpers

[](#grouping-helpers)

`groupByType` splits an array of queries into categorized buckets:

```
$queries = [
    Query::equal('status', ['active']),
    Query::greaterThan('age', 18),
    Query::orderAsc('name'),
    Query::limit(25),
    Query::offset(10),
    Query::select(['name', 'email']),
    Query::cursorAfter('abc123'),
];

$grouped = Query::groupByType($queries);

// $grouped['filters']         — filter Query objects
// $grouped['selections']      — select Query objects
// $grouped['limit']           — int|null
// $grouped['offset']          — int|null
// $grouped['orderAttributes'] — ['name']
// $grouped['orderTypes']      — ['ASC']
// $grouped['cursor']          — 'abc123'
// $grouped['cursorDirection'] — 'after'
```

`getByType` filters queries by one or more method types:

```
$cursors = Query::getByType($queries, [Query::TYPE_CURSOR_AFTER, Query::TYPE_CURSOR_BEFORE]);
```

### Building an Adapter

[](#building-an-adapter)

The `Query` object is backend-agnostic — your library decides how to translate it. Use `groupByType` to break queries apart, then map each piece to your target syntax:

```
use Utopia\Query\Query;

class SQLAdapter
{
    /**
     * @param array $queries
     */
    public function find(string $table, array $queries): array
    {
        $grouped = Query::groupByType($queries);

        // SELECT
        $columns = '*';
        if (!empty($grouped['selections'])) {
            $columns = implode(', ', $grouped['selections'][0]->getValues());
        }

        $sql = "SELECT {$columns} FROM {$table}";

        // WHERE
        $conditions = [];
        foreach ($grouped['filters'] as $filter) {
            $conditions[] = match ($filter->getMethod()) {
                Query::TYPE_EQUAL        => $filter->getAttribute() . ' IN (' . $this->placeholders($filter->getValues()) . ')',
                Query::TYPE_NOT_EQUAL    => $filter->getAttribute() . ' != ?',
                Query::TYPE_GREATER      => $filter->getAttribute() . ' > ?',
                Query::TYPE_LESSER       => $filter->getAttribute() . ' < ?',
                Query::TYPE_BETWEEN      => $filter->getAttribute() . ' BETWEEN ? AND ?',
                Query::TYPE_IS_NULL      => $filter->getAttribute() . ' IS NULL',
                Query::TYPE_IS_NOT_NULL  => $filter->getAttribute() . ' IS NOT NULL',
                Query::TYPE_STARTS_WITH  => $filter->getAttribute() . " LIKE CONCAT(?, '%')",
                // ... handle other types
            };
        }

        if (!empty($conditions)) {
            $sql .= ' WHERE ' . implode(' AND ', $conditions);
        }

        // ORDER BY
        foreach ($grouped['orderAttributes'] as $i => $attr) {
            $sql .= ($i === 0 ? ' ORDER BY ' : ', ') . $attr . ' ' . $grouped['orderTypes'][$i];
        }

        // LIMIT / OFFSET
        if ($grouped['limit'] !== null) {
            $sql .= ' LIMIT ' . $grouped['limit'];
        }
        if ($grouped['offset'] !== null) {
            $sql .= ' OFFSET ' . $grouped['offset'];
        }

        // Execute $sql with bound parameters ...
    }
}
```

The same pattern works for any backend. A Redis adapter might map filters to sorted-set range commands, an Elasticsearch adapter might build a `bool` query, or a MongoDB adapter might produce a `find()` filter document — the Query objects stay the same regardless:

```
class RedisAdapter
{
    /**
     * @param array $queries
     */
    public function find(string $key, array $queries): array
    {
        $grouped = Query::groupByType($queries);

        foreach ($grouped['filters'] as $filter) {
            match ($filter->getMethod()) {
                Query::TYPE_BETWEEN => $this->redis->zRangeByScore(
                    $key,
                    $filter->getValues()[0],
                    $filter->getValues()[1],
                ),
                Query::TYPE_GREATER => $this->redis->zRangeByScore(
                    $key,
                    '(' . $filter->getValue(),
                    '+inf',
                ),
                // ... handle other types
            };
        }

        // ...
    }
}
```

This keeps your application code decoupled from any particular storage engine — swap adapters without changing a single query.

Contributing
------------

[](#contributing)

All code contributions should go through a pull request and be approved by a core developer before being merged. This is to ensure a proper review of all the code.

```
# Install dependencies
composer install

# Run tests
composer test

# Run linter
composer lint

# Auto-format code
composer format

# Run static analysis
composer check
```

License
-------

[](#license)

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance88

Actively maintained with recent releases

Popularity18

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

77d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/023f08a9df59f81cc4a04b1cebd20f45ede5db53ef2f9e9ad3d75f4c69be66b8?d=identicon)[eldadfux](/maintainers/eldadfux)

---

Top Contributors

[![abnegate](https://avatars.githubusercontent.com/u/5857008?v=4)](https://github.com/abnegate "abnegate (7 commits)")

---

Tags

phpframeworkqueryupfutopia

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/utopia-php-query/health.svg)

```
[![Health](https://phpackages.com/badges/utopia-php-query/health.svg)](https://phpackages.com/packages/utopia-php-query)
```

###  Alternatives

[utopia-php/database

A simple library to manage application persistence using multiple database adapters

74327.7k5](/packages/utopia-php-database)[utopia-php/domains

Utopia Domains library is simple and lite library for parsing web domains. This library is aiming to be as simple and easy to learn and use.

54600.9k13](/packages/utopia-php-domains)[utopia-php/cli

A simple CLI library to manage command line applications

41396.4k10](/packages/utopia-php-cli)[utopia-php/cache

A simple cache library to manage application cache storing, loading and purging

31379.3k8](/packages/utopia-php-cache)[utopia-php/storage

A simple Storage library to manage application storage

30240.5k5](/packages/utopia-php-storage)[utopia-php/system

A simple library for obtaining information about the host's system.

25252.2k12](/packages/utopia-php-system)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
