PHPackages                             jlm/serializer-expression - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. jlm/serializer-expression

AbandonedArchivedLibrary[Utility &amp; Helpers](/categories/utility)

jlm/serializer-expression
=========================

Allows you to exclude fields at runtime via expressions using the Symfony expression language

28.3k1PHP

Since Jan 16Pushed 10y agoCompare

[ Source](https://github.com/jmcclell/JLMSerializerExpression)[ Packagist](https://packagist.org/packages/jlm/serializer-expression)[ RSS](/packages/jlm-serializer-expression/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependenciesVersions (1)Used By (0)

Status Badges (for master)
==========================

[](#status-badges-for-master)

[![Build Status](https://camo.githubusercontent.com/caa47761126fa97ee22bace99e039ec663f2f49ded138881918ad72301b66f2b/68747470733a2f2f7472617669732d63692e6f72672f6a6d63636c656c6c2f4a4c4d53657269616c697a657245787072657373696f6e2e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/jmcclell/JLMSerializerExpression)[![Coverage Status](https://camo.githubusercontent.com/1f91fc52390964fb76e5c491a32ba98196397e16b75f52186342322efdabdae8/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6a6d63636c656c6c2f4a4c4d53657269616c697a657245787072657373696f6e2f62616467652e706e673f6272616e63683d6d6173746572)](https://coveralls.io/r/jmcclell/JLMSerializerExpression?branch=master)[![Total Downloads](https://camo.githubusercontent.com/37f6f4ef71156a992222d2bebdd36e3dd2a243fc93af1ed4d1abf9823963e842/68747470733a2f2f706f7365722e707567782e6f72672f6a6c6d2f73657269616c697a65722d65787072657373696f6e2f646f776e6c6f6164732e706e67)](https://packagist.org/packages/jlm/serializer-expression)[![Latest Stable Version](https://camo.githubusercontent.com/ac13fabe252594d0d950a876f4b77a99f8d8755779e94356d93029aee19971c9/68747470733a2f2f706f7365722e707567782e6f72672f6a6c6d2f73657269616c697a65722d65787072657373696f6e2f762f737461626c652e706e67)](https://packagist.org/packages/jlm/serializer-expression)

\#JLMSerializerExpression

This library adds expression language support to Johannes Schmitt's [Serializer](https://github.com/schmittjoh/Serializer) library so that individual fields can be hidden based on expressions at runtime via an `@excludeIf` annotation.

Installation
============

[](#installation)

This library can be included via Composer by adding the following to your `composer.json` file:

```
"require": {
    # ..
    "jlm/serializer-expression": "dev-master"
    # ..
}
```

Usage
=====

[](#usage)

The library provides an exclusion strategy that must be configured and then added to your serialization context. Using this serialization context, when you serialize an object it is inspected for `@excludeIf` annotations which are then processed at runtime.

The exclusion strategy has two dependencies, an instance of `JMS\Metadata\MetadataFactory` from Johannes Schmitt's [Metadata](http://github.com/schmittjoh/Metadata) library and an instance of `Symfony\Component\ExpressionLanguage\ExpressionLanguage` which must be extended to provide the functionality you need.

### Annotating Your Objects

[](#annotating-your-objects)

The `@excludeIf` annotation accepts an expression that must be processable by the `ExpressionLanguage` instance you pass to the exclusion strategy. In the example below, we are using a dummy `hasAccessIfTrue` expression function that is created in the *Creating the Expression Language* section below. It isn't very useful, naturally. Creating a useful expression language is application-specific and left up to you. To see an example for Symfony applications, check out the [Symfony bundle of this library](http://github.com/jmcclell/JLMSerializerExpressionBundle).

```
