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 abstract class Varien_Convert_Container_Abstract implements Varien_Convert_Container_Interface
00036 {
00037 protected $_vars;
00038 protected $_profile;
00039 protected $_data;
00040 protected $_position;
00041
00042 public function getVar($key, $default=null)
00043 {
00044 if (!isset($this->_vars[$key])) {
00045 return $default;
00046 }
00047 return $this->_vars[$key];
00048 }
00049
00050 public function getVars()
00051 {
00052 return $this->_vars;
00053 }
00054
00055 public function setVar($key, $value=null)
00056 {
00057 if (is_array($key) && is_null($value)) {
00058 $this->_vars = $key;
00059 } else {
00060 $this->_vars[$key] = $value;
00061 }
00062 return $this;
00063 }
00064
00065 public function getProfile()
00066 {
00067 return $this->_profile;
00068 }
00069
00070 public function setProfile(Varien_Convert_Profile_Abstract $profile)
00071 {
00072 $this->_profile = $profile;
00073 return $this;
00074 }
00075
00076 public function getData()
00077 {
00078 if (is_null($this->_data) && $this->getProfile()) {
00079 $this->_data = $this->getProfile()->getContainer()->getData();
00080 }
00081 return $this->_data;
00082 }
00083
00084 public function setData($data)
00085 {
00086 if ($this->getProfile()) {
00087 $this->getProfile()->getContainer()->setData($data);
00088 }
00089 $this->_data = $data;
00090 return $this;
00091 }
00092
00093 public function validateDataString($data=null)
00094 {
00095 if (is_null($data)) {
00096 $data = $this->getData();
00097 }
00098 if (!is_string($data)) {
00099 $this->addException("Invalid data type, expecting string.", Varien_Convert_Exception::FATAL);
00100 }
00101 return true;
00102 }
00103
00104 public function validateDataArray($data=null)
00105 {
00106 if (is_null($data)) {
00107 $data = $this->getData();
00108 }
00109 if (!is_array($data)) {
00110 $this->addException("Invalid data type, expecting array.", Varien_Convert_Exception::FATAL);
00111 }
00112 return true;
00113 }
00114
00115 public function validateDataGrid($data=null)
00116 {
00117 if (is_null($data)) {
00118 $data = $this->getData();
00119 }
00120 if (!is_array($data) || !is_array(current($data))) {
00121 if (count($data)==0) {
00122 return true;
00123 }
00124 $this->addException("Invalid data type, expecting 2D grid array.", Varien_Convert_Exception::FATAL);
00125 }
00126 return true;
00127 }
00128
00129 public function getGridFields($grid)
00130 {
00131 $fields = array();
00132 foreach ($grid as $i=>$row) {
00133 foreach ($row as $fieldName=>$data) {
00134 if (!in_array($fieldName, $fields)) {
00135 $fields[] = $fieldName;
00136 }
00137 }
00138 }
00139 return $fields;
00140 }
00141
00142 public function addException($error, $level=null)
00143 {
00144 $e = new Varien_Convert_Exception($error);
00145 $e->setLevel(!is_null($level) ? $level : Varien_Convert_Exception::NOTICE);
00146 $e->setContainer($this);
00147 $e->setPosition($this->getPosition());
00148
00149 if ($this->getProfile()) {
00150 $this->getProfile()->addException($e);
00151 }
00152
00153 return $e;
00154 }
00155
00156 public function getPosition()
00157 {
00158 return $this->_position;
00159 }
00160
00161 public function setPosition($position)
00162 {
00163 $this->_position = $position;
00164 return $this;
00165 }
00166 }