Completed
Push — trunk ( f009e4...c8f413 )
by SuperNova.WS
06:07
created

PTLTag   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 137
ccs 0
cts 54
cp 0
rs 10
c 0
b 0
f 0
wmc 17

4 Methods

Rating   Name   Duplication   Size   Complexity  
C resolveTemplateVars() 0 26 7
A removeParam() 0 6 2
A getCacheKey() 0 8 2
B __construct() 0 29 6
1
<?php
2
3
/**
4
 * Created by Gorlum 23.02.2017 14:34
5
 */
6
class PTLTag {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
8
  /**Raw tag
9
   *
10
   * @var string $raw
11
   */
12
  public $raw = '';
13
  /**
14
   * Fully resolved tag without params
15
   *
16
   * @var mixed|string $resolved
17
   */
18
  public $resolved = '';
19
  /**
20
   * Namespace for block refs
21
   *
22
   * @var string $namespace
23
   */
24
  public $namespace = '';
25
  /**
26
   * Array of tag params
27
   *
28
   * @var array $params
29
   */
30
  public $params = array();
31
32
  /**
33
   * Template via which this tag is compiled
34
   *
35
   * @var template $template
36
   */
37
  public $template;
38
39
  /**
40
   * Ресолвит переменные и парсит тэг
41
   *
42
   * @param string        $stringTag - tag from PTL
43
   * @param template|null $template - template object which used to resolve tags
44
   * @param array         $allowedParamsAsKeys - array of param name as a key like array('paramName' => mixed)
45
   */
46
  public function __construct($stringTag, $template = null, $allowedParamsAsKeys = array()) {
47
    $this->template = $template;
48
    $this->raw = $stringTag;
49
50
    // Separating params - so there are will be no false-positives for template's variable names
51
    if (strpos($this->raw, '|') !== false) {
52
      $this->params = explode('|', $this->raw);
53
      $this->resolved = $this->params[0];
54
      unset($this->params[0]);
55
    } else {
56
      $this->params = array();
57
      $this->resolved = $this->raw;
58
    }
59
60
    // Is there any template variables? Template variables passed in square brackets
61
    if (strpos($this->resolved, '[') !== false && is_object($this->template)) {
62
      $this->resolved = $this->resolveTemplateVars($this->resolved);
63
    }
64
65
    // Is there any namespaces in tag?
66
    if (strpos($this->resolved, '.') !== false) {
67
      $this->namespace = substr($this->resolved, 0, strrpos($this->resolved, '.'));
68
    }
69
70
    // Here so we potentially can use 2nd-level params - i.e. in template's variables
71
    if (count($this->params)) {
72
      $this->params = HelperArray::parseParamStrings($this->params);
73
      $this->params = array_intersect_key($this->params, $allowedParamsAsKeys);
74
      ksort($this->params);
75
    }
76
  }
77
78
  /**
79
   * Resolves template's variables in tag
80
   *
81
   * Template's variables defined in square brackets:
82
   *    [$DEFINE] - Defines is a DEFINE-d variable like <!-- DEFINE $VAR = 'value' -->
83
   *    [VAR_NAME] - Root variable like $template->assign_var($var_name, $value)
84
   *    [block_name.BLOCK_VAR_NAME] - Block variable like $template->assign_block_var($block_name, array($block_var => $value));
85
   *                                  Use last level block name for access multilevel blocks - i.e. [e.E1] for accessing current value of q.w.e.E1
86
   *
87
   * Other constructions (like {D_xxx}, {C_xxx} etc) is not supported
88
   *
89
   * @param string $rawTag
90
   *
91
   * @return mixed|string
92
   */
93
  protected function resolveTemplateVars($rawTag) {
94
    // Многоуровневые вложения ?! Надо ли и где их можно применить
95
    preg_match_all('#(\[.+?\])#', $rawTag, $matches);
96
97
    foreach ($matches[0] as &$match) {
98
      $var_name = str_replace(array('[', ']'), '', $match);
99
100
      if (strpos($var_name, '.') !== false) {
101
        // Block variables
102
        list($block_name, $block_var) = explode('.', $var_name);
103
        isset($this->template->_block_value[$block_name][$block_var])
104
          ? $rawTag = str_replace($match, $this->template->_block_value[$block_name][$block_var], $rawTag) : false;
105
      } elseif (strpos($var_name, '$') !== false) {
106
        // Defines
107
        // Only one $VAR can be used per square bracket
108
        $define_name = substr($var_name, 1);
109
        isset($this->template->_tpldata['DEFINE']['.'][$define_name])
110
          ? $rawTag = str_replace($match, $this->template->_tpldata['DEFINE']['.'][$define_name], $rawTag) : false;
111
      } else {
112
        // Root variable
113
        isset($this->template->_rootref[$var_name])
114
          ? $rawTag = str_replace($match, $this->template->_rootref[$var_name], $rawTag) : false;
115
      }
116
    }
117
118
    return $rawTag;
119
  }
120
121
122
  public function removeParam($paramName) {
123
    if (!array_key_exists($paramName, $this->params)) {
124
      return;
125
    }
126
127
    unset($this->params[$paramName]);
128
  }
129
130
  /**
131
   * Generates unique cache key for tag
132
   *
133
   * @return string
134
   */
135
  public function getCacheKey() {
136
    $result = [$this->resolved];
137
138
    foreach ($this->params as $key => $value) {
139
      $result[] = $key . '=' . $value;
140
    }
141
142
    return implode('|', $result);
143
  }
144
145
}
146