Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
baseaction.php
1<?php
2
4
8
14{
18 protected function onBeforeRun()
19 {
20 $checkPermissionResult = $this->checkPermission();
21 if (!$checkPermissionResult->isSuccess())
22 {
23 $this->addErrors($checkPermissionResult->getErrors());
24 return false;
25 }
26
27 $arguments = $this->getArguments();
28
29 $checkCaseArgumentsResult = $this->checkArguments($arguments);
30 if (!$checkCaseArgumentsResult->isSuccess())
31 {
32 $this->addErrors($checkCaseArgumentsResult->getErrors());
33 return false;
34 }
35
36 // convert keys
37 $converter = new Main\Engine\Response\Converter(
38 Main\Engine\Response\Converter::KEYS
39 | Main\Engine\Response\Converter::RECURSIVE
40 | Main\Engine\Response\Converter::TO_SNAKE
41 | Main\Engine\Response\Converter::TO_SNAKE_DIGIT
42 | Main\Engine\Response\Converter::TO_UPPER
43 );
44 $arguments = $converter->process($arguments);
45
46 $this->setArguments($arguments);
47
48 return parent::onBeforeRun();
49 }
50
51 private function checkPermission(): Sale\Result
52 {
53 $result = new Sale\Result();
54
55 try
56 {
57 Sale\Helpers\Rest\AccessChecker::checkAccessPermission();
58 }
59 catch (Rest\AccessException $ex)
60 {
61 $result->addError(
62 new Main\Error(
63 $ex->getMessage(),
65 )
66 );
67 }
68
69 return $result;
70 }
71
72 private function checkArguments(array $arguments): Sale\Result
73 {
74 $result = new Sale\Result();
75
76 $checkCaseOfKeysResult = $this->checkCaseOfKeys($arguments);
77 if (!$checkCaseOfKeysResult->isSuccess())
78 {
79 $result->addErrors($checkCaseOfKeysResult->getErrors());
80 }
81
82 return $result;
83 }
84
85 private function checkCaseOfKeys(array $fields): Sale\Result
86 {
87 $result = new Sale\Result();
88
89 static $count = 0;
90
91 foreach ($fields as $key => $value)
92 {
93 if (\is_int($key))
94 {
95 continue;
96 }
97
98 if (mb_strtoupper($key) === $key)
99 {
100 $result->addError(
101 new Main\Error(
102 "key \"{$key}\" has UPPERCASE notation. Use lowerCamelCase notation",
104 )
105 );
106
107 $count++;
108 }
109
110 if (\is_array($value))
111 {
112 $internalCheckResult = $this->checkCaseOfKeys($value);
113 if (!$internalCheckResult->isSuccess())
114 {
115 $result->addErrors($internalCheckResult->getErrors());
116 }
117 }
118 }
119
120 return $result;
121 }
122
124 {
125 $result = parent::runWithSourceParametersList();
126
127 if ($this->errorCollection->isEmpty())
128 {
129 if (\is_array($result) && Main\Loader::includeModule('rest'))
130 {
131 $result = Rest\Integration\Externalizer::multiSortKeysArray($result);
132 }
133
134 return Main\Engine\Response\Converter::toJson()->process($result);
135 }
136
137 return $result;
138 }
139}
addErrors(array $errors)
Definition action.php:213
setArguments(array $arguments)
Definition action.php:79