This commit is contained in:
2018-11-21 17:02:14 +03:00
parent 1e728726d0
commit 5a8b88b225
40 changed files with 1500 additions and 8 deletions

3
common/hhapi/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.idea
init.php
/vendor/

View File

@ -0,0 +1,15 @@
{
"name": "kavalar/hhapi",
"type": "library",
"license": "BSD-3-Clause",
"authors": [
{
"name": "Kavalar",
"email": "apuc06@mail.ru"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=7.0.0"
}
}

View File

@ -0,0 +1,53 @@
<?php
/**
* Created by PhpStorm.
* User: kirill
* Date: 15.11.18
* Time: 21:42
*/
namespace common\hhapi\core\lib;
use common\classes\Debug;
use common\hhapi\core\request\Request;
class Company
{
use Request;
public $company;
public $jobs;
public $id;
public function __construct($id)
{
if ($id) {
$this->id = $id;
$company = $this->baseRequest('employers/' . $id)->get();
$this->company = $company;
}
}
public function getName()
{
return $this->company->name;
}
public function getDescription()
{
return $this->company->description;
}
public function getJobs()
{
$j = $this->baseRequest('vacancies')->addParams(['employer_id' => $this->id])->get();
if($j){
foreach ($j->items as $item){
$this->jobs[] = new Vacancy($item);
}
}
return $this->jobs;
}
}

View File

@ -0,0 +1,44 @@
<?php
/**
* Created by PhpStorm.
* User: kirill
* Date: 15.11.18
* Time: 21:16
*/
namespace common\hhapi\core\lib;
use common\hhapi\core\request\Request;
class Vacancy
{
use Request;
public $item;
public function __construct($data = null)
{
if (is_string($data) || is_integer($data)) {
$item = $this->baseRequest('vacancies/' . $data)->get();
$this->item = $item;
}
else {
$this->item = $data;
}
}
public function getName()
{
return ($this->item) ? $this->item->name : null;
}
public static function search($params)
{
$v = new self();
return $v->baseRequest('vacancies')->addParams($params)->get();
}
}

View File

@ -0,0 +1,84 @@
<?php
/**
* Created by PhpStorm.
* User: kirill
* Date: 15.11.18
* Time: 17:37
*/
namespace common\hhapi\core\request;
use Exception;
trait Request
{
public $url;
private $params;
public function parseUrl($url, $method = 'get')
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; tr-TR) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27');
if ($method === 'post') {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $this->getPostParams());
}
$res = curl_exec($curl);
curl_close($curl);
$res = json_decode($res);
// if ($res->http_code == '404') {
// throw new Exception('User not found!', 404);
// }
// if ($res->http_code == '403') {
// throw new Exception('Bad token!', 403);
// }
return $res;
}
public function baseRequest($path)
{
$this->url = 'https://api.hh.ru/' . $path;
return $this;
}
public function addParams($params)
{
if ($params) {
$this->params = $params;
$i = 0;
foreach ((array)$params as $key => $param) {
$s = ($i === 0) ? '?' : '&';
$this->url .= $s . $key . '=' . $param;
$i++;
}
}
return $this;
}
private function getPostParams()
{
$params = '';
if ($this->params) {
foreach ((array)$this->params as $key => $param) {
$params .= $key . '=' . $param . '&';
}
$params = mb_substr($params, 0, -1);
}
return $params;
}
public function get()
{
return $this->parseUrl($this->url);
}
public function post()
{
return $this->parseUrl($this->url, 'post');
}
}

View File

@ -0,0 +1,44 @@
<?php
/**
* Created by PhpStorm.
* User: kirill
* Date: 15.11.18
* Time: 22:24
*/
namespace common\hhapi\core\service;
use common\hhapi\core\lib\Company;
use common\hhapi\core\lib\Vacancy;
class HHService
{
/**
* @param $id
* @return Company
*/
public function company($id)
{
return new Company($id);
}
/**
* @param $id
* @return Vacancy
*/
public function vacancy($id)
{
return new Vacancy($id);
}
/**
* @return HHService
*/
public static function run()
{
return new self();
}
}

16
common/hhapi/index.php Normal file
View File

@ -0,0 +1,16 @@
<?php
/**
* Created by PhpStorm.
* User: kirill
* Date: 15.11.18
* Time: 17:34
*/
require_once 'init.php';
$v = new \core\lib\Vacancy('28246746');
$c = new \core\lib\Company('2495437');
echo '<pre>';
print_r(\core\service\HHService::run()->company('2495437')->getJobs());
echo '</pre>';

53
common/models/Hh.php Normal file
View File

@ -0,0 +1,53 @@
<?php
namespace common\models;
use Yii;
/**
* This is the model class for table "hh".
*
* @property int $id
* @property int $hh_id
* @property string $url
* @property string $title
* @property int $dt_add
* @property string $photo
*/
class Hh extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'hh';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['hh_id', 'dt_add'], 'integer'],
[['url'], 'required'],
[['url', 'title', 'photo'], 'string', 'max' => 255],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'hh_id' => 'Hh ID',
'url' => 'Url',
'title' => 'Название',
'dt_add' => 'Дата добавления',
'photo' => 'Фото',
];
}
}

88
common/models/HhJob.php Normal file
View File

@ -0,0 +1,88 @@
<?php
namespace common\models;
use Yii;
/**
* This is the model class for table "hh_job".
*
* @property int $id
* @property int $employer_id
* @property int $hh_id
* @property string $title
* @property string $url
* @property int $salary_from
* @property int $salary_to
* @property string $salary_currency
* @property string $address
* @property int $dt_add
*/
class HhJob extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'hh_job';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['employer_id', 'hh_id', 'salary_from', 'salary_to', 'dt_add'], 'integer'],
[['title', 'url', 'address'], 'string', 'max' => 255],
[['salary_currency'], 'string', 'max' => 100],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'employer_id' => 'Работодатель',
'hh_id' => 'Hh ID',
'title' => 'Заголовок',
'url' => 'Url',
'salary_from' => 'З.П. от',
'salary_to' => 'З.П. до',
'salary_currency' => 'З.П. валюта',
'address' => 'Адрес',
'dt_add' => 'Дата',
];
}
public function gethhcompany()
{
return $this->hasOne(Hh::class, ['hh_id' => 'employer_id']);
}
public static function createFromHH($data)
{
if (!self::find()->where(['hh_id' => $data->item->id])->exists()) {
$j = new self();
$j->dt_add = time();
$j->title = $data->item->name;
$j->hh_id = $data->item->id;
$j->url = $data->item->alternate_url;
$j->employer_id = $data->item->employer->id;
if (!empty($data->item->address)) {
$j->address = $data->item->address->city . ', ' . $data->item->address->street . ', ' . $data->item->address->building;
}
if (!empty($data->item->salary)) {
$j->salary_from = $data->item->salary->from;
$j->salary_to = $data->item->salary->to;
$j->salary_currency = $data->item->salary->currency;
}
return $j->save();
}
return false;
}
}

View File

@ -16,6 +16,7 @@ use yii\db\Expression;
* @property string $updated_at
* @property string $budget
* @property int $company_id
* @property int $hh_id
*
* @property FieldsValue[] $fieldsValues
* @property Company $company
@ -55,6 +56,7 @@ class Project extends \yii\db\ActiveRecord
[['name'], 'string', 'max' => 255],
[['budget'], 'string', 'max' => 100],
[['company_id'], 'exist', 'skipOnError' => true, 'targetClass' => Company::class, 'targetAttribute' => ['company_id' => 'id']],
[['hh_id'], 'exist', 'skipOnError' => true, 'targetClass' => Hh::class, 'targetAttribute' => ['hh_id' => 'id']],
];
}
@ -71,6 +73,7 @@ class Project extends \yii\db\ActiveRecord
'updated_at' => 'Дата редактирования',
'budget' => 'Бюджет',
'company_id' => 'Компания',
'hh_id' => 'Проект на hh.ru',
];
}
@ -90,6 +93,14 @@ class Project extends \yii\db\ActiveRecord
return $this->hasOne(Company::class, ['id' => 'company_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getHh()
{
return $this->hasOne(Hh::class, ['id' => 'hh_id']);
}
/**
* @return \yii\db\ActiveQuery
*/