/** * @param array $data * @param string $method * * @return ApiResponseOk|ApiResponseError */ private function _send(array $data, $method = 'post') { $client = new Client(['requestConfig' => ['format' => Client::FORMAT_JSON]]); $response = $client->createRequest()->setMethod($method)->setUrl($this->url)->addHeaders(['Content-type' => 'application/json'])->addHeaders(['user-agent' => 'JSON-RPC PHP Client'])->setData($data)->setOptions(['timeout' => $this->timeout])->send(); //Нам сказали это всегда json. А... нет, все мы люди, бывает и не json ) try { $dataResponse = (array) Json::decode($response->content); } catch (\Exception $e) { \Yii::error("Json api response error: " . $e->getMessage() . ". Response: \n{$response->content}", self::className()); //Лайф хак, вдруг разработчики апи оставили var dump if ($pos = strpos($response->content, "{")) { $content = StringHelper::substr($response->content, $pos, StringHelper::strlen($response->content)); try { $dataResponse = (array) Json::decode($content); } catch (\Exception $e) { \Yii::error("Api response error: " . $response->content, self::className()); } } } if (!$response->isOk) { \Yii::error($response->content, self::className()); $responseObject = new ApiResponseError($dataResponse); } else { $responseObject = new ApiResponseOk($dataResponse); } $responseObject->statusCode = $response->statusCode; return $responseObject; }
/** * @return null|string */ public function getVerifyedCaptcha() { $captcha = $this->_captchaSource; $captcha = StringHelper::substr($this->_captchaSource, 4, 5); $captcha = $captcha . StringHelper::substr($this->_captchaSource, 1, 4); $captcha = $captcha . StringHelper::substr($this->_captchaSource, 1, 2); $captcha = $captcha . StringHelper::substr($this->_captchaSource, 5, 6); return $captcha; }
/** * @inheritdoc */ protected function renderDataCellContent($model, $key, $index) { $text = $model->{$this->attribute}; return "<small>" . StringHelper::substr($text, 0, $this->maxLength) . (StringHelper::strlen($text) > $this->maxLength ? " ..." : "") . "</small>"; }
/** * Генерация логина пользователя * @return $this */ public function generateUsername() { /*if ($this->email) { $userName = \skeeks\cms\helpers\StringHelper::substr($this->email, 0, strpos() ); }*/ $userLast = static::find()->orderBy("id DESC")->one(); $this->username = "******" . ($userLast->id + 1); if (static::find()->where(['username' => $this->username])->one()) { $this->username = $this->username . "_" . \skeeks\cms\helpers\StringHelper::substr(md5(time()), 0, 6); } return $this; }