1: <?php
2: /**
3: * This file contains the number datatype class.
4: *
5: * @package Core
6: * @subpackage Datatype
7: * @version SVN Revision $Rev:$
8: *
9: * @author unknown
10: * @copyright four for business AG <www.4fb.de>
11: * @license http://www.contenido.org/license/LIZENZ.txt
12: * @link http://www.4fb.de
13: * @link http://www.contenido.org
14: */
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: /**
19: * Number datatype class.
20: *
21: * @package Core
22: * @subpackage Datatype
23: */
24: class cDatatypeNumber extends cDatatype {
25:
26: /**
27: *
28: * @var int
29: */
30: protected $_iPrecision;
31:
32: /**
33: *
34: * @var string
35: */
36: protected $_sThousandSeparatorCharacter;
37:
38: /**
39: *
40: * @var string
41: */
42: protected $_sDecimalPointCharacter;
43:
44: /**
45: *
46: */
47: public function __construct() {
48: $language = cI18n::getLanguage();
49:
50: // Try to find out the current locale settings
51: $aLocaleSettings = cLocaleConv($language);
52:
53: $this->setDecimalPointCharacter($aLocaleSettings["mon_decimal_point"]);
54: $this->setThousandSeparatorCharacter($aLocaleSettings["mon_thousands_sep"]);
55:
56: parent::__construct();
57: }
58:
59: /**
60: * (non-PHPdoc)
61: * @see cDatatype::set()
62: */
63: public function set($value) {
64: $this->_mValue = floatval($value);
65: }
66:
67: /**
68: * (non-PHPdoc)
69: * @see cDatatype::get()
70: */
71: public function get() {
72: return $this->_mValue;
73: }
74:
75: /**
76: *
77: * @param int $iPrecision
78: */
79: public function setPrecision($iPrecision) {
80: $this->_iPrecision = $iPrecision;
81: }
82:
83: /**
84: *
85: * @param string $sCharacter
86: */
87: public function setDecimalPointCharacter($sCharacter) {
88: $this->_sDecimalPointCharacter = $sCharacter;
89: }
90:
91: /**
92: *
93: * @return string
94: */
95: public function getDecimalPointCharacter() {
96: return $this->_sDecimalPointCharacter;
97: }
98:
99: /**
100: *
101: * @param string $sCharacter
102: */
103: public function setThousandSeparatorCharacter($sCharacter) {
104: $this->_sThousandSeparatorCharacter = $sCharacter;
105: }
106:
107: /**
108: *
109: * @return string
110: */
111: public function getThousandSeparatorCharacter() {
112: return $this->_sThousandSeparatorCharacter;
113: }
114:
115: /**
116: *
117: * @throws cException if the decimal separator character and the thousand
118: * separator character are equal
119: */
120: public function parse($value) {
121: if ($this->_sDecimalPointCharacter == $this->_sThousandSeparatorCharacter) {
122: throw new cException("Decimal point character cannot be the same as the thousand separator character. Current decimal point character is '{$this->_sDecimalPointCharacter}', current thousand separator character is '{$this->_sThousandSeparatorCharacter}'");
123: }
124:
125: // Convert to standard english format
126: $value = str_replace($this->_sThousandSeparatorCharacter, "", $value);
127: $value = str_replace($this->_sDecimalPointCharacter, ".", $value);
128:
129: $this->_mValue = floatval($value);
130: }
131:
132: /**
133: * (non-PHPdoc)
134: * @see cDatatype::render()
135: */
136: public function render() {
137: return number_format($this->_mValue, $this->_iPrecision, $this->_sDecimalPointCharacter, $this->_sThousandSeparatorCharacter);
138: }
139:
140: }
141:
142: ?>