Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bu committed Dec 22, 2010
0 parents commit 590cca2
Show file tree
Hide file tree
Showing 9 changed files with 518 additions and 0 deletions.
85 changes: 85 additions & 0 deletions DataObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace haFeed;

class DataObject implements \SeekableIterator
{
protected $items = array();
protected $item_id = 0;

public function dump()
{
return $this->items;
}

public function addItemArray($array)
{
$this->addItem($array['title'], $array['pubDate'], $array['desc'], $array['link']);

return true;
}

public function addItem($title, $pubDate, $desc, $link = '')
{
$item_id = $this->item_id;

$this->items[$item_id] = array(
'title' => $title,
'pubDate' => $pubDate,
'desc' => $desc,
'link' => $link
);

$this->item_id++;

return $item_id;
}

public function removeItem($item_id)
{
unset($this->items[$item_id]);

return true;
}

#
# 實作 FOR SeekableIterator
#
protected $current_seek_position = 0;

public function current()
{
return $this->items[$this->current_seek_position];
}

public function key()
{
return $this->current_seek_position;
}

public function next()
{
++$this->current_seek_position;
}

public function rewind()
{
$this->current_seek_position = 0;
}

public function valid()
{
return isset($this->items[$this->current_seek_position]);
}

public function seek($position)
{
$this->current_seek_position = $position;

if(!$this->valid())
{
throw new OutOfBoundsException("invalid seek position ($position)");
}
}
}

104 changes: 104 additions & 0 deletions Exchange.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace haFeed;

require_once 'DataObject.php';
require_once 'Sorter.php';

class Exchanger
{
const RENDER_ONLY = 1;
const DISPLAY_CONTENT = 0;

protected $importer = array();
protected $export_obj = null;
protected $sorter_obj = null;

public function addImporter(Import\Importer $import_obj)
{
$this->importer[$import_obj->getName()] = $import_obj;

return true;
}

public function removeImporter(Import\Importer $import_obj)
{
unset($this->importer[$import_obj->getName()]);

return true;
}

public function setExporter(Export\Exporter $export_obj)
{
$this->exporter = $export_obj;

return true;
}

public function checkIfRequirementWereFit()
{
if(sizeof($this->importer) == 0)
{
throw new \Exception('haFeed->Exchange: No importer');
}

if(is_null($this->exporter))
{
throw new \Exception('haFeed->Exchange: No exporter');
}

if(is_null($this->sorter_obj))
{
throw new \Exception('haFeed->Exchange: No sorter');
}

return true;
}

public function process($output_control = DISPLAY_CONTENT)
{
# 檢查是否相依的變數都已準備好
$this->checkIfRequirementWereFit();

$data_collection = array();

foreach($this->importer as $importer)
{
$data = $importer->import();

$data_collection[] = $data;
}

$new_data_set = new DataObject;

foreach($data_collection as $dataset)
{
foreach($dataset->dump() as $data_row)
{
$new_data_set->addItemArray($data_row);
}
}

$final_data = $this->sorter_obj->sort($new_data_set);

if($output_control == RENDER_ONLY)
{
$result = $this->exporter->render($final_data);

return $result;
}
else
{
$this->exporter->export($final_data);

return true;
}
}

public function setSorter(Sorter $sorter_object)
{
$this->sorter_obj = $sorter_object;

return true;
}
}
78 changes: 78 additions & 0 deletions Sorter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace haFeed;

abstract class Sorter
{
abstract public function compare($a, $b);

protected function exportArray(DataObject $dataset)
{
$dataArray = $dataset->dump();

return $dataArray;
}

protected function buildDataObject($data_array)
{
$dataObject = new DataObject;

foreach($data_array as $data)
{
$dataObject->addItemArray($data);
}

return $dataObject;
}

public function sort(DataObject $data_collection)
{
$data = $this->exportArray($data_collection);

usort($data, array($this, "compare"));

$sorted_data_object = $this->buildDataObject($data);

return $sorted_data_object;
}


}

class DummySorter extends Sorter
{
public function compare($a, $b)
{
return 0;
}

}

class TimeSorter extends Sorter
{
public function compare($a, $b)
{
if($a['pubDate'] == $b['pubDate'])
{
return 0;
}

return ($a['pubDate'] < $b['pubDate']) ? -1 : 1;
}
}

class TitleSorter extends Sorter
{
public function compare($a, $b)
{
return strcmp($a['title'],$b['title']);
}
}

class LinkSorter extends Sorter
{
public function compare($a, $b)
{
return strcmp($a['link'],$b['link']);
}
}
15 changes: 15 additions & 0 deletions export/ArrayExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace haFeed\Export;

use haFeed\DataObject as DataObject;

require_once 'Exporter.php';

class ArrayExporter extends Exporter
{
public function export(DataObject $data)
{
return $data->dump();
}
}
10 changes: 10 additions & 0 deletions export/Exporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace haFeed\Export;

use haFeed\DataObject as DataObject;

abstract class Exporter
{
abstract public function export(DataObject $data);
}
39 changes: 39 additions & 0 deletions export/RSS_1_0.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace haFeed\Export;

use haFeed\DataObject as DataObject;

require_once 'Exporter.php';

class RSS_1_0_Exporter extends Exporter
{
public function export(DataObject $data)
{
header("Content-type: application/rdf+xml; charset=UTF-8");
}

public function render(DataObject $data)
{
$rss_source = '<?xml version="1.0"?><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/">' . "\n"; # RDF RSS Header

$rss_source .= '<channel rdf:about="http://hafeed.hax4.in/"><title>haFeed Generated</title><items><rdf:Seq>';

foreach($data->dump() as $data)
{
$rss_source .= '<rdf:li resource="'.$data['link'].'" />';
}

$rss_source .= '</rdf:Seq></items><link>http://hafeed.hax4.in/</link><description>This feed were created by haFeed.</description></channel>' . "\n"; # RDF RSS Channel Description

foreach($data->dump() as $data)
{
$rss_source .= '<item rdf:about="'.$data['link'].'"><title>'.$data['title'].'</title><link>'.$data['link'].'</link><description><[CDATA['.$data['desc'].']]></description></item>' . "\n"; # RDF RSS Item

}

$rss_source .= '</rdf:RDF>'; # RSS RDF END

return $rss_source;
}
}
Loading

0 comments on commit 590cca2

Please sign in to comment.