Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

moved core functions from library to model

  • Loading branch information...
commit 6af43dd6edf0427d54dc001a2107c6eea40e752a 1 parent 16f1cd8
@rjz authored
View
2  config/autoload.php
@@ -1,6 +1,6 @@
<?php
-# Load the markdown helper
+// Autoload the atomizer library
$autoload['libraries'] = array('atomizer');
?>
View
344 libraries/atomizer.php
@@ -1,306 +1,21 @@
<?php if (! defined('BASEPATH')) exit('No direct script access');
/**
- * A structure for storing and converting between array, object, and XML data.
- */
-class AtomizerStruct {
-
- protected
- $data,
- $strict,
- $template;
-
- /**
- * Create a new object based on the specified template
- * @param array the template to validate this object's data against
- * @param boolean whether or not the template's data types must be matched
- */
- public function __construct( $template, $strict = false ) {
- $this->template = $template;
- foreach( $this->template as $key => $value ) {
- $this->data[ $key ] = NULL;
- }
-
- $this->strict = $strict;
- }
-
- /**
- * Getter: retrieve the value of the specified key
- * @param string Key
- * @return mixed Value
- */
- public function __get( $key ) {
- if( array_key_exists( $key, $this->template ) ) {
- return $this->data[ $key ];
- }
- }
-
- /**
- * Setter: set the value specified by a key
- * @param string Key
- * @param mixed Value
- */
- public function __set( $key, $value ) {
- if( array_key_exists( $key, $this->template ) ) {
-
- $validator = 'is_' . $this->template[ $key ];
-
- if( !$this->strict || $validator( $value ) ) {
- $this->data[ $key ] = $value;
- } else {
- throw new Exception( 'Data types do not match in ' . get_class( $this ) . '::' . $key );
- }
- }
- }
-
- /**
- * Find the first child of a DOMElement that matches the specfied tag
- * @param DOMElement Parent element
- * @param string The tag name (e.g., "item") to match
- * @return DOMElement|NULL
- */
- protected function findChild( $node, $tagName ) {
- $tags = $node->getElementsByTagName( $tagName );
- $i = 0;
- while( $tag = $tags->item($i++) ) {
- if( $tag->parentNode === $node && $tag->hasChildNodes() ) {
-
- return $tag;
- }
- }
- return NULL;
- }
-
- /**
- * Clone object
- * @param object The object to clone
- */
- public function fromCopy( $copy ) {
- foreach( $this->template as $key => $type ) {
- $this->data[$key] = $copy->$key;
- }
- }
-
- /**
- * Construct object from array data
- * @param array The array to use to set parameters
- */
- public function fromArray( $data ) {
-
- foreach( $this->template as $key => $type ) {
- if( array_key_exists( $key, $data ) ) {
- $this->$key = $data[ $key ];
- }
- }
- }
-
- /**
- * Construct object from XML template
- * @param DOMElement The parent element containing parameters
- */
- public function fromXML( $xml ) {
-
- foreach( $this->template as $key => $type ) {
-
- $el = $this->findChild( $xml, $key );
- if( $el ) {
- $this->$key = $el->firstChild->textContent;
- }
- }
- }
-}
-
-/**
- * A single item in an atomizer feed
- */
-class AtomizerItem extends AtomizerStruct {
-
- public function __construct( $copy = NULL ) {
-
- parent::__construct( array(
- 'title' => 'string',
- 'link' => 'string',
- 'description' => 'string',
- 'pubDate' => 'string'
- )
- );
-
- if( is_a( $copy, get_class( $this ) ) ) {
- $this->fromCopy( $copy );
- } else if( is_array( $copy ) ) {
- $this->fromArray( $copy );
- } else if( $copy ) {
- $this->fromXML( $copy );
- }
- }
-
- /**
- * Append this item to a channel
- * @param DOMElement The channel to append to
- * @param DOMDocument The document that contains the channel
- */
- public function appendTo( $channel, $doc ) {
-
- $item = $doc->createElement('item');
-
- foreach( $this->data as $key => $value ) {
-
- $tag = $doc->createElement( $key, $value );
- $item->appendChild( $tag );
- }
-
- $channel->appendChild( $item );
- }
-}
-
-/**
- * A single channel in an atomizer feed
- */
-class AtomizerChannel extends AtomizerStruct {
-
- public
- $items = array();
-
- public function __construct( $copy = NULL ) {
- parent::__construct( array(
- 'title' => 'string',
- 'link' => 'string',
- 'description' => 'string',
- 'language' => 'string',
- 'lastBuildDate' => 'string'
- )
- );
-
- if( is_array( $copy ) ) {
- $this->fromArray( $copy );
- } else if( $copy ) {
- $this->fromXML( $copy );
- }
- }
-
- /**
- * Add an item to this channel
- * @param AtomizerItem The item to add
- */
- public function addItem( $item ) {
- $this->items[] = $item;
- }
-
- /**
- * Append this channel to a feed
- * @param DOMElement The feed to append to
- * @param DOMDocument The document that contains the feed
- */
- public function appendTo( $feed, $doc ) {
-
- $channel = $doc->createElement('channel');
-
- foreach( $this->data as $key => $value ) {
-
- $tag = $doc->createElement( $key, $value );
- $channel->appendChild( $tag );
- }
-
- foreach( $this->items as $item ) {
- $item->appendTo( $channel, $doc );
- }
-
- $feed->appendChild( $channel );
- }
-
- /**
- * Overload default {@see AtomizerStruct::fromArray} function to support
- * inclusion of items
- *
- * @param Array The array to create this feed from
- */
- public function fromArray( $arr ) {
-
- parent::fromArray( $arr );
-
- if( $arr && $arr['items']) {
- foreach( $arr['items'] as $item ) {
- $this->addItem( new AtomizerItem( $item ) );
- }
- }
- }
-
- /**
- * Overload default {@see AtomizerStruct::fromXML} function to support
- * inclusion of items
- *
- * @param DOMElement The XML data to create this feed from
- */
- public function fromXML( $xml ) {
-
- parent::fromXML( $xml );
-
- $items = $xml->getElementsByTagName('item');
-
- foreach( $items as $node ) {
- $this->addItem( new AtomizerItem( $node ) );
- }
- }
-}
-
-/**
- * An Atomizer feed
+ * Provide Atomizer functions to the world!
*/
-class AtomizerFeed {
-
- public
- $channels = array();
-
- /**
- * Add a channel to this feed
- * @param AtomizerChannel The channel to add
- */
- public function addChannel( $channel ) {
- $this->channels[] = $channel;
- }
-
- /**
- * Add an item to this feed.
- * @param AtomizerItem The item to add
- * @param number (optional) The index of the channel to attach the item to;
- * if no channel is specified, item will be added to all channels.
- */
- public function addItem( $item, $index = -1 ) {
-
- if( array_key_exists( $index, $this->channels ) ) {
- $this->channels[ $index ]->addItem( $item );
- } else {
- foreach( $this->channels as &$channel ) {
- $channel->addItem( $item );
- }
- }
- }
-
- /**
- * Append this feed to a document
- * @param DOMDocument The document to attach the feed to
- */
- public function appendTo( $doc ) {
-
- $rss = $doc->createElement('rss');
- $rss->setAttribute('version','2.0');
+class Atomizer {
- foreach( $this->channels as $channel ) {
+ protected $ci;
- $channel->appendTo( $rss, $doc );
- }
+ public function __construct() {
- $doc->appendChild( $rss );
+ $this->ci = &get_instance();
+ $this->ci->load->model('Atomizer_feed');
}
-}
-
-/**
- * Provide Atomizer functions to the world!
- */
-class Atomizer {
/**
* Create a new feed
+ * @deprecated
* @param array (optional) Information (title,link,description,etc.) to use
* that describe the first channel
* @param array (optional) Items (if any) to append to the first channel
@@ -308,54 +23,43 @@ class Atomizer {
*/
public function create( $info = NULL, $entries = NULL ) {
- $feed = new AtomizerFeed();
-
- if( $info ) {
-
- $info['items'] = $entries;
- }
-
- $channel = new AtomizerChannel( $info );
+ return $this->ci->Atomizer_feed->create( $info, $entries );
+ }
- $feed->addChannel( $channel );
+ /**
+ * Grab a feed over HTTP
+ *
+ * @deprecated
+ * @param string The feed address (e.g., http://mysite.com/feed.rss)
+ * @return AtomizerFeed
+ */
+ public function loadURL( $url ) {
- return $feed;
+ return $this->ci->Atomizer_feed->loadURL( $url );
}
/**
* Read a feed from XML Data
+ *
+ * @deprecated
* @param string The XML content (i.e., RSS feed) to parse
* @return AtomizerFeed
*/
public function load( $content ) {
- $doc = new DOMDocument();
- $doc->loadXML( $content );
-
- $channels = $doc->getElementsByTagName( 'channel' );
- $result = new AtomizerFeed();
-
- foreach( $channels as $node ) {
- $channel = new AtomizerChannel();
- $channel->fromXML( $node );
- $result->addChannel( $channel );
- }
-
- return $result;
+ return $this->ci->Atomizer_feed->load( $content );
}
/**
* Convert an {@see AtomizerFeed} to XML
+ *
+ * @deprecated
* @param AtomizerFeed The feed object to convert to XML
* @return string
*/
public function save( $feed ) {
- $doc = new DOMDocument('1.0', 'iso-8859-1');
-
- $feed->appendTo( $doc );
-
- return $doc->saveXML();
+ return $this->ci->Atomizer_feed->save( $feed );
}
}
View
57 models/atomizer_feed.php
@@ -0,0 +1,57 @@
+<?php
+
+class Atomizer_feed extends CI_Model {
+
+ public function __construct() {
+
+ require_once('classes/AtomizerFeed.php');
+ }
+
+ /**
+ * Create a new feed
+ * @param array (optional) Information (title,link,description,etc.) to use
+ * that describe the first channel
+ * @param array (optional) Items (if any) to append to the first channel
+ * @return AtomizerFeed
+ */
+ public function create( $info = NULL, $entries = NULL ) {
+
+ $feed = new AtomizerFeed( $info, $entries );
+ }
+
+ /**
+ * Grab a feed over HTTP
+ * @param string The feed address (e.g., http://mysite.com/feed.rss)
+ * @return AtomizerFeed
+ */
+ public function loadURL( $url ) {
+
+ $feed = new AtomizerFeed();
+ $feed->loadUrl( $url );
+
+ return $feed;
+ }
+
+ /**
+ * Read a feed from XML Data
+ * @param string The XML content (i.e., RSS feed) to parse
+ * @return AtomizerFeed
+ */
+ public function load( $content ) {
+
+ $feed = new AtomizerFeed();
+ $feed->load( $url );
+
+ return $feed;
+ }
+
+ /**
+ * Convert an {@see AtomizerFeed} to XML
+ * @param AtomizerFeed The feed object to convert to XML
+ * @return string
+ */
+ public function save( $feed ) {
+
+ return $feed->save();
+ }
+}
View
120 models/classes/AtomizerChannel.php
@@ -0,0 +1,120 @@
+<?php if (! defined('BASEPATH')) exit('No direct script access');
+
+require_once('AtomizerStruct.php');
+require_once('AtomizerItem.php');
+
+/**
+ * A single channel in an atomizer feed
+ */
+class AtomizerChannel extends AtomizerStruct {
+
+ public
+ $items = array();
+
+ public function __construct( $copy = NULL ) {
+ parent::__construct( array(
+ 'title' => 'string',
+ 'link' => 'string',
+ 'description' => 'string',
+ 'language' => 'string',
+ 'lastBuildDate' => 'string'
+ )
+ );
+
+ if( is_array( $copy ) ) {
+ $this->fromArray( $copy );
+ } else if( $copy ) {
+ $this->fromXML( $copy );
+ }
+ }
+
+ /**
+ * Add an item to this channel
+ * @param AtomizerItem The item to add
+ */
+ public function addItem( $item ) {
+ $this->items[] = $item;
+ }
+
+ /**
+ * Append this channel to a feed
+ * @param DOMElement The feed to append to
+ * @param DOMDocument The document that contains the feed
+ */
+ public function appendTo( $feed, $doc ) {
+
+ $channel = $doc->createElement('channel');
+
+ foreach( $this->data as $key => $value ) {
+
+ $tag = $doc->createElement( $key, $value );
+ $channel->appendChild( $tag );
+ }
+
+ foreach( $this->items as $item ) {
+ $item->appendTo( $channel, $doc );
+ }
+
+ $feed->appendChild( $channel );
+ }
+
+ /**
+ * Overload default {@see AtomizerStruct::fromArray} function to support
+ * inclusion of items
+ *
+ * @param Array The array to create this feed from
+ */
+ public function fromArray( $arr ) {
+
+ parent::fromArray( $arr );
+
+ if( $arr && $arr['items']) {
+ foreach( $arr['items'] as $item ) {
+ $this->addItem( new AtomizerItem( $item ) );
+ }
+ }
+ }
+
+ /**
+ * Overload default {@see AtomizerStruct::fromXML} function to support
+ * inclusion of items
+ *
+ * @param DOMElement The XML data to create this feed from
+ */
+ public function fromXML( $xml ) {
+
+ parent::fromXML( $xml );
+
+ $items = $xml->getElementsByTagName('item');
+
+ foreach( $items as $node ) {
+ $this->addItem( new AtomizerItem( $node ) );
+ }
+ }
+
+ /**
+ * Sort this channel's items
+ */
+ public function sort() {
+ usort( $this->items, array( $this, 'compare_item' ) );
+ }
+
+ /**
+ * Compare two items by date for sorting
+ *
+ * @param AtomizerItem the first item to compare
+ * @param AtomizerItem the second item to compare
+ */
+ protected function compare_item( $a, $b ) {
+
+ $adate = strtotime($a->pubDate);
+ $bdate = strtotime($b->pubDate);
+
+ if( $adate === $bdate ) {
+
+ return 0;
+ }
+
+ return ($adate < $bdate) ? 1 : -1;
+ }
+}
View
152 models/classes/AtomizerFeed.php
@@ -0,0 +1,152 @@
+<?php if (! defined('BASEPATH')) exit('No direct script access');
+
+require_once('AtomizerChannel.php');
+require_once('AtomizerItem.php');
+
+
+/**
+ * An Atomizer feed
+ */
+class AtomizerFeed {
+
+ public
+ /**
+ * @type array.AtomizerChannel
+ */
+ $channels = array();
+
+ /**
+ * Initialize a feed
+ * @param array (optional) Information (title,link,description,etc.) to use
+ * that describe the first channel
+ * @param array (optional) Items (if any) to append to the first channel
+ * @return AtomizerFeed
+ */
+ public function __construct( $info = NULL, $entries = NULL ) {
+
+ if( !$info ) {
+ $info = array();
+ }
+
+ $info['items'] = $entries;
+
+ $channel = new AtomizerChannel( $info );
+ $channel->sort();
+
+ $this->addChannel( $channel );
+
+ return $this;
+ }
+
+ /**
+ * Add a channel to this feed
+ * @param AtomizerChannel The channel to add
+ */
+ public function addChannel( $channel ) {
+
+ $this->channels[] = $channel;
+ }
+
+ /**
+ * Add an item to this feed.
+ * @param AtomizerItem The item to add
+ * @param number (optional) The index of the channel to attach the item to;
+ * if no channel is specified, item will be added to all channels.
+ */
+ public function addItem( $item, $index = -1 ) {
+
+ if( array_key_exists( $index, $this->channels ) ) {
+ $this->channels[ $index ]->addItem( $item );
+ } else {
+ foreach( $this->channels as &$channel ) {
+ $channel->addItem( $item );
+ }
+ }
+ }
+
+ /**
+ * Append this feed to a DOM document (e.g. for {@link AtomizerFeed::save})
+ * @param DOMDocument The document to attach the feed to
+ */
+ public function appendTo( $doc ) {
+
+ $rss = $doc->createElement('rss');
+ $rss->setAttribute('version','2.0');
+
+ foreach( $this->channels as $channel ) {
+
+ $channel->appendTo( $rss, $doc );
+ }
+
+ $doc->appendChild( $rss );
+ }
+
+ /**
+ * Convolve this and another feed to create a whacky combination of the two
+ */
+ public function convolve( $feed, $info = NULL ) {
+
+ $items = array();
+
+ foreach( $feed->channels as $channel ) {
+ $items = array_merge( $items, $channel->items );
+ }
+
+ foreach( $this->channels as $channel ) {
+ $items = array_merge( $items, $channel->items );
+ }
+
+ return new AtomizerFeed( $info, $items );
+ }
+
+ /**
+ * Grab a feed over HTTP
+ * @param string The feed address (e.g., http://mysite.com/feed.rss)
+ * @return AtomizerFeed
+ */
+ public function loadURL( $url ) {
+
+ $content = file_get_contents( $url);
+
+ if( !$content ) {
+ return NULL;
+ }
+
+ return $this->load( $content );
+ }
+
+ /**
+ * Read a feed from XML Data
+ * @param string The XML content (i.e., RSS feed) to parse
+ * @return AtomizerFeed
+ */
+ public function load( $content ) {
+
+ $doc = new DOMDocument();
+ $doc->loadXML( $content );
+
+ $channels = $doc->getElementsByTagName( 'channel' );
+
+ foreach( $channels as $node ) {
+ $channel = new AtomizerChannel();
+ $channel->fromXML( $node );
+ $this->addChannel( $channel );
+ }
+
+ return $this;
+ }
+
+ /**
+ * Convert an {@see AtomizerFeed} to XML
+ * @param AtomizerFeed The feed object to convert to XML
+ * @return string
+ */
+ public function save() {
+
+ $doc = new DOMDocument('1.0', 'iso-8859-1');
+
+ $this->appendTo( $doc );
+
+ return $doc->saveXML();
+ }
+}
View
46 models/classes/AtomizerItem.php
@@ -0,0 +1,46 @@
+<?php if (! defined('BASEPATH')) exit('No direct script access');
+
+require_once( 'AtomizerStruct.php' );
+
+/**
+ * A single item in an atomizer feed
+ */
+class AtomizerItem extends AtomizerStruct {
+
+ public function __construct( $copy = NULL ) {
+
+ parent::__construct( array(
+ 'title' => 'string',
+ 'link' => 'string',
+ 'description' => 'string',
+ 'pubDate' => 'string'
+ )
+ );
+
+ if( is_a( $copy, get_class( $this ) ) ) {
+ $this->fromCopy( $copy );
+ } else if( is_array( $copy ) ) {
+ $this->fromArray( $copy );
+ } else if( $copy ) {
+ $this->fromXML( $copy );
+ }
+ }
+
+ /**
+ * Append this item to a channel
+ * @param DOMElement The channel to append to
+ * @param DOMDocument The document that contains the channel
+ */
+ public function appendTo( $channel, $doc ) {
+
+ $item = $doc->createElement('item');
+
+ foreach( $this->data as $key => $value ) {
+
+ $tag = $doc->createElement( $key, $value );
+ $item->appendChild( $tag );
+ }
+
+ $channel->appendChild( $item );
+ }
+}
View
111 models/classes/AtomizerStruct.php
@@ -0,0 +1,111 @@
+<?php if (! defined('BASEPATH')) exit('No direct script access');
+
+/**
+ * A structure for storing and converting between array, object, and XML data.
+ */
+class AtomizerStruct {
+
+ protected
+ $data,
+ $strict,
+ $template;
+
+ /**
+ * Create a new object based on the specified template
+ * @param array the template to validate this object's data against
+ * @param boolean whether or not the template's data types must be matched
+ */
+ public function __construct( $template, $strict = false ) {
+ $this->template = $template;
+ foreach( $this->template as $key => $value ) {
+ $this->data[ $key ] = NULL;
+ }
+
+ $this->strict = $strict;
+ }
+
+ /**
+ * Getter: retrieve the value of the specified key
+ * @param string Key
+ * @return mixed Value
+ */
+ public function __get( $key ) {
+ if( array_key_exists( $key, $this->template ) ) {
+ return $this->data[ $key ];
+ }
+ }
+
+ /**
+ * Setter: set the value specified by a key
+ * @param string Key
+ * @param mixed Value
+ */
+ public function __set( $key, $value ) {
+ if( array_key_exists( $key, $this->template ) ) {
+
+ $validator = 'is_' . $this->template[ $key ];
+
+ if( !$this->strict || $validator( $value ) ) {
+ $this->data[ $key ] = $value;
+ } else {
+ throw new Exception( 'Data types do not match in ' . get_class( $this ) . '::' . $key );
+ }
+ }
+ }
+
+ /**
+ * Find the first child of a DOMElement that matches the specfied tag
+ * @param DOMElement Parent element
+ * @param string The tag name (e.g., "item") to match
+ * @return DOMElement|NULL
+ */
+ protected function findChild( $node, $tagName ) {
+ $tags = $node->getElementsByTagName( $tagName );
+ $i = 0;
+ while( $tag = $tags->item($i++) ) {
+ if( $tag->parentNode === $node && $tag->hasChildNodes() ) {
+
+ return $tag;
+ }
+ }
+ return NULL;
+ }
+
+ /**
+ * Clone object
+ * @param object The object to clone
+ */
+ public function fromCopy( $copy ) {
+ foreach( $this->template as $key => $type ) {
+ $this->data[$key] = $copy->$key;
+ }
+ }
+
+ /**
+ * Construct object from array data
+ * @param array The array to use to set parameters
+ */
+ public function fromArray( $data ) {
+
+ foreach( $this->template as $key => $type ) {
+ if( array_key_exists( $key, $data ) ) {
+ $this->$key = $data[ $key ];
+ }
+ }
+ }
+
+ /**
+ * Construct object from XML template
+ * @param DOMElement The parent element containing parameters
+ */
+ public function fromXML( $xml ) {
+
+ foreach( $this->template as $key => $type ) {
+
+ $el = $this->findChild( $xml, $key );
+ if( $el ) {
+ $this->$key = $el->firstChild->textContent;
+ }
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.