<commit_before>Webmozart Expression
====================

[![Build Status](https://travis-ci.org/webmozart/expression.svg?branch=master)](https://travis-ci.org/webmozart/expression)
[![Build status](https://ci.appveyor.com/api/projects/status/6dstc380h5pr5rk3/branch/master?svg=true)](https://ci.appveyor.com/project/webmozart/expression/branch/master)
[![Latest Stable Version](https://poser.pugx.org/webmozart/expression/v/stable.svg)](https://packagist.org/packages/webmozart/expression)
[![Total Downloads](https://poser.pugx.org/webmozart/expression/downloads.svg)](https://packagist.org/packages/webmozart/expression)
[![Dependency Status](https://www.versioneye.com/php/webmozart:expression/1.0.0/badge.svg)](https://www.versioneye.com/php/webmozart:expression/1.0.0)

Latest release: [1.0.0](https://packagist.org/packages/webmozart/expression#1.0.0)

PHP >= 5.3.9

This library implements the [Specification Pattern] for PHP. You can use it to
easily filter results of your domain services by evaluating logical expressions.

Conversely to [rulerz], this library focuses on providing a usable and efficient
PHP API first. An expression language that converts string expressions into
`Expression` instances can be built on top, but is not included in the current
release.

Visitors can be implemented that convert `Expression` objects into Doctrine
queries and similar objects.

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

Use [Composer] to install the package:

```
$ composer require webmozart/expression
```

Basic Usage
-----------

Use the [`Expression`] interface in finder methods of your service classes:

```php
use Webmozart\Expression\Expression;

interface PersonRepository
{
    public function findPersons(Expression $expr);
}
```

When querying persons from the repository, you can create new expressions with
the [`Expr`] factory class:

```php
$expr = Expr::method('getFirstName', Expr::startsWith('Tho'))
    ->andMethod('getAge', Expr::greaterThan(35));
    
$persons = $repository->findPersons($expr);
```

The repository implementation can use the `evaluate()` method to match 
individual persons against the criteria:

```php
class PersonRepositoryImpl implements PersonRepository
{
    private $persons = [];
    
    public function findPersons(Expression $expr)
    {
        return Expr::filter($this->persons, $expr);
    }
}
```

Domain Expressions
------------------

Extend existing expressions to build domain-specific expressions:

```php
class IsPremium extends Method
{
    public function __construct()
    {
        parent::__construct('isPremium', [], Expr::same(true));
    }<commit_msg>Added note about converting expressions to Doctrine queries<commit_after>Webmozart Expression
====================

[![Build Status](https://travis-ci.org/webmozart/expression.svg?branch=master)](https://travis-ci.org/webmozart/expression)
[![Build status](https://ci.appveyor.com/api/projects/status/6dstc380h5pr5rk3/branch/master?svg=true)](https://ci.appveyor.com/project/webmozart/expression/branch/master)
[![Latest Stable Version](https://poser.pugx.org/webmozart/expression/v/stable.svg)](https://packagist.org/packages/webmozart/expression)
[![Total Downloads](https://poser.pugx.org/webmozart/expression/downloads.svg)](https://packagist.org/packages/webmozart/expression)
[![Dependency Status](https://www.versioneye.com/php/webmozart:expression/1.0.0/badge.svg)](https://www.versioneye.com/php/webmozart:expression/1.0.0)

Latest release: [1.0.0](https://packagist.org/packages/webmozart/expression#1.0.0)

PHP >= 5.3.9

This library implements the [Specification Pattern] for PHP. You can use it to
easily filter results of your domain services by evaluating logical expressions.

Conversely to [rulerz], this library focuses on providing a usable and efficient
PHP API first. An expression language that converts string expressions into
`Expression` instances can be built on top, but is not included in the current
release.

Visitors can be implemented that convert `Expression` objects into Doctrine
queries and similar objects.

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

Use [Composer] to install the package:

```
$ composer require webmozart/expression
```

Basic Usage
-----------

Use the [`Expression`] interface in finder methods of your service classes:

```php
use Webmozart\Expression\Expression;

interface PersonRepository
{
    public function findPersons(Expression $expr);
}
```

When querying persons from the repository, you can create new expressions with
the [`Expr`] factory class:

```php
$expr = Expr::method('getFirstName', Expr::startsWith('Tho'))
    ->andMethod('getAge', Expr::greaterThan(35));
    
$persons = $repository->findPersons($expr);
```

The repository implementation can use the `evaluate()` method to match 
individual persons against the criteria:

```php
class PersonRepositoryImpl implements PersonRepository
{
    private $persons = [];
    
    public function findPersons(Expression $expr)
    {
        return Expr::filter($this->persons, $expr);
    }
}
```

[Visitors](#expression-transformation) can be built to convert expressions into
other types of specifications, such as Doctrine query builders.

Domain Expressions
------------------

Extend existing expressions to build domain-specific expressions:

```php
class IsPremium extends Method
{
    public function __construct()
    {
        parent::__construct('isPremium', [], Expr::same(true));
    }
{'fraction_non_alphanumeric': 0.1412502304997234, 'fraction_numerical': 0.008851189378572745, 'mean_word_length': 4.832258064516129, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 22, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3645980', 'n_tokens_mistral': 1614, 'n_tokens_neox': 1564, 'n_words': 420}