Archived
1
0
Fork 0

Re-written models abit

This commit is contained in:
Fredric N 2010-10-02 15:35:34 +02:00
parent f0ca8ccdec
commit f21f1599bc
6 changed files with 90 additions and 125 deletions

View file

@ -2,17 +2,16 @@
class BlogPost extends Fiktiv_Model_Abstract
{
protected $_id;
protected $_title;
protected $_content;
protected $_author;
protected $_pubDate;
protected $_isPublished = false;
public function __construct(array $data = array())
{
$this->setAttribs($data);
}
protected $_data = array();
protected $_default = array(
'id' => 0,
'title' => null,
'content' => null,
'pubDate' => null,
'author' => null,
'isPublished' => false
);
public function setAttribs(array $data) {
@ -35,83 +34,55 @@ class BlogPost extends Fiktiv_Model_Abstract
}
}
public function getId()
{
return $this->_id;
}
public function setId($id)
{
if (is_numeric($id))
$this->_id = $id;
}
public function getTitle()
{
return $this->_title;
$this->_data['id'] = $id;
}
public function setTitle($title)
{
if (is_string($title) && null == $title[30]) {
$this->_title = $title;
$this->_data['title'] = $title;
return true;
}
return false;
}
public function getContent()
{
return $this->_content;
}
public function setContent($content)
{
if (is_string($content)) {
$this->_content = $content;
$this->_data['content'] = $content;
return true;
}
return false;
}
public function getAuthor()
{
return $this->_author;
}
public function setAuthor($author)
{
if ($author instanceof User) {
$this->_author = $author;
$this->_data['author'] = $author;
return true;
}
return false;
}
public function getPubDate()
{
return $this->_pubDate;
}
public function setPubDate($date)
{
if (Fiktiv_Date::isDate($date)) {
$this->_pubDate = new Fiktiv_Date($date);
$this->_data['pubDate'] = new Fiktiv_Date($date);
} else if ($date instanceof Zend_Date) {
$this->_pubDate = $date;
$this->_data['pubDate'] = $date;
} else {
@ -124,9 +95,9 @@ class BlogPost extends Fiktiv_Model_Abstract
public function isPublished($flag = null)
{
if (null === $flag) {
return $this->_isPublished;
return $this->_data['isPublished'];
}
$this->_isPublished = (boolean)$flag;
$this->_data['isPublished'] = (boolean)$flag;
}
}

View file

@ -65,7 +65,10 @@ class Mapper_BlogPost extends Fiktiv_Model_Mapper_DbTableAbstract
public function findAll($limit = null)
{
$rows = $this->_dbTable->fetchAll(null, 'pubDate DESC');
if (!is_numeric($limit))
throw new Fiktiv_Exception('limit must be numeric');
$rows = $this->_dbTable->fetchAll(null, 'pubDate DESC', $limit);
$posts = array();

View file

@ -5,55 +5,21 @@
*/
class User extends Fiktiv_Model_Abstract
{
protected $_id = 0;
protected $_email;
protected $_firstName;
protected $_lastName;
protected $_regDate;
protected $_isDeleted = false;
protected $_data = array();
public function __construct($data = array())
{
$data = (array)$data;
$this->setAttribs($data);
}
/**
* Quick way to set user data
* @param array $data
*/
public function setAttribs(array $data)
{
foreach ($data as $key => $value) {
switch(strtolower($key)) {
case 'email':
$this->setEmail($value);
break;
case 'firstname':
$this->setFirstName($value);
break;
case 'lastname':
$this->setLastName($value);
break;
case 'regdate':
$this->setRegDate(new Zend_Date($value));
break;
}
}
}
public function getId()
{
return $this->_id;
}
protected $_default = array(
'id' => 0,
'email' => null,
'firstName' => null,
'lastName' => null,
'regDate' => null,
'isDeleted' => false
);
public function setId($id)
{
if (is_numeric($id))
$this->_id = $id;
$this->_data['id'] = $id;
}
/**
@ -66,7 +32,7 @@ class User extends Fiktiv_Model_Abstract
$validator = new Zend_Validate_EmailAddress();
if ($validator->isValid($email)) {
$this->_email = $email;
$this->_data['email'] = $email;
return true;
}
@ -83,7 +49,7 @@ class User extends Fiktiv_Model_Abstract
public function setLastName($name)
{
if (is_string($name) && preg_match('/^[A-ö\ \.\-]{0,20}$/', $name)) {
$this->_lastName = $name;
$this->_data['lastName'] = $name;
return true;
}
@ -101,7 +67,7 @@ class User extends Fiktiv_Model_Abstract
$name = trim($name);
if (is_string($name) && preg_match('/^[A-ö]{2,8}\-?[A-ö]{2,8}$/', $name)) {
$this->_firstName = $name;
$this->_data['firstName'] = $name;
return true;
}
@ -119,10 +85,10 @@ class User extends Fiktiv_Model_Abstract
if (is_string($date) && preg_match('/^(?:(?:19[0-9]{2})|(?:20[0-9]{2}))\-' .
'(?:(?:0[1-9])|(?:1[012]))\-(?:(?:0[1-9])|(?:[12][0-9])|(?:3[01]))$/', $date)) {
$this->_regDate = $date;
$this->_data['regDate'] = $date;
} else if ($date instanceof Zend_Date) {
$this->_regDate = $date->toString('yyyy-MM-dd');
$this->_data['regDate'] = $date->toString('yyyy-MM-dd');
} else {
return false;
@ -156,7 +122,7 @@ class User extends Fiktiv_Model_Abstract
*/
public function isActive()
{
return !$this->_isDeleted;
return !$this->_data['isDeleted'];
}
/**
@ -165,7 +131,7 @@ class User extends Fiktiv_Model_Abstract
*/
public function __toString()
{
return '(' . __CLASS__ . '){' . $this->_email . ', ' . $this->_firstName . ' ' . $this->_lastName . '}';
return '(' . __CLASS__ . '){' . $this->_data['email'] . ', ' . $this->_data['firstName'] . ' ' . $this->_data['lastName'] . '}';
}
/**
@ -173,14 +139,7 @@ class User extends Fiktiv_Model_Abstract
*/
public function toArray()
{
return array(
'id' => $this->_id,
'email' => $this->_email,
'firstName' => $this->_firstName,
'lastName' => $this->_lastName,
'isDeleted' => $this->_isDeleted,
'regDate' => $this->_regDate
);
return $this->_data;
}
/**

View file

@ -9,7 +9,8 @@ class Blog_IndexController extends Fiktiv_Controller_Action
public function latestAction()
{
$this->view->posts = $this->dataService->BlogPost->findAll();
// Fetch last ten posts
$this->view->posts = $this->dataService->BlogPost->findAll(10);
}
public function readableAction()

View file

@ -1,18 +1,16 @@
<h1 class="leap"><?=$this->translate('u:readable') ?></h1>
<p>Lite text</p>
<p><?=$this->translate('u:BLOG_READABLE_TXT') ?></p>
<p>
<ul class="custom">
<li>
<div class="item">
<h3>Chris Shiflett</h3>
<p>php guru and security expert. Blogs about ... </p>
</div>
<li class="item">
<h3><a href="http://shiflett.org" target="_blank">Chris Shiflett</a></h3>
<p><?=$this->translate('BLOG_READABLE_SHIFLETT') ?></p>
</li>
<li>
<h3>Chris Shiflett</h3>
<p>php guru and security expert. Blogs about ... </p>
<li class="item">
<h3><a href="http://net.tutsplus.com" target="_blank">NetTuts Plus</a></h3>
<p><?=$this->translate('BLOG_READABLE_NETTUTS') ?></p>
</li>
</ul>
</p>

View file

@ -3,24 +3,57 @@
abstract class Fiktiv_Model_Abstract
{
public function __call($name, $args) {
protected $_default = array();
public function __construct($data = array())
{
$data = (array)$data;
$data = array_merge($this->_default, array_intersect_key($data, $this->_default));
$this->setAttribs($data);
}
public function __call($name, $args)
{
// getX methods
if ('get' == substr($name,0,3)) {
$property = substr($name, 3);
$dataPart = '_' . strtolower($property[0]) . substr($property, 1);
return isset($this->$dataPart) ? $this->$dataPart : false;
$property[0] = strtolower($property[0]);
if (isset($this->_data[$property]))
return $this->_data[$property];
}
}
public function __get($name) {
public function __set($name, $value)
{
$methodName = 'set' . ucfirst($name);
if (method_exists($this, $methodName)) {
return $this->$methodName($value);
}
$name = '_'.$name;
if (isset($this->$name))
return $this->$name;
if (array_key_exists($name, $this->_data))
return $this->_data[$name] = $value;
}
// Direct access to read data fields
public function __get($name)
{
if (isset($this->_data[$name])) {
return $this->_data[$name];
}
}
abstract public function setAttribs(array $data);
public function setAttribs(array $data)
{
foreach ($data as $key => $value) {
$this->$key = $value;
}
}
}