PHPackages                             khalyomede/odata-query-parser - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. khalyomede/odata-query-parser

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

khalyomede/odata-query-parser
=============================

Parse OData v4 query strings.

v0.1.0(6y ago)23.4k31MITPHPPHP &gt;=7.2.0CI failing

Since Dec 5Pushed 1y ago2 watchersCompare

[ Source](https://github.com/khalyomede/odata-query-parser)[ Packagist](https://packagist.org/packages/khalyomede/odata-query-parser)[ RSS](/packages/khalyomede-odata-query-parser/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (2)Used By (1)

Unmaintained notice
===================

[](#unmaintained-notice)

Be aware I am not longer able to provide updates regularily. Please check this repository for a more up-to-date version: .

odata-query-parser
==================

[](#odata-query-parser)

Parse OData v4 query strings.

[![Packagist Version](https://camo.githubusercontent.com/4c8b2a2eba98f0d54ae33a08a351bf3078a4e1c3b88d9863ac68ba5157a49b7b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6b68616c796f6d6564652f6f646174612d71756572792d706172736572)](https://packagist.org/packages/khalyomede/odata-query-parser) [![Packagist](https://camo.githubusercontent.com/eaef50957e55cc0d9c177802dee69908ec9d704be7b81d265569d3d5309adf1c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6b68616c796f6d6564652f6f646174612d71756572792d706172736572)](https://github.com/khalyomede/odata-query-parser/blob/master/LICENSE) [![PHP from Packagist](https://camo.githubusercontent.com/4b0916336bc38661cf646b3c13e935d47b56c18c9f84e77945de25c4b098718f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6b68616c796f6d6564652f6f646174612d71756572792d706172736572)](https://github.com/khalyomede/odata-query-parser/blob/master/composer.json#L14) [![Build Status](https://camo.githubusercontent.com/918703bcce41c79d3139c0a5198d5e49fdbe8eabc3f536200e4cfd9715611987/68747470733a2f2f7472617669732d63692e636f6d2f6b68616c796f6d6564652f6f646174612d71756572792d7061727365722e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/khalyomede/odata-query-parser) [![Maintainability](https://camo.githubusercontent.com/bde601b7919d845b9acff529898e39ea831408009fe3cb19050d897b79cceb64/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f31636138663137366665646563376462383161322f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/khalyomede/odata-query-parser/maintainability) [![Known Vulnerabilities](https://camo.githubusercontent.com/f024d2819e8d37000dc0467659dfc78c4f6676cf4587da2cc092e8f1464929b6/68747470733a2f2f736e796b2e696f2f746573742f6769746875622f6b68616c796f6d6564652f6f646174612d71756572792d7061727365722f62616467652e7376673f74617267657446696c653d636f6d706f7365722e6c6f636b)](https://snyk.io/test/github/khalyomede/odata-query-parser?targetFile=composer.lock)

Summary
-------

[](#summary)

- [About](#about)
- [Features](#features)
- [Requirements](#requirements)
- [Installation](#installation)
- [Examples](#examples)
- [API](#api)
- [Known issues](#known-issues)

About
-----

[](#about)

I needed to only parse query strings to convert OData v4 commands into an understandable array that I could use to make a Laravel package to offer a way to automatically use Eloquent to filter the response according to this parsed array of OData v4 command.

As I did not see a package exclusively dealing with parsing the query strings, and saw that [some people worked on their own without open sourcing it](https://stackoverflow.com/questions/14145604/parse-odata-query-uri-into-php-array), I decided I would start one myself.

Features
--------

[](#features)

- Parses an URL and returns an array
- Supports `$select`, `$top`, `$skip`, `$orderby`, `$count`
- Partial support for `$filter` (see [Known issues](#known-issues) section)
- You can use a parse mode that let you parse these keywords without prepending `$`

Requirements
------------

[](#requirements)

- PHP &gt;= 7.2.0
- [Composer](https://getcomposer.org/)

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

[](#installation)

Add the package to your dependencies:

```
composer require khalyomede/odata-query-parser
```

Examples
--------

[](#examples)

- [1. Use $select to filter on some fields](#1-use-select-to-filter-on-some-fields)
- [2. Use non dollar syntax](#2-use-non-dollar-syntax)

### 1. Use $select to filter on some fields

[](#1-use-select-to-filter-on-some-fields)

In this example, we will use the `$select` OData query string command to filter the fields returned by our API.

```
use Khalyomede\OdataQueryParser;

$data = OdataQueryParser::parse('http://example.com/api/user?$select=id,name,age');
```

If you inspect `$data`, this is what you will get:

```
[
  "select" => [
    "id",
    "name",
    "age"
  ]
]
```

### 2. Use non dollar syntax

[](#2-use-non-dollar-syntax)

In this example, we will use a unique feature of this library: to be able to not specify any dollar, while still being able to use the OData v4 URL query parameter grammar.

```
use Khalyomede/OdataQueryParser;

$data = OdataQueryParser::parse("http://example.com/api/user?select=id,name,age", $withDollar = false);
```

If you inspect `$data`, this is what you will get:

```
[
  "select" => [
    "id",
    "name",
    "age"
  ]
]
```

API
---

[](#api)

```
OdataQueryParser::parse(string $url, bool $withDollar = true): array;
```

**parameters**

- string `$url`: The URL to parse the query strings from. It should be a "complete" or "full" URL, which means that `http://example.com` will pass while `example.com` will not pass
- bool `$withDollar`: Set it to false if you want to parse query strings without having to add the `$` signs before each keys.

**returns**

An associative array:

```
return = [
	string? "select" => array,
	string? "count" => bool,
	string? "top" => int,
	string? "skip" => int,
	string? "orderBy" => array,
	string? "filter" => array
];

OrderBy = [
	string "property" => string,
	string "direction" => Direction
]

Direction = "asc" | "desc"

Filter = [
	string "left" => string,
	string "operator" => string,
	string "right" => mixed
]
```

**throws**

- `InvalidArgumentException`
    - If the parameter `$url` is not a valid URL (see the parameter description to know what is a valid URL)
    - If the `$top` query string value is not an integer
    - If the `$top` query string value is lower than 0
    - If the `$skip` query string value is not an integer
    - If the `$skip` query string value is lower than 0
    - If the direction of the `$orderby` query string value is neither `asc` or `desc`

Known issues
------------

[](#known-issues)

- `$filter` command will not parse `or` and functions (like `contains()` of `substringof`), because I did not focused on this for the moment (the parser for `$filter` is too simplist, I should find a way to create an AST).

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance29

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity41

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

Unknown

Total

1

Last Release

2346d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/15908747?v=4)[Anwar](/maintainers/khalyomede)[@khalyomede](https://github.com/khalyomede)

---

Top Contributors

[![khalyomede](https://avatars.githubusercontent.com/u/15908747?v=4)](https://github.com/khalyomede "khalyomede (10 commits)")

---

Tags

odata-query-parserphp

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/khalyomede-odata-query-parser/health.svg)

```
[![Health](https://phpackages.com/badges/khalyomede-odata-query-parser/health.svg)](https://phpackages.com/packages/khalyomede-odata-query-parser)
```

###  Alternatives

[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k472.8M135](/packages/mtdowling-jmespathphp)[opis/closure

A library that can be used to serialize closures (anonymous functions) and arbitrary data.

2.6k230.0M283](/packages/opis-closure)[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M226](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M63](/packages/sabberworm-php-css-parser)[michelf/php-markdown

PHP Markdown

3.5k52.4M343](/packages/michelf-php-markdown)[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)

PHPackages © 2026

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