Public Member Functions | |
getEncryptor () | |
formatCurrency ($value, $includeContainer=true) | |
formatPrice ($price, $includeContainer=true) | |
formatDate ($date=null, $format='short', $showTime=false) | |
formatTime ($time=null, $format='short', $showDate=false) | |
encrypt ($data) | |
decrypt ($data) | |
validateKey ($key) | |
getRandomString ($len, $chars=null) | |
getHash ($password, $salt=false) | |
validateHash ($password, $hash) | |
getStoreId ($store=null) | |
removeAccents ($string, $german=false) | |
isDevAllowed ($storeId=null) | |
getCacheTypes () | |
getCacheBetaTypes () | |
copyFieldset ($fieldset, $aspect, $source, $target, $root='global') | |
decorateArray ($array, $prefix= 'decorated_', $forceSetAll=false) | |
assocToXml (array $array, $rootName= '_') | |
xmlToAssoc (SimpleXMLElement $xml) | |
Static Public Member Functions | |
static | currency ($value, $format=true, $includeContainer=true) |
Protected Attributes | |
$_encryptor = null |
Definition at line 32 of file Data.php.
assocToXml | ( | array $ | array, | |
$ | rootName = '_' | |||
) |
Transform an assoc array to SimpleXMLElement object Array has some limitations. Appropriate exceptions will be thrown
array | $array | |
string | $rootName |
Exception |
Definition at line 469 of file Data.php.
00470 { 00471 if (empty($rootName) || is_numeric($rootName)) { 00472 throw new Exception('Root element must not be empty or numeric'); 00473 } 00474 00475 $xmlstr = <<<XML 00476 <?xml version='1.0' encoding='UTF-8' standalone='yes'?> 00477 <$rootName></$rootName> 00478 XML; 00479 $xml = new SimpleXMLElement($xmlstr); 00480 foreach ($array as $key => $value) { 00481 if (is_numeric($key)) { 00482 throw new Exception('Array root keys must not be numeric.'); 00483 } 00484 } 00485 return self::_assocToXml($array, $rootName, $xml); 00486 }
copyFieldset | ( | $ | fieldset, | |
$ | aspect, | |||
$ | source, | |||
$ | target, | |||
$ | root = 'global' | |||
) |
Copy data from object|array to object|array containing fields from fieldset matching an aspect.
Contents of $aspect are a field name in target object or array. If '*' - will be used the same name as in the source object or array.
string | $fieldset | |
string | $aspect | |
array|Varien_Object | $source | |
array|Varien_Object | $target | |
string | $root |
Definition at line 342 of file Data.php.
00343 { 00344 if (!(is_array($source) || $source instanceof Varien_Object) 00345 || !(is_array($target) || $target instanceof Varien_Object)) { 00346 00347 return false; 00348 } 00349 $fields = Mage::getConfig()->getFieldset($fieldset, $root); 00350 if (!$fields) { 00351 return false; 00352 } 00353 00354 $sourceIsArray = is_array($source); 00355 $targetIsArray = is_array($target); 00356 00357 $result = false; 00358 foreach ($fields as $code=>$node) { 00359 if (empty($node->$aspect)) { 00360 continue; 00361 } 00362 00363 if ($sourceIsArray) { 00364 $value = isset($source[$code]) ? $source[$code] : null; 00365 } else { 00366 $value = $source->getDataUsingMethod($code); 00367 } 00368 00369 $targetCode = (string)$node->$aspect; 00370 $targetCode = $targetCode == '*' ? $code : $targetCode; 00371 00372 if ($targetIsArray) { 00373 $target[$targetCode] = $value; 00374 } else { 00375 $target->setDataUsingMethod($targetCode, $value); 00376 } 00377 00378 $result = true; 00379 } 00380 00381 return $result; 00382 }
Convert and format price value for current application store
float | $value | |
bool | $format | |
bool | $includeContainer |
Definition at line 65 of file Data.php.
00066 { 00067 try { 00068 $value = Mage::app()->getStore()->convertPrice($value, $format, $includeContainer); 00069 } 00070 catch (Exception $e){ 00071 $value = $e->getMessage(); 00072 } 00073 return $value; 00074 }
decorateArray | ( | $ | array, | |
$ | prefix = 'decorated_' , |
|||
$ | forceSetAll = false | |||
) |
Decorate a plain array of arrays or objects The array actually can be an object with Iterator interface
Keys with prefix_* will be set: *_is_first - if the element is first *_is_odd / *_is_even - for odd/even elements *_is_last - if the element is last
The respective key/attribute will be set to element, depending on object it is or array. Varien_Object is supported.
$forceSetAll true will cause to set all possible values for all elements. When false (default), only non-empty values will be set.
mixed | $array | |
string | $prefix | |
bool | $forceSetAll |
Definition at line 404 of file Data.php.
00405 { 00406 // check if array or an object to be iterated given 00407 if (!(is_array($array) || is_object($array))) { 00408 return $array; 00409 } 00410 00411 $keyIsFirst = "{$prefix}is_first"; 00412 $keyIsOdd = "{$prefix}is_odd"; 00413 $keyIsEven = "{$prefix}is_even"; 00414 $keyIsLast = "{$prefix}is_last"; 00415 00416 $count = count($array); // this will force Iterator to load 00417 $i = 0; 00418 $isEven = false; 00419 foreach ($array as $key => $element) { 00420 if (is_object($element)) { 00421 $this->_decorateArrayObject($element, $keyIsFirst, (0 === $i), $forceSetAll || (0 === $i)); 00422 $this->_decorateArrayObject($element, $keyIsOdd, !$isEven, $forceSetAll || !$isEven); 00423 $this->_decorateArrayObject($element, $keyIsEven, $isEven, $forceSetAll || $isEven); 00424 $isEven = !$isEven; 00425 $i++; 00426 $this->_decorateArrayObject($element, $keyIsLast, ($i === $count), $forceSetAll || ($i === $count)); 00427 } 00428 elseif (is_array($element)) { 00429 if ($forceSetAll || (0 === $i)) { 00430 $array[$key][$keyIsFirst] = (0 === $i); 00431 } 00432 if ($forceSetAll || !$isEven) { 00433 $array[$key][$keyIsOdd] = !$isEven; 00434 } 00435 if ($forceSetAll || $isEven) { 00436 $array[$key][$keyIsEven] = $isEven; 00437 } 00438 $isEven = !$isEven; 00439 $i++; 00440 if ($forceSetAll || ($i === $count)) { 00441 $array[$key][$keyIsLast] = ($i === $count); 00442 } 00443 } 00444 } 00445 00446 return $array; 00447 }
decrypt | ( | $ | data | ) |
Decrypt data using application key
string | $data |
Definition at line 193 of file Data.php.
00194 { 00195 if (!Mage::isInstalled()) { 00196 return $data; 00197 } 00198 return $this->getEncryptor()->decrypt($data); 00199 }
encrypt | ( | $ | data | ) |
Encrypt data using application key
string | $data |
Definition at line 179 of file Data.php.
00180 { 00181 if (!Mage::isInstalled()) { 00182 return $data; 00183 } 00184 return $this->getEncryptor()->encrypt($data); 00185 }
formatCurrency | ( | $ | value, | |
$ | includeContainer = true | |||
) |
formatDate | ( | $ | date = null , |
|
$ | format = 'short' , |
|||
$ | showTime = false | |||
) |
Format date using current locale options
date|Zend_Date|null | $date in GMT timezone | |
string | $format | |
bool | $showTime |
Definition at line 108 of file Data.php.
00109 { 00110 if (Mage_Core_Model_Locale::FORMAT_TYPE_FULL !==$format && 00111 Mage_Core_Model_Locale::FORMAT_TYPE_LONG !==$format && 00112 Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM !==$format && 00113 Mage_Core_Model_Locale::FORMAT_TYPE_SHORT !==$format) { 00114 return $date; 00115 } 00116 if (!($date instanceof Zend_Date) && $date && !strtotime($date)) { 00117 return ''; 00118 } 00119 if (is_null($date)) { 00120 $date = Mage::app()->getLocale()->date(Mage::getSingleton('core/date')->gmtTimestamp(), null, null); 00121 } 00122 elseif (!$date instanceof Zend_Date) { 00123 $date = Mage::app()->getLocale()->date(strtotime($date), null, null, $showTime); 00124 } 00125 00126 if ($showTime) { 00127 $format = Mage::app()->getLocale()->getDateTimeFormat($format); 00128 } 00129 else { 00130 $format = Mage::app()->getLocale()->getDateFormat($format); 00131 } 00132 00133 return $date->toString($format); 00134 }
formatPrice | ( | $ | price, | |
$ | includeContainer = true | |||
) |
formatTime | ( | $ | time = null , |
|
$ | format = 'short' , |
|||
$ | showDate = false | |||
) |
Format time using current locale options
date|Zend_Date|null | $time | |
string | $format | |
bool | $showTime |
Definition at line 144 of file Data.php.
00145 { 00146 if (Mage_Core_Model_Locale::FORMAT_TYPE_FULL !==$format && 00147 Mage_Core_Model_Locale::FORMAT_TYPE_LONG !==$format && 00148 Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM !==$format && 00149 Mage_Core_Model_Locale::FORMAT_TYPE_SHORT !==$format) { 00150 return $time; 00151 } 00152 00153 if (is_null($time)) { 00154 $date = Mage::app()->getLocale()->date(time()); 00155 } 00156 elseif ($time instanceof Zend_Date) { 00157 $date = $time; 00158 } 00159 else { 00160 $date = Mage::app()->getLocale()->date(strtotime($time)); 00161 } 00162 00163 if ($showDate) { 00164 $format = Mage::app()->getLocale()->getDateTimeFormat($format); 00165 } 00166 else { 00167 $format = Mage::app()->getLocale()->getTimeFormat($format); 00168 } 00169 00170 return $date->toString($format); 00171 }
getCacheBetaTypes | ( | ) |
Get information about available cache beta types
Definition at line 318 of file Data.php.
00319 { 00320 $types = array(); 00321 $config = Mage::getConfig()->getNode('global/cache/betatypes'); 00322 foreach ($config->children() as $type=>$node) { 00323 $types[$type] = (string)$node->label; 00324 } 00325 return $types; 00326 }
getCacheTypes | ( | ) |
Get information about available cache types
Definition at line 303 of file Data.php.
00304 { 00305 $types = array(); 00306 $config = Mage::getConfig()->getNode('global/cache/types'); 00307 foreach ($config->children() as $type=>$node) { 00308 $types[$type] = (string)$node->label; 00309 } 00310 return $types; 00311 }
getEncryptor | ( | ) |
Definition at line 42 of file Data.php.
00043 { 00044 if ($this->_encryptor === null) { 00045 $encryptionModel = (string)Mage::getConfig()->getNode('global/helpers/core/encryption_model'); 00046 if ($encryptionModel) { 00047 $this->_encryptor = new $encryptionModel; 00048 } else { 00049 $this->_encryptor = Mage::getModel('core/encryption'); 00050 } 00051 00052 $this->_encryptor->setHelper($this); 00053 } 00054 return $this->_encryptor; 00055 }
getHash | ( | $ | password, | |
$ | salt = false | |||
) |
Generate salted hash from password
string | $password | |
string|integer|boolean | $salt |
Definition at line 224 of file Data.php.
00225 { 00226 return $this->getEncryptor()->getHash($password, $salt); 00227 }
getRandomString | ( | $ | len, | |
$ | chars = null | |||
) |
Definition at line 206 of file Data.php.
00207 { 00208 if (is_null($chars)) { 00209 $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 00210 } 00211 mt_srand(10000000*(double)microtime()); 00212 for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++) { 00213 $str .= $chars[mt_rand(0, $lc)]; 00214 } 00215 return $str; 00216 }
getStoreId | ( | $ | store = null |
) |
isDevAllowed | ( | $ | storeId = null |
) |
Definition at line 282 of file Data.php.
00283 { 00284 $allow = true; 00285 00286 $allowedIps = Mage::getStoreConfig('dev/restrict/allow_ips', $storeId); 00287 if (!empty($allowedIps) && isset($_SERVER['REMOTE_ADDR'])) { 00288 $allowedIps = preg_split('#\s*,\s*#', $allowedIps, null, PREG_SPLIT_NO_EMPTY); 00289 if (array_search($_SERVER['REMOTE_ADDR'], $allowedIps)===false 00290 && array_search($_SERVER['HTTP_HOST'], $allowedIps)===false) { 00291 $allow = false; 00292 } 00293 } 00294 00295 return $allow; 00296 }
removeAccents | ( | $ | string, | |
$ | german = false | |||
) |
Definition at line 245 of file Data.php.
00246 { 00247 static $replacements; 00248 00249 if (empty($replacements[$german])) { 00250 $subst = array( 00251 // single ISO-8859-1 letters 00252 192=>'A', 193=>'A', 194=>'A', 195=>'A', 196=>'A', 197=>'A', 199=>'C', 208=>'D', 200=>'E', 201=>'E', 202=>'E', 203=>'E', 204=>'I', 205=>'I', 206=>'I', 207=>'I', 209=>'N', 210=>'O', 211=>'O', 212=>'O', 213=>'O', 214=>'O', 216=>'O', 138=>'S', 217=>'U', 218=>'U', 219=>'U', 220=>'U', 221=>'Y', 142=>'Z', 224=>'a', 225=>'a', 226=>'a', 227=>'a', 228=>'a', 229=>'a', 231=>'c', 232=>'e', 233=>'e', 234=>'e', 235=>'e', 236=>'i', 237=>'i', 238=>'i', 239=>'i', 241=>'n', 240=>'o', 242=>'o', 243=>'o', 244=>'o', 245=>'o', 246=>'o', 248=>'o', 154=>'s', 249=>'u', 250=>'u', 251=>'u', 252=>'u', 253=>'y', 255=>'y', 158=>'z', 00253 // HTML entities 00254 258=>'A', 260=>'A', 262=>'C', 268=>'C', 270=>'D', 272=>'D', 280=>'E', 282=>'E', 286=>'G', 304=>'I', 313=>'L', 317=>'L', 321=>'L', 323=>'N', 327=>'N', 336=>'O', 340=>'R', 344=>'R', 346=>'S', 350=>'S', 354=>'T', 356=>'T', 366=>'U', 368=>'U', 377=>'Z', 379=>'Z', 259=>'a', 261=>'a', 263=>'c', 269=>'c', 271=>'d', 273=>'d', 281=>'e', 283=>'e', 287=>'g', 305=>'i', 322=>'l', 314=>'l', 318=>'l', 324=>'n', 328=>'n', 337=>'o', 341=>'r', 345=>'r', 347=>'s', 351=>'s', 357=>'t', 355=>'t', 367=>'u', 369=>'u', 378=>'z', 380=>'z', 00255 // ligatures 00256 198=>'Ae', 230=>'ae', 140=>'Oe', 156=>'oe', 223=>'ss', 00257 ); 00258 00259 if ($german) { 00260 // umlauts 00261 $subst = array_merge($subst, array(196=>'Ae', 228=>'ae', 214=>'Oe', 246=>'oe', 220=>'Ue', 252=>'ue')); 00262 } 00263 00264 $replacements[$german] = array(); 00265 foreach ($subst as $k=>$v) { 00266 $replacements[$german][$k<256 ? chr($k) : '&#'.$k.';'] = $v; 00267 } 00268 } 00269 00270 // convert string from default database format (UTF-8) 00271 // to encoding which replacement arrays made with (ISO-8859-1) 00272 if ($s = @iconv('UTF-8', 'ISO-8859-1', $string)) { 00273 $string = $s; 00274 } 00275 00276 // Replace 00277 $string = strtr($string, $replacements[$german]); 00278 00279 return $string; 00280 }
validateHash | ( | $ | password, | |
$ | hash | |||
) |
Definition at line 229 of file Data.php.
00230 { 00231 return $this->getEncryptor()->validateHash($password, $hash); 00232 }
validateKey | ( | $ | key | ) |
Definition at line 201 of file Data.php.
00202 { 00203 return $this->getEncryptor()->validateKey($key); 00204 }
xmlToAssoc | ( | SimpleXMLElement $ | xml | ) |
Transform SimpleXMLElement to associative array SimpleXMLElement must be conform structure, generated by assocToXml()
SimpleXMLElement | $xml |
Definition at line 532 of file Data.php.
00533 { 00534 $array = array(); 00535 foreach ($xml as $key => $value) { 00536 if (isset($value->$key)) { 00537 $i = 0; 00538 foreach ($value->$key as $v) { 00539 $array[$key][$i++] = (string)$v; 00540 } 00541 } 00542 else { 00543 // try to transform it into string value, trimming spaces between elements 00544 $array[$key] = trim((string)$value); 00545 if (empty($array[$key]) && !empty($value)) { 00546 $array[$key] = self::xmlToAssoc($value); 00547 } 00548 // untrim strings values 00549 else { 00550 $array[$key] = (string)$value; 00551 } 00552 } 00553 } 00554 return $array; 00555 }