get options merging working

This commit is contained in:
John Bintz 2009-11-12 07:56:32 -05:00
parent b287c05e21
commit 2de2df60ff
2 changed files with 201 additions and 110 deletions

View File

@ -7,18 +7,30 @@ require_once(dirname(__FILE__) . '/ComicPressComicPost.inc');
*/
class ComicPress {
var $comicpress_options = array(
'comic_dimensions' => '760x',
'rss_dimensions' => '350x',
'archive_dimensions' => '125x',
'mini_dimensions' => '100x',
'image_types' => array(
'comic' => array(
'default' => true,
'name' => 'Comic',
'dimensions' => '760x'
),
'rss' => array(
'default' => false,
'name' => 'RSS',
'dimensions' => '350x'
),
'archive' => array(
'default' => false,
'name' => 'Archive',
'dimensions' => '125x'
),
'mini' => array(
'default' => false,
'name' => 'Minithumb',
'dimensions' => '100x'
),
),
'helpers' => array(),
'addons' => array(),
'storyline_order' => '',
'subattachment_types' => array(
'rss' => 'RSS',
'archive' => 'Archive',
'mini' => 'Mini Thumb'
)
);
var $backends = array();
@ -39,10 +51,34 @@ class ComicPress {
function load() {
$result = get_option('comicpress-options');
if (is_array($result)) {
$this->comicpress_options = array_merge($this->comicpress_options, $result);
$this->comicpress_options = $this->_array_merge_replace_recursive($this->comicpress_options, $result);
}
}
function _array_merge_replace_recursive() {
$args = func_get_args();
foreach ($args as $arg) {
if (!is_array($arg)) { return end($args); }
}
$all_keys = array();
$result = array();
foreach ($args as $arg) {
$all_keys = array_merge($all_keys, array_keys($arg));
}
foreach ($all_keys as $key) {
$parts_to_merge = array();
foreach ($args as $arg) {
if (isset($arg[$key])) { $parts_to_merge[] = $arg[$key]; }
}
$result[$key] = call_user_func_array(array($this, '_array_merge_replace_recursive'), $parts_to_merge);
}
return $result;
}
/**
* Save ComicPress options.
*/

View File

@ -129,8 +129,19 @@ class ComicPressTest extends PHPUnit_Framework_TestCase {
array(false, 'default'),
array(array(), 'default'),
array(array(
'comic_dimensions' => '1000x'
), '1000x')
'image_types' => array(
'comic' => array(
'dimensions' => '1000x'
)
)
), '1000x'),
array(array(
'image_types' => array(
'comic' => array(
'test' => 'hello'
)
)
), 'default')
);
}
@ -139,9 +150,53 @@ class ComicPressTest extends PHPUnit_Framework_TestCase {
*/
function testLoad($options_array, $expected_dimensions) {
update_option('comicpress-options', $options_array);
if ($expected_dimensions == 'default') { $expected_dimensions = $this->cp->comicpress_options['comic_dimensions']; }
if ($expected_dimensions == 'default') { $expected_dimensions = $this->cp->comicpress_options['image_types']['comic']['dimensions']; }
$this->cp->load();
$this->assertEquals($expected_dimensions, $this->cp->comicpress_options['comic_dimensions']);
$this->assertEquals($expected_dimensions, $this->cp->comicpress_options['image_types']['comic']['dimensions']);
}
function providerTestArrayMergeReplaceRecursive() {
return array(
array(
array(1,2,3),
3
),
array(
array(
array(3),
array(5),
),
array(5)
),
array(
array(
array('test' => 3),
array('test' => 5),
),
array('test' => 5)
),
array(
array(
array('test' => array('test2' => 3)),
array('test' => array('test2' => 5)),
),
array('test' => array('test2' => 5))
),
array(
array(
array('test' => array()),
array('test' => array('test2' => 5)),
),
array('test' => array('test2' => 5))
),
);
}
/**
* @dataProvider providerTestArrayMergeReplaceRecursive
*/
function testArrayMergeReplaceRecursive($inputs, $expected_output) {
$this->assertEquals($expected_output, call_user_func_array(array($this->cp, '_array_merge_replace_recursive'), $inputs));
}
}