Skip to content

Commit

Permalink
Реформат кода. Добавлен доп параметры для запросов.
Browse files Browse the repository at this point in the history
  • Loading branch information
antflk committed Apr 5, 2018
1 parent 15fdd27 commit 8f89c5e
Show file tree
Hide file tree
Showing 25 changed files with 103 additions and 17 deletions.
15 changes: 9 additions & 6 deletions src/Common/AnnotationHelper.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace NS\ABCPApi\Common;

use ReflectionClass;
Expand All @@ -15,38 +16,40 @@ class AnnotationHelper
*
* @var string[]
*/
private static $availableTypes = array(
private static $availableTypes = [
'int',
'string',
'\DateTime',
'boolean',
'integer',
);
];

/**
* Возвращает список свойств по имени класса.
*
* @param $className
* @return array [string 'name', string 'type']
* @throws \ReflectionException
*/
public static function getPropertiesTypes($className)
{
if (!class_exists($className)) {
return array();
return [];
}
$reflection = new ReflectionClass($className);
$outputPropertiesNames = array();
$outputPropertiesNames = [];
foreach ($reflection->getProperties() as $onePropertyReflection) {
$propertyType = self::DEFAULT_PROPERTY_TYPE;
preg_match('/@var\s+([\\\\]?\w+)/', $onePropertyReflection->getDocComment(), $matches);
if (isset($matches[1]) && in_array($matches[1], self::$availableTypes, true)) {
$propertyType = $matches[1];
}
$outputPropertiesNames[] = array(
$outputPropertiesNames[] = [
'name' => $onePropertyReflection->getName(),
'type' => $propertyType
);
];
}

return $outputPropertiesNames;
}
}
1 change: 1 addition & 0 deletions src/Common/CarType.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace NS\ABCPApi\Common;

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Common/ServiceErrors.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

namespace NS\ABCPApi\Common;

class ServiceErrors
{
private static $errors = array(
private static $errors = [
0 => 'Неизвестная ошибка',
1 => 'Некорректный запрос',
2 => 'Недостаточно параметров',
Expand All @@ -18,7 +19,7 @@ class ServiceErrors
301 => 'Объект не найден',
302 => 'Ошибка инициализации кэша',
303 => 'Ресурс заблокирован'
);
];

/**
* Вовзращает сообщение об ошибке по её коду.
Expand Down
49 changes: 46 additions & 3 deletions src/RestApiClients/TecDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,45 @@ class TecDoc extends RestClient
* @var string
*/
private $host = '';
/**
* Дополнительные параметры для запроса
*
* @var array
*/
private $additionalParams = [];

/**
* Возвращает список дополнительных параметров запроса
*
* @return array
*/
public function getAdditionalParams()
{
return $this->additionalParams;
}

/**
* Устанавливает дополнительные параметры
*
* @param array $additionalParams
* @return $this
*/
public function setAdditionalParams($additionalParams = [])
{
$this->additionalParams = $additionalParams;

return $this;
}

/**
* Добавляет дополнительные параметры
*
* @param array $additionalParams
*/
public function addAdditionalParams($additionalParams)
{
$this->additionalParams[] = $additionalParams;
}

/**
* Возвращает хост для сервиса TecDoc API, либо если хост не задан дефолтный.
Expand Down Expand Up @@ -163,6 +202,7 @@ public function __construct($userKey = '', $userLogin = '', $userPsw = '')
* @param int $carType
* @param int $motorcyclesFilter
* @return Manufacturer[]
* @throws \Exception
*/
public function getManufacturers($carType = CarType::ALL, $motorcyclesFilter = Motorcycle::ALL)
{
Expand Down Expand Up @@ -233,11 +273,13 @@ public function getBrandsAsArray()
*/
private function getAuthenticationData()
{
return array(
$authData = [
'userlogin' => $this->getUserLogin(),
'userpsw' => $this->getUserPsw(),
'userkey' => $this->getUserKey()
);
];

return array_merge($authData, $this->additionalParams);
}

/**
Expand Down Expand Up @@ -426,7 +468,7 @@ public function getArticlesAsArray($modificationId, $categoryId, $brandName, $ca
$requestVars['categoryId'] = $categoryId;
$requestVars['carType'] = $carType;
if (!empty($brandName)) {
$requestVars['brandNames'] = array($brandName);
$requestVars['brandNames'] = [$brandName];
}
$request = new Request(TecDoc::getTecdocHost());
$request->setParameters($requestVars)
Expand Down Expand Up @@ -660,6 +702,7 @@ public function getRealNumberArticlesAsArray($articleId)
* @param int $modelId
* @param int $modificationId
* @return AssignedArticleAttributes[]
* @throws \Exception
*/
public function getAssignedArticleAttributes($articleIdLinkIdPairs, $manufacturerId, $modelId, $modificationId)
{
Expand Down
10 changes: 9 additions & 1 deletion src/RestClient/Request.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace NS\ABCPApi\RestClient;

/**
Expand Down Expand Up @@ -32,7 +33,7 @@ class Request
*
* @var array
*/
protected $parameters = array();
protected $parameters = [];

/**
* Имя операци.
Expand Down Expand Up @@ -65,6 +66,7 @@ public function getServiceUrl()
public function setServiceUrl($serviceUrl)
{
$this->serviceUrl = $serviceUrl;

return $this;
}

Expand All @@ -87,6 +89,7 @@ public function getHttpAccept()
public function setHttpAccept($httpAccept)
{
$this->httpAccept = $httpAccept;

return $this;
}

Expand All @@ -109,6 +112,7 @@ public function getMethod()
public function setMethod($method)
{
$this->method = $method;

return $this;
}

Expand All @@ -131,6 +135,7 @@ public function getParameters()
public function setParameters(array $parameters)
{
$this->parameters = $parameters;

return $this;
}

Expand All @@ -144,6 +149,7 @@ public function setParameters(array $parameters)
public function addParameter($name, $value)
{
$this->parameters[$name] = $value;

return $this;
}

Expand All @@ -156,6 +162,7 @@ public function addParameter($name, $value)
public function removeParameter($name)
{
unset($this->parameters[$name]);

return $this;
}

Expand All @@ -178,6 +185,7 @@ public function getOperation()
public function setOperation($operation)
{
$this->operation = $operation;

return $this;
}
}
7 changes: 6 additions & 1 deletion src/RestClient/Response.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace NS\ABCPApi\RestClient;

/**
Expand Down Expand Up @@ -47,6 +48,7 @@ public function getStatus()
public function setStatus($status)
{
$this->status = (int)$status;

return $this;
}

Expand All @@ -69,6 +71,7 @@ public function getHeaders()
public function setHeaders($headers)
{
$this->headers = $headers;

return $this;
}

Expand All @@ -81,7 +84,8 @@ public function setHeaders($headers)
public function getAsArray()
{
$jsonData = json_decode($this->getBody(), true);
return $jsonData ? $jsonData : array();

return $jsonData ? $jsonData : [];
}

/**
Expand All @@ -103,6 +107,7 @@ public function getBody()
public function setBody($body)
{
$this->body = $body;

return $this;
}
}
5 changes: 3 additions & 2 deletions src/RestClient/RestClient.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace NS\ABCPApi\RestClient;

/**
Expand Down Expand Up @@ -97,7 +98,7 @@ private function doExecute($curlHandle, Request $request)
curl_setopt($curlHandle, CURLOPT_TIMEOUT, 120);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlHandle, CURLOPT_HEADER, true);
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array('Accept: ' . $request->getHttpAccept()));
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, ['Accept: ' . $request->getHttpAccept()]);
$res = curl_exec($curlHandle);
$this->requestInfo = $info = curl_getinfo($curlHandle);
$response = new Response();
Expand Down Expand Up @@ -140,7 +141,7 @@ private function executePost($curlHandle, Request $request)
*/
private static function convertArray($data, $path = '')
{
$out = array();
$out = [];
if (is_array($data)) {
foreach ($data as $k => $value) {
$path1 = $path ? $path . "[$k]" : $k;
Expand Down
1 change: 1 addition & 0 deletions src/TecDocEntities/AnalogArticle.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace NS\ABCPApi\TecDocEntities;

/**
Expand Down
1 change: 1 addition & 0 deletions src/TecDocEntities/AnalogTypes.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace NS\ABCPApi\TecDocEntities;

/**
Expand Down
1 change: 1 addition & 0 deletions src/TecDocEntities/Article.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace NS\ABCPApi\TecDocEntities;

/**
Expand Down
1 change: 1 addition & 0 deletions src/TecDocEntities/ArticleAttribute.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace NS\ABCPApi\TecDocEntities;

/**
Expand Down
1 change: 1 addition & 0 deletions src/TecDocEntities/ArticleDocument.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace NS\ABCPApi\TecDocEntities;

/**
Expand Down
1 change: 1 addition & 0 deletions src/TecDocEntities/ArticleInfo.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace NS\ABCPApi\TecDocEntities;

/**
Expand Down
1 change: 1 addition & 0 deletions src/TecDocEntities/ArticlePart.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace NS\ABCPApi\TecDocEntities;

/**
Expand Down
1 change: 1 addition & 0 deletions src/TecDocEntities/ArticleSimplified.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace NS\ABCPApi\TecDocEntities;

/**
Expand Down
1 change: 1 addition & 0 deletions src/TecDocEntities/AssignedArticleAttributes.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace NS\ABCPApi\TecDocEntities;

/**
Expand Down
Loading

0 comments on commit 8f89c5e

Please sign in to comment.