2 /* This file is part of Syj, Copyright (c) 2010-2011 Arnaud Renevier,
3 and is published under the AGPL license. */
5 abstract class Syj_Model_Generic
7 protected $_translator = null;
9 public function __construct(array $options = null) {
10 if (is_array($options)) {
11 $this->setOptions($options);
15 public function __set($name, $value) {
16 $method = 'set' . $name;
17 if (('mapper' == $name) || !method_exists($this, $method)) {
18 throw new Zend_Exception($name . ': Invalid property for ' . get_class($this));
20 $this->$method($value);
23 public function __get($name) {
24 $method = 'get' . $name;
25 if (('mapper' == $name) || !method_exists($this, $method)) {
26 throw new Zend_Exception($name . ': Invalid property for ' . get_class($this));
28 return $this->$method();
31 public function __isset($name) {
32 $method = 'get' . $name;
33 if (('mapper' == $name) || !method_exists($this, $method)) {
34 throw new Zend_Exception($name . ': Invalid property for ' . get_class($this));
36 $result = $this->$method();
37 return isset($result);
40 public function setOptions(array $options) {
41 $methods = get_class_methods($this);
42 foreach ($options as $key => $value) {
43 $method = 'set' . ucfirst($key);
44 if (in_array($method, $methods)) {
45 $this->$method($value);
51 protected function getTranslator() {
52 if ($this->_translator) {
53 return $this->_translator;
55 if (Zend_Registry::isRegistered('Zend_Translate')) {
56 $translator = Zend_Registry::get('Zend_Translate');
57 $this->_translator = $translator->getAdapter();
59 return $this->_translator;
62 protected function translate($messageId, $locale = null) {
63 $translator = $this->getTranslator();
65 return $translator->translate($messageId, $locale);