-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Paul Matthews
committed
Oct 12, 2010
1 parent
1a2c290
commit e18b76e
Showing
6 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
... | ||
private function getCollection() | ||
{ | ||
// if the collection is not already cached | ||
if (empty($this->collection)) { | ||
|
||
// selects the collection if it exists | ||
$this->collection = $this->db->selectCollection(self::COLLECTION); | ||
|
||
// or creates a new one if it doesn't | ||
if (empty($this->collection)) { | ||
$this->collection = $this->db->createCollection( | ||
self::COLLECTION | ||
); | ||
} | ||
} | ||
|
||
return $this->collection; | ||
} | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
... | ||
|
||
public function getIdFromDataset(array $data) | ||
{ | ||
if (!isset($data[self::DB_ID_FIELD])) { | ||
return false; | ||
} | ||
return $data[self::DB_ID_FIELD]; | ||
} | ||
|
||
public function translateDataset(array $data, $toDb = true) | ||
{ | ||
$keyFrom = self::ID_FIELD; | ||
$keyTo = self::DB_ID_FIELD; | ||
if (!$toDb) { | ||
$keyFrom = self::DB_ID_FIELD; | ||
$keyTo = self::ID_FIELD; | ||
} | ||
|
||
if (!isset($data[$keyFrom])) { | ||
return false; | ||
} | ||
|
||
$data[$keyTo] = $data[$keyFrom]; | ||
unset($data[$keyFrom]); | ||
return $data; | ||
} | ||
|
||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
... | ||
|
||
public function insert(Link $link) | ||
{ | ||
return $this->update($link); | ||
} | ||
|
||
public function update(Link $link) | ||
{ | ||
$data = $this->translateDataset($link->toArray(), true); | ||
|
||
if ($data && $this->getCollection()->save($data)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
... | ||
|
||
public function fetchAll() | ||
{ | ||
$results = array(); | ||
|
||
// perform a find with a blank query | ||
foreach ($this->getCollection()->find() as $result) { | ||
// Create a Link out of each of the results | ||
$results[] = $this->factory($result); | ||
} | ||
|
||
return $results; | ||
} | ||
|
||
public function fetchByUrl($url) | ||
{ | ||
$query = array('url' => $url); | ||
$mongoQuery = $this->translateDataset($query); | ||
$response = $this->getCollection()->findOne( | ||
$mongoQuery | ||
); | ||
|
||
// if the database returns a result return the link | ||
if ($response) { | ||
return $this->factory($response); | ||
} | ||
|
||
// Otherwise return null | ||
return null; | ||
} | ||
|
||
private function factory(array $linkArray) | ||
{ | ||
if (!isset($linkArray['_id'])) { | ||
throw new Exception('Missing data'); | ||
} | ||
|
||
$linkArray = $this->translateDataset($linkArray, false); | ||
|
||
$tmp = new Link(); | ||
return $tmp->fromArray($linkArray); | ||
} | ||
|
||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
require_once(dirname(__FILE__) . '/LinkPeer.php'); | ||
|
||
class Link { | ||
private $url; | ||
private $tags = array(); | ||
|
||
public function __construct() | ||
{ | ||
// Not required to do anything at this time. | ||
} | ||
|
||
public function getUrl() | ||
{ | ||
return $this->url; | ||
} | ||
|
||
public function getTags() | ||
{ | ||
return $this->tags; | ||
} | ||
|
||
public function setUrl($url) | ||
{ | ||
$this->url = (string) $url; | ||
} | ||
|
||
public function addTag($tag) | ||
{ | ||
$this->tags[] = (string) $tag; | ||
} | ||
|
||
public function removeTag($tag) | ||
{ | ||
$success = false; | ||
// look through the current tags for the specified tag | ||
foreach ($this->tags as $key => $tagName) { | ||
if ($tagName == $tag) { | ||
// remove the tag from the list | ||
unset($this->tags[$key]); | ||
$success = true; | ||
} | ||
} | ||
return $success; | ||
} | ||
|
||
public function __toString() | ||
{ | ||
// example: http://www.google.com/ [usa, search] | ||
return sprintf('%s [%s]', $this->url, implode(', ', $this->tags)); | ||
} | ||
|
||
public function toArray() | ||
{ | ||
return array( | ||
// _id is the name of the url in this case as it makes it easier | ||
// for interoperability with the Database | ||
'url' => $this->url, | ||
'tags' => $this->tags, | ||
); | ||
} | ||
|
||
public function fromArray($link) | ||
{ | ||
// Ensure the url is set | ||
if (empty($link['url']) || !is_string($link['url'])) { | ||
throw new Exception('Incorrect data supplied'); | ||
} | ||
|
||
$this->url = $link['url']; | ||
|
||
// Only set tags if they conform to our structure | ||
$tags = array(); | ||
if (!empty($link['tags']) && is_array($link['tags'])) { | ||
foreach ($link['tags'] as $tag) { | ||
if (is_string($tag)) { | ||
$tags[] = $tag; | ||
} | ||
} | ||
} | ||
$this->tags = $tags; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
... | ||
|
||
public function delete(Link $link) | ||
{ | ||
$link = $this->translateDataset($link->toArray(), true); | ||
|
||
$linkKey = $this->getIdFromDataset($link); | ||
$key = array( | ||
'_id' => $linkKey, | ||
); | ||
|
||
if ($linkKey) { | ||
$options = array( | ||
'fsync' => true, // Forces the update to be synced to disk | ||
); | ||
try { | ||
if ($this->getCollection()->remove($key, $options)) { | ||
|
||
return true; | ||
} | ||
} catch (MongoException $e) { | ||
// @TODO log the error | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
... |