Skip to content

Instantly share code, notes, and snippets.

@michaeltwofish
Created June 12, 2010 10:46
Show Gist options
  • Save michaeltwofish/435628 to your computer and use it in GitHub Desktop.
Save michaeltwofish/435628 to your computer and use it in GitHub Desktop.
/**
* Constructor for the Tags class.
* @param mixed $tags String or array of tags
*/
public function __construct( $tags = array() )
{
if ( is_string( $tags ) && ( '' !== $tags ) ) {
// dirrty ;)
$rez = array( '\\"'=>':__unlikely_quote__:', '\\\''=>':__unlikely_apos__:' );
$zer = array( ':__unlikely_quote__:'=>'"', ':__unlikely_apos__:'=>"'" );
// escape
$tagstr = str_replace( array_keys( $rez ), $rez, $tags );
// match-o-matic
preg_match_all( '/((("|((?<= )|^)\')\\S([^\\3]*?)\\3((?=[\\W])|$))|[^,])+/u', $tagstr, $matches );
// cleanup
$tags = array_map( 'trim', $matches[0] );
//return new Tag( array( 'tag_text' => $term->term_display, 'tag_slug' => $term->term, 'id' => $term->id ) );
$tags = preg_replace( array_fill( 0, count( $tags ), '/^(["\'])(((?!").)+)(\\1)$/'), '$2', $tags );
// unescape
$tags = str_replace( array_keys( $zer ), $zer, $tags );
}
// Turn each of the tags into a Tag
array_walk( $tags, create_function('&$tag', 'return new Tag( array( "tag_text" => $tag ) );') );
parent::__construct( $tags );
}
<?php
require_once dirname( dirname( dirname( __FILE__ ) ) ) . DIRECTORY_SEPARATOR . 'phpunit_bootstrap.php';
class system_classes_TagsTest extends PHPUnit_Framework_TestCase
{
public function setup()
{
}
public function teardown()
{
}
public function test_construct_tags()
{
// Construct tags from tag string
$tags = new Tags('one, two, three');
$this->assertType('Tags', $tags);
$this->assertEquals(count($tags), 3);
// Construct tags from tag array
$tags = new Tags(array('one', 'two', 'three'));
$this->assertType('Tags', $tags);
$this->assertEquals(count($tags), 3);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment