PHPackages                             mediagone/symfony-powerpack - 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. mediagone/symfony-powerpack

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

mediagone/symfony-powerpack
===========================

Provides efficiency and code-quality helpers for Symfony.

0.4.3(1y ago)02.1k11MITPHPPHP ^8.1

Since Nov 28Pushed 1y ago1 watchersCompare

[ Source](https://github.com/Mediagone/symfony-powerpack)[ Packagist](https://packagist.org/packages/mediagone/symfony-powerpack)[ RSS](/packages/mediagone-symfony-powerpack/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (2)Versions (18)Used By (1)

Symfony Powerpack
=================

[](#symfony-powerpack)

⚠️ This project is in experimental phase, it might be subject to changes.

[![Latest Version on Packagist](https://camo.githubusercontent.com/3735387009d4add3af5fec5b2507493701113f82f458534ad93cbef2803c32c5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d65646961676f6e652f73796d666f6e792d706f7765727061636b2e737667)](https://packagist.org/packages/mediagone/symfony-powerpack)[![Total Downloads](https://camo.githubusercontent.com/2d508d464794369e032ad727040fbad9cc47aeb80d742965d9fa196899f084b4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d65646961676f6e652f73796d666f6e792d706f7765727061636b2e737667)](https://packagist.org/packages/mediagone/symfony-powerpack)[![Software License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE)

This package provides efficiency and code-quality helpers for Symfony:

1. [Generic param converters](#paramConverters)
2. [Primitive types parameters](#primitiveParameters)

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

[](#installation)

This package requires **PHP 7.4+**

Add it as Composer dependency:

```
composer require mediagone/symfony-powerpack
```

In order to use primitive type parameters in your controllers, you must register the converters in your `services.yaml` by adding the following service declaration:

```
services:

    Mediagone\Symfony\PowerPack\Converters\Primitives\Services\:
        resource: '../vendor/mediagone/symfony-powerpack/src/Converters/Primitives/Services/'
```

1) Generic param converter
---------------------------------------------------------------------

[](#1-generic-param-converter)

Param Converters are the best way to convert URL or route parameters into entity or Value Object instances. They allow to extract retrieval or conversion logic, preventing code duplication and keeping your controllers clean.

*For more details, see [Symfony's documentation](https://symfony.com/bundles/SensioFrameworkExtraBundle/current/annotations/converters.html).*

Custom converters are very powerful, but doing Clean Code implies writing a lot of these converters. This package provides a base class that handles boilerplate code for you: you only have to define resolvers that will convert the request's parameter into the desired value.

### Value Object converter

[](#value-object-converter)

Let's take this very basic ValueObject:

```
final class LowercaseString
{
    private string $value;

    public function getValue() : string
    {
        return $this->value;
    }

    public function __construct(string $value)
    {
        $this->value = strtolower($value);
    }
}
```

You can use it in your controller by typehinting an argument:

```
use Mediagone\Symfony\PowerPack\Converters\Primitives\StringParam;
use Symfony\Component\Routing\Annotation\Route;

final class SearchController
{
    /**
     * @Route("/search", name="app_search")
     */
    public function __invoke(LowercaseString $searched): Response
    {
        // Return search results...
    }
}
```

The associated param converter only needs a single resolver to transform the value:

```
final class LowercaseStringParamConverter extends ValueParamConverter
{
    public function __construct()
    {
        $resolvers = [
            '' => static function(string $value) {
                return new LowercaseString($value);
            },
        ];

        parent::__construct(LowercaseString::class, $resolvers);
    }

}
```

The array key acts as suffix for controller's argument name, thus an empty string means that the converter will look for a request parameter with the exact same name than the controller's argument ("searched").

**Note:** *the converters is using `$request->get()` internally, so it will look successively in all request data available (Route attributes, GET and POST parameters).*

### Entity converter

[](#entity-converter)

Entity converters work the exact same way, but generally imply more complexity in data retrieval. For example, you can define multiple way of getting back an User, by registering multiple resolvers in the converter:

```
use App\Entity\User;

final class StringParamConverter extends ValueParamConverter
{
    public function __construct(UserRepository $userRepository)
    {
        $resolvers = [
           'Id' => static function(string $value) use($userRepository) : ?User {
               return $userRepository->findById($value);
           },
           'Name' => static function(string $value) use($userRepository) : ?User {
               return $userRepository->findByName($value);
           },
        ];

        parent::__construct(User::class, $resolvers);
    }

}
```

This way, the converter will be able to fetch the user by Id if an `userId` parameter is supplied to the controller, or by its Name if the given parameter is `userName`. In other words, the request parameter name is the concatenation of the *controller's argument name* and the *resolver's array key*.

Again, it works the same for GET, POST or route's attributes.

```
use App\Entity\User;
use Symfony\Component\Routing\Annotation\Route;

final class ShowUserController
{
    /**
     * @Route("/users/{userId}", name="app_user_show")
     */
    public function __invoke(User $user): Response
    {
        // Return response...
    }
}
```

### Optional parameters

[](#optional-parameters)

If you need to allow a nullable argument, just make the argument nullable and handle it in your code (eg. to return a custom response):

```
public function __invoke(?User $user): Response
{
    if ($user === null) {
        // do something...
    }
}
```

### Exception handling

[](#exception-handling)

Exceptions can be thrown in your resolvers, for example if the supplied value is not valid. In some cases, you don't need to handle those errors and you can just consider them as missing values.

You can either:

- Enable the `convertResolverExceptionsToNull` option on your controller's action, to automatically handle errors and convert the parameter value to `null`.
- Catch exceptions directly in the resolver, and customize the return value by yourself.

Example of `@ParamConverter` usage:

```
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route('/api/search/{name}')
 * @ParamConverter("name", options={"convertResolverExceptionsToNull": true})
 */
public function __invoke(?LowercaseString $name): Response
{
    if ($name === null) {
        return new JsonResponse(['error' => 'Invalid or missing value for `$name` parameter.']);
    }

    ...
}
```

*Note: don't forget to make your method's argument nullable!*

2) Primitive types parameters
----------------------------------------------------------------------------

[](#2-primitive-types-parameters)

The only drawback of ParamConverters is they only work with classes but not with primitive PHP types (int, string, float...) so this package also provides a set of classes that can be used to enforce type-safety for primitive types.

Class nameParameter value exampleConverted PHP valueBoolParam`1` or `0``true` or `false`FloatParam`3.14159``3.14159`IntParam`42``42`StringParam`hello``'hello'`JsonParam`["1","2","3"]``['1', '2', '3']`It also provides parameters to extract serialized arrays from the query, built from comma-separated values string :

Class nameParameter value exampleConverted PHP valueBoolArrayParam`1,0,1``[true, false, true]`FloatArrayParam`1.1,2.2,3.3``[1.1, 2.2, 3.3]`IntArrayParam`1,2,3``[1, 2, 3]`StringArrayParam`one,two,three``['one', 'two', 'three']`Again, you only have to typehint arguments in your controller to get the request's values:

```
// Request URL:  /do-something?data=23&options=1,1,0

public function __invoke(IntParam $data, BoolArrayParam $options): Response
{
    $data->getValue(); // 23
    foreach ($options->getValue() as $option) {
        // ...
    }
}
```

License
-------

[](#license)

*Symfony Powerpack* is licensed under MIT license. See LICENSE file.

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance37

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~74 days

Recently: every ~145 days

Total

16

Last Release

567d ago

PHP version history (2 changes)0.1.0PHP ^7.4|^8.0

0.4.0PHP ^8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/32240357?v=4)[Bruce](/maintainers/Mediagone)[@Mediagone](https://github.com/Mediagone)

---

Top Contributors

[![Mediagone](https://avatars.githubusercontent.com/u/32240357?v=4)](https://github.com/Mediagone "Mediagone (44 commits)")

---

Tags

clean-codeparam-convertersymfonysymfony

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mediagone-symfony-powerpack/health.svg)

```
[![Health](https://phpackages.com/badges/mediagone-symfony-powerpack/health.svg)](https://phpackages.com/packages/mediagone-symfony-powerpack)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
