better media handling

This commit is contained in:
John Bintz 2010-02-03 20:11:03 -05:00
parent 2b3940b1f7
commit b55fcf268c
2 changed files with 45 additions and 17 deletions

View File

@ -133,6 +133,7 @@ class ComicPressTagBuilder {
const HAS_POST_METHOD = 'has post method'; const HAS_POST_METHOD = 'has post method';
const HAS_EXTRACT_METHOD = 'has extract method'; const HAS_EXTRACT_METHOD = 'has extract method';
const IS_SETUP = 'is setup'; const IS_SETUP = 'is setup';
const IS_MEDIA = 'is media';
public function parse_method($method_name, $extract_method_arguments = null) { public function parse_method($method_name, $extract_method_arguments = null) {
$methods = array(); $methods = array();
@ -144,6 +145,7 @@ class ComicPressTagBuilder {
$post_method = null; $post_method = null;
$extract_method = null; $extract_method = null;
$is_setup = false; $is_setup = false;
$is_media = false;
while (!empty($parts)) { while (!empty($parts)) {
$current = strtolower(array_shift($parts)); $current = strtolower(array_shift($parts));
@ -167,11 +169,11 @@ class ComicPressTagBuilder {
} }
if (in_array($current, array('id', 'permalink', 'title', 'timestamp', 'date', 'post'))) { if (in_array($current, array('id', 'permalink', 'title', 'timestamp', 'date', 'post'))) {
if ($is_setup) { if ($is_setup || $is_media) {
if ($current != 'post') { if ($current != 'post') {
throw new ComicPressException('You can only set up a post'); throw new ComicPressException('You can only set up a post');
} }
$extract_method = 'setup'; $extract_method = $is_setup ? 'setup' : 'media';
} else { } else {
if ($state == self::HAS_EXTRACT_METHOD) { if ($state == self::HAS_EXTRACT_METHOD) {
throw new ComicPressException('Only one extract method can be specified'); throw new ComicPressException('Only one extract method can be specified');
@ -182,20 +184,35 @@ class ComicPressTagBuilder {
} }
} }
if (in_array($state, array(self::START_PARSE, self::IS_SETUP))) { if (in_array($state, array(self::START_PARSE, self::IS_SETUP, self::IS_MEDIA))) {
if ($current == 'setup') { switch ($current) {
if ($state == self::IS_SETUP) { case 'setup':
throw new ComicPressException('Setup need only be called once'); if ($state != self::START_PARSE) {
} throw new ComicPressException('Setup can only be called at the beginning');
$is_setup = true; }
$state = self::IS_SETUP; $is_setup = true;
} else { $state = self::IS_SETUP;
if (in_array($current, array('first', 'previous', 'next', 'last'))) { break;
$post_method = $current; case 'media':
$state = self::HAS_POST_METHOD; if ($state != self::START_PARSE) {
} else { throw new ComicPressException('Media can only be called at the beginning');
throw new ComicPressException("${current} isn't allowed at this point"); }
} $is_media = true;
$state = self::IS_MEDIA;
break;
case 'for':
if ($state != self::IS_MEDIA) {
throw new ComicPressException('"for" only allowed in media methods');
}
break;
default:
if (in_array($current, array('first', 'previous', 'next', 'last'))) {
$post_method = $current;
$state = self::HAS_POST_METHOD;
} else {
throw new ComicPressException("${current} isn't allowed at this point");
}
break;
} }
} }
} }

View File

@ -278,6 +278,14 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase {
array('first'), array('first'),
array('setup') array('setup')
) )
),
array(
'media_for_first_post_in_category_1',
array(
array('in', 'category-1'),
array('first'),
array('media')
)
) )
); );
} }
@ -294,7 +302,10 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase {
array('first_in_'), array('first_in_'),
array('first_post_id'), array('first_post_id'),
array('setup_setup'), array('setup_setup'),
array('setup_first_permalink') array('setup_first_permalink'),
array('setup_media'),
array('media_setup'),
array('setup_for')
); );
} }