Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add nillable properties #149

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/DirectApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,6 @@ public function getResponse(DirectApiRequest $request): DirectApiResponse
);

$payload = json_encode($request->getPayload(), JSON_UNESCAPED_UNICODE);
$payload = preg_replace('/,\s*"[^"]+":null|"[^"]+":null,?/', '', $payload);

$httpResponse = $this->doRequest($httpRequest->withBody(stream_for($payload)));
$response = new DirectApiResponse();
Expand Down
55 changes: 54 additions & 1 deletion src/components/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,24 @@
namespace directapi\components;

use directapi\exceptions\UnknownPropertyException;
use JsonSerializable;

class Model
class Model implements JsonSerializable
{
/**
* Массив имён nillable свойств модели
*
* @var string[]
*/
protected static $nillableProperties = [];

/**
* Карта nillable свойств, которым задали null
*
* @var bool[]
*/
protected $nullPropertiesMap = [];

/**
* Model constructor.
*
Expand Down Expand Up @@ -33,4 +48,42 @@ public function getDescription(): ?string
{
return null;
}

/**
* Установить значение null для свойства. Дополнительно заносит свойство в список
*
* @param string $name
*
* @throws UnknownPropertyException
*/
public function setNullProperty(string $name)
{
if (!property_exists(static::class, $name)) {
throw new UnknownPropertyException('Свойство ' . $name . ' не существует в классе ' . static::class);
}

if (in_array($name, static::$nillableProperties, true)) {
$this->$name = null;
$this->nullPropertiesMap[$name] = true;
} else {
throw new UnknownPropertyException('Свойство ' . $name . ' не определено как nillable в классе ' . static::class);
}
}

public function jsonSerialize()
{
$result = new \stdClass();
foreach ($this as $name => $value) {
if ($name == 'nullPropertiesMap') {
continue;
}

$is_array = is_array($value);
if (($is_array && \count($value) !== 0) || (!$is_array && $value !== null) || ($this->nullPropertiesMap[$name] ?? false)) {
$result->$name = $value;
}
}

return $result;
}
}
5 changes: 5 additions & 0 deletions src/services/adgroups/models/AdGroupUpdateItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

class AdGroupUpdateItem extends Model
{
protected static $nillableProperties = [
'NegativeKeywords',
'NegativeKeywordSharedSetIds',
];

/**
* @var int
* @Assert\NotBlank()
Expand Down
5 changes: 5 additions & 0 deletions src/services/adgroups/models/SmartAdGroupUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

class SmartAdGroupUpdate extends Model
{
protected static $nillableProperties = [
'AdTitleSource',
'AdBodySource',
];

/**
* @var string
*/
Expand Down
13 changes: 13 additions & 0 deletions src/services/ads/models/TextAdUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@

class TextAdUpdate extends TextAd
{
protected static $nillableProperties = [
'Title2',
'Href',
'DisplayUrlPath',
'VCardId',
'AdImageHash',
'SitelinkSetId',
'CalloutSetting',
'PriceExtension',
'TurboPageId',
'BusinessId',
];

/**
* @var \directapi\services\adextensions\models\AdExtensionSetting
*/
Expand Down
2 changes: 1 addition & 1 deletion src/services/keywordbids/models/SearchItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SearchItem extends Model
public $Bid;

/**
* Ставки и списываемые цены на поиске, соответствующие различным объемам трафика для данной фразы.
* Ставки и списываемые цены на поиске, соответствующие различным объемам трафика для данной фразы.
*
* @var AuctionBidsItem|null
*/
Expand Down
5 changes: 5 additions & 0 deletions src/services/keywords/models/KeywordUpdateItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

class KeywordUpdateItem extends Model
{
protected static $nillableProperties = [
'UserParam1',
'UserParam2',
];

/**
* @var int
* @Assert\NotBlank()
Expand Down