This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component
Fabien Potencier 420989f5a9 feature #17113 [Serializer] Add a MaxDepth option (dunglas)
This PR was squashed before being merged into the 3.1-dev branch (closes #17113).

Discussion
----------

[Serializer] Add a MaxDepth option

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | https://github.com/symfony/symfony/issues/14924#issuecomment-111977173, https://github.com/dunglas/DunglasApiBundle/issues/104#issuecomment-110644569
| License       | MIT
| Doc PR        | todo

Add a max depth option during the normalization process. Especially useful when normalizing trees.

Usage:

```php
use Symfony\Component\Serializer\Annotation\MaxDepth;

class MyObj
{
    /**
     * @MaxDepth(2)
     */
    public $foo;

    /**
     * @var self
     */
    public $child;
}

use Doctrine\Common\Annotations\AnnotationReader;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;

$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$normalizer = new ObjectNormalizer($classMetadataFactory);
$serializer = new Serializer(array($this->normalizer));

$level1 = new MyObj();
$level1->foo = 'level1';

$level2 = new MyObj();
$level2->foo = 'level2';
$level1->child = $level2;

$level3 = new MyObj();
$level3->foo = 'level3';
$level2->child = $level3;
$result = $serializer->normalize($level1, null, array(ObjectNormalizer::ENABLE_MAX_DEPTH => true));
/*      $result = array(
            'foo' => 'level1',
            'child' => array(
                    'foo' => 'level2',
                    'child' => array(
                            'child' => null,
                        ),
                ),
        );
*/
```

* [x] Metadata support
* [x] `@MaxDepth(2)` annotation and loader
* [x] XML loader
* [x] YAML loader
* [x] Delegate recursive normalization at the end of the process
* [x] Use constants with Late Static Binding in the abstract class instead of raw strings
* [x] Move common logic to the abstract class

/cc @mRoca @csarrazi

Commits
-------

a44bead [Serializer] Add a MaxDepth option
2016-01-26 13:18:18 +01:00
..
Asset Merge branch '3.0' 2016-01-20 13:19:44 +01:00
BrowserKit Merge branch '3.0' 2016-01-13 17:23:43 +01:00
Cache [Cache] Handle and log errors properly 2016-01-25 20:18:55 +01:00
ClassLoader Merge branch '3.0' 2016-01-20 13:19:44 +01:00
Config Merge branch '3.0' 2016-01-21 11:01:50 +01:00
Console [console] Add truncate method to FormatterHelper 2016-01-25 13:26:46 +01:00
CssSelector Merge branch '3.0' 2016-01-03 16:35:40 +01:00
Debug Merge branch '3.0' 2016-01-21 11:01:50 +01:00
DependencyInjection feature #17132 [DependencyInjection] Properly ignore invalid reference arguments in collection arguments (ogizanagi) 2016-01-25 12:14:38 +01:00
DomCrawler Merge branch '3.0' 2016-01-03 16:35:40 +01:00
EventDispatcher minor #17166 Add placeholders into log messages (hason) 2016-01-14 08:58:06 +01:00
ExpressionLanguage Merge branch '3.0' 2016-01-03 16:35:40 +01:00
Filesystem Merge branch '3.0' 2016-01-21 11:01:50 +01:00
Finder Merge branch '3.0' 2016-01-21 11:01:50 +01:00
Form Merge branch '3.0' 2016-01-21 11:01:50 +01:00
HttpFoundation Merge branch '3.0' 2016-01-21 11:01:50 +01:00
HttpKernel made some cleanup in tests 2016-01-25 12:33:17 +01:00
Intl Merge branch '3.0' 2016-01-21 11:01:50 +01:00
Ldap Merge branch '3.0' 2016-01-03 16:35:40 +01:00
OptionsResolver Merge branch '3.0' 2016-01-21 11:01:50 +01:00
Process feature #17427 [Process] Allow a callback whenever the output is disabled (romainneutron) 2016-01-25 12:11:57 +01:00
PropertyAccess [2.3] Static Code Analysis for Components 2016-01-12 12:31:34 +01:00
PropertyInfo Merge branch '3.0' 2016-01-21 11:01:50 +01:00
Routing Merge branch '3.0' 2016-01-21 11:01:50 +01:00
Security [Security] add USERNAME_NONE_PROVIDED constant 2016-01-25 13:06:55 +01:00
Serializer feature #17113 [Serializer] Add a MaxDepth option (dunglas) 2016-01-26 13:18:18 +01:00
Stopwatch Merge branch '3.0' 2016-01-03 16:35:40 +01:00
Templating Merge branch '3.0' 2016-01-03 16:35:40 +01:00
Translation Merge branch '3.0' 2016-01-21 11:01:50 +01:00
Validator feature #16909 Allows access to payload in callback validator (conradkleinespel) 2016-01-25 09:16:45 +01:00
VarDumper Merge branch '2.7' into 2.8 2016-01-21 10:05:51 +01:00
Yaml feature #17462 [Yaml] deprecate parsing the !!php/object tag (xabbuh) 2016-01-22 07:37:40 +01:00