Skip to content
Browse files

Created TableGateway for Peep table

- Created a table gateway with a few convenience methods, all operating on
  PeepEntities, or returning iterators containing them
  - fetchTimeline
  - fetchUserTimeline
  - fetchPeep
  - insertPeep
  • Loading branch information...
1 parent 22678b5 commit eccefedd96376b03fe892fc615cc721f730db13f @weierophinney committed May 31, 2012
Showing with 86 additions and 0 deletions.
  1. +86 −0 src/PhlyPeep/Model/PeepTable.php
View
86 src/PhlyPeep/Model/PeepTable.php
@@ -0,0 +1,86 @@
+<?php
+
+namespace PhlyPeep\Model;
+
+use Zend\Db\Adapter\Adapter;
+use Zend\Db\ResultSet\ResultSet;
+use Zend\Db\TableGateway\AbstractTableGateway;
+
+class PeepTable extends AbstractTableGateway
+{
+ protected $table = 'peep';
+ protected $tableName = 'peep';
+
+ public function __construct(Adapter $adapter, $tableName = 'peep')
+ {
+ $this->adapter = $adapter;
+ $this->table = $this->tableName = $tableName;
+ $this->peepPrototype = new PeepEntity;
+ $this->resultSetPrototype = new ResultSet;
+ $this->resultSetPrototype->setReturnType(ResultSet::TYPE_ARRAY);
+ $this->initialize();
+ }
+
+ public function fetchTimeline($offset = 0, $limit = 20)
+ {
+ $select = $this->getSql()->select();
+ $select->offset($offset)
+ ->limit($limit)
+ ->order('timestamp DESC');
+ return $this->getPeepsFromSelect($select);
+ }
+
+ public function fetchUserTimeline($user, $offset = 0, $limit = 20)
+ {
+ $select = $this->getSql()->select();
+
+ $where = $select->where();
+ $where->equalTo('username', $user);
+
+ $select->offset($offset)
+ ->limit($limit)
+ ->order('timestamp DESC');
+ return $this->getPeepsFromSelect($select);
+ }
+
+ public function fetchPeep($identifier)
+ {
+ $rowset = $this->select(array('identifier' => $identifier));
+ $row = $rowset->current();
+ if (!$row) {
+ return false;
+ }
+ $peep = $this->getPeepFromRow($row);
+ return $peep;
+ }
+
+ public function insertPeep(PeepEntity $peep)
+ {
+ $data = $peep->getArrayCopy();
+ if (false !== ($peep = $this->fetchPeep($data['identifier']))) {
+ throw new \DomainException(sprintf(
+ 'Unable to create peep; peep with that identifier ("%s") already exists',
+ $data['identifier']
+ ));
+ }
+ $this->insert($data);
+ }
+
+ protected function getPeepsFromSelect($select)
+ {
+ $resultset = $this->selectWith($select);
+ $peeps = new SplObjectStorage();
+ foreach ($resultSet as $row) {
+ $peep = $this->getPeepFromRow($row);
+ $peeps->attach($peep);
+ }
+ return $peeps;
+ }
+
+ protected function getPeepFromRow($row)
+ {
+ $peep = clone $this->peepPrototype;
+ $peep->exchangeArray($row);
+ return $peep;
+ }
+}

0 comments on commit eccefed

Please sign in to comment.
Something went wrong with that request. Please try again.