cache = $cache; } public function sendCode(Chat $chat, $code) { $params = [ "log" => 'on', "mes" => '', "legend" => 'on', "nostat" => '', "notext" => '', "refresh" => '0', "bonus" => '', "kladMap" => '', "notags" => '', "cod" => $code, "action" => 'entcod', ]; $data = http_build_query($params); $contextData = [ 'http' => [ 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $data ] ]; $context = stream_context_create($contextData); $result = file_get_contents($this->getLink($chat), false, $context); $contextData['result'] = $result; return serialize($contextData); } private function parseResult($html) { $crawler = new Crawler($html); $answer = [ $this->parseCodeEnter($html), $this->parseTask($crawler) ]; $answer = array_merge($answer, $this->parseCodes($crawler)); $answer = array_filter($answer); return implode(PHP_EOL, $answer); } private function parseCodeEnter($html) { $result = iconv('cp1251', 'utf8', $html); $result = mb_strtolower($result, 'utf8'); if (strpos($result, 'код принят')) { $result = 'Код принят'; } else { $result = 'Код НЕ принят'; } return $result; } private function parseTask(Crawler $crawler) { return $crawler->filter('body > p')->reduce(function ($node) { return strpos($node->text(), 'Задание '); })->text(); } private function parseCodes(Crawler $crawler) { $codesHtml = $crawler->filter('body > .dcodes')->html(); $codesHtml = preg_replace('/\(.+?)\<\/span\>/i', '*[$1]*', $codesHtml); $codesHtml = preg_replace('/\/i', PHP_EOL, $codesHtml); $codesHtml = strip_tags($codesHtml); $codesHtml = explode(PHP_EOL, $codesHtml); array_shift($codesHtml); return $codesHtml; } private function getLink(Chat $chat) { return $this->cache->fetch("link_{$chat->getId()}"); } }