From 91df87a151a4587920a8372942510ce8f9eef19e Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 17 Jan 2009 13:36:29 -0500 Subject: [PATCH] initial commit --- classes/PublisherInfo.php | 99 ++++++++++++++++++++++++++++++++++++++ plugin_wonderful.php | 28 +++++++++++ test/TestPublisherInfo.php | 65 +++++++++++++++++++++++++ 3 files changed, 192 insertions(+) create mode 100644 classes/PublisherInfo.php create mode 100644 plugin_wonderful.php create mode 100644 test/TestPublisherInfo.php diff --git a/classes/PublisherInfo.php b/classes/PublisherInfo.php new file mode 100644 index 0000000..ca41141 --- /dev/null +++ b/classes/PublisherInfo.php @@ -0,0 +1,99 @@ +{$param} = null; + } + } + + function parse($string) { + $this->parser = xml_parser_create(); + xml_set_object($this->parser, $this); + xml_set_element_handler($this->parser, 'start_element_handler', 'end_element_handler'); + xml_set_character_data_handler($this->parser, 'character_data_handler'); + + $this->is_valid = true; + if (($result = xml_parse($this->parser, $string, true)) != 1) { $this->is_valid = false; } + xml_parser_free($this->parser); + + if ($this->is_valid) { + foreach (array('memberid', 'adboxes') as $required_parameter) { + if (is_null($this->{$required_parameter})) { + $this->is_valid = false; break; + } + } + } + + return $this->is_valid; + } + + function start_element_handler($parser, $name, $attributes) { + $this->current_string = ""; + switch ($name) { + case "PW:MEMBER": + $valid = false; + if (isset($attributes['MEMBERID'])) { + if (is_numeric($attributes['MEMBERID'])) { + $this->memberid = $attributes['MEMBERID']; + $valid = true; + } + } + $this->is_valid = $valid; + break; + case "PW:ADBOXES": + $this->adboxes = array(); + break; + case "PW:ADBOX": + $new_attributes = array(); + foreach (array('ADBOXID', 'SITENAME', 'URL', 'DIMENSIONS', 'RATING', 'CATEGORY') as $field) { + if (!isset($attributes[$field])) { + $this->is_valid = false; break; + } else { + $new_attributes[strtolower($field)] = $attributes[$field]; + } + } + if ($this->is_valid) { + if (!is_numeric($attributes['ADBOXID'])) { $this->is_valid = false; break; } + if (preg_match('#^[0-9]+x[0-9]+$#', $attributes['DIMENSIONS']) == 0) { $this->is_valid = false; break; } + if (($result = parse_url($attributes['URL'])) === false) { $this->is_valid = false; break; } + foreach (array('scheme', 'host', 'path') as $part) { + if (!isset($result[$part])) { $this->is_valid = false; break; } + } + + if ($this->is_valid) { + $this->current_adbox = (object)$new_attributes; + } + } + break; + } + } + + function end_element_handler($parser, $name) { + switch ($name) { + case "PW:ADBOX": + foreach (array("description", "tags", "standardcode", "advancedcode") as $element) { + if (!isset($this->current_adbox->{$element})) { + $this->is_valid = false; break; + } + } + if ($this->is_valid) { $this->adboxes[] = $this->current_adbox; } + break; + case "PW:DESCRIPTION": + case "PW:TAGS": + case "PW:STANDARDCODE": + case "PW:ADVANCEDCODE": + $short_name = strtolower(substr($name, 3)); + $this->current_adbox->{$short_name} = $this->current_string; + break; + } + } + + function character_data_handler($parser, $data) { + $this->current_string .= $data; + } +} + +?> \ No newline at end of file diff --git a/plugin_wonderful.php b/plugin_wonderful.php new file mode 100644 index 0000000..e98824d --- /dev/null +++ b/plugin_wonderful.php @@ -0,0 +1,28 @@ + \ No newline at end of file diff --git a/test/TestPublisherInfo.php b/test/TestPublisherInfo.php new file mode 100644 index 0000000..d944ba4 --- /dev/null +++ b/test/TestPublisherInfo.php @@ -0,0 +1,65 @@ +parser = new PublisherInfo(); + } + + public static function badDataProvider() { + return array( + array(""), + array("?xml version=\"1.0\""), + array(''), + array(''), + array(''), + array(''), + array(''), + array('aaaa'), + array('aaaa'), + array('aaaa') + ); + } + + /** + * @dataProvider badDataProvider + */ + public function testBadPWData($string) { + $this->assertFalse($this->parser->parse($string)); + } + + public static function goodDataProvider() { + return array( + array(''), + array('aaaa') + ); + } + + /** + * @dataProvider goodDataProvider + */ + public function testGoodPWData($string) { + $this->assertTrue($this->parser->parse($string)); + } + + public function testPWAPI() { + $this->parser->parse('aaaa'); + + $this->assertEquals(1, $this->parser->memberid); + $this->assertEquals(1, count($this->parser->adboxes)); + $this->assertEquals(1, $this->parser->adboxes[0]->adboxid); + $this->assertEquals("a", $this->parser->adboxes[0]->sitename); + $this->assertEquals("http://meow.raow/", $this->parser->adboxes[0]->url); + $this->assertEquals("1x1", $this->parser->adboxes[0]->dimensions); + $this->assertEquals("a", $this->parser->adboxes[0]->rating); + $this->assertEquals("a", $this->parser->adboxes[0]->description); + $this->assertEquals("a", $this->parser->adboxes[0]->tags); + $this->assertEquals("a", $this->parser->adboxes[0]->standardcode); + $this->assertEquals("a", $this->parser->adboxes[0]->advancedcode); + } +} + +?> \ No newline at end of file