Download the PHP package sanderdekroon/parlant without Composer

On this page you can find all versions of the php package sanderdekroon/parlant. 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 parlant

Parlant

Latest Version on Packagist Build Status Coverage Status Quality Score Total Downloads

Parlant is a PHP library to query posts within WordPress in an expressive way. Get rid of the messy WP_Query array's and start writing expressive queries.

Remember this?

What if you could simplify it to this?

Parlant is a Query Builder, which means you can build queries in a simple and expressive way. It still uses WP_Query under the hood, but it takes away the pain and clutter of argument building.

Install

Via Composer

Usage

Basic usage

Parlant's syntax is heavily inspired by Laravel's Query Builder package.

To start building queries, simply start by calling the static method type() on the Parlant class. Either pass in the posttype you're querying into the type() method or call all() to query all posttypes.

These methods return a PosttypeBuilder instance on which you can chain multiple methods. End a query by calling get() to get all results of the query.

Although Parlant uses WP_Query in the background, it overrides some of it's default settings. Parlant will, by default, return all found posts ('posts_per_page' => -1) instead of the default. This behavior can be overwritten by changing the settings of Parlant.

For example: get all posts of posttype 'article':

The variable $articles will now contain an array of WP_Post instances. Alternatively Parlant can be configured to return a WP_Query instance or an array of query arguments. It is also possible to inject your own output formatter.

Limiting results

Besides changing Parlant's configuration, it's also possible to pass any limiting methods to the Query Builder. Instead of calling get() to return results, you can call first() to return the first result:

Depending on your configuration, this returns a single WP_Post instance. Remember, you can define your own output settings.

When you're needing a specific amount of posts to be returned, add the limit() method to the query and end it with get():

Specify results

Use the where() method when specifying results. Basically, all the arguments used for WP_Query in the outer array are supported through this method.

For example, you can specify the author by calling where('author', 7) on the PosttypeBuilder. Want to get all posts in 1970? Go right ahead: where('year', 1970).

A simple query with where() would look like this:

It's possible to add multiple where statements, apply a limiting method and order the result. All in one chain.

Meta query

Of course you can also utilize the meta_query of the WP_Query class. Just add the whereMeta() method. This means you will be querying for certain post_meta key/value combination. For example:

Operators

You can pass in any of the supported operators. If no operator is supplied, Parlant defaults to '='.

Since Parlant is utilizing the WP_Query class, you can pass in any of the supported operators of WP_Query. For reference, the operators are:

Meta type

You can also specify the meta type to query for. If you're supplying a meta type, you have to supply an operator.

The default meta type is 'CHAR' which should suffice in the most (basic) situations. You can pass in any of the supported meta types of WP_Query. For reference, the types are:

Just like with the WP_Query class, it's possible to specify the precision or scale for the DECIMAL and NUMERIC TYPES. For example 'DECIMAL(10,5)' or 'NUMERIC(10)' are valid meta types.

Meta relation

It's possible to define the relation between multiple meta queries. Chaining multiple whereMeta() methods will create an 'AND' relation by default. Use orWhereMeta() to set the relation to 'OR'.

For example: we want to query all posts within the posttype 'shirts' but only want the posts where the size is either 'M' or 'L'.

To construct more advanced meta queries, use a closure to start a nested meta query. Continuing from the example above, we want to get all shirts where the size is either 'M' or 'L' and where the color is red.

When using a closure, use orWhere() to specify an OR relation between the nested queries. The default is AND.

Note: Parlant knows that the closure is within the whereMeta() method, therefor it's not needed to call whereMeta() within the closure. Instead, you can just call the where() method.

Note: Just like with a normal whereMeta() method, you can still pass in different operators and meta types.

Nesting madness

Parlant will resolve all nested queries recursively, so there's no hard limit on the level of queries.

Although it's possible to nest the queries quite deep, I would not recommend to go deeper than 3 levels.

Just for your own sanity.

Taxonomy queries / Term queries

Besides custom post meta, it's also possible to query custom taxonomies. Like the meta query, start a taxonomy/term query by using the whereTerm() method. Start by passing in the taxonomy, then the term field, the operator (optional) and finally the term value.

Of course you can pass in any of the other tax query operators. Omitting the operator will make Parlant fallback to the default 'IN' operator.

Note: the tax query has different operators than the meta query. The operators are IN, NOT IN, AND, EXISTS, NOT EXISTS.

Taxonomy/Term relation

The relationship handling between multiple taxonomy queries is handled the same way as a meta query. Chaining multiple whereTerm() methods will create an 'AND' relation by default. Use orWhereTerm() to set the relation to 'OR'.

For example: we want to query all posts within the posttype 'jeans' but only want the posts where the size is either '32' or '33'. In this example, size is the taxonomy.

The taxonomy/term query also supports nested queries. There's one difference: there's no need to specify which field you are querying as there are different methods taking care of that. When you're within a closure constructing a nested query, simply call the field you want to query as a method. E.g. slug(), name(), id() or termTaxonomyId().

Continuing from the example above, we want to get all jeans where the size is either '32' or '33' and where the color is 'blue'.

It's possible to mix the different methods when you are within a nested taxonomy query.

Setting a default taxonomy

When you are querying a lot of values within a single taxonomy, it's possible to set a default taxonomy for a nested taxonomy query. Instead of immediately starting a closure, pass in the taxonomy name first and then a closure:

Configuration

The default configuration of Parlant can be changed at any time, but it's recommended to configure it as early as possible to avoid unexpected results. Changes are made by calling the configure() method on an instance of Parlant.

These settings are set globally, so there's no need to change the configuration every time you're starting a query. Parlant configures itself to return all posts which are published and returns found posts as an array of WP_POST instances.

Note: This is the default configuration, there's no need to copy this exact code. Use it as a reference or starting point.

Now let's change the default posts_per_page from -1 to 9.

Changing output

Parlant uses Output Formatters to determine how to output the query results. By default it will return an array of WP_Post instances. If you want to start a WP_Query loop, simply change the default settings of Parlant:

This configuration will make Parlant return a WP_Query instance on all queries. If you only want to change the ouput of one query, call the setConfig() method on a PosttypeBuilderinstance:

Parlant has three built in output formatters:

Alternatively, you can supply your own output formatter by supplying the fully namespaced classname. The formatter should adhere to the Sanderdekroon\Parlant\Formatter\FormatterInterface interface which looks like this:

The output() method always receives an array of arguments. As an example, the query formatter looks like this:

Methods

Below is a list of supported methods and their parameters. Some methods have not yet been implemented, but will be added in future versions.

Returns results

get() - Returns all results

first() - Returns only the first result.

all() - Same as get, but ignores the limit() method or any other default setting.

count() - Returns an integer representing the count of found posts.

find($id) - Returns a single result (if any) where the ID is equal to the integer passed in.

pluck($column_name) - Only returns an array of the specified column name.

avg('column_name') - Will trigger a BadMethodCallException as this is unimplemented.

max('column_name') - Will trigger a BadMethodCallException as this is unimplemented.

min('column_name') - Will trigger a BadMethodCallException as this is unimplemented.

Limiting methods

limit($number) - Limits the output to the number passed in.

select('column_name') - Will trigger a BadMethodCallException as this is unimplemented.

Where

where($column, $value) - Specifies the result.

whereMeta() - Specify the result by searching for a certain meta key/value relation.

whereTaxonomy() - Specify the result by searching for a certain taxonomy/term relation.

whereBetween() / whereNotBetween() - Will trigger a BadMethodCallException as this is unimplemented.

whereIn() / whereNotIn() - Will trigger a BadMethodCallException as this is unimplemented.

Ordering

orderBy($column, $direction = null) - Order the result by a given column. The direction parameter is optional.

order($direction) - Specify in which direction the results should be ordered. Usually this is not needed, as the orderBy() method can be used to specify the direction.

groupBy() - Will trigger a BadMethodCallException as this is unimplemented.

inRandomOrder() - Will trigger a BadMethodCallException as this is unimplemented.

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.


All versions of parlant with dependencies

PHP Build Version
Package Version
Requires php Version ~5.6|~7.0
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 sanderdekroon/parlant contains the following files

Loading the files please wait ....