PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/beta/datars.class.php

https://github.com/mcgivrer/Games
PHP | 280 lines | 210 code | 15 blank | 55 comment | 41 complexity | 1254b2dea467f3b16ef9a212a5859134 MD5 | raw file
  1. <?php
  2. class DataRS extends TData implements IData{
  3. private static $_indexes=array();
  4. public function Data(){
  5. $id=1;
  6. __debug("parse data directory","Data",__CLASS__);
  7. $rep=opendir(dirname(__FILE__)."/../data/");
  8. while($filename=readdir($rep)){
  9. if(
  10. $filename!=".." &&
  11. $filename!="." &&
  12. (strstr($filename,".txt")!=false || strstr($filename,".xml")!=false)
  13. ){
  14. // Extract entity name ($type) and file extension
  15. $expltype = explode('s.',$filename);
  16. $entityName = $expltype[0];
  17. $fileExtension =$expltype[1];
  18. self::$_data[$entityName]=array();
  19. switch ($fileExtension){
  20. case "txt":
  21. $this->parseTxtFile($filename,$entityName,$id);
  22. break;
  23. case "xml":
  24. $this->parseXmlFile($filename,$entityName,$id);
  25. break;
  26. default:
  27. throw new Exception("File type '".$fileExtension."' not known.");
  28. }
  29. }
  30. }
  31. }
  32. /**
  33. * Parse a text file name with EntityName.txt
  34. * @param $filename : the name of the file to read
  35. * @param $entityName : name of entity to be instanciate with data.
  36. **/
  37. private function parseTxtFile($filename, $entityName, $id=0){
  38. $entityUID=0;
  39. $file = @fopen(dirname(__FILE__)."/../data/".$filename,"r");
  40. if ($file) {
  41. __debug("find '$filename', generate entity(ies) for this file data","Data",__CLASS__);
  42. $line=0;
  43. while (!feof($file)) {
  44. $buffer = fgets($file);
  45. $line++;
  46. //__debug("retrieving data :". print_r($buffer,true),"Data",__CLASS__);
  47. if($line==1){
  48. $datamapping = preg_split("/[\s]*[|][\s]*/",$buffer);
  49. }else if($buffer!="" && !empty($buffer) && !strstr($buffer,"END")){
  50. $entityUID++;
  51. $data=preg_split("/[\s]*[|][\s]*/",$buffer);
  52. if(isset($data) && $data[0]!="END"){
  53. //__debug("Type identified : ".print_r($type,true),"Data",__CLASS__);
  54. $entity = new $entityName();
  55. $entity->$entityName($id);
  56. $entity->load($id, $data,$datamapping);
  57. self::$_data["".$entityName]["".$entityUID]= $entity;
  58. self::$_indexes[$id]=array($entityName,"".$entityUID);
  59. //__debug("Entity created for key (".self::$_indexes[$id][0]."/".self::$_indexes[$id][1]."):[".print_r($entity,true)."]","Data",__CLASS__);
  60. }
  61. $id++;
  62. }
  63. }
  64. fclose($file);
  65. }
  66. }
  67. /**
  68. * @see IData#getDataById($id)
  69. */
  70. public function getDataById($id){
  71. __debug("retrieve data for id:".$id,"getDataById",__CLASS__);
  72. $keys = self::$_indexes[$id];
  73. //echo "keys:(".print_r($keys,true).")<br/>";
  74. $data = $this->getData($keys[0],$keys[1]);
  75. //echo "retrieved:[".print_r($mydata,true)."]<br/>";
  76. return $data;
  77. }
  78. /**
  79. * @see IData#getData($entityName,$entityUID)
  80. */
  81. public function getData($entityName="",$entityUID="",$options=null){
  82. __debug("GetData for ($entityName/$entityUID)","getData",__CLASS__);
  83. if($entityName=="" && $entityUID==""){
  84. $ret= self::$_data;
  85. }elseif($entityName != "" && $entityUID==""){
  86. $ret= self::$_data["".$entityName];
  87. }elseif($entityName != "" && $entityUID != ""){
  88. $ret= self::$_data["".$entityName]["".$entityUID];
  89. }else{
  90. $ret=null;
  91. }
  92. $ret = $this->transformToRS($ret);
  93. if(is_array($ret)){
  94. $ret = $this->manageOptions($ret, $options);
  95. }
  96. __debug("returned data:[count=".count($ret)."]","getData",__CLASS__);
  97. return $ret;
  98. }
  99. private function transformToRS($ret) {
  100. $rs = new ResultSet();
  101. foreach($ret as $item){
  102. $rs->add($item);
  103. }
  104. return $rs;
  105. }
  106. /**
  107. * Manage Options on data retrieving,like 'limit',...
  108. * If 'limit' is set in the <code>$options</code> array, slice array for limit value.
  109. * @param array $items to be optionized.
  110. * @param array $options list of options to evaluate.
  111. */
  112. private function manageOptions($items, $options){
  113. // calculate number of items
  114. $count = $items->getCount();
  115. $itemsOptionized = $items->getData();
  116. // manager options
  117. //print_r($options);
  118. if($options!=null){
  119. // If limit is set, slice array for limit value.
  120. if(isset($options['limit'])){
  121. __debug("limit=".$options['limit'],__METHOD__,__CLASS__);
  122. //echo 'limit:'.$options['limit'];
  123. $arrayOffset =(isset($items['array_offset'])?$items['array_offset']:0);
  124. if($count>$options['limit']){
  125. $itemsOptionized = array_slice($items-getData(), 0, $options['limit'],true);
  126. }
  127. $itemsOptionized ->setMeta('offset', $arrayOffset);
  128. $itemsOptionized ->setMeta('pagesize', $options['limit']);
  129. }
  130. }
  131. $itemsOptionized ->setMeta('count',$count);
  132. print_r($itemsOptionized);
  133. return $itemsOptionized;
  134. }
  135. /**
  136. * @see IData#getDataDistinct($entityName,$distinctOnAttribute,$attributes=null)
  137. */
  138. public function getDataListDistinct(
  139. $entityName,
  140. $distinctOnAttribute,
  141. $attributes=null,
  142. $options=null){
  143. $ret=array();
  144. foreach(self::$_data[$entityName] as $entity){
  145. if(!array_key_exists("".$entity->getInfo($distinctOnAttribute),$ret)){
  146. if($attributes==null){
  147. $ret["".$entity->getInfo($distinctOnAttribute)]=$entity;
  148. }else{
  149. $retItem=array();
  150. foreach($attributes as $attribute){
  151. $retItem[$attribute] = "".$entity->getInfo($attribute);
  152. }
  153. $ret["".$entity->getInfo($distinctOnAttribute)]=$retItem;
  154. }
  155. }
  156. }
  157. //echo "<pre>data=".print_r($ret,true)."</pre>";
  158. if($attributes==null){
  159. uasort($ret,array($entityName,'compare'));
  160. }
  161. $ret= $this->transformToRS($ret);
  162. if($options!=null){
  163. $ret = $this->manageOptions($ret, $options);
  164. }
  165. return $ret;
  166. }
  167. /**
  168. * Prepare Array containing all condition to verify.
  169. * @param $conditions
  170. */
  171. private function analyzeConditions($conditions=""){
  172. $conditionsArray = array();
  173. //split string on ',' to separate conditions.
  174. if($conditions!=""){
  175. $conditionsItems=explode(",",$conditions);
  176. __debug("conditions item:[".print_r($conditionsItems,true)."]","analyzeConditions",__CLASS__);
  177. foreach($conditionsItems as $conditionItem){
  178. $split = preg_split('/([^\*]+)+([\<|\>|\=|\!\=])([^\*]+)/',$conditionItem,5,PREG_SPLIT_DELIM_CAPTURE);
  179. $conditionsArray[]=array('attribute'=>$split[1],'comparator'=>$split[2],'value'=>$split[3]);
  180. }
  181. }
  182. __debug("conditions array:[".print_r($conditionsArray,true)."]","analyzeConditions",__CLASS__);
  183. return $conditionsArray;
  184. }
  185. /**
  186. * Verify if a list of condition for the $item.
  187. * @param object $item
  188. * @param array $conditions Array of string containing condition.
  189. */
  190. private function isConditionArrayTrue($item, $conditionsArray){
  191. if(count($conditionsArray)==0){
  192. return true;
  193. }else{
  194. foreach($conditionsArray as $condition){
  195. //__debug("item:".print_r($item,true),"isConditionArrayTrue",__CLASS__);
  196. //__debug("condition:".print_r($condition,true),"isConditionArrayTrue",__CLASS__);
  197. //echo "<pre>".print_r($item,true)."</pre>";
  198. switch(trim($condition['comparator'])){
  199. case "=" :
  200. if(trim($item->getAttribute($condition['attribute']))==trim($condition['value'])){
  201. __debug("condition true for item(".$item->getAttribute('id').")","isConditionArrayTrue",__CLASS__);
  202. return true;
  203. }
  204. break;
  205. case "<":
  206. if($item->getAttribute($condition['attribute'])>$condition['value']){
  207. return true;
  208. }
  209. break;
  210. case ">":
  211. if($item->getAttribute($condition['attribute'])<$condition['value']){
  212. return true;
  213. }
  214. break;
  215. }
  216. }
  217. return false;
  218. }
  219. }
  220. /**
  221. * Verify condition for one item.
  222. * @param unknown_type $conditions
  223. */
  224. private function verifyConditions($item, $conditions){
  225. return $this->isConditionTrue($item,$this->analyzeConditions($conditions));
  226. }
  227. /**
  228. * Return an array of <code>$type</code> entity extract, with filtered list of <code>$attributes</code>
  229. * @param string $type
  230. * @param array of string $attributes list of attributes to be extracted from objects.
  231. */
  232. public function getDataFiltered($entityName,$attributes=null,$conditions="",$options=null){
  233. __debug("retrieve all instance of $entityName and generate array with [".print_r(($attributes!=null?implode(',',$attributes):"null"),true)."]","getDataFiltered",__CLASS__);
  234. $ret=array();
  235. $conditionsArray = array();
  236. if($conditions!=""){
  237. $conditionsArray = $this->analyzeConditions($conditions);
  238. }
  239. foreach(self::$_data[$entityName] as $key=>$item){
  240. $retitem = array();
  241. if($this->isConditionArrayTrue($item,$conditionsArray)){
  242. //echo "<strong>item $key:</strong>[".print_r($item,true)."]<br/>";
  243. if(is_array($attributes)){
  244. __debug("Add item(".$item->getAttribute('id').") as array of attributes[$attributes]","getDataFiltered",__CLASS__);
  245. foreach($attributes as $attribute){
  246. $retitem[$attribute] = "".$item->getInfo($attribute);
  247. }
  248. }else{
  249. __debug("Add item(".$item->getAttribute['id'].") as object $entityname","getDataFiltered",__CLASS__);
  250. $retitem = $item;
  251. }
  252. $ret[$key]=$retitem;
  253. }
  254. }
  255. $ret = $this->transformToRS($ret);
  256. if($options!=null){
  257. $ret = $this->manageOptions($ret, $options);
  258. }
  259. return $ret;
  260. }
  261. public static function getInstance(){
  262. return parent::getSingletonInstance(__CLASS__);
  263. }
  264. }
  265. ?>