Skip to content

Commit a6349e8

Browse files
committed
Add whitelist and blacklist implementations
1 parent c6505d3 commit a6349e8

File tree

4 files changed

+241
-0
lines changed

4 files changed

+241
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Extractor;
13+
14+
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
15+
16+
/**
17+
* Remove properties given a blacklist in the context.
18+
*
19+
* @author Joel Wurtz <joel.wurtz@gmail.com>
20+
*/
21+
final class BlacklistPropertyListExtractor implements PropertyListExtractorInterface
22+
{
23+
public const ATTRIBUTES = 'ignored_attributes';
24+
25+
private $extractor;
26+
27+
public function __construct(PropertyListExtractorInterface $extractor)
28+
{
29+
$this->extractor = $extractor;
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function getProperties($class, array $context = [])
36+
{
37+
$properties = $this->extractor->getProperties($class, $context);
38+
39+
if (null === $properties) {
40+
return null;
41+
}
42+
43+
$blacklist = $context[self::ATTRIBUTES] ?? null;
44+
45+
if (null === $blacklist) {
46+
return $properties;
47+
}
48+
49+
return array_diff($properties, $blacklist);
50+
}
51+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Extractor;
13+
14+
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
15+
16+
/**
17+
* Allow properties given a whitelist in the context.
18+
*
19+
* @author Joel Wurtz <joel.wurtz@gmail.com>
20+
*/
21+
final class WhitelistPropertyListExtractor implements PropertyListExtractorInterface
22+
{
23+
public const ATTRIBUTES = 'attributes';
24+
25+
private $extractor;
26+
27+
public function __construct(PropertyListExtractorInterface $extractor)
28+
{
29+
$this->extractor = $extractor;
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function getProperties($class, array $context = [])
36+
{
37+
$properties = $this->extractor->getProperties($class, $context);
38+
39+
if (null === $properties) {
40+
return null;
41+
}
42+
43+
$whitelist = $context[self::ATTRIBUTES] ?? null;
44+
45+
if (null === $whitelist) {
46+
return $properties;
47+
}
48+
49+
return array_intersect($properties, $whitelist);
50+
}
51+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Extractor;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
16+
use Symfony\Component\Serializer\Extractor\BlacklistPropertyListExtractor;
17+
18+
class BlacklistPropertyListExtractorTest extends TestCase
19+
{
20+
public function testRemoveAttributes()
21+
{
22+
$extractor = $this
23+
->getMockBuilder(PropertyListExtractorInterface::class)
24+
->getMock()
25+
;
26+
27+
$extractor->method('getProperties')->willReturn([
28+
'foo',
29+
'bar',
30+
'baz'
31+
]);
32+
33+
$blacklist = new BlacklistPropertyListExtractor($extractor);
34+
$properties = $blacklist->getProperties('SomeClass');
35+
36+
$this->assertInternalType('array', $properties);
37+
$this->assertContains('foo', $properties);
38+
$this->assertContains('bar', $properties);
39+
$this->assertContains('baz', $properties);
40+
41+
$properties = $blacklist->getProperties('SomeClass', [
42+
BlacklistPropertyListExtractor::ATTRIBUTES => ['foo', 'bar', 'dummy'],
43+
]);
44+
45+
$this->assertInternalType('array', $properties);
46+
$this->assertNotContains('foo', $properties);
47+
$this->assertNotContains('bar', $properties);
48+
$this->assertNotContains('dummy', $properties);
49+
$this->assertContains('baz', $properties);
50+
}
51+
52+
public function testNullProperties()
53+
{
54+
$extractor = $this
55+
->getMockBuilder(PropertyListExtractorInterface::class)
56+
->getMock()
57+
;
58+
59+
$extractor->method('getProperties')->willReturn(null);
60+
61+
$blacklist = new BlacklistPropertyListExtractor($extractor);
62+
$properties = $blacklist->getProperties('SomeClass');
63+
64+
$this->assertNull($properties);
65+
}
66+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Extractor;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
16+
use Symfony\Component\Serializer\Extractor\WhitelistPropertyListExtractor;
17+
18+
class WhitelistPropertyListExtractorTest extends TestCase
19+
{
20+
public function testAllowAttributes()
21+
{
22+
$extractor = $this
23+
->getMockBuilder(PropertyListExtractorInterface::class)
24+
->getMock()
25+
;
26+
27+
$extractor->method('getProperties')->willReturn([
28+
'foo',
29+
'bar',
30+
'baz'
31+
]);
32+
33+
$blacklist = new WhitelistPropertyListExtractor($extractor);
34+
$properties = $blacklist->getProperties('SomeClass');
35+
36+
$this->assertInternalType('array', $properties);
37+
$this->assertContains('foo', $properties);
38+
$this->assertContains('bar', $properties);
39+
$this->assertContains('baz', $properties);
40+
41+
$properties = $blacklist->getProperties('SomeClass', [
42+
WhitelistPropertyListExtractor::ATTRIBUTES => ['foo', 'bar', 'dummy'],
43+
]);
44+
45+
$this->assertInternalType('array', $properties);
46+
$this->assertContains('foo', $properties);
47+
$this->assertContains('bar', $properties);
48+
$this->assertNotContains('dummy', $properties);
49+
$this->assertNotContains('baz', $properties);
50+
51+
$properties = $blacklist->getProperties('SomeClass', [
52+
WhitelistPropertyListExtractor::ATTRIBUTES => [],
53+
]);
54+
55+
$this->assertInternalType('array', $properties);
56+
$this->assertEmpty($properties);
57+
}
58+
59+
public function testNullProperties()
60+
{
61+
$extractor = $this
62+
->getMockBuilder(PropertyListExtractorInterface::class)
63+
->getMock()
64+
;
65+
66+
$extractor->method('getProperties')->willReturn(null);
67+
68+
$blacklist = new WhitelistPropertyListExtractor($extractor);
69+
$properties = $blacklist->getProperties('SomeClass');
70+
71+
$this->assertNull($properties);
72+
}
73+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy