checkEntryParams($params, $controls); $exEn = $this->checkException($entry); if ($exEn) { return $entry; } $diff = $this->checkDiffParams($params, $controls); $exDi = $this->checkException($diff); if ($exDi) { return $diff; } $control = $this->checkControls($controls, $params, $nullable); $exCo = $this->checkException($control); if ($exCo) { return $control; } return true; } /** * @param array $params [description] * @param array $controls [description] * * @return bool|Exception [description] */ protected function checkEntryParams(array $params, array $controls) { $countParams = $this->checkCount($params); if (!$countParams) { return new Exception('No parameters provided !'); } $countControls = $this->checkCount($controls); if (!$countControls) { return new Exception('No controls provided !'); } return true; } /** * @param array $params [description] * @param array $controls [description] * * @return bool|Exception [description] */ private function checkDiffParams(array $params, array $controls) { $diff = array_diff_key($params, array_flip($controls)); $countDiff = $this->checkCount($diff); if ($countDiff) { $concat = ''; foreach ($diff as $key => $param) { $concat .= "$key, "; } return new Exception("Unexpected $concat send as request controls ! Compare with parameters send in request."); } return true; } /** * @param mixed $data [description] * * @return bool [description] */ /*protected function checkIsArray($data): bool // TODO : Specific case, see how catch and control sub-array (if needed) { $result = \is_array($data); return $result; }*/ /** * @param mixed $data [description] * * @return bool [description] */ protected function checkException($data): bool { // TODO : Move in a dedicated service if not exist $result = false; if ($data instanceof Exception) { $result = true; } return $result; } /** * @param array $array [description] * * @return bool [description] */ protected function checkCount(array $array): bool { $result = false; if (!empty($array)) { $result = true; } return $result; } /** * @param array $controls * @param array $params * @param bool $nullable * * @return bool|Exception */ protected function checkControls(array $controls, array $params, bool $nullable) { for ($i = 0; $i < count($controls); ++$i) { // TODO : Specific case, see how catch and control sub-array (if needed) /*$isArray = $this->checkIsArray($controls[$i]);*/ // TODO : Specific case, see how catch and control sub-array (if needed) /*if (!$isArray) {*/ $controlValue = $this->checkControlValue($controls[$i], $params, $nullable); $valEx = $this->checkException($controlValue); if ($valEx) { return $controlValue; } // TODO : Specific case, see how catch and control sub-array (if needed) /*} else { foreach ($params[$controls[$i - 1]] as $param) { $checkMultidimensionnalContent = $this->checkContent($param, $controls[$i]); $valAr = $this->checkException($checkMultidimensionnalContent); if ($valAr) { return $checkMultidimensionnalContent; } } }*/ } return true; } /** * @param string $control * @param array $params * @param bool $nullable * * @return bool|Exception */ protected function checkControlValue(string $control, array $params, bool $nullable) { $isset = $this->checkIsset($control, $params); if (!$isset) { $result = new Exception('Parameter ' . $control . ' not found in request !'); return $result; } $value = $this->checkValue($control, $params, $nullable); if (!$value) { $result = new Exception('Invalid value for parameter ' . $control . ' , send "' . $params[$control] . '" in request !'); return $result; } return true; } /** * @param string $control [description] * @param array $params [description] * * @return bool [description] */ protected function checkIsset(string $control, array $params): bool { $result = array_key_exists($control, $params); return $result; } /** * @param string $control * @param array $params * @param bool $nullable * * @return bool */ protected function checkValue(string $control, array $params, bool $nullable): bool { $result = false; if ($params[$control] || is_bool($params[$control]) || 0 === $params[$control] || "" === $params[$control] || (null === $params[$control] && $nullable)) { $result = true; } return $result; } }