Download the PHP package webmozart/expression without Composer

On this page you can find all versions of the php package webmozart/expression. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package expression

Webmozart Expression

Build Status Build status Latest Stable Version Total Downloads Dependency Status

Latest release: 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:

Basic Usage

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

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

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

Visitors 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:

The following sections describe the core expressions in detail.

Expressions

The Expr class is able to create the following expressions:

Method Description
null() Check that a value is null
notNull() Check that a value is not null
isEmpty() Check that a value is empty (using empty())
notEmpty() Check that a value is not empty (using empty())
isInstanceOf($className) Check that a value is instance of a class (using instanceof)
equals($value) Check that a value equals another value (using ==)
notEquals($value) Check that a value does not equal another value (using !=)
same($value) Check that a value is identical to another value (using ===)
notSame($value) Check that a value does not equal another value (using !==)
greaterThan($value) Check that a value is greater than another value
greaterThanEqual($value) Check that a value is greater than or equal to another value
lessThan($value) Check that a value is less than another value
lessThanEqual($value) Check that a value is less than or equal to another value
startsWith($prefix) Check that a value starts with a given string
endsWith($suffix) Check that a value ends with a given string
contains($string) Check that a value contains a given string
matches($regExp) Check that a value matches a regular expression
in($values) Check that a value occurs in a list of values
keyExists($key) Check that a key exists in a value
keyNotExists($key) Check that a key does not exist in a value
true() Always true (tautology)
false() Always false (contradiction)

Selectors

With composite values like arrays or objects, you often want to match only a part of that value (like an array key or the result of a getter) against an expression. You can select the evaluated parts with a selector.

When you evaluate arrays, use the key() selector to match the value of an array key:

Each selector method accepts the expression as last argument that should be evaluated for the selected value.

When evaluating objects, use property() and method() to evaluate the values of properties and the results of method calls:

The method() selector also accepts arguments that will be passed to the method. Pass the arguments before the evaluated expression:

You can nest selectors to evaluate expressions for nested objects or arrays:

The following table lists all available selectors:

Method Description
key($key, $expr) Evaluate an expression for a key of an array
method($name, $expr) Evaluate an expression for the result of a method call
property($name, $expr) Evaluate an expression for the value of a property
count($expr) Evaluate an expression for the count of a collection

The count() selector accepts arrays and Countable objects.

Quantors

Quantors are applied to collections and test whether an expression matches for a certain number of elements. A famous one is the all-quantor:

Quantors accept both arrays and Traversable instances. The following table lists all available quantors:

Method Description
all($expr) Check that an expression matches for all entries of a collection
atLeast($count, $expr) Check that an expression matches for at least $count entries of a collection
atMost($count, $expr) Check that an expression matches for at most $count entries of a collection
exactly($count, $expr) Check that an expression matches for exactly $count entries of a collection

Logical Operators

You can negate an expression with not():

You can connect multiple expressions with "and" using the and*() methods:

The same is possible for the "or" operator:

You can use and/or inside selectors:

If you want to mix and match "and" and "or" operators, use andX() and orX() to add embedded expressions:

Testing

To make sure that PHPUnit compares Expression objects correctly, you should register the ExpressionComparator with PHPUnit in your PHPUnit bootstrap file:

Make sure the file is registered correctly in phpunit.xml.dist:

The ExpressionComparator makes sure that PHPUnit compares different Expression instances by logical equivalence instead of by object equality. For example, the following Expression are logically equivalent, but not equal as objects:

Expression Transformation

In some cases, you will want to transform expressions to some other representation. A prime example is the transformation of an expression to a Doctrine query.

You can implement a custom ExpressionVisitor to do the transformation. The visitor's methods enterExpression() and leaveExpression() are called for every node of the expression tree:

Use an ExpressionTraverser to traverse an expression with your visitor:

Authors

Contribute

Contributions to the package are always welcome!

Support

If you are having problems, send a mail to [email protected] or shout out to @webmozart on Twitter.

License

All contents of this package are licensed under the MIT license.


All versions of expression with dependencies

PHP Build Version
Package Version
Requires php Version >=5.3.9
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package webmozart/expression contains the following files

Loading the files please wait ....