'staff_3', 'requestData' => [ 'template' => [ 'validation' => DataValidator::notBlank()->length(LengthConfig::MIN_LENGTH_TEMPLATE), 'error' => ERRORS::INVALID_TEMPLATE ], 'language' => [ 'validation' => DataValidator::oneOf(DataValidator::in(Language::getSupportedLanguages()), DataValidator::nullType()), 'error' => ERRORS::INVALID_LANGUAGE ], 'subject' => [ 'validation' => DataValidator::notBlank()->length(LengthConfig::MIN_LENGTH_SUBJECT), 'error' => ERRORS::INVALID_SUBJECT ], ] ]; } public function handler() { $this->language = Controller::request('language'); $this->templateType = Controller::request('template'); $this->subject = Controller::request('subject', true); $this->texts = [ Controller::request('text1'), Controller::request('text2'), Controller::request('text3'), ]; $mailTemplate = MailTemplate::findOne(' language = ? AND template = ?', [$this->language, $this->templateType]); if($mailTemplate->isNull()) { throw new RequestException(ERRORS::INVALID_TEMPLATE); } $this->validateReplacements(); $mailTemplate->subject = $this->subject; $mailTemplate->text1 = $this->texts[0]; $mailTemplate->text2 = $this->texts[1]; $mailTemplate->text3 = $this->texts[2]; $mailTemplate->store(); Response::respondSuccess(); } public function validateReplacements() { $originalText = MailTexts::getTexts()[$this->language][$this->templateType]; if(array_key_exists(1, $originalText) && !$this->includes( $this->getReplacementStrings($originalText[1]), $this->getReplacementStrings($this->texts[0]) )) { throw new RequestException(ERRORS::INVALID_TEXT_1); } if(array_key_exists(2, $originalText) && !$this->includes( $this->getReplacementStrings($originalText[2]), $this->getReplacementStrings($this->texts[1]) )) { throw new RequestException(ERRORS::INVALID_TEXT_2); } if(array_key_exists(3, $originalText) && !$this->includes( $this->getReplacementStrings($originalText[3]), $this->getReplacementStrings($this->texts[2]) )) { throw new RequestException(ERRORS::INVALID_TEXT_3); } } public function includes($array1, $array2) { foreach($array1 as $item) { if(!in_array($item, $array2)) return false; } return true; } public function getReplacementStrings($string) { $replacements = []; for($i=0; $i