Skip to content

Commit

Permalink
Merge pull request #59 from DataValues/options
Browse files Browse the repository at this point in the history
Add `withDefaultOption()` to replace `defaultOption()`
  • Loading branch information
JeroenDeDauw committed Dec 6, 2022
2 parents d5cfcaf + 3cb96d5 commit 7c54a89
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -36,6 +36,12 @@ via Github Actions.

## Release notes

### 1.1.0 (dev)

* Introduced `ParserOptions::withDefaultOption()` and `FormatterOptions::withDefaultOption()`,
which return a copy of the options with the default applied;
the `defaultOption()` methods, which modify the options in place, are deprecated.

### 1.0.0 (2021-01-20)

* Updated minimum required PHP version from 5.5.9 to 7.2
Expand Down
25 changes: 25 additions & 0 deletions src/ValueFormatters/FormatterOptions.php
Expand Up @@ -32,6 +32,30 @@ public function __construct( array $options = [] ) {
$this->options = $options;
}

/**
* Create and return a copy of these options,
* where the value of an option was set to the provided default
* if it had not been set already.
*
* Example usage:
* ```php
* public function __construct( FormatterOptions $options ) {
* $this->options = $options
* ->withDefaultOption( self::OPT_A, 'A' )
* ->withDefaultOption( self::OPT_B, 'B' );
* }
* ```
*
* @param string $option
* @param mixed $default
* @return self
*/
public function withDefaultOption( string $option, $default ): self {
$options = new self( $this->options );
$options->defaultOption( $option, $default );
return $options;
}

/**
* Sets the value of the specified option.
*
Expand Down Expand Up @@ -75,6 +99,7 @@ public function hasOption( string $option ): bool {
/**
* Sets the value of an option to the provided default in case the option is not set yet.
*
* @deprecated Use {@link withDefaultOption()} instead.
* @param string $option
* @param mixed $default
*/
Expand Down
25 changes: 25 additions & 0 deletions src/ValueParsers/ParserOptions.php
Expand Up @@ -33,6 +33,30 @@ public function __construct( array $options = [] ) {
$this->options = $options;
}

/**
* Create and return a copy of these options,
* where the value of an option was set to the provided default
* if it had not been set already.
*
* Example usage:
* ```php
* public function __construct( ParserOptions $options ) {
* $this->options = $options
* ->withDefaultOption( self::OPT_A, 'A' )
* ->withDefaultOption( self::OPT_B, 'B' );
* }
* ```
*
* @param string $option
* @param mixed $default
* @return self
*/
public function withDefaultOption( string $option, $default ): self {
$options = new self( $this->options );
$options->defaultOption( $option, $default );
return $options;
}

/**
* Sets the value of the specified option.
*
Expand Down Expand Up @@ -76,6 +100,7 @@ public function hasOption( string $option ): bool {
/**
* Sets the value of an option to the provided default in case the option is not set yet.
*
* @deprecated Use {@link withDefaultOption()} instead.
* @param string $option
* @param mixed $default
*/
Expand Down
19 changes: 19 additions & 0 deletions tests/ValueFormatters/FormatterOptionsTest.php
Expand Up @@ -191,4 +191,23 @@ public function testDefaultOption() {
}
}

public function testWithDefaultOption() {
$originalOptions = new FormatterOptions( [ 'foo' => 'foo' ] );

$newOptions = $originalOptions
->withDefaultOption( 'foo', 'FOO' )
->withDefaultOption( 'bar', 'BAR' );

$this->assertNotSame( $originalOptions, $newOptions,
'should be a fresh instance' );
$this->assertSame( 'foo', $originalOptions->getOption( 'foo' ),
'original options should have same non-default option' );
$this->assertFalse( $originalOptions->hasOption( 'bar' ),
'original options should not have default option' );
$this->assertSame( 'foo', $newOptions->getOption( 'foo' ),
'new options should have same non-default option' );
$this->assertSame( 'BAR', $newOptions->getOption( 'bar' ),
'new options should have default option' );
}

}
25 changes: 22 additions & 3 deletions tests/ValueParsers/ParserOptionsTest.php
Expand Up @@ -45,7 +45,7 @@ public function testConstructorFail() {
42 => [ 'o_O', false, null, '42' => 42, [] ]
];

$this->expectException( 'Exception' );
$this->expectException( \Exception::class );

new ParserOptions( $options );
}
Expand Down Expand Up @@ -117,11 +117,11 @@ public function testSetOption() {
*/
public function testGetOption( $nonExistingOption ) {
$this->assertTrue( true );
$formatterOptions = new ParserOptions( [ 'foo' => 'bar' ] );
$parserOptions = new ParserOptions( [ 'foo' => 'bar' ] );

$this->expectException( 'InvalidArgumentException' );

$formatterOptions->getOption( $nonExistingOption );
$parserOptions->getOption( $nonExistingOption );
}

public function nonExistingOptionsProvider() {
Expand Down Expand Up @@ -191,4 +191,23 @@ public function testDefaultOption() {
}
}

public function testWithDefaultOption() {
$originalOptions = new ParserOptions( [ 'foo' => 'foo' ] );

$newOptions = $originalOptions
->withDefaultOption( 'foo', 'FOO' )
->withDefaultOption( 'bar', 'BAR' );

$this->assertNotSame( $originalOptions, $newOptions,
'should be a fresh instance' );
$this->assertSame( 'foo', $originalOptions->getOption( 'foo' ),
'original options should have same non-default option' );
$this->assertFalse( $originalOptions->hasOption( 'bar' ),
'original options should not have default option' );
$this->assertSame( 'foo', $newOptions->getOption( 'foo' ),
'new options should have same non-default option' );
$this->assertSame( 'BAR', $newOptions->getOption( 'bar' ),
'new options should have default option' );
}

}

0 comments on commit 7c54a89

Please sign in to comment.