Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
externalizer.php
1<?php
2
3
5
6
11
18 implements Arrayable
19{
20 public function __construct(ViewManager $manager, $data = [])
21 {
22 $this->setFormat([
23 self::TO_WHITE_LIST,
24 self::TO_CAMEL,
25 self::SORTING_KEYS
26 ]);
27 parent::__construct($manager, $data);
28 }
29
30 public function process()
31 {
32 $r = new Result();
33
34 $data = $this->getData();
35 $id = $this->getIdList($data);
36
37 $data = $data[$id];
38 if(in_array(self::TO_WHITE_LIST, $this->format))
39 {
40 $data = $this->externalize($data);
41 }
42
43 if(in_array(self::TO_CAMEL, $this->format))
44 {
45 $data = static::convertKeysToCamelCase([$id=>$data]);
46 }
47
48 if(in_array(self::SORTING_KEYS, $this->format))
49 {
50 $data = static::multiSortKeysArray($data);
51 }
52
53 return $r->setData(['data'=>$data]);
54 }
55
56 public function toArray()
57 {
58 return $this->process()->getData()['data'];
59 }
60
61 static public function multiSortKeysArray(array $data)
62 {
63 ksort($data, SORT_NATURAL);
64
65 foreach ($data as $k=>&$item)
66 {
67 if(is_array($item))
68 $item = static::multiSortKeysArray($item);
69 }
70
71 return $data;
72 }
73
79 private function externalize($fields)
80 {
81 $name = $this->getName();
82 $controller = $this->getController();
83 $view = $this->getView($controller);
84
85 if($name == 'getfields'){}
86 elseif($name == 'delete'){}
87 elseif($name == 'get'
88 || $name == 'add'
89 || $name == 'update')
90 {
91 $fields = $view->externalizeFieldsGet($fields);
92 }
93 elseif($name == 'list')
94 {
95 $fields = $view->externalizeListFields($fields);
96 }
97 else
98 {
99 $fields = $view->externalizeResult($name, $fields);
100 }
101
102 return $fields;
103 }
104
110 private function getIdList($data)
111 {
112 return key($data);
113 }
114
115 static public function convertKeysToCamelCase($fields)
116 {
117 return Converter::toJson()
118 ->process($fields);
119 }
120
121 public function getPage(Page $page)
122 {
123 $id = static::convertKeysToCamelCase($page->getId());
124 return new Page($id, $this->toArray()[$id], $page->getTotalCount());
125 }
126}
__construct(ViewManager $manager, $data=[])