ATK /utils/class.atkstringparser.inc

Language PHP Lines 243
MD5 Hash 469f89f77338bc8a6adb01c8d2b28817
Repository https://github.com/ibuildingsnl/ATK.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
  /**
   * This file is part of the Achievo ATK distribution.
   * Detailed copyright and licensing information can be found
   * in the doc/COPYRIGHT and doc/LICENSE files which should be
   * included in the distribution.
   *
   * @package atk
   * @subpackage utils
   *
   * @copyright (c)2000-2004 Ibuildings.nl BV
   * @license http://www.achievo.org/atk/licensing ATK Open Source License
   *
   * @version $Revision: 6320 $
   * $Id$
   */

  /**
   * Generic string parser.
   *
   * @author Ivo Jansch <ivo@achievo.org>
   * @package atk
   * @subpackage utils
   *
   */
  class atkStringParser
  {
    var $m_fields = array();
    var $m_string = "";

    /**
     * Create a new stringparser
     *
     * @param string $string The string to parse
     */
    function atkStringParser($string)
    {
      $this->m_string = $string;
    }

    /**
     * Parse data into the string.
     *
     * @param array $data                The data to parse in the string
     * @param bool  $encode              Wether or not to do a rawurlencode
     * @param bool  $ignoreUnknownFields Replace unknown fields with an empty string,
     *                                   if set to false unknown fields will be left
     *                                   untouched.
     *
     * @return String The parsed string
     */
    function parse($data, $encode=false, $replaceUnknownFields=true)
    {
      $string = $this->m_string;
      
      $fields = $this->getFields();
      foreach ($fields as $field)
      {
      	$value = $data;
      	
        $elements = explode(".", $field);
        foreach ($elements as $i => $el)
        {
          if (is_array($value) || $value instanceof ArrayAccess)
          {
          	if (isset($value[$el]))
          	{
          		$value = $value[$el];
          	}
          	else if ($replaceUnknownFields)
          	{
              atknotice("atkStringparser({$this->m_string})->parse(): Attempting to get element '{$el}', but {$elements[$i - 1]} is not an array!");
              $value = '';
              break;
          	}
           	else
           	{
           		// field not found, continue with next field without
           		// replacing the field in the template
           		continue 2;
           	}
          }
          else if (!$replaceUnknownFields)
          {
            // field not found, continue with next field without
            // replacing the field in the template
          	continue 2;
          }
        }
        
        if ($encode)
        {
        	$value = rawurlencode($value);
        }
        
        $string = str_replace("[".$field."]", $value, $string);
      }
      
      return $string;
    }

    /**
     * Does the data contains everything needed to be parsed into the string?
     *
     * @param array $data
     */
    function isComplete($data)
    {
      $fields = $this->getFields();
      for ($i=0;$i<count($fields);$i++)
      {
        $elements = explode(".",$fields[$i]);
        $databin = $data;
        for($j=0;$j<count($elements);$j++)
        {
          $value = $databin[$elements[$j]];
          if (!isset($value)) return false; // Missing value.
          $databin = $databin[$elements[$j]];
        }
        if (!isset($value)) return false; // Missing value.
      }
      return true;
    }

    /**
     * Get the [ ] Fields out of a String
     */
    function getFields()
    {
      if (!count($this->m_fields))
      {
        $tmp = "";
        $adding = false;
        
        $strlen = strlen($this->m_string);
        for ($i=0;$i<$strlen;$i++)
        {
          if ($this->m_string[$i]=="]")
          {
            $adding = false;
            $this->m_fields[] = $tmp;
            $tmp="";
          }
          else if ($this->m_string[$i]=="[")
          {
            $adding = true;
          }
          else
          {
            if ($adding) $tmp.=$this->m_string[$i];
          }
        }
      }
      return $this->m_fields;
    }


    /**
     * Get all fields from a string
     *
     * <b>Example:</b>
     *        string: [firstname], [lastname] [city]
     *        would return array('[firstname]',', ','[lastname]',' ','[city]')
     *
     * @return array
     */
    function getAllFieldsAsArray()
    {
      $matches = array();
      preg_match_all("/\[[^\]]*\]|[^[]+/",$this->m_string,$matches);
      return $matches;
    }

    /**
     * Parse data into the string and return all fields as an array
     *
     * @param array $data
     * @param boolean $split_tags_and_fields return fields and separators separated in resultarray (separators are not used in query, so quotes aren't used)
     * @return array
     */
    function getAllParsedFieldsAsArray($data, $split_tags_and_fields = false)
    {
      $matches = $this->getAllFieldsAsArray();
      atk_var_dump($matches,"MATCHES".($split_tags_and_fields?" (split tags and separators)":""));
            
      $fields=array();
      if(is_array($matches))
      {
        foreach ($matches[0] as $match)
        {
          // Check if need to parse the match
          if(strpos($match,'[')!==false && strpos($match,']')!==false)
          {
            $parser = new atkStringParser($match);
            
            if ($split_tags_and_fields)
            {
              $fields['tags'][] = $parser->parse($data);
            }
            else
            {
              $fields[] = $parser->parse($data);
            }
          }
          else
          {
            if ($split_tags_and_fields)
            {
              $fields['separators'][] = $match;
            }
            else
            {
              $fields[] = "'".$match."'";
            }
          }
        }
      }
      return $fields;
    }


    /**
     * Same as getFields but if a relation is referenced using
     * a dot only returns the attribute name before the dot.
     *
     * @return array attributes used in template
     */
    function getAttributes()
    {
      $attrs = array();

      $fields = $this->getFields();
      foreach ($fields as $field)
      {
        list($attr) = explode('.', $field);
        $attrs[] = $attr;
      }

      return $attrs;
    }
  }
?>
Back to Top