PHPackages                             jlm/serializer-expression-bundle - 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-bundle

AbandonedArchivedSymfony-bundle[Utility &amp; Helpers](/categories/utility)

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

Allows you to use Symfony expressions in a new runtime exclusion strategy

47.3k1[1 PRs](https://github.com/jmcclell/JLMSerializerExpressionBundle/pulls)PHP

Since Jan 15Pushed 7y agoCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

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

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

[![Build Status](https://camo.githubusercontent.com/172f65a67ffc90b469cfc45c63631804294f8e7502551333947e48d833a9adbf/68747470733a2f2f7472617669732d63692e6f72672f6a6d63636c656c6c2f4a4c4d41777342756e646c652e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/jmcclell/JLMAwsBundle)[![Coverage Status](https://camo.githubusercontent.com/a11428ff2079da00b2a13378111b7c7f9380d322ee9320e21188472f6f0a968a/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6a6d63636c656c6c2f4a4c4d53657269616c697a657245787072657373696f6e42756e646c652f62616467652e706e673f6272616e63683d6d6173746572)](https://coveralls.io/r/jmcclell/JLMSerializerExpressionBundle?branch=master)[![Total Downloads](https://camo.githubusercontent.com/c1c5b0818336d80d5d85baf92d64156336c25dbe9399ec0b9b3177751d71c96b/68747470733a2f2f706f7365722e707567782e6f72672f6a6c6d2f73657269616c697a65722d65787072657373696f6e2d62756e646c652f646f776e6c6f6164732e706e67)](https://packagist.org/packages/jlm/serializer-expression-bundle)[![Latest Stable Version](https://camo.githubusercontent.com/6679e61224e21846f3710cd1440f64b1a8510c13bb8c9743339fdacb550d20ae/68747470733a2f2f706f7365722e707567782e6f72672f6a6c6d2f73657269616c697a65722d65787072657373696f6e2d62756e646c652f762f737461626c652e706e67)](https://packagist.org/packages/jlm/serializer-expression-bundle)

\#JLMSerializerExpressionBundle

This bundle adds makes it simple to integrate the [JLMSerializerExpression](http://github.com/jmcclell/JLMSerializerExpression) library into your Symfony application. The library adds an `@excludeIf` annotation with expression language support to [JMSSerializerBundle](https://github.com/schmittjoh/JMSSerializerBundle) so that individual fields can be hidden based on expressions at runtime.

This bundle registers the necessary services in the DI container including a pre-configured `ExclusionStrategy` object which can be added directly to any `SerializationContext`.

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

[](#installation)

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

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

Then add the bundle to your Kernel in `AppKernel.php` (if using Symfony Standard):

```
public function registerBundles()
    {
        $bundles = array(
            /* ... */
            new JLM\SerializerExpressionBundle\JLMSerializerExpressionBundle(),
        );

        return $bundles;
    }
```

Configuration
=============

[](#configuration)

This bundle currently has no configuration exposed. It will work out of the box.

Services
========

[](#services)

The bundle will provide the following pre-configured services. Please see the [SerializerExpression library](http://github.com/jmcclell/JLMSerializerExpression) for more detail on what these individual services are for.

### jlm\_serializer\_expression.expression\_based\_exclusion\_strategy

[](#jlm_serializer_expressionexpression_based_exclusion_strategy)

This is the bundled, pre-configured exclusion strategy that will run expressions provided by `@excludeIf` annotations to determine at runtime whether or not to include a particular object property. **This is likely the only service you will need**.

### jlm\_serializer\_expression.metadata.metadata\_factory

[](#jlm_serializer_expressionmetadatametadata_factory)

This service is an instance of `Metadata\MetadataFactory` from Johannes Schmitt's [Metadata](http://github.com/schmittjoh/Metadata) library. This factory comes preconfigured with an Annotation driver to read the `excludeIf` annotations and a file based cache using your Symfony application's kernel cache directory. If you choose to instantiate a separate instance of `JLM\SerializerExpression\Exclusion\ExpressionBasedExclusionStrategy` this is the metadata factory you should pass along to it unless you have a good reason for using another.

### jlm\_serializer\_expression.expression\_language

[](#jlm_serializer_expressionexpression_language)

This is the bundled expression language (instance of `Symfony\Component\ExpressionLanguage\ExpressionLanguage` which provides access to all services, containers, and includes a `secure` method for directly passing security expressions. This is the language used by the `@excludeIf` annotation and is the language that the pre-configured exclusion strategy is passed. **For more information on the expression language, see the** [relevant Symfony documentation](http://symfony.com/doc/current/components/expression_language/index.html).

The bundled version of the `ExpressionLanguage` provides the following methods:

- **param** - Accepts the name of a parameter and returns its value, eg: `@excludeIf("param('myParam') == true")`
- **service** - Accepts the ID of a service and returns it, eg: `@excludeIf("service('mySerivce').foo()")`
- **secure** - Accepts a string which is itself an expression, passed directly to the security context, eg: `@excludeIf("secure(""has\_role('ROLE\_ADMIN')"")") (Note the double quotes which is how we embed an expression within an expression)

### jlm\_serializer\_expression.fos\_rest\_view\_handler

[](#jlm_serializer_expressionfos_rest_view_handler)

This service provides integration with `FOSRestBundle`, detailed below.

### Annotating Your Classes

[](#annotating-your-classes)

```
