Created
April 6, 2011 18:15
-
-
Save zeelot/906199 to your computer and use it in GitHub Desktop.
Wrapper for ORM models to add view logic on top
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
<?php defined('SYSPATH') or die('No direct script access.'); | |
class View_Model | |
{ | |
public static function factory(Model $model) | |
{ | |
// Get the associated View_ name (ex: View_Model_User) | |
$class = 'View_'.get_class($model); | |
if (class_exists($class)) | |
{ | |
$reflection = new ReflectionClass($class); | |
$instance = $reflection->newInstance($model); | |
} | |
else | |
{ | |
$instance = new View_Model($model); | |
} | |
return $instance; | |
} | |
protected $_model; | |
public function __construct(Model $model) | |
{ | |
$this->_model = $model; | |
} | |
public function __get($key) | |
{ | |
return method_exists($this, $key) | |
? $this->{$key}() | |
: $this->_model->{$key}; | |
} | |
public function __call($method, $args) | |
{ | |
return call_user_func_array(array($this->_model, $method), $args); | |
} | |
public function __isset($column) | |
{ | |
return ($this->_model->__isset($column) OR | |
((property_exists($this, $column) OR method_exists($this, $column)) AND | |
$this->{$column} !== NULL)); | |
} | |
public function save() | |
{ | |
throw new Kohana_Exception('Model in read-only mode'); | |
} | |
public function delete() | |
{ | |
throw new Kohana_Exception('Model in read-only mode'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment