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

Allow dash-separated keys #48

Merged
merged 1 commit into from
Jul 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/ContentNegotiationOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,41 @@ class ContentNegotiationOptions extends AbstractOptions
*/
protected $contentTypeWhitelist = [];

/**
* {@inheritDoc}
*
* Normalizes dash-separated keys to underscore-separated to ensure
* backwards compatibility with old options (even though dash-separated
* were previously ignored!).
*
* @see \Zend\Stdlib\ParameterObject::__set()
* @param string $key
* @param mixed $value
* @throws \Zend\Stdlib\Exception\BadMethodCallException
* @return void
*/
public function __set($key, $value)
{
parent::__set(str_replace('-', '_', $key), $value);
}

/**
* {@inheritDoc}
*
* Normalizes dash-separated keys to underscore-separated to ensure
* backwards compatibility with old options (even though dash-separated
* were previously ignored!).
*
* @see \Zend\Stdlib\ParameterObject::__get()
* @param string $key
* @throws \Zend\Stdlib\Exception\BadMethodCallException
* @return mixed
*/
public function __get($key)
{
return parent::__get(str_replace('-', '_', $key));
}

/**
* @param array $controllers
*/
Expand Down
42 changes: 42 additions & 0 deletions test/ContentNegotiationOptionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @copyright Copyright (c) 2014 Zend Technologies USA Inc. (http://www.zend.com)
*/

namespace ZFTest\ContentNegotiation;

use PHPUnit_Framework_TestCase as TestCase;
use ZF\ContentNegotiation\ContentNegotiationOptions;

class ContentNegotiationOptionsTest extends TestCase
{
public function dashSeparatedOptions()
{
return [
'accept-whitelist' => ['accept-whitelist', 'accept_whitelist'],
'content-type-whitelist' => ['content-type-whitelist', 'content_type_whitelist'],
];
}

/**
* @dataProvider dashSeparatedOptions
*/
public function testSetNormalizesDashSeparatedKeysToUnderscoreSeparated($key, $normalized)
{
$options = new ContentNegotiationOptions();
$options->{$key} = ['value'];
$this->assertEquals(['value'], $options->{$key});
$this->assertEquals(['value'], $options->{$normalized});
}

/**
* @dataProvider dashSeparatedOptions
*/
public function testConstructorAllowsDashSeparatedKeys($key, $normalized)
{
$options = new ContentNegotiationOptions([$key => ['value']]);
$this->assertEquals(['value'], $options->{$key});
$this->assertEquals(['value'], $options->{$normalized});
}
}