PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/Adapto/src/Adapto/Util/StringParser.php

http://github.com/egeniq/adapto
PHP | 213 lines | 119 code | 20 blank | 74 comment | 30 complexity | 2a6124d2fd28264b29b8be669d729a4f MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Adapto Toolkit.
  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 adapto
  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. */
  15. namespace Adapto\Util;
  16. /**
  17. * Generic string parser.
  18. *
  19. * @author ijansch
  20. * @package adapto
  21. * @subpackage utils
  22. *
  23. */
  24. class StringParser
  25. {
  26. public $m_fields = array(); // defaulted to public
  27. public $m_string = ""; // defaulted to public
  28. /**
  29. * Create a new stringparser
  30. *
  31. * @param string $string The string to parse
  32. */
  33. public function __construct($string)
  34. {
  35. $this->m_string = $string;
  36. }
  37. /**
  38. * Parse data into the string.
  39. *
  40. * @param array $data The data to parse in the string
  41. * @param bool $encode Wether or not to do a rawurlencode
  42. * @param bool $ignoreUnknownFields Replace unknown fields with an empty string,
  43. * if set to false unknown fields will be left
  44. * untouched.
  45. *
  46. * @return String The parsed string
  47. */
  48. function parse($data, $encode = false, $replaceUnknownFields = true)
  49. {
  50. $string = $this->m_string;
  51. $fields = $this->getFields();
  52. foreach ($fields as $field) {
  53. $value = $data;
  54. $elements = explode(".", $field);
  55. foreach ($elements as $i => $el) {
  56. if (is_array($value) || $value instanceof ArrayAccess) {
  57. if (isset($value[$el])) {
  58. $value = $value[$el];
  59. } else if ($replaceUnknownFields) {
  60. atknotice("atkStringparser({$this->m_string})->parse(): Attempting to get element '{$el}', but {$elements[$i - 1]} is not an array!");
  61. $value = '';
  62. break;
  63. } else {
  64. // field not found, continue with next field without
  65. // replacing the field in the template
  66. continue 2;
  67. }
  68. } else if (!$replaceUnknownFields) {
  69. // field not found, continue with next field without
  70. // replacing the field in the template
  71. continue 2;
  72. }
  73. }
  74. if ($encode) {
  75. $value = rawurlencode($value);
  76. }
  77. $string = str_replace("[" . $field . "]", $value, $string);
  78. }
  79. return $string;
  80. }
  81. /**
  82. * Does the data contains everything needed to be parsed into the string?
  83. *
  84. * @param array $data
  85. */
  86. function isComplete($data)
  87. {
  88. $fields = $this->getFields();
  89. for ($i = 0; $i < count($fields); $i++) {
  90. $elements = explode(".", $fields[$i]);
  91. $databin = $data;
  92. for ($j = 0; $j < count($elements); $j++) {
  93. $value = $databin[$elements[$j]];
  94. if (!isset($value))
  95. return false;
  96. // Missing value.
  97. $databin = $databin[$elements[$j]];
  98. }
  99. if (!isset($value))
  100. return false;
  101. // Missing value.
  102. }
  103. return true;
  104. }
  105. /**
  106. * Get the [ ] Fields out of a String
  107. */
  108. function getFields()
  109. {
  110. if (!count($this->m_fields)) {
  111. $tmp = "";
  112. $adding = false;
  113. $strlen = strlen($this->m_string);
  114. for ($i = 0; $i < $strlen; $i++) {
  115. if ($this->m_string[$i] == "]") {
  116. $adding = false;
  117. $this->m_fields[] = $tmp;
  118. $tmp = "";
  119. } else if ($this->m_string[$i] == "[") {
  120. $adding = true;
  121. } else {
  122. if ($adding)
  123. $tmp .= $this->m_string[$i];
  124. }
  125. }
  126. }
  127. return $this->m_fields;
  128. }
  129. /**
  130. * Get all fields from a string
  131. *
  132. * <b>Example:</b>
  133. * string: [firstname], [lastname] [city]
  134. * would return array('[firstname]',', ','[lastname]',' ','[city]')
  135. *
  136. * @return array
  137. */
  138. function getAllFieldsAsArray()
  139. {
  140. $matches = array();
  141. preg_match_all("/\[[^\]]*\]|[^[]+/", $this->m_string, $matches);
  142. return $matches;
  143. }
  144. /**
  145. * Parse data into the string and return all fields as an array
  146. *
  147. * @param array $data
  148. * @param boolean $split_tags_and_fields return fields and separators separated in resultarray (separators are not used in query, so quotes aren't used)
  149. * @return array
  150. */
  151. function getAllParsedFieldsAsArray($data, $split_tags_and_fields = false)
  152. {
  153. $matches = $this->getAllFieldsAsArray();
  154. $fields = array();
  155. if (is_array($matches)) {
  156. foreach ($matches[0] as $match) {
  157. // Check if need to parse the match
  158. if (strpos($match, '[') !== false && strpos($match, ']') !== false) {
  159. $parser = new StringParser($match);
  160. if ($split_tags_and_fields) {
  161. $fields['tags'][] = $parser->parse($data);
  162. } else {
  163. $fields[] = $parser->parse($data);
  164. }
  165. } else {
  166. if ($split_tags_and_fields) {
  167. $fields['separators'][] = $match;
  168. } else {
  169. $fields[] = "'" . $match . "'";
  170. }
  171. }
  172. }
  173. }
  174. return $fields;
  175. }
  176. /**
  177. * Same as getFields but if a relation is referenced using
  178. * a dot only returns the attribute name before the dot.
  179. *
  180. * @return array attributes used in template
  181. */
  182. function getAttributes()
  183. {
  184. $attrs = array();
  185. $fields = $this->getFields();
  186. foreach ($fields as $field) {
  187. list($attr) = explode('.', $field);
  188. $attrs[] = $attr;
  189. }
  190. return $attrs;
  191. }
  192. }
  193. ?>