Skip to content

Commit

Permalink
Adding the different coding steps
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Matthews committed Oct 12, 2010
1 parent 1a2c290 commit e18b76e
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 0 deletions.
22 changes: 22 additions & 0 deletions step4/LinkPeer.php
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;
}
...
31 changes: 31 additions & 0 deletions step5/LinkPeer.php
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;
}

...
21 changes: 21 additions & 0 deletions step6/LinkPeer.php
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;
}

...
47 changes: 47 additions & 0 deletions step7/LinkPeer.php
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);
}

...
85 changes: 85 additions & 0 deletions step8/Link.php
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;
}
}
30 changes: 30 additions & 0 deletions step8/LinkPeer.php
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;
}

...

0 comments on commit e18b76e

Please sign in to comment.