More conventions than code added:
[shipsimu.git] / inc / classes / main / helper / class_BaseHelper.php
1 <?php
2 /**
3  * A generic helper class with generic methods
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class BaseHelper extends BaseFrameworkSystem {
25         /**
26          * Rendered content created by the helper class
27          */
28         private $content = "";
29
30         /**
31          * Instance to the class which provides field values
32          */
33         private $valueInstance = null;
34
35         // Exception constants
36         const EXCEPTION_XML_PARSER_ERROR  = 0x1e0;
37         const EXCEPTION_XML_NODE_UNKNOWN  = 0x1e1;
38         const EXCEPTION_XML_NODE_MISMATCH = 0x1e2;
39
40         /**
41          * Protected constructor
42          *
43          * @param       $className      Real name of the class
44          * @return      void
45          */
46         protected function __construct ($className) {
47                 // Call parent constructor
48                 parent::__construct($className);
49
50                 // Clean up a little
51                 $this->removeNumberFormaters();
52                 $this->removeSystemArray();
53         }
54
55         /**
56          * Add content
57          *
58          * @param       $newContent             New content to add
59          * @return      void
60          */
61         protected final function addContent ($newContent) {
62                 $this->content .= (string) trim($newContent)."\r\n";
63         }
64
65         /**
66          * Getter for content
67          *
68          * @return      $content        The rendered content by this helper
69          */
70         protected final function getContent () {
71                 return $this->content;
72         }
73
74         /**
75          *  Assigns a field from the value instance with a template variable
76          *
77          * @param       $fieldName      Name of the field to assign
78          * @return      void
79          */
80         public function assignField ($fieldName) {
81                 // Get the value from value instance
82                 $fieldValue = $this->getField($fieldName);
83
84                 // Add a group
85                 $this->getTemplateInstance()->setVariableGroup('values');
86
87                 // Assign it with a template variable
88                 $this->getTemplateInstance()->addGroupVariable($fieldName, $fieldValue);
89         }
90
91         /**
92          * Assigns a field from the value instance with a template variable but
93          * parses its content through a given filter method of the value instance
94          *
95          * @param       $fieldName              Name of the field to assign
96          * @param       $filterMethod   Method name to call of the value instance
97          * @return      void
98          */
99         public function assignFieldWithFilter ($fieldName, $filterMethod) {
100                 // Get the value
101                 $fieldValue = $this->getField($fieldName);
102
103                 // Now filter it through the value through the filter method
104                 $filteredValue = call_user_func_array(array($this->valueInstance, "doFilter" . ucfirst($filterMethod)), array($fieldValue));
105
106                 // Add a group
107                 $this->getTemplateInstance()->setVariableGroup('values');
108
109                 // Assign it with a template variable
110                 $this->getTemplateInstance()->addGroupVariable($fieldName, $fieldValue);
111         }
112
113         /**
114          * Pre-fetches field default values from the given registry key instance into this class
115          *
116          * @param       $registryKey    Registry key which holds an object with values
117          * @return      void
118          * @throws      NullPointerException    If an instance from registry is null
119          */
120         public function prefetchValueInstance ($registryKey) {
121                 // Get the required instance
122                 $this->valueInstance = Registry::getRegistry()->getInstance($registryKey);
123
124                 // Is the instance valid?
125                 if (is_null($this->valueInstance)) {
126                         // Throw an exception
127                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
128                 } // END - if
129         }
130
131         /**
132          * Getter for direct field values
133          *
134          * @param       $fieldName              Name of the field we shall fetch
135          * @return      $fieldValue             Value from field
136          */
137         public function getField ($fieldName) {
138                 // Get the field value
139                 $fieldValue = call_user_func_array(array($this->valueInstance, 'getField'), array($fieldName));
140
141                 // Return it
142                 return $fieldValue;
143         }
144 }
145
146 // [EOF]
147 ?>