Skip to content

Commit 81d3c7c

Browse files
committed
Add OptionsResolver::allowUnknownKeys
1 parent 675316a commit 81d3c7c

File tree

8 files changed

+98
-20
lines changed

8 files changed

+98
-20
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636

3737
phpstan:
3838
docker:
39-
- image: php:7.3.6-cli-alpine3.10
39+
- image: php:7.2-cli-alpine3.10
4040
steps:
4141
- checkout
4242
- restore_cache:
@@ -48,7 +48,7 @@ jobs:
4848

4949
phpunit:
5050
docker:
51-
- image: php:7.3.6-cli-alpine3.10
51+
- image: php:7.2-fpm-alpine3.10
5252
steps:
5353
- checkout
5454
- restore_cache:

README.md

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
[![version](https://img.shields.io/badge/version-1.0.1-green.svg)](https://github.com/steevanb/symfony-options-resolver/tree/1.0.1)
1+
[![version](https://img.shields.io/badge/version-1.1.0-green.svg)](https://github.com/steevanb/symfony-options-resolver/tree/1.1.0)
22
[![doctrine](https://img.shields.io/badge/symfony/options_resolver-^2.6||^3.0||^4.0-blue.svg)](https://github.com/symfony/options-resolver)
33
[![php](https://img.shields.io/badge/php-^7.1-blue.svg)](http://www.php.net)
4-
![Lines](https://img.shields.io/badge/code%20lines-376-green.svg)
4+
![Lines](https://img.shields.io/badge/code%20lines-454-green.svg)
55
![Total Downloads](https://poser.pugx.org/steevanb/symfony-options-resolver/downloads)
66
[![Scrutinizer](https://scrutinizer-ci.com/g/steevanb/symfony-options-resolver/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/steevanb/symfony-options-resolver/)
77

@@ -14,41 +14,51 @@ Add features to Symfony [OptionsResolver](https://github.com/symfony/options-res
1414
### Installation
1515

1616
```bash
17-
composer require "steevanb/symfony-options-resolver": "^1.0.1"
17+
composer require "steevanb/symfony-options-resolver": "^1.1.0"
1818
```
1919

2020
### Examples
2121

2222
Define an optional option with a default value:
2323
```php
24-
use steevanb\SymfonyOptionsResolver\OptionsResolver
24+
use steevanb\SymfonyOptionsResolver\OptionsResolver;
2525

2626
$optionsResolver = new OptionsResolver();
2727

2828
# Configure an optional option
29-
$optionsResolver->configureOption('foo', ['string'], ['bar', 'baz'], 'bar');
29+
$optionsResolver->configureOption('foo', ['string'], 'default', ['default', 'value1', 'value2']);
3030

3131
# Equivalent to call original OptionsResolver methods:
3232
$optionsResolver
3333
->setDefined('foo')
3434
->setAllowedTypes('foo', ['string'])
35-
->setAllowedValues('foo', ['bar', 'baz'])
36-
->setDefault('foo', 'bar');
35+
->setDefault('foo', 'default')
36+
->setAllowedValues('foo', ['default', 'value1', 'value2']);
3737
```
3838

3939
Define a required option:
4040
```php
41-
use steevanb\SymfonyOptionsResolver\OptionsResolver
41+
use steevanb\SymfonyOptionsResolver\OptionsResolver;
4242

4343
$optionsResolver = new OptionsResolver();
4444

4545
# Configure a required option
46-
$optionsResolver->configureRequiredOption('foo', ['string'], ['bar', 'baz']);
46+
$optionsResolver->configureRequiredOption('foo', ['string'], ['value1', 'value2']);
4747

4848
# Equivalent to call original OptionsResolver methods:
4949
$optionsResolver
50-
->setDefined('foo')
50+
->setRequired('foo')
5151
->setAllowedTypes('foo', ['string'])
52-
->setAllowedValues('foo', ['bar', 'baz'])
53-
->setRequired('foo');
52+
->setAllowedValues('foo', ['value1', 'value2']);
53+
```
54+
55+
Allow unknown keys:
56+
```php
57+
use steevanb\SymfonyOptionsResolver\OptionsResolver;
58+
59+
# This will not throw an exception because extraKey is not configured
60+
(new OptionsResolver())
61+
->configureRequiredOption('foo', ['string'], ['value1', 'value2'])
62+
->setAllowUnknownKeys(true)
63+
->resolve(['foo' => 'value1', 'extraKey' => 'extraValue']);
5464
```

bin/phpstan

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ if [ $(which docker || false) ]; then
99
--rm \
1010
-v ${PROJECT_DIRECTORY}:/var/phpstan:ro \
1111
-w /var/phpstan \
12-
php:7.3.6-cli-alpine3.10 \
12+
php:7.2-cli-alpine3.10 \
1313
/var/phpstan/bin/phpstan
1414
else
1515
./vendor/bin/phpstan analyse --ansi -c phpstan.neon

changelog.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
### [1.0.1](../../compare/1.0.0...1.0.1) - 2017-09-30
1+
### [1.1.0](../../compare/1.0.1...1.1.0) - 2019-12-06
2+
3+
- Add `OptionsResolver::allowUnknownKeys()`
4+
5+
### [1.0.1](../../compare/1.0.0...1.0.1) - 2019-09-30
26

37
- `$allowedValues` parameter of `OptionsResolver::confogureOptions()` and `OptionsResolver::configureRequiredOptions()` is now optional.
48
- [BC BREAK (should not impact a lot of people since library is not downloaded for now)] Inverse `OptionsResolver::configureOptions()` `$default` and `$allowedValues` parameters order.

src/OptionsResolver.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
class OptionsResolver extends SymfonyOptionsResolver
1010
{
11+
/** @var bool */
12+
protected $allowUnknownKeys = false;
13+
1114
public function configureOption(string $name, array $allowedTypes, $default, array $allowedValues = null): self
1215
{
1316
$this
@@ -26,6 +29,22 @@ public function configureRequiredOption(string $name, array $allowedTypes, array
2629
return $this;
2730
}
2831

32+
public function setAllowUnknownKeys(bool $allow): self
33+
{
34+
$this->allowUnknownKeys = $allow;
35+
36+
return $this;
37+
}
38+
39+
public function resolve(array $options = [])
40+
{
41+
if ($this->allowUnknownKeys) {
42+
$this->setDefined(array_keys($options));
43+
}
44+
45+
return parent::resolve($options);
46+
}
47+
2948
protected function setAllowedTypesAndValues(string $name, array $allowedTypes, array $allowedValues = null): self
3049
{
3150
$this
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace steevanb\SymfonyOptionsResolver\Tests\OptionsResolver;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use steevanb\SymfonyOptionsResolver\OptionsResolver;
9+
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
10+
11+
final class AllowUnknownKeysTest extends TestCase
12+
{
13+
public function testDefault(): void
14+
{
15+
static::expectException(UndefinedOptionsException::class);
16+
$this
17+
->createOptionsResolver()
18+
->resolve(['foo' => 'bar', 'extraKey' => 'extraValue']);
19+
}
20+
21+
public function testAllowed(): void
22+
{
23+
$this
24+
->createOptionsResolver()
25+
->setAllowUnknownKeys(true)
26+
->resolve(['foo' => 'bar', 'extraKey' => 'extraValue']);
27+
28+
static::addToAssertionCount(1);
29+
}
30+
31+
public function testNotAllowed(): void
32+
{
33+
static::expectException(UndefinedOptionsException::class);
34+
$this
35+
->createOptionsResolver()
36+
->setAllowUnknownKeys(false)
37+
->resolve(['foo' => 'bar', 'extraKey' => 'extraValue']);
38+
}
39+
40+
private function createOptionsResolver(string $defaultValue = null): OptionsResolver
41+
{
42+
return (new OptionsResolver())
43+
->configureOption('foo', ['string'], $defaultValue, ['bar']);
44+
}
45+
}

tests/OptionsResolverConfigureOptionTest.php renamed to tests/OptionsResolver/ConfigureOptionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
declare(strict_types=1);
44

5-
namespace steevanb\SymfonyOptionsResolver\Tests;
5+
namespace steevanb\SymfonyOptionsResolver\Tests\OptionsResolver;
66

77
use PHPUnit\Framework\TestCase;
88
use steevanb\SymfonyOptionsResolver\OptionsResolver;
99
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
1010

11-
final class OptionsResolverConfigureOptionTest extends TestCase
11+
final class ConfigureOptionTest extends TestCase
1212
{
1313
public function testValid(): void
1414
{

tests/OptionsResolverConfigureRequiredOptionTest.php renamed to tests/OptionsResolver/ConfigureRequiredOptionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace steevanb\SymfonyOptionsResolver\Tests;
5+
namespace steevanb\SymfonyOptionsResolver\Tests\OptionsResolver;
66

77
use PHPUnit\Framework\TestCase;
88
use steevanb\SymfonyOptionsResolver\OptionsResolver;
@@ -11,7 +11,7 @@
1111
Exception\MissingOptionsException
1212
};
1313

14-
final class OptionsResolverConfigureRequiredOptionTest extends TestCase
14+
final class ConfigureRequiredOptionTest extends TestCase
1515
{
1616
public function testValid(): void
1717
{

0 commit comments

Comments
 (0)