My favorites | Sign in
Project Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* LilFloat Behavior for CakePHP 1.2
*
* @copyright Copyright 2008, Miha Nahtigal (http://www.nahtigal.com)
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*
*
* This LilFloat behavior for CakePHP allows you to auto convert float values from local
* format to default format suitable for saving into database
*
*/
class LilFloatBehavior extends ModelBehavior {

/**
* An array of settings set by the $actsAs property
*/
var $_settings = array();

/**
* Instantiates the behavior and sets the magic methods
*
* @param object $model The Model object
* @param array $settings Array of scope properties
*/
function setup($model, $settings = array()) {
$settings = (array)$settings;
foreach ($settings as $named => $options) {
if (!is_array($options)) {
unset($settings[$named]);
$settings[$options] = array();
$named = $options;
}
}
if (empty($settings['decimalSeparator'])) {
if (!$settings['decimalSeparator'] = Configure::read('decimalSeparator')) {
$settings['decimalSeparator'] = '.';
}
}
if (!isset($settings['thousandsSeparator'])) {
if (!$settings['thousandsSeparator'] = Configure::read('thousandsSeparator')) {
$settings['thousandsSeparator'] = ',';
}
}
$this->_settings[$model->alias] = $settings;
}

/**
* Model callback function which converts local float values to international representation
*
*/
function beforeSave(&$model) {
$fields = $model->schema();
foreach ($fields as $field_name=>$field_data) {
if (($field_data['type']=='float' || $field_data['type']=='double') &&
!empty($this->data[$model->alias][$field_name]))
{
$this->data[$model->alias][$field_name] =
strtr($this->data[$model->alias][$field_name], array(
$this->_settings[$model->alias]['thousandsSeparator'] => '',
$this->_settings[$model->alias]['decimalSeparator'] => '.',
));
}
}
}

/**
* Sets new decimal separator
*
* @param string $decimalSeparator New decimal separator
* @access public
*/
function setDecimalSeparator(&$model, $decimalSeparator = null) {
if (!empty($decimalSeparator)) {
$this->_settings[$model->alias]['decimalSeparator'] = $decimalSeparator;
}
}

/**
* Sets new thousands separator
*
* @param string $decimalSeparator New decimal separator
* @access public
*/
function setThousandsSeparator(&$model, $thousandsSeparator = null) {
if (!empty($thousandsSeparator)) {
$this->_settings[$model->alias]['thousandsSeparator'] = $thousandsSeparator;
}
}

/**
* Checks that a value is a valid float. If $places is null, field type length is used.
* If no decimal point is found a false will be returned. The sign is optional.
*
* @param array $data Field-value array pair to check
* @param mixed $places if set $check value must have exactly $places after the decimal point
* @return boolean Success
* @access public
*/
function isValidFloat(&$model, $data, $places = null) {
// try to get decimal places from db schema
if (is_null($places)) {
$fields = $model->schema();
$field_name = reset(array_keys($data));
if (isset($fields[$field_name]) &&
($fields[$field_name]['type']=='float' || $fields[$field_name]['type']=='double') &&
strpos($fields[$field_name]['length'], ',')!==false)
{
$places = substr($fields[$field_name]['length'],
strpos($fields[$field_name]['length'], ',')+1);
}
}

//$regex = '/^[-+]?(0|([1-9]([0-9]*|([0-9]{0,2}(\,[0-9]{3})*))))(\.{1}[0-9]{2})?$/';
$regex = '/^[-+]?(0|([1-9]([0-9]*';
if (!empty($this->_settings[$model->alias]['thousandsSeparator'])) {
$regex .= '|([0-9]{0,2}(\\'.$this->_settings[$model->alias]['thousandsSeparator'].'[0-9]{3})*)';
}
$regex .= ')))';

if (!empty($places)) {
$regex .= '\\'.$this->_settings[$model->alias]['decimalSeparator'].'{1}[0-9]{'.$places.'}';
}
$regex .= '$/';

return preg_match($regex, reset($data));

}

}
Show details Hide details

Change log

r14 by m...@nahtigal.com on Mar 16, 2009   Diff
Added a few publicly available behaviors.
Go to: 
Project members, sign in to write a code review

Older revisions

r10 by m...@nahtigal.com on Dec 28, 2008   Diff
[1] Added new LilFloat behavior with
tests. [2] Added sanitize helper. [3]
Added date helper.
All revisions of this file

File info

Size: 4330 bytes, 130 lines
Hosted by Google Code