root/trunk/patForms/Definition.php

Revision 330, 6.8 kB (checked in by sfuchs, 3 years ago)

closing #197:

- removed overloaded access to patForms_Definition data (get())
- added getName(), getAutoValidate() and getElements() to patForms_Definition
- changed patForms_Definition::
construct() protocoll to now use an $attributes array for better extensibility
- updated patForms_Creator_Definition and patForms_Definition_Propel according to these changes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 /**
3  * Base class for xml based Definitions of a patForms instance
4  *
5  * A patForms_Definition can be written to and populated from an xml file.
6  * It can be used to populate a patForms instance with form elements, rules
7  * and other properties.
8  *
9  * Please note: this class is currently php5 only since its solely used
10  * as a base class for patForms_Definitions_Propel in order to integrate
11  * patForms with Propel5 as a "rapid form solution". This might change in
12  * future.
13  *
14  * This requires the PEAR XML_Serializer package to be installed and
15  * available in the include_path
16  *
17  * $Id$
18  *
19  * @author        Sven Fuchs <svenfuchs@artweb-design.de>
20  * @package        patForms
21  * @subpackage    Definition
22  * @license        LGPL
23  * @copyright    PHP Application Tools <http://www.php-tools.net>
24  * @see         patForms_Creator_Definition
25  */
26
27 /**
28  * Base class for xml based Definitions of a patForms instance
29  *
30  * A patForms_Definition can be written to and populated from an xml file.
31  * It can be used to populate a patForms instance with form elements, rules
32  * and other properties.
33  *
34  * Please note: this class is currently php5 only since its solely used
35  * as a base class for patForms_Definitions_Propel in order to integrate
36  * patForms with Propel5 as a "rapid form solution". This might change in
37  * future.
38  *
39  * @author        Sven Fuchs <svenfuchs@artweb-design.de>
40  * @package        patForms
41  * @subpackage    Definition
42  * @license        LGPL
43  * @copyright    PHP Application Tools <http://www.php-tools.net>
44  * @see         patForms_Creator_Definition
45  */
46 class patForms_Definition {
47
48    /**
49     * Stores the form name
50     *
51     * @var      string
52     * @access    private
53     */
54     private $name = '';
55
56    /**
57     * Stores the form's autoValidate attribute
58     *
59     * @var      boolean
60     * @access    private
61     */
62     private $autoValidate = false;
63
64    /**
65     * Stores the form element definitions
66     *
67     * @var      array
68     * @access    private
69     */
70     private $elements = array();
71
72    /**
73     * The constructor
74     *
75     * Adds a timestamp as mtime to the definition's data, that might be
76     * overwritten by loading a definition file
77     *
78     * @access    public
79     * @param    string   $name           The name of the form
80     * @param    string   $autoValidate   The autoValidate property of the form
81     */
82     public function __construct($name, $attributes = array())
83     {
84         $this->name = $name;
85
86         if (isset($attributes['autoValidate'])) {
87             $this->autoValidate = $attributes['autoValidate'];
88         }
89     }
90
91    /**
92     * Factory method to create a new patForms_Definition instance
93     *
94     * not implemented
95     *
96     * @static
97     * @access    public
98     * @param    array    $conf   Data needed by the factory method
99     */
100     static public function create($conf)
101     {
102     }
103
104    /**
105     * Returns the form name
106     *
107     * @access    public
108     * @return   string  The form name
109     */
110     public function getName()
111     {
112         return $this->name;
113     }
114
115    /**
116     * Returns the form's autoValidate attribute
117     *
118     * @access    public
119     * @return   boolean  the form's autoValidate attribute
120     */
121     public function getAutoValidate()
122     {
123         return $this->autoValidate;
124     }
125
126    /**
127     * Returns the form element definitions
128     *
129     * @access    public
130     * @return   array  The form element definitions
131     */
132     public function getElements()
133     {
134         return $this->elements;
135     }
136
137    /**
138     * Adds the definition for a form element
139     *
140     * @access    public
141     * @param    string   $name   The name of the element
142     * @param    string   $type   The type of the element
143     * @param    array    $attributes   Attributes for the element
144     * @param    array    $rules  Definitions for patForm_Rules
145     * @todo     Change protocol to addElement(array $data) to be more flexible
146     */
147     public function addElement($name, $type, $attributes = array(), $rules = array())
148     {
149         if (is_array($type)) {
150             extract($type);
151         }
152
153         $this->elements[$name] = array(
154             'name' => $name,
155             'type' => $type
156         );
157
158         foreach ($attributes as $key => $value) {
159             $value = $this->cast($value);
160             $this->elements[$name]['attributes'][$key] = $value;
161         }
162         foreach ($rules as $key => $rule) {
163             $this->elements[$name]['rules'][$key] = $rule;
164         }
165     }
166
167    /**
168     * Loads the definitions properties from an xml file
169     *
170     * @access    public
171     * @param    string   $filename   The filename of the xml file
172     */
173     public function load($filename)
174     {
175         $data = $this->read($filename);
176
177         foreach ($data as $key => $value) {
178             if ($key == 'elements') {
179                 foreach ($value as $name => $element) {
180                     $this->addElement($name, $element);
181                 }
182             } else {
183                 $this->data[$key] = $this->cast($value);
184             }
185         }
186     }
187
188    /**
189     * Writes the definitions properties to an xml file
190     *
191     * @access    public
192     * @param    string   $filename   The filename of the xml file
193     */
194     public function save($filename)
195     {
196         $this->write($filename, array (
197             'name' => $this->name,
198             'autoValidate' => $this->autoValidate,
199             'elements' => $this->elements,
200         ));
201     }
202
203    /**
204     * Reads an xml file
205     *
206     * @access    protected
207     * @param    string   $filename   The filename of the xml file
208     * @return   array    The data read from the file
209     */
210     protected function read($filename)
211     {
212         require_once "XML/Unserializer.php";
213
214         $xml = file_get_contents($filename);
215         $unserializer = new XML_Unserializer();
216         $unserializer->unserialize($xml);
217         return $unserializer->getUnserializedData();
218     }
219
220    /**
221     * Writes data to an xml file
222     *
223     * @access    protected
224     * @param    string   $filename   The filename of the xml file
225     * @param    array    $data       The data to write
226     */
227     protected function write($filename, $data)
228     {
229         require_once "XML/Serializer.php";
230
231         $serializer = new XML_Serializer(array (
232             'addDecl' => true,
233             'encoding' => 'ISO-8859-1',
234             'indent' => '  ',
235             'rootName' => 'form',
236             'defaultTagName' => 'tag'
237         ));
238         $serializer->serialize($data);
239         $xml = $serializer->getSerializedData();
240
241         $fp = fopen($filename, 'w+');
242         fputs($fp, $xml);
243         fclose($fp);
244     }
245
246    /**
247     * Tries to guess the correct datatype for a variable and casts it if
248     * necessary
249     *
250     * Please note: this doesn't seem to work right now with patForms_Elements
251     * and will therefor return the value parameter without changing anything
252     * for now. This may change in future.
253     *
254     * @access    protected
255     * @param    string   $value     The value to check
256     * @return   array    The value (probably cast to another type)
257     */
258     protected function cast($value)
259     {
260         return $value;
261
262         // seems as if patForms_Element(s) are broken here
263         // e.g. in patForms_Element_Text::serializeHtmlDefault()
264         // at line 245 if( $this->attributes['display'] == 'no' )
265         // will result to true if the display attribute is set
266         // to (php boolean) true
267         // so casting the 'true'/'false' and 'yes'/'no' values
268         // would break intended behaviour here
269
270         if (is_array($value) OR is_bool($value)) {
271             return $value;
272         }
273         if ($value === 'true') {
274             return true;
275         }
276         if ($value === 'false') {
277             return false;
278         }
279         if (preg_match('/^[+-]?[0-9]+$/', $value)) {
280             settype($value, 'int');
281             return $value;
282         }
283         if (preg_match('/^[+-]?[0-9]*\.[0-9]+$/', $value)) {
284             settype($value, 'double');
285             return $value;
286         }
287         return $value;
288     }
289 }
290
291
292 ?>
Note: See TracBrowser for help on using the browser.