Blame view

common/components/artboxtree/ArtboxTreeHelper.php 1.35 KB
a8370482   Alexander Karnovsky   init project
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  <?php
  
  namespace common\components\artboxtree;
  
  use yii\base\Object;
  use yii\helpers\ArrayHelper;
  
  class ArtboxTreeHelper extends Object {
      public static function treeMap($tree, $from, $to, $symbol = '.')
      {
          $result = [];
  
          self::_recursiveTreeMap($result, $tree, $from, $to, $symbol);
  
          return $result;
      }
  
ad9b9ca9   Karnovsky A   Karnovsky-2904201...
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
      public static function setArrayField($path, $as_string = false) {
          if (is_array($path)) {
              if ($as_string) {
                  foreach ($path as &$item) {
                      $item = "'$item'";
                  }
              }
              $path = implode(',', $path);
          }
          return '{'. $path .'}';
      }
  
      public static function getArrayField($path) {
          $path = trim($path, '{}');
          return empty($path) ? [] : explode(',', $path);
      }
  
a8370482   Alexander Karnovsky   init project
35
36
37
38
39
40
41
42
43
44
45
46
47
      protected static function _recursiveTreeMap(&$result, $tree, $from, $to, $symbol = '&ndash;') {
          foreach ($tree as $item) {
              $element = $item['item'];
              $key = ArrayHelper::getValue($element, $from);
              $value = ArrayHelper::getValue($element, $to);
              $row = str_repeat($symbol, $element->depth+1) . $value;
              $result[$key] = $row;
              if (!empty($item['children'])) {
                  self::_recursiveTreeMap($result, $item['children'], $from, $to, $symbol);
              }
          }
      }
  }