F. Factory Classes

In this MVC tutorial we covered a series of classes including: Post_Search, Post_Collection, Post_Model, Post_Handler, Post_Block_List and Post_Block_Form. The last problem we created is about the learning curve. In the spirit of Eden, we want to create a Simple Interface for the end developer to interact with.

In the module folder, create a file called post.php and paste Figure 1.

Figure 1. Factory Class
<?php //-->
class Post extends Eden_Class {
	/* Constants
	-------------------------------*/
	/* Public Properties
	-------------------------------*/
	/* Protected Properties
	-------------------------------*/
	/* Private Properties
	-------------------------------*/
	/* Magic
	-------------------------------*/
	/* Public Methods
	-------------------------------*/
	/**
	 * Returns form block
	 *
	 * @param mixed
	 * @param string
	 * @return Post_Block_Form
	 */
	public function form() {
		return $this->Post_Block_Form();
	}
	
	/**
	 * Returns list block
	 *
	 * @param mixed
	 * @param string
	 * @return Post_Block_List
	 */
	public function table(Post_Collection $collection, array $columns = array()) {
		$block = $this->Post_Block_List($collection);
		if(!empty($columns)) {
			$block->setColumns($columns);
		}
		
		return $block;
	}
	
	/**
	 * Returns a post model
	 *
	 * @param mixed
	 * @param string
	 * @return Post_Model
	 */
	public function model($value = NULL, $key = 'post_id') {
		$model = $this->Post_Model();
		if($value) {
			$model->load($value, $key);
		}
		
		return $model;
	}
	
	/**
	 * Returns the search class
	 *
	 * @return Post_Search
	 */
	public function search() {
		return $this->_database
			->search()
			->setTable('post')
			->setModel('Eve_Post_Model');
	}
	
	/* Protected Methods
	-------------------------------*/
	/* Private Methods
	-------------------------------*/
}

The reason why we did this is because instead of the end developer needing to know about all the class names they only need to know the methods to get to those class names. With this class, things like Figure 2 are now possible.

Figure 2. Calling Classes by Factory
echo front()->Post()->form(); 	//renders the form block

front()							//the cool chain
	->Post()
	->search()
	->filterByPostActive(1)
    ->filterByUserName('Chris')
    ->sortByUserId('DESC')
    ->setStart(0)
    ->setRange(25)
    ->getCollection()
    ->setUserByEmail('sample@email.com')
    ->save()
    ->formatTime('post_created', 'F d, Y g:ia')
    ->debug(true)
    ->get();

An there you have an MVC framework by Openovate Labs. By design it's not as extensive as our Code Igniter, Symfony, Zend Framework neighbors. It's mainly because we value simplistic design. Enjoy!


© 2012 Openovate Labs. All rights reserved.