| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
@copyright |
|---|
| 6 |
@author will@flourishlib.com |
|---|
| 7 |
@author dan@imarc.net |
|---|
| 8 |
@license http://flourishlib.com/license |
|---|
| 9 |
|
|---|
| 10 |
@package |
|---|
| 11 |
@link http://flourishlib.com/fORMMoney |
|---|
| 12 |
|
|---|
| 13 |
@version |
|---|
| 14 |
@changes |
|---|
| 15 |
@changes |
|---|
| 16 |
@changes |
|---|
| 17 |
@changes |
|---|
| 18 |
@changes |
|---|
| 19 |
@changes |
|---|
| 20 |
@changes |
|---|
| 21 |
@changes |
|---|
| 22 |
@changes |
|---|
| 23 |
|
|---|
| 24 |
class fORMMoney |
|---|
| 25 |
{ |
|---|
| 26 |
|
|---|
| 27 |
const configureMoneyColumn = 'fORMMoney::configureMoneyColumn'; |
|---|
| 28 |
const encodeMoneyColumn = 'fORMMoney::encodeMoneyColumn'; |
|---|
| 29 |
const inspect = 'fORMMoney::inspect'; |
|---|
| 30 |
const makeMoneyObjects = 'fORMMoney::makeMoneyObjects'; |
|---|
| 31 |
const objectifyMoney = 'fORMMoney::objectifyMoney'; |
|---|
| 32 |
const objectifyMoneyWithCurrency = 'fORMMoney::objectifyMoneyWithCurrency'; |
|---|
| 33 |
const prepareMoneyColumn = 'fORMMoney::prepareMoneyColumn'; |
|---|
| 34 |
const reflect = 'fORMMoney::reflect'; |
|---|
| 35 |
const reset = 'fORMMoney::reset'; |
|---|
| 36 |
const setCurrencyColumn = 'fORMMoney::setCurrencyColumn'; |
|---|
| 37 |
const setMoneyColumn = 'fORMMoney::setMoneyColumn'; |
|---|
| 38 |
const validateMoneyColumns = 'fORMMoney::validateMoneyColumns'; |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
@var |
|---|
| 45 |
|
|---|
| 46 |
static private $currency_columns = array(); |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
@var |
|---|
| 52 |
|
|---|
| 53 |
static private $money_columns = array(); |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
@param |
|---|
| 60 |
@param |
|---|
| 61 |
@param |
|---|
| 62 |
@return |
|---|
| 63 |
|
|---|
| 64 |
static private function compose($message) |
|---|
| 65 |
{ |
|---|
| 66 |
$args = array_slice(func_get_args(), 1); |
|---|
| 67 |
|
|---|
| 68 |
if (class_exists('fText', FALSE)) { |
|---|
| 69 |
return call_user_func_array( |
|---|
| 70 |
array('fText', 'compose'), |
|---|
| 71 |
array($message, $args) |
|---|
| 72 |
); |
|---|
| 73 |
} else { |
|---|
| 74 |
return vsprintf($message, $args); |
|---|
| 75 |
} |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
@param |
|---|
| 83 |
@param |
|---|
| 84 |
@param |
|---|
| 85 |
@return |
|---|
| 86 |
|
|---|
| 87 |
static public function configureMoneyColumn($class, $column, $currency_column=NULL) |
|---|
| 88 |
{ |
|---|
| 89 |
$class = fORM::getClass($class); |
|---|
| 90 |
$table = fORM::tablize($class); |
|---|
| 91 |
$schema = fORMSchema::retrieve($class); |
|---|
| 92 |
$data_type = $schema->getColumnInfo($table, $column, 'type'); |
|---|
| 93 |
|
|---|
| 94 |
$valid_data_types = array('float'); |
|---|
| 95 |
if (!in_array($data_type, $valid_data_types)) { |
|---|
| 96 |
throw new fProgrammerException( |
|---|
| 97 |
'The column specified, %1$s, is a %2$s column. Must be %3$s to be set as a money column.', |
|---|
| 98 |
$column, |
|---|
| 99 |
$data_type, |
|---|
| 100 |
join(', ', $valid_data_types) |
|---|
| 101 |
); |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
if ($currency_column !== NULL) { |
|---|
| 105 |
$currency_column_data_type = $schema->getColumnInfo($table, $currency_column, 'type'); |
|---|
| 106 |
$valid_currency_column_data_types = array('varchar', 'char', 'text'); |
|---|
| 107 |
if (!in_array($currency_column_data_type, $valid_currency_column_data_types)) { |
|---|
| 108 |
throw new fProgrammerException( |
|---|
| 109 |
'The currency column specified, %1$s, is a %2$s column. Must be %3$s to be set as a currency column.', |
|---|
| 110 |
$currency_column, |
|---|
| 111 |
$currency_column_data_type, |
|---|
| 112 |
join(', ', $valid_currency_column_data_types) |
|---|
| 113 |
); |
|---|
| 114 |
} |
|---|
| 115 |
} |
|---|
| 116 |
|
|---|
| 117 |
$camelized_column = fGrammar::camelize($column, TRUE); |
|---|
| 118 |
|
|---|
| 119 |
fORM::registerActiveRecordMethod( |
|---|
| 120 |
$class, |
|---|
| 121 |
'encode' . $camelized_column, |
|---|
| 122 |
self::encodeMoneyColumn |
|---|
| 123 |
); |
|---|
| 124 |
|
|---|
| 125 |
fORM::registerActiveRecordMethod( |
|---|
| 126 |
$class, |
|---|
| 127 |
'prepare' . $camelized_column, |
|---|
| 128 |
self::prepareMoneyColumn |
|---|
| 129 |
); |
|---|
| 130 |
|
|---|
| 131 |
if (!fORM::checkHookCallback($class, 'post::validate()', self::validateMoneyColumns)) { |
|---|
| 132 |
fORM::registerHookCallback($class, 'post::validate()', self::validateMoneyColumns); |
|---|
| 133 |
} |
|---|
| 134 |
|
|---|
| 135 |
fORM::registerReflectCallback($class, self::reflect); |
|---|
| 136 |
fORM::registerInspectCallback($class, $column, self::inspect); |
|---|
| 137 |
|
|---|
| 138 |
$value = FALSE; |
|---|
| 139 |
|
|---|
| 140 |
if ($currency_column) { |
|---|
| 141 |
$value = $currency_column; |
|---|
| 142 |
|
|---|
| 143 |
if (empty(self::$currency_columns[$class])) { |
|---|
| 144 |
self::$currency_columns[$class] = array(); |
|---|
| 145 |
} |
|---|
| 146 |
self::$currency_columns[$class][$currency_column] = $column; |
|---|
| 147 |
|
|---|
| 148 |
if (!fORM::checkHookCallback($class, 'post::loadFromResult()', self::makeMoneyObjects)) { |
|---|
| 149 |
fORM::registerHookCallback($class, 'post::loadFromResult()', self::makeMoneyObjects); |
|---|
| 150 |
} |
|---|
| 151 |
|
|---|
| 152 |
if (!fORM::checkHookCallback($class, 'pre::validate()', self::makeMoneyObjects)) { |
|---|
| 153 |
fORM::registerHookCallback($class, 'pre::validate()', self::makeMoneyObjects); |
|---|
| 154 |
} |
|---|
| 155 |
|
|---|
| 156 |
fORM::registerActiveRecordMethod( |
|---|
| 157 |
$class, |
|---|
| 158 |
'set' . $camelized_column, |
|---|
| 159 |
self::setMoneyColumn |
|---|
| 160 |
); |
|---|
| 161 |
|
|---|
| 162 |
fORM::registerActiveRecordMethod( |
|---|
| 163 |
$class, |
|---|
| 164 |
'set' . fGrammar::camelize($currency_column, TRUE), |
|---|
| 165 |
self::setCurrencyColumn |
|---|
| 166 |
); |
|---|
| 167 |
|
|---|
| 168 |
} else { |
|---|
| 169 |
fORM::registerObjectifyCallback($class, $column, self::objectifyMoney); |
|---|
| 170 |
} |
|---|
| 171 |
|
|---|
| 172 |
if (empty(self::$money_columns[$class])) { |
|---|
| 173 |
self::$money_columns[$class] = array(); |
|---|
| 174 |
} |
|---|
| 175 |
|
|---|
| 176 |
self::$money_columns[$class][$column] = $value; |
|---|
| 177 |
} |
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
@internal |
|---|
| 184 |
|
|---|
| 185 |
@param |
|---|
| 186 |
@param |
|---|
| 187 |
@param |
|---|
| 188 |
@param |
|---|
| 189 |
@param |
|---|
| 190 |
@param |
|---|
| 191 |
@param |
|---|
| 192 |
@return |
|---|
| 193 |
|
|---|
| 194 |
static public function encodeMoneyColumn($object, &$values, &$old_values, &$related_records, &$cache, $method_name, $parameters) |
|---|
| 195 |
{ |
|---|
| 196 |
list ($action, $column) = fORM::parseMethod($method_name); |
|---|
| 197 |
|
|---|
| 198 |
$value = $values[$column]; |
|---|
| 199 |
|
|---|
| 200 |
if ($value instanceof fMoney) { |
|---|
| 201 |
$value = $value->__toString(); |
|---|
| 202 |
} |
|---|
| 203 |
|
|---|
| 204 |
return fHTML::prepare($value); |
|---|
| 205 |
} |
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
@internal |
|---|
| 212 |
|
|---|
| 213 |
@param |
|---|
| 214 |
@param |
|---|
| 215 |
@param |
|---|
| 216 |
@return |
|---|
| 217 |
|
|---|
| 218 |
static public function inspect($class, $column, &$metadata) |
|---|
| 219 |
{ |
|---|
| 220 |
unset($metadata['auto_increment']); |
|---|
| 221 |
$metadata['feature'] = 'money'; |
|---|
| 222 |
} |
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
@internal |
|---|
| 229 |
|
|---|
| 230 |
@param |
|---|
| 231 |
@param |
|---|
| 232 |
@param |
|---|
| 233 |
@param |
|---|
| 234 |
@param |
|---|
| 235 |
@return |
|---|
| 236 |
|
|---|
| 237 |
static public function makeMoneyObjects($object, &$values, &$old_values, &$related_records, &$cache) |
|---|
| 238 |
{ |
|---|
| 239 |
$class = get_class($object); |
|---|
| 240 |
|
|---|
| 241 |
if (!isset(self::$currency_columns[$class])) { |
|---|
| 242 |
return; |
|---|
| 243 |
} |
|---|
| 244 |
|
|---|
| 245 |
foreach(self::$currency_columns[$class] as $currency_column => $value_column) { |
|---|
| 246 |
self::objectifyMoneyWithCurrency($values, $old_values, $value_column, $currency_column); |
|---|
| 247 |
} |
|---|
| 248 |
} |
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
@internal |
|---|
| 255 |
|
|---|
| 256 |
@param |
|---|
| 257 |
@param |
|---|
| 258 |
@param |
|---|
| 259 |
@return |
|---|
| 260 |
|
|---|
| 261 |
static public function objectifyMoney($class, $column, $value) |
|---|
| 262 |
{ |
|---|
| 263 |
if ((!is_string($value) && !is_numeric($value) && !is_object($value)) || !strlen(trim($value))) { |
|---|
| 264 |
return $value; |
|---|
| 265 |
} |
|---|
| 266 |
|
|---|
| 267 |
try { |
|---|
| 268 |
return new fMoney($value); |
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 |
} catch (fExpectedException $e) { |
|---|
| 272 |
return $value; |
|---|
| 273 |
} |
|---|
| 274 |
} |
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
@internal |
|---|
| 281 |
|
|---|
| 282 |
@param |
|---|
| 283 |
@param |
|---|
| 284 |
@param |
|---|
| 285 |
@param |
|---|
| 286 |
@return |
|---|
| 287 |
|
|---|
| 288 |
static public function objectifyMoneyWithCurrency(&$values, &$old_values, $value_column, $currency_column) |
|---|
| 289 |
{ |
|---|
| 290 |
if ((!is_string($values[$value_column]) && !is_numeric($values[$value_column]) && !is_object($values[$value_column])) || !strlen(trim($values[$value_column]))) { |
|---|
| 291 |
return; |
|---|
| 292 |
} |
|---|
| 293 |
|
|---|
| 294 |
try { |
|---|
| 295 |
$value = $values[$value_column]; |
|---|
| 296 |
if ($value instanceof fMoney) { |
|---|
| 297 |
$value = $value->__toString(); |
|---|
| 298 |
} |
|---|
| 299 |
|
|---|
| 300 |
$currency = $values[$currency_column]; |
|---|
| 301 |
if (!$currency && $currency !== '0' && $currency !== 0) { |
|---|
| 302 |
$currency = NULL; |
|---|
| 303 |
} |
|---|
| 304 |
|
|---|
| 305 |
$value = new fMoney($value, $currency); |
|---|
| 306 |
|
|---|
| 307 |
if (fActiveRecord::hasOld($old_values, $currency_column) && !fActiveRecord::hasOld($old_values, $value_column)) { |
|---|
| 308 |
fActiveRecord::assign($values, $old_values, $value_column, $value); |
|---|
| 309 |
} else { |
|---|
| 310 |
$values[$value_column] = $value; |
|---|
| 311 |
} |
|---|
| 312 |
|
|---|
| 313 |
if ($values[$currency_column] === NULL) { |
|---|
| 314 |
fActiveRecord::assign($values, $old_values, $currency_column, $value->getCurrency()); |
|---|
| 315 |
} |
|---|
| 316 |
|
|---|
| 317 |
|
|---|
| 318 |
} catch (fExpectedException $e) { } |
|---|
| 319 |
} |
|---|
| 320 |
|
|---|
| 321 |
|
|---|
| 322 |
|
|---|
| 323 |
|
|---|
| 324 |
|
|---|
| 325 |
@internal |
|---|
| 326 |
|
|---|
| 327 |
@param |
|---|
| 328 |
@param |
|---|
| 329 |
@param |
|---|
| 330 |
@param |
|---|
| 331 |
@param |
|---|
| 332 |
@param |
|---|
| 333 |
@param |
|---|
| 334 |
@return |
|---|
| 335 |
|
|---|
| 336 |
static public function prepareMoneyColumn($object, &$values, &$old_values, &$related_records, &$cache, $method_name, $parameters) |
|---|
| 337 |
{ |
|---|
| 338 |
list ($action, $column) = fORM::parseMethod($method_name); |
|---|
| 339 |
|
|---|
| 340 |
if (empty($values[$column])) { |
|---|
| 341 |
return $values[$column]; |
|---|
| 342 |
} |
|---|
| 343 |
$value = $values[$column]; |
|---|
| 344 |
|
|---|
| 345 |
$remove_zero_fraction = FALSE; |
|---|
| 346 |
if (count($parameters)) { |
|---|
| 347 |
$remove_zero_fraction = $parameters[0]; |
|---|
| 348 |
} |
|---|
| 349 |
|
|---|
| 350 |
if ($value instanceof fMoney) { |
|---|
| 351 |
$value = $value->format($remove_zero_fraction); |
|---|
| 352 |
} |
|---|
| 353 |
|
|---|
| 354 |
return fHTML::prepare($value); |
|---|
| 355 |
} |
|---|
| 356 |
|
|---|
| 357 |
|
|---|
| 358 |
|
|---|
| 359 |
|
|---|
| 360 |
|
|---|
| 361 |
@internal |
|---|
| 362 |
|
|---|
| 363 |
@param |
|---|
| 364 |
@param |
|---|
| 365 |
@param |
|---|
| 366 |
@return |
|---|
| 367 |
|
|---|
| 368 |
static public function reflect($class, &$signatures, $include_doc_comments) |
|---|
| 369 |
{ |
|---|
| 370 |
if (!isset(self::$money_columns[$class])) { |
|---|
| 371 |
return; |
|---|
| 372 |
} |
|---|
| 373 |
|
|---|
| 374 |
foreach(self::$money_columns[$class] as $column => $enabled) { |
|---|
| 375 |
$camelized_column = fGrammar::camelize($column, TRUE); |
|---|
| 376 |
|
|---|
| 377 |
|
|---|
| 378 |
$signature = ''; |
|---|
| 379 |
if ($include_doc_comments) { |
|---|
| 380 |
$signature .= "/**\n"; |
|---|
| 381 |
$signature .= " * Gets the current value of " . $column . "\n"; |
|---|
| 382 |
$signature .= " * \n"; |
|---|
| 383 |
$signature .= " * @return fMoney The current value\n"; |
|---|
| 384 |
$signature .= " */\n"; |
|---|
| 385 |
} |
|---|
| 386 |
$get_method = 'get' . $camelized_column; |
|---|
| 387 |
$signature .= 'public function ' . $get_method . '()'; |
|---|
| 388 |
|
|---|
| 389 |
$signatures[$get_method] = $signature; |
|---|
| 390 |
|
|---|
| 391 |
|
|---|
| 392 |
$signature = ''; |
|---|
| 393 |
if ($include_doc_comments) { |
|---|
| 394 |
$signature .= "/**\n"; |
|---|
| 395 |
$signature .= " * Sets the value for " . $column . "\n"; |
|---|
| 396 |
$signature .= " * \n"; |
|---|
| 397 |
$signature .= " * @param fMoney|string|integer \$" . $column . " The new value - a string or integer will be converted to the default currency (if defined)\n"; |
|---|
| 398 |
$signature .= " * @return fActiveRecord The record object, to allow for method chaining\n"; |
|---|
| 399 |
$signature .= " */\n"; |
|---|
| 400 |
} |
|---|
| 401 |
$set_method = 'set' . $camelized_column; |
|---|
| 402 |
$signature .= 'public function ' . $set_method . '($' . $column . ')'; |
|---|
| 403 |
|
|---|
| 404 |
$signatures[$set_method] = $signature; |
|---|
| 405 |
|
|---|
| 406 |
$signature = ''; |
|---|
| 407 |
if ($include_doc_comments) { |
|---|
| 408 |
$signature .= "/**\n"; |
|---|
| 409 |
$signature .= " * Encodes the value of " . $column . " for output into an HTML form\n"; |
|---|
| 410 |
$signature .= " * \n"; |
|---|
| 411 |
$signature .= " * If the value is an fMoney object, the ->__toString() method will be called\n"; |
|---|
| 412 |
$signature .= " * resulting in the value minus the currency symbol and thousands separators\n"; |
|---|
| 413 |
$signature .= " * \n"; |
|---|
| 414 |
$signature .= " * @return string The HTML form-ready value\n"; |
|---|
| 415 |
$signature .= " */\n"; |
|---|
| 416 |
} |
|---|
| 417 |
$encode_method = 'encode' . $camelized_column; |
|---|
| 418 |
$signature .= 'public function ' . $encode_method . '()'; |
|---|
| 419 |
|
|---|
| 420 |
$signatures[$encode_method] = $signature; |
|---|
| 421 |
|
|---|
| 422 |
$signature = ''; |
|---|
| 423 |
if ($include_doc_comments) { |
|---|
| 424 |
$signature .= "/**\n"; |
|---|
| 425 |
$signature .= " * Prepares the value of " . $column . " for output into HTML\n"; |
|---|
| 426 |
$signature .= " * \n"; |
|---|
| 427 |
$signature .= " * If the value is an fMoney object, the ->format() method will be called\n"; |
|---|
| 428 |
$signature .= " * resulting in the value including the currency symbol and thousands separators\n"; |
|---|
| 429 |
$signature .= " * \n"; |
|---|
| 430 |
$signature .= " * @param boolean \$remove_zero_fraction If a fraction of all zeros should be removed\n"; |
|---|
| 431 |
$signature .= " * @return string The HTML-ready value\n"; |
|---|
| 432 |
$signature .= " */\n"; |
|---|
| 433 |
} |
|---|
| 434 |
$prepare_method = 'prepare' . $camelized_column; |
|---|
| 435 |
$signature .= 'public function ' . $prepare_method . '($remove_zero_fraction=FALSE)'; |
|---|
| 436 |
|
|---|
| 437 |
$signatures[$prepare_method] = $signature; |
|---|
| 438 |
} |
|---|
| 439 |
} |
|---|
| 440 |
|
|---|
| 441 |
|
|---|
| 442 |
|
|---|
| 443 |
|
|---|
| 444 |
|
|---|
| 445 |
@internal |
|---|
| 446 |
|
|---|
| 447 |
@return |
|---|
| 448 |
|
|---|
| 449 |
static public function reset() |
|---|
| 450 |
{ |
|---|
| 451 |
self::$currency_columns = array(); |
|---|
| 452 |
self::$money_columns = array(); |
|---|
| 453 |
} |
|---|
| 454 |
|
|---|
| 455 |
|
|---|
| 456 |
|
|---|
| 457 |
|
|---|
| 458 |
|
|---|
| 459 |
@internal |
|---|
| 460 |
|
|---|
| 461 |
@param |
|---|
| 462 |
@param |
|---|
| 463 |
@param |
|---|
| 464 |
@param |
|---|
| 465 |
@param |
|---|
| 466 |
@param |
|---|
| 467 |
@param |
|---|
| 468 |
@return |
|---|
| 469 |
|
|---|
| 470 |
static public function setCurrencyColumn($object, &$values, &$old_values, &$related_records, &$cache, $method_name, $parameters) |
|---|
| 471 |
{ |
|---|
| 472 |
list ($action, $column) = fORM::parseMethod($method_name); |
|---|
| 473 |
|
|---|
| 474 |
$class = get_class($object); |
|---|
| 475 |
|
|---|
| 476 |
if (count($parameters) < 1) { |
|---|
| 477 |
throw new fProgrammerException( |
|---|
| 478 |
'The method, %s(), requires at least one parameter', |
|---|
| 479 |
$method_name |
|---|
| 480 |
); |
|---|
| 481 |
} |
|---|
| 482 |
|
|---|
| 483 |
fActiveRecord::assign($values, $old_values, $column, $parameters[0]); |
|---|
| 484 |
|
|---|
| 485 |
|
|---|
| 486 |
self::objectifyMoneyWithCurrency( |
|---|
| 487 |
$values, |
|---|
| 488 |
$old_values, |
|---|
| 489 |
self::$currency_columns[$class][$column], |
|---|
| 490 |
$column |
|---|
| 491 |
); |
|---|
| 492 |
|
|---|
| 493 |
return $object; |
|---|
| 494 |
} |
|---|
| 495 |
|
|---|
| 496 |
|
|---|
| 497 |
|
|---|
| 498 |
|
|---|
| 499 |
|
|---|
| 500 |
@internal |
|---|
| 501 |
|
|---|
| 502 |
@param |
|---|
| 503 |
@param |
|---|
| 504 |
@param |
|---|
| 505 |
@param |
|---|
| 506 |
@param |
|---|
| 507 |
@param |
|---|
| 508 |
@param |
|---|
| 509 |
@return |
|---|
| 510 |
|
|---|
| 511 |
static public function setMoneyColumn($object, &$values, &$old_values, &$related_records, &$cache, $method_name, $parameters) |
|---|
| 512 |
{ |
|---|
| 513 |
list ($action, $column) = fORM::parseMethod($method_name); |
|---|
| 514 |
|
|---|
| 515 |
$class = get_class($object); |
|---|
| 516 |
|
|---|
| 517 |
if (count($parameters) < 1) { |
|---|
| 518 |
throw new fProgrammerException( |
|---|
| 519 |
'The method, %s(), requires at least one parameter', |
|---|
| 520 |
$method_name |
|---|
| 521 |
); |
|---|
| 522 |
} |
|---|
| 523 |
|
|---|
| 524 |
$value = $parameters[0]; |
|---|
| 525 |
|
|---|
| 526 |
fActiveRecord::assign($values, $old_values, $column, $value); |
|---|
| 527 |
|
|---|
| 528 |
$currency_column = self::$money_columns[$class][$column]; |
|---|
| 529 |
|
|---|
| 530 |
|
|---|
| 531 |
self::objectifyMoneyWithCurrency($values, $old_values, $column, $currency_column); |
|---|
| 532 |
|
|---|
| 533 |
if ($currency_column) { |
|---|
| 534 |
if ($value instanceof fMoney) { |
|---|
| 535 |
fActiveRecord::assign($values, $old_values, $currency_column, $value->getCurrency()); |
|---|
| 536 |
} |
|---|
| 537 |
} |
|---|
| 538 |
|
|---|
| 539 |
return $object; |
|---|
| 540 |
} |
|---|
| 541 |
|
|---|
| 542 |
|
|---|
| 543 |
|
|---|
| 544 |
|
|---|
| 545 |
|
|---|
| 546 |
@internal |
|---|
| 547 |
|
|---|
| 548 |
@param |
|---|
| 549 |
@param |
|---|
| 550 |
@param |
|---|
| 551 |
@param |
|---|
| 552 |
@param |
|---|
| 553 |
@param |
|---|
| 554 |
@return |
|---|
| 555 |
|
|---|
| 556 |
static public function validateMoneyColumns($object, &$values, &$old_values, &$related_records, &$cache, &$validation_messages) |
|---|
| 557 |
{ |
|---|
| 558 |
$class = get_class($object); |
|---|
| 559 |
|
|---|
| 560 |
if (empty(self::$money_columns[$class])) { |
|---|
| 561 |
return; |
|---|
| 562 |
} |
|---|
| 563 |
|
|---|
| 564 |
foreach (self::$money_columns[$class] as $column => $currency_column) { |
|---|
| 565 |
if ($values[$column] instanceof fMoney || $values[$column] === NULL) { |
|---|
| 566 |
continue; |
|---|
| 567 |
} |
|---|
| 568 |
|
|---|
| 569 |
|
|---|
| 570 |
unset($validation_messages[$column]); |
|---|
| 571 |
|
|---|
| 572 |
$column_name = fValidationException::formatField(fORM::getColumnName($class, $currency_column)); |
|---|
| 573 |
|
|---|
| 574 |
if ($currency_column && !in_array($values[$currency_column], fMoney::getCurrencies())) { |
|---|
| 575 |
$validation_messages[$column] = self::compose( |
|---|
| 576 |
'%sThe currency specified is invalid', |
|---|
| 577 |
$column_name |
|---|
| 578 |
); |
|---|
| 579 |
|
|---|
| 580 |
} else { |
|---|
| 581 |
$validation_messages[$column] = self::compose( |
|---|
| 582 |
'%sPlease enter a monetary value', |
|---|
| 583 |
$column_name |
|---|
| 584 |
); |
|---|
| 585 |
} |
|---|
| 586 |
} |
|---|
| 587 |
} |
|---|
| 588 |
|
|---|
| 589 |
|
|---|
| 590 |
|
|---|
| 591 |
|
|---|
| 592 |
|
|---|
| 593 |
@return |
|---|
| 594 |
|
|---|
| 595 |
private function __construct() { } |
|---|
| 596 |
} |
|---|
| 597 |
|
|---|
| 598 |
|
|---|
| 599 |
|
|---|
| 600 |
|
|---|
| 601 |
will@flourishlib.com |
|---|
| 602 |
|
|---|
| 603 |
|
|---|
| 604 |
|
|---|
| 605 |
|
|---|
| 606 |
|
|---|
| 607 |
|
|---|
| 608 |
|
|---|
| 609 |
|
|---|
| 610 |
|
|---|
| 611 |
|
|---|
| 612 |
|
|---|
| 613 |
|
|---|
| 614 |
|
|---|
| 615 |
|
|---|
| 616 |
|
|---|
| 617 |
|
|---|
| 618 |
|
|---|
| 619 |
|
|---|
| 620 |
|
|---|