filtering

This commit is contained in:
John Bintz 2009-12-01 21:37:28 -05:00
parent 220322318d
commit 1f428c786a
2 changed files with 103 additions and 6 deletions

View File

@ -1,6 +1,7 @@
<?php <?php
class ComicPressMediaHandling { class ComicPressMediaHandling {
var $root_filter = '%wordpress%/%type-folder%/';
var $default_filter = '%wordpress%/%type-folder%/%date-Y-m-d%*.*'; var $default_filter = '%wordpress%/%type-folder%/%date-Y-m-d%*.*';
function _bundle_global_variables() { function _bundle_global_variables() {
@ -26,7 +27,7 @@ class ComicPressMediaHandling {
if (!is_null($filter_to_use)) { if (!is_null($filter_to_use)) {
if (is_string($filter_to_use)) { if (is_string($filter_to_use)) {
if (isset($comic_filename_filters[$filter_to_use])) { if (isset($comic_filename_filters[$filter_to_use])) {
return $comic_filename_filters[$filter_to_use]; return $this->_convert_to_percent_filter($comic_filename_filters[$filter_to_use]);
} }
} }
} }
@ -34,7 +35,52 @@ class ComicPressMediaHandling {
return $this->default_filter; return $this->default_filter;
} }
function get_comic_path($source = 'comic', $override_post = null, $filter = 'default', $multi = false) { function _convert_to_percent_filter($old) {
if (strpos(strtolower($old), '%wordpress%') !== 0) {
$old = str_replace('{date}', '%date-Y-m-d%', $old);
return $this->root_filter . $old;
}
return $old;
}
function _expand_filter($filter, $type_filter, $override_post = null) {
global $post;
$this->post_to_use = !is_null($override_post) ? $override_post : $post;
$this->type_filter = $type_filter;
$result = preg_replace_callback('#%([a-z0-9-]+)%#i', array(&$this, '_expand_filter_callback'), $filter);
$result = str_replace('.', '\.', $result);
$result = str_replace('*', '.*', $result);
unset($this->post_to_use);
unset($this->type_filter);
return $result;
}
function _abspath() { return ABSPATH; }
function _expand_filter_callback($matches) {
$value = '';
switch (strtolower($matches[1])) {
case 'wordpress':
$value = $this->_abspath();
break;
case 'type-folder':
$value = $this->type_filter;
break;
default:
if (preg_match('#^date-(.*)$#', $matches[1], $date_matches) > 0) {
$value = date($date_matches[1], strtotime($this->post_to_use->post_date));
break;
}
$value = $matches[0];
break;
}
return $value;
}
function get_comic_path($type = 'comic', $override_post = null, $filter = 'default', $multi = false) {
$filter = $this->get_filter($filter);
} }
} }

View File

@ -9,6 +9,10 @@ class ComicPressMediaHandlingTest extends PHPUnit_Framework_TestCase {
function setUp() { function setUp() {
_reset_wp(); _reset_wp();
$this->cpmh = new ComicPressMediaHandling(); $this->cpmh = new ComicPressMediaHandling();
$this->default_filter = $this->cpmh->default_filter;
vfsStreamWrapper::register();
vfsStreamWrapper::setRoot(new vfsStreamDirectory('root'));
} }
function testBundleGlobalVariables() { function testBundleGlobalVariables() {
@ -28,10 +32,12 @@ class ComicPressMediaHandlingTest extends PHPUnit_Framework_TestCase {
} }
function providerTestGetFilter() { function providerTestGetFilter() {
$cpmh = new ComicPressMediaHandling();
return array( return array(
array(null, '%wordpress%/%type-folder%/%date-Y-m-d%*.*'), array(null, $cpmh->default_filter),
array('fail', '%wordpress%/%type-folder%/%date-Y-m-d%*.*'), array('fail', $cpmh->default_filter),
array(array(), '%wordpress%/%type-folder%/%date-Y-m-d%*.*'), array(array(), $cpmh->default_filter),
array('test', 'test') array('test', 'test')
); );
} }
@ -44,6 +50,51 @@ class ComicPressMediaHandlingTest extends PHPUnit_Framework_TestCase {
$comic_filename_filters['test'] = 'test'; $comic_filename_filters['test'] = 'test';
$this->assertEquals($expected_result, $this->cpmh->_get_filter($filter_to_use)); $cpmh = $this->getMock('ComicPressMediaHandling', array('_convert_to_percent_filter'));
if ($expected_result !== $cpmh->default_filter) {
$cpmh->expects($this->once())->method('_convert_to_percent_filter')->with($expected_result)->will($this->returnValue($expected_result));
} else {
$cpmh->expects($this->never())->method('_convert_to_percent_filter');
}
$this->assertEquals($expected_result, $cpmh->_get_filter($filter_to_use));
}
function providerTestConvertToPercentFilter() {
return array(
array('', '%wordpress%/%type-folder%/'),
array('{date}', '%wordpress%/%type-folder%/%date-Y-m-d%'),
array('%wordpress%/%type-folder%/{date}', '%wordpress%/%type-folder%/{date}'),
);
}
/**
* @dataProvider providerTestConvertToPercentFilter
*/
function testConvertToPercentFilter($old_filter, $new_filter) {
$this->assertEquals($new_filter, $this->cpmh->_convert_to_percent_filter($old_filter));
}
function providerTestExpandFilter() {
return array(
array('', ''),
array('%test%', '%test%'),
array('%wordpress%', vfsStream::url('root')),
array('%wordpress%%wordpress%', vfsStream::url('root') . vfsStream::url('root')),
array('%test test%', '%test test%'),
array('%wordpress%/%type-folder%', vfsStream::url('root') . '/comic'),
array('%date-Y%', '2009'),
array('%wordpress%/%type-folder%/%date-Y-m-d%*.*', vfsStream::url('root') . '/comic/2009-01-01.*\..*'),
);
}
/**
* @dataProvider providerTestExpandFilter
*/
function testExpandFilter($filter, $expected_result) {
$cpmh = $this->getMock('ComicPressMediaHandling', array('_abspath'));
$cpmh->expects($this->any())->method('_abspath')->will($this->returnValue(vfsStream::url('root')));
$this->assertEquals($expected_result, $cpmh->_expand_filter($filter, 'comic', (object)array('ID' => 1, 'post_date' => '2009-01-01 15:00:00')));
} }
} }