PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/utils/class.atkstringparser.inc

https://github.com/ibuildingsnl/ATK
PHP | 242 lines | 148 code | 20 blank | 74 comment | 23 complexity | 469f89f77338bc8a6adb01c8d2b28817 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, LGPL-3.0
  1. <?php
  2. /**
  3. * This file is part of the Achievo ATK distribution.
  4. * Detailed copyright and licensing information can be found
  5. * in the doc/COPYRIGHT and doc/LICENSE files which should be
  6. * included in the distribution.
  7. *
  8. * @package atk
  9. * @subpackage utils
  10. *
  11. * @copyright (c)2000-2004 Ibuildings.nl BV
  12. * @license http://www.achievo.org/atk/licensing ATK Open Source License
  13. *
  14. * @version $Revision: 6320 $
  15. * $Id$
  16. */
  17. /**
  18. * Generic string parser.
  19. *
  20. * @author Ivo Jansch <ivo@achievo.org>
  21. * @package atk
  22. * @subpackage utils
  23. *
  24. */
  25. class atkStringParser
  26. {
  27. var $m_fields = array();
  28. var $m_string = "";
  29. /**
  30. * Create a new stringparser
  31. *
  32. * @param string $string The string to parse
  33. */
  34. function atkStringParser($string)
  35. {
  36. $this->m_string = $string;
  37. }
  38. /**
  39. * Parse data into the string.
  40. *
  41. * @param array $data The data to parse in the string
  42. * @param bool $encode Wether or not to do a rawurlencode
  43. * @param bool $ignoreUnknownFields Replace unknown fields with an empty string,
  44. * if set to false unknown fields will be left
  45. * untouched.
  46. *
  47. * @return String The parsed string
  48. */
  49. function parse($data, $encode=false, $replaceUnknownFields=true)
  50. {
  51. $string = $this->m_string;
  52. $fields = $this->getFields();
  53. foreach ($fields as $field)
  54. {
  55. $value = $data;
  56. $elements = explode(".", $field);
  57. foreach ($elements as $i => $el)
  58. {
  59. if (is_array($value) || $value instanceof ArrayAccess)
  60. {
  61. if (isset($value[$el]))
  62. {
  63. $value = $value[$el];
  64. }
  65. else if ($replaceUnknownFields)
  66. {
  67. atknotice("atkStringparser({$this->m_string})->parse(): Attempting to get element '{$el}', but {$elements[$i - 1]} is not an array!");
  68. $value = '';
  69. break;
  70. }
  71. else
  72. {
  73. // field not found, continue with next field without
  74. // replacing the field in the template
  75. continue 2;
  76. }
  77. }
  78. else if (!$replaceUnknownFields)
  79. {
  80. // field not found, continue with next field without
  81. // replacing the field in the template
  82. continue 2;
  83. }
  84. }
  85. if ($encode)
  86. {
  87. $value = rawurlencode($value);
  88. }
  89. $string = str_replace("[".$field."]", $value, $string);
  90. }
  91. return $string;
  92. }
  93. /**
  94. * Does the data contains everything needed to be parsed into the string?
  95. *
  96. * @param array $data
  97. */
  98. function isComplete($data)
  99. {
  100. $fields = $this->getFields();
  101. for ($i=0;$i<count($fields);$i++)
  102. {
  103. $elements = explode(".",$fields[$i]);
  104. $databin = $data;
  105. for($j=0;$j<count($elements);$j++)
  106. {
  107. $value = $databin[$elements[$j]];
  108. if (!isset($value)) return false; // Missing value.
  109. $databin = $databin[$elements[$j]];
  110. }
  111. if (!isset($value)) return false; // Missing value.
  112. }
  113. return true;
  114. }
  115. /**
  116. * Get the [ ] Fields out of a String
  117. */
  118. function getFields()
  119. {
  120. if (!count($this->m_fields))
  121. {
  122. $tmp = "";
  123. $adding = false;
  124. $strlen = strlen($this->m_string);
  125. for ($i=0;$i<$strlen;$i++)
  126. {
  127. if ($this->m_string[$i]=="]")
  128. {
  129. $adding = false;
  130. $this->m_fields[] = $tmp;
  131. $tmp="";
  132. }
  133. else if ($this->m_string[$i]=="[")
  134. {
  135. $adding = true;
  136. }
  137. else
  138. {
  139. if ($adding) $tmp.=$this->m_string[$i];
  140. }
  141. }
  142. }
  143. return $this->m_fields;
  144. }
  145. /**
  146. * Get all fields from a string
  147. *
  148. * <b>Example:</b>
  149. * string: [firstname], [lastname] [city]
  150. * would return array('[firstname]',', ','[lastname]',' ','[city]')
  151. *
  152. * @return array
  153. */
  154. function getAllFieldsAsArray()
  155. {
  156. $matches = array();
  157. preg_match_all("/\[[^\]]*\]|[^[]+/",$this->m_string,$matches);
  158. return $matches;
  159. }
  160. /**
  161. * Parse data into the string and return all fields as an array
  162. *
  163. * @param array $data
  164. * @param boolean $split_tags_and_fields return fields and separators separated in resultarray (separators are not used in query, so quotes aren't used)
  165. * @return array
  166. */
  167. function getAllParsedFieldsAsArray($data, $split_tags_and_fields = false)
  168. {
  169. $matches = $this->getAllFieldsAsArray();
  170. atk_var_dump($matches,"MATCHES".($split_tags_and_fields?" (split tags and separators)":""));
  171. $fields=array();
  172. if(is_array($matches))
  173. {
  174. foreach ($matches[0] as $match)
  175. {
  176. // Check if need to parse the match
  177. if(strpos($match,'[')!==false && strpos($match,']')!==false)
  178. {
  179. $parser = new atkStringParser($match);
  180. if ($split_tags_and_fields)
  181. {
  182. $fields['tags'][] = $parser->parse($data);
  183. }
  184. else
  185. {
  186. $fields[] = $parser->parse($data);
  187. }
  188. }
  189. else
  190. {
  191. if ($split_tags_and_fields)
  192. {
  193. $fields['separators'][] = $match;
  194. }
  195. else
  196. {
  197. $fields[] = "'".$match."'";
  198. }
  199. }
  200. }
  201. }
  202. return $fields;
  203. }
  204. /**
  205. * Same as getFields but if a relation is referenced using
  206. * a dot only returns the attribute name before the dot.
  207. *
  208. * @return array attributes used in template
  209. */
  210. function getAttributes()
  211. {
  212. $attrs = array();
  213. $fields = $this->getFields();
  214. foreach ($fields as $field)
  215. {
  216. list($attr) = explode('.', $field);
  217. $attrs[] = $attr;
  218. }
  219. return $attrs;
  220. }
  221. }
  222. ?>