Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge 4772c0d into d64b473
Browse files Browse the repository at this point in the history
  • Loading branch information
func0der committed Nov 21, 2017
2 parents d64b473 + 4772c0d commit 250befd
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 3 deletions.
73 changes: 71 additions & 2 deletions src/ContentNegotiationOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,75 @@ class ContentNegotiationOptions extends AbstractOptions
*/
protected $httpOverrideMethods = [];

/**
* @var array
*/
private $keysToNormalize = [
'accept-whitelist',
'content-type-whitelist',
'x-http-method-override-enabled',
'http-override-methods',
];

/**
* {@inheritDoc}
*
* Normalizes and merges the configuration for specific configuration keys
* @see self::normalizeOptions
*/
public function setFromArray($options)
{
return parent::setFromArray(
$this->normalizeOptions($options)
);
}

/**
* This method uses the config keys given in $keyToNormalize to merge
* the config.
* It uses Zend's default approach of merging configs, by merging them with
* `array_merge_recursive()`.
*
* @param array $config
*
* @return array
*/
private function normalizeOptions(array $config)
{
$mergedConfig = $config;

foreach ($this->keysToNormalize as $key) {
$normalizedKey = $this->normalizeKey($key);

if (isset($config[$key]) && isset($config[$normalizedKey])) {
$mergedConfig[$normalizedKey] = array_merge_recursive(
$config[$key],
$config[$normalizedKey]
);

unset($config[$key]);
} elseif (isset($config[$key])) {
$mergedConfig[$normalizedKey] = $config[$key];

unset($config[$key]);
} elseif (isset($config[$normalizedKey])) {
$mergedConfig[$normalizedKey] = $config[$normalizedKey];
}
}

return $mergedConfig;
}

/**
* @param string $key
*
* @return string
*/
private function normalizeKey($key)
{
return str_replace('-', '_', $key);
}

/**
* {@inheritDoc}
*
Expand All @@ -55,7 +124,7 @@ class ContentNegotiationOptions extends AbstractOptions
*/
public function __set($key, $value)
{
parent::__set(str_replace('-', '_', $key), $value);
parent::__set($this->normalizeKey($key), $value);
}

/**
Expand All @@ -72,7 +141,7 @@ public function __set($key, $value)
*/
public function __get($key)
{
return parent::__get(str_replace('-', '_', $key));
return parent::__get($this->normalizeKey($key));
}

/**
Expand Down
37 changes: 37 additions & 0 deletions test/ContentNegotiationOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,41 @@ public function testConstructorAllowsDashSeparatedKeys($key, $normalized)
$this->assertEquals(['value'], $options->{$key});
$this->assertEquals(['value'], $options->{$normalized});
}

/**
* @dataProvider dashSeparatedOptions
*/
public function testDashAndUnderscoreSeparatedValuesGetMerged(
$key,
$normalized
) {
$keyValue = 'valueKey';
$normalizedValue = 'valueNormalized';
$expectedResult = [
$keyValue,
$normalizedValue,
];

$options = new ContentNegotiationOptions(
[
$key => [
$keyValue,
],
$normalized => [
$normalizedValue,
],
]
);

$this->assertEquals(
$expectedResult,
$options->{$key},
'The value for the hyphen separated key was not as expected.'
);
$this->assertEquals(
$expectedResult,
$options->{$normalized},
'The value for the normalized key was not as expected.'
);
}
}
31 changes: 30 additions & 1 deletion test/Factory/ContentNegotiationOptionsFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,41 @@ public function testCreateServiceShouldReturnContentNegotiationOptionsInstance()
];

$serviceManager = new ServiceManager();
$serviceManager->setService('Config', $config);
$serviceManager->setService('config', $config);

$factory = new ContentNegotiationOptionsFactory();

$service = $factory($serviceManager, 'ContentNegotiationOptions');

$this->assertInstanceOf('ZF\ContentNegotiation\ContentNegotiationOptions', $service);
}

public function testCreateServiceShouldReturnContentNegotiationOptionsInstanceWithOptions()
{
$config = [
'zf-content-negotiation' => [
'accept_whitelist' => [],
],
];

$serviceManager = new ServiceManager();
$serviceManager->setService('config', $config);

$factory = new ContentNegotiationOptionsFactory();

$service = $factory($serviceManager, 'ContentNegotiationOptions');

$this->assertNotEmpty($service->toArray());
}

public function testCreateServiceWithoutConfigShouldReturnContentNegotiationOptionsInstance()
{
$serviceManager = new ServiceManager();

$factory = new ContentNegotiationOptionsFactory();

$service = $factory($serviceManager, 'ContentNegotiationOptions');

$this->assertNotEmpty($service->toArray());
}
}

0 comments on commit 250befd

Please sign in to comment.