]> dev.renevier.net Git - syj.git/blob - application/models/Generic.php
version 0.1
[syj.git] / application / models / Generic.php
1 <?php
2 /*  This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
3     and is published under the AGPL license. */
4
5 abstract class Syj_Model_Generic
6 {
7     protected $_translator = null;
8
9     public function __construct(array $options = null) {
10         if (is_array($options)) {
11             $this->setOptions($options);
12         }
13     }
14
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));
19         }
20         $this->$method($value);
21     }
22
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));
27         }
28         return $this->$method();
29     }
30
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));
35         }
36         $result = $this->$method();
37         return isset($result);
38     }
39
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);
46             }
47         }
48         return $this;
49     }
50
51     protected function getTranslator() {
52         if ($this->_translator) {
53             return $this->_translator;
54         }
55         if (Zend_Registry::isRegistered('Zend_Translate')) {
56             $translator = Zend_Registry::get('Zend_Translate');
57             $this->_translator = $translator->getAdapter();
58         }
59         return $this->_translator;
60     }
61
62     protected function translate($messageId, $locale = null) {
63         $translator = $this->getTranslator();
64         if ($translator) {
65             return $translator->translate($messageId, $locale);
66         }
67         return $messageId;
68     }
69
70 }