Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
param.php
1<?
3
8
9class Param implements Errorable
10{
12
13 const ERROR_REQUIRED_PARAMETERS_MISSING = "ERROR_REQUIRED_PARAMETERS_MISSING";
14
15 private $params = [];
16 private $interchangeableParams = [];
17
18 public function __construct(array $params)
19 {
20 $this->params = $this->changeKeyCaseRecursive($params);
21
22 $this->interchangeableParams = [
23 "IBLOCK_ID" => "IBLOCK_CODE",
24 "IBLOCK_CODE" => "IBLOCK_ID",
25 "ELEMENT_ID" => "ELEMENT_CODE",
26 "ELEMENT_CODE" => "ELEMENT_ID",
27 "SECTION_ID" => "SECTION_CODE",
28 "SECTION_CODE" => "SECTION_ID"
29 ];
30
31 $this->errorCollection = new ErrorCollection;
32 }
33
39 public function checkRequiredInputParams(array $requiredInputParams)
40 {
41 foreach ($requiredInputParams as $param)
42 {
43 if (is_array($param))
44 {
45 $this->checkArrayParam($param);
46 }
47 else
48 {
49 $this->checkParam($param);
50 }
51 }
52 }
53
59 public function setParam(array $params)
60 {
61 $params = $this->changeKeyCaseRecursive($params);
62 $this->params = array_merge($this->params, $params);
63 }
64
70 public function getParams()
71 {
72 return $this->params;
73 }
74
75 private function changeKeyCaseRecursive(array $params)
76 {
77 return array_map(function($item) {
78 if (is_array($item))
79 {
80 $item = $this->changeKeyCaseRecursive($item);
81 }
82 return $item;
83 }, array_change_key_case($params, CASE_UPPER));
84 }
85
86 private function checkArrayParam(array $param)
87 {
88 foreach ($param as $innerParam => $listParam)
89 {
90 if (is_array($listParam))
91 {
92 foreach ($listParam as $listInnerParam)
93 {
94 if (!isset($this->params[$innerParam][$listInnerParam]))
95 {
96 if (
97 !isset($this->interchangeableParams[$listInnerParam]) ||
98 !isset($this->params[$this->interchangeableParams[$listInnerParam]])
99 )
100 {
101 $this->setParamError($listInnerParam);
102 }
103 }
104 }
105 }
106 }
107 }
108
109 private function checkParam($param)
110 {
111 if (!isset($this->params[$param]))
112 {
113 if (
114 !isset($this->interchangeableParams[$param]) ||
115 !isset($this->params[$this->interchangeableParams[$param]])
116 )
117 {
118 $this->setParamError($param);
119 }
120 }
121 }
122
123 private function setParamError($param)
124 {
125 $this->errorCollection->setError(
126 new Error(
127 "Required parameter \"".$param."\" is missing", self::ERROR_REQUIRED_PARAMETERS_MISSING
128 )
129 );
130 }
131}
checkRequiredInputParams(array $requiredInputParams)
Definition param.php:39
__construct(array $params)
Definition param.php:18
setParam(array $params)
Definition param.php:59
const ERROR_REQUIRED_PARAMETERS_MISSING
Definition param.php:13