00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Varien_Object implements ArrayAccess
00036 {
00037
00038
00039
00040
00041
00042
00043 protected $_data = array();
00044
00045
00046
00047
00048
00049
00050 protected $_origData;
00051
00052
00053
00054
00055
00056
00057 protected $_idFieldName = null;
00058
00059
00060
00061
00062
00063
00064 protected static $_underscoreCache = array();
00065
00066 protected static $_camelizeCache = array();
00067
00068
00069
00070
00071
00072
00073 protected $_isDeleted = false;
00074
00075
00076
00077
00078
00079
00080
00081
00082 public function __construct()
00083 {
00084 $args = func_get_args();
00085 if (empty($args[0])) {
00086 $args[0] = array();
00087 }
00088 $this->_data = $args[0];
00089
00090 $this->_construct();
00091 }
00092
00093
00094
00095
00096
00097 protected function _construct()
00098 {
00099
00100 }
00101
00102
00103
00104
00105
00106
00107
00108 public function isDeleted($isDeleted=null)
00109 {
00110 $result = $this->_isDeleted;
00111 if (!is_null($isDeleted)) {
00112 $this->_isDeleted = $isDeleted;
00113 }
00114 return $result;
00115 }
00116
00117
00118
00119
00120
00121
00122
00123 public function setIdFieldName($name)
00124 {
00125 $this->_idFieldName = $name;
00126 return $this;
00127 }
00128
00129
00130
00131
00132
00133
00134
00135 public function getIdFieldName()
00136 {
00137 return $this->_idFieldName;
00138 }
00139
00140
00141
00142
00143
00144
00145 public function getId()
00146 {
00147 if ($this->getIdFieldName()) {
00148 return $this->getData($this->getIdFieldName());
00149 }
00150 return $this->getData('id');
00151 }
00152
00153
00154
00155
00156
00157
00158
00159 public function setId($value)
00160 {
00161 if ($this->getIdFieldName()) {
00162 $this->setData($this->getIdFieldName(), $value);
00163 }
00164 else {
00165 $this->setData('id', $value);
00166 }
00167 return $this;
00168 }
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178 public function addData(array $arr)
00179 {
00180 foreach($arr as $index=>$value) {
00181 $this->setData($index, $value);
00182 }
00183 return $this;
00184 }
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201 public function setData($key, $value=null)
00202 {
00203 if(is_array($key)) {
00204 $this->_data = $key;
00205 } else {
00206 $this->_data[$key] = $value;
00207 }
00208 return $this;
00209 }
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 public function unsetData($key=null)
00223 {
00224 if (is_null($key)) {
00225 $this->_data = array();
00226 } else {
00227 unset($this->_data[$key]);
00228 }
00229 return $this;
00230 }
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246 public function getData($key='', $index=null)
00247 {
00248 if (''===$key) {
00249 return $this->_data;
00250 }
00251
00252 $default = null;
00253
00254
00255 if (strpos($key,'/')) {
00256 $keyArr = explode('/', $key);
00257 $data = $this->_data;
00258 foreach ($keyArr as $i=>$k) {
00259 if ($k==='') {
00260 return $default;
00261 }
00262 if (is_array($data)) {
00263 if (!isset($data[$k])) {
00264 return $default;
00265 }
00266 $data = $data[$k];
00267 } elseif ($data instanceof Varien_Object) {
00268 $data = $data->getData($k);
00269 } else {
00270 return $default;
00271 }
00272 }
00273 return $data;
00274 }
00275
00276
00277 if (isset($this->_data[$key])) {
00278 if (is_null($index)) {
00279 return $this->_data[$key];
00280 }
00281
00282 $value = $this->_data[$key];
00283 if (is_array($value)) {
00284
00285
00286
00287
00288 if (isset($value[$index])) {
00289 return $value[$index];
00290 }
00291 return null;
00292 } elseif (is_string($value)) {
00293 $arr = explode("\n", $value);
00294 return (isset($arr[$index]) && (!empty($arr[$index]) || strlen($arr[$index]) > 0)) ? $arr[$index] : null;
00295 } elseif ($value instanceof Varien_Object) {
00296 return $value->getData($index);
00297 }
00298 return $default;
00299 }
00300 return $default;
00301 }
00302
00303
00304
00305
00306
00307
00308
00309 protected function _getData($key)
00310 {
00311 return isset($this->_data[$key]) ? $this->_data[$key] : null;
00312 }
00313
00314 public function setDataUsingMethod($key, $args=array())
00315 {
00316 $method = 'set'.$this->_camelize($key);
00317 $this->$method($args);
00318 return $this;
00319 }
00320
00321 public function getDataUsingMethod($key, $args=null)
00322 {
00323 $method = 'get'.$this->_camelize($key);
00324 return $this->$method($args);
00325 }
00326
00327
00328
00329
00330
00331
00332
00333
00334 public function getDataSetDefault($key, $default)
00335 {
00336 if (!isset($this->_data[$key])) {
00337 $this->_data[$key] = $default;
00338 }
00339 return $this->_data[$key];
00340 }
00341
00342
00343
00344
00345
00346
00347
00348
00349 public function hasData($key='')
00350 {
00351 if (empty($key) || !is_string($key)) {
00352 return !empty($this->_data);
00353 }
00354 return array_key_exists($key, $this->_data);
00355 }
00356
00357
00358
00359
00360
00361
00362
00363 public function __toArray(array $arrAttributes = array())
00364 {
00365 if (empty($arrAttributes)) {
00366 return $this->_data;
00367 }
00368
00369 $arrRes = array();
00370 foreach ($arrAttributes as $attribute) {
00371 if (isset($this->_data[$attribute])) {
00372 $arrRes[$attribute] = $this->_data[$attribute];
00373 }
00374 else {
00375 $arrRes[$attribute] = null;
00376 }
00377 }
00378 return $arrRes;
00379 }
00380
00381
00382
00383
00384
00385
00386
00387 public function toArray(array $arrAttributes = array())
00388 {
00389 return $this->__toArray($arrAttributes);
00390 }
00391
00392
00393
00394
00395
00396
00397
00398
00399 protected function _prepareArray(&$arr, array $elements=array())
00400 {
00401 foreach ($elements as $element) {
00402 if (!isset($arr[$element])) {
00403 $arr[$element] = null;
00404 }
00405 }
00406 return $arr;
00407 }
00408
00409
00410
00411
00412
00413
00414
00415
00416 protected function __toXml(array $arrAttributes = array(), $rootName = 'item', $addOpenTag=false, $addCdata=true)
00417 {
00418 $xml = '';
00419 if ($addOpenTag) {
00420 $xml.= '<?xml version="1.0" encoding="UTF-8"?>'."\n";
00421 }
00422 if (!empty($rootName)) {
00423 $xml.= '<'.$rootName.'>'."\n";
00424 }
00425 $xmlModel = new Varien_Simplexml_Element('<node></node>');
00426 $arrData = $this->toArray($arrAttributes);
00427 foreach ($arrData as $fieldName => $fieldValue) {
00428 if ($addCdata === true) {
00429 $fieldValue = "<![CDATA[$fieldValue]]>";
00430 } else {
00431 $fieldValue = $xmlModel->xmlentities($fieldValue);
00432 }
00433 $xml.= "<$fieldName>$fieldValue</$fieldName>"."\n";
00434 }
00435 if (!empty($rootName)) {
00436 $xml.= '</'.$rootName.'>'."\n";
00437 }
00438 return $xml;
00439 }
00440
00441
00442
00443
00444
00445
00446
00447
00448 public function toXml(array $arrAttributes = array(), $rootName = 'item', $addOpenTag=false, $addCdata=true)
00449 {
00450 return $this->__toXml($arrAttributes, $rootName, $addOpenTag, $addCdata);
00451 }
00452
00453
00454
00455
00456
00457
00458
00459 protected function __toJson(array $arrAttributes = array())
00460 {
00461 $arrData = $this->toArray($arrAttributes);
00462 $json = Zend_Json::encode($arrData);
00463 return $json;
00464 }
00465
00466
00467
00468
00469
00470
00471
00472 public function toJson(array $arrAttributes = array())
00473 {
00474 return $this->__toJson($arrAttributes);
00475 }
00476
00477
00478
00479
00480
00481
00482
00483
00484 public function __toString(array $arrAttributes = array(), $valueSeparator=',')
00485 {
00486 $arrData = $this->toArray($arrAttributes);
00487 return implode($valueSeparator, $arrData);
00488 }
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498 public function toString($format='')
00499 {
00500 if (empty($format)) {
00501 $str = implode(', ', $this->getData());
00502 } else {
00503 preg_match_all('/\{\{([a-z0-9_]+)\}\}/is', $format, $matches);
00504 foreach ($matches[1] as $var) {
00505 $format = str_replace('{{'.$var.'}}', $this->getData($var), $format);
00506 }
00507 $str = $format;
00508 }
00509 return $str;
00510 }
00511
00512
00513
00514
00515
00516
00517
00518
00519 public function __call($method, $args)
00520 {
00521 switch (substr($method, 0, 3)) {
00522 case 'get' :
00523
00524 $key = $this->_underscore(substr($method,3));
00525 $data = $this->getData($key, isset($args[0]) ? $args[0] : null);
00526
00527 return $data;
00528
00529 case 'set' :
00530
00531 $key = $this->_underscore(substr($method,3));
00532 $result = $this->setData($key, isset($args[0]) ? $args[0] : null);
00533
00534 return $result;
00535
00536 case 'uns' :
00537
00538 $key = $this->_underscore(substr($method,3));
00539 $result = $this->unsetData($key);
00540
00541 return $result;
00542
00543 case 'has' :
00544
00545 $key = $this->_underscore(substr($method,3));
00546
00547 return isset($this->_data[$key]);
00548 }
00549 throw new Varien_Exception("Invalid method ".get_class($this)."::".$method."(".print_r($args,1).")");
00550 }
00551
00552
00553
00554
00555
00556
00557
00558
00559 public function __get($var)
00560 {
00561 $var = $this->_underscore($var);
00562 return $this->getData($var);
00563 }
00564
00565
00566
00567
00568
00569
00570
00571 public function __set($var, $value)
00572 {
00573 $this->_isChanged = true;
00574 $var = $this->_underscore($var);
00575 $this->setData($var, $value);
00576 }
00577
00578
00579
00580
00581
00582
00583 public function isEmpty()
00584 {
00585 if (empty($this->_data)) {
00586 return true;
00587 }
00588 return false;
00589 }
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600 protected function _underscore($name)
00601 {
00602 if (isset(self::$_underscoreCache[$name])) {
00603 return self::$_underscoreCache[$name];
00604 }
00605 #Varien_Profiler::start('underscore');
00606 $result = strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", $name));
00607 #Varien_Profiler::stop('underscore');
00608 self::$_underscoreCache[$name] = $result;
00609 return $result;
00610 }
00611
00612 protected function _camelize($name)
00613 {
00614 return uc_words($name, '');
00615 }
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626 public function serialize($attributes = array(), $valueSeparator='=', $fieldSeparator=' ', $quote='"')
00627 {
00628 $res = '';
00629 $data = array();
00630 if (empty($attributes)) {
00631 $attributes = array_keys($this->_data);
00632 }
00633
00634 foreach ($this->_data as $key => $value) {
00635 if (in_array($key, $attributes)) {
00636 $data[] = $key . $valueSeparator . $quote . $value . $quote;
00637 }
00638 }
00639 $res = implode($fieldSeparator, $data);
00640 return $res;
00641 }
00642
00643
00644
00645
00646
00647
00648
00649 public function getOrigData($key=null)
00650 {
00651 if (is_null($key)) {
00652 return $this->_origData;
00653 }
00654 return isset($this->_origData[$key]) ? $this->_origData[$key] : null;
00655 }
00656
00657
00658
00659
00660
00661
00662
00663
00664 public function setOrigData($key=null, $data=null)
00665 {
00666 if (is_null($key)) {
00667 $this->_origData = $this->_data;
00668 } else {
00669 $this->_origData[$key] = $data;
00670 }
00671 return $this;
00672 }
00673
00674
00675
00676
00677
00678
00679
00680 public function dataHasChangedFor($field)
00681 {
00682 $newData = $this->getData($field);
00683 $origData = $this->getOrigData($field);
00684 return $newData!=$origData;
00685 }
00686
00687
00688
00689
00690
00691
00692
00693 public function isDirty($field=null)
00694 {
00695 if (empty($this->_dirty)) {
00696 return false;
00697 }
00698 if (is_null($field)) {
00699 return true;
00700 }
00701 return isset($this->_dirty[$field]);
00702 }
00703
00704
00705
00706
00707
00708
00709
00710
00711 public function flagDirty($field, $flag=true)
00712 {
00713 if (is_null($field)) {
00714 foreach ($this->getData() as $field=>$value) {
00715 $this->flagDirty($field, $flag);
00716 }
00717 } else {
00718 if ($flag) {
00719 $this->_dirty[$field] = true;
00720 } else {
00721 unset($this->_dirty[$field]);
00722 }
00723 }
00724 return $this;
00725 }
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737 public function debug($data=null, &$objects=array())
00738 {
00739 if (is_null($data)) {
00740 $hash = spl_object_hash($this);
00741 if (!empty($objects[$hash])) {
00742 return '*** RECURSION ***';
00743 }
00744 $objects[$hash] = true;
00745 $data = $this->getData();
00746 }
00747 $debug = array();
00748 foreach ($data as $key=>$value) {
00749 if (is_scalar($value)) {
00750 $debug[$key] = $value;
00751 } elseif (is_array($value)) {
00752 $debug[$key] = $this->debug($value, $objects);
00753 } elseif ($value instanceof Varien_Object) {
00754 $debug[$key.' ('.get_class($value).')'] = $value->debug(null, $objects);
00755 }
00756 }
00757 return $debug;
00758 }
00759
00760
00761
00762
00763
00764
00765
00766
00767 public function offsetSet($offset, $value)
00768 {
00769 $this->_data[$offset] = $value;
00770 }
00771
00772
00773
00774
00775
00776
00777
00778
00779 public function offsetExists($offset)
00780 {
00781 return isset($this->_data[$offset]);
00782 }
00783
00784
00785
00786
00787
00788
00789
00790 public function offsetUnset($offset)
00791 {
00792 unset($this->_data[$offset]);
00793 }
00794
00795
00796
00797
00798
00799
00800
00801
00802 public function offsetGet($offset)
00803 {
00804 return isset($this->_data[$offset]) ? $this->_data[$offset] : null;
00805 }
00806
00807 }