Skip to content

Commit

Permalink
fix creative urls
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvsk committed Feb 25, 2023
1 parent 12a8571 commit 7915c2c
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 84 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"php-http/multipart-stream-builder": "^1.2"
},
"require-dev": {
"klkvsk/dto-generator": "^0.5.4",
"klkvsk/dto-generator": "^0.5.6",
"php-http/mock-client": "^1.5",
"php-http/curl-client": "^2.2",
"nyholm/psr7": "^1.5",
Expand Down
4 changes: 2 additions & 2 deletions src/Data/Creative/CreativeCreateModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(
bool $isReadyForErir,
int $initialContractId,
array $urls = [],
array $okveds = [],
?array $okveds = [],
?string $targetAudienceDescription = null,
?int $organizationId = null
) {
Expand Down Expand Up @@ -117,7 +117,7 @@ public static function create(array $data): self
$constructorParams["isReadyForErir"],
$constructorParams["initialContractId"],
$constructorParams["urls"],
$constructorParams["okveds"],
$constructorParams["okveds"] ?? null,
$constructorParams["targetAudienceDescription"] ?? null,
$constructorParams["organizationId"] ?? null
);
Expand Down
45 changes: 14 additions & 31 deletions src/Data/Creative/CreativeEditModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ class CreativeEditModel implements \JsonSerializable
protected bool $isSocial;
protected bool $isNative;

/** @var array<mixed> $urls */
/** @var array<CreativeUrl> $urls */
protected array $urls;

/** @var array<string> $okveds */
protected array $okveds;
/** @var ?array<string> $okveds */
protected ?array $okveds;
protected ?string $targetAudienceDescription;
protected bool $isReadyForErir;
protected int $initialContractId;
Expand All @@ -38,34 +38,21 @@ public function __construct(
bool $isReadyForErir,
int $initialContractId,
array $urls = [],
array $okveds = [],
?array $okveds = [],
?string $targetAudienceDescription = null
) {
$this->type = $type;
$this->form = $form;
$this->description = $description;
$this->isSocial = $isSocial;
$this->isNative = $isNative;
(function( ...$_) {})( ...$urls);
$urls && (function(CreativeUrl ...$_) {})( ...$urls);
$this->urls = $urls;
(function(string ...$_) {})( ...$okveds);
$okveds && (function(string ...$_) {})( ...$okveds);
$this->okveds = $okveds;
$this->targetAudienceDescription = $targetAudienceDescription;
$this->isReadyForErir = $isReadyForErir;
$this->initialContractId = $initialContractId;
$this->validate([
'urls' => [
'should be at least one' => fn (array $urls) => \count($urls) > 0,
'should be [ {url: ...}, ]' => function (array $urls) {
foreach ($urls as $url) {
if (!\is_array($url) || !isset($url['url'])) {
return false;
}
}
return true;
},
],
]);
}

public function getType(): CreativeType
Expand Down Expand Up @@ -94,17 +81,17 @@ public function getIsNative(): bool
}

/**
* @return array<mixed>
* @return array<CreativeUrl>
*/
public function getUrls(): array
{
return $this->urls;
}

/**
* @return array<string>
* @return ?array<string>
*/
public function getOkveds(): array
public function getOkveds(): ?array
{
return $this->okveds;
}
Expand All @@ -124,13 +111,6 @@ public function getInitialContractId(): int
return $this->initialContractId;
}

protected function validate(array $rules): void
{
array_walk($rules, fn(&$vs, $f) => array_walk($vs, fn(&$v) => $v = false === call_user_func($v, $this->{$f})));
$failedRules = array_filter(array_map(fn($r) => array_keys(array_filter($r)), $rules));
if ($failedRules) throw new \InvalidArgumentException(json_encode($failedRules));
}

protected static function required(): array
{
return ['type', 'form', 'description', 'isSocial', 'isNative', 'urls', 'isReadyForErir', 'initialContractId'];
Expand Down Expand Up @@ -162,7 +142,10 @@ protected static function importers(string $key): iterable
break;

case "urls":
yield static fn ($array) => (array)$array;
yield fn ($array) => array_map(
fn ($data) => call_user_func([ '\BeelineOrd\Data\Creative\CreativeUrl', 'create' ], $data),
(array)$array
);
break;

case "okveds":
Expand Down Expand Up @@ -210,7 +193,7 @@ public static function create(array $data): self
$constructorParams["isReadyForErir"],
$constructorParams["initialContractId"],
$constructorParams["urls"],
$constructorParams["okveds"],
$constructorParams["okveds"] ?? null,
$constructorParams["targetAudienceDescription"] ?? null
);
}
Expand Down
38 changes: 17 additions & 21 deletions src/Data/Creative/CreativeViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ class CreativeViewModel extends CreativeListModel implements \JsonSerializable
protected bool $isSocial;
protected bool $isNative;

/** @var array<string> $urls */
/** @var array<CreativeUrl> $urls */
protected array $urls;

/** @var array<string> $okveds */
protected array $okveds;
/** @var ?array<string> $okveds */
protected ?array $okveds;
protected ?string $targetAudienceDescription;
protected bool $isReadyForErir;
protected int $initialContractId;
protected ?int $organizationId;

Expand All @@ -38,12 +37,11 @@ public function __construct(
CreativeForm $form,
bool $isSocial,
bool $isNative,
bool $isReadyForErir,
int $initialContractId,
?\DateTimeInterface $erirExportedOn = null,
?\DateTimeInterface $erirPlannedExportDate = null,
array $urls = [],
array $okveds = [],
?array $okveds = [],
?string $targetAudienceDescription = null,
?int $organizationId = null
) {
Expand All @@ -53,12 +51,11 @@ public function __construct(
$this->description = $description;
$this->isSocial = $isSocial;
$this->isNative = $isNative;
(function(string ...$_) {})( ...$urls);
$urls && (function(CreativeUrl ...$_) {})( ...$urls);
$this->urls = $urls;
(function(string ...$_) {})( ...$okveds);
$okveds && (function(string ...$_) {})( ...$okveds);
$this->okveds = $okveds;
$this->targetAudienceDescription = $targetAudienceDescription;
$this->isReadyForErir = $isReadyForErir;
$this->initialContractId = $initialContractId;
$this->organizationId = $organizationId;
}
Expand Down Expand Up @@ -89,17 +86,17 @@ public function getIsNative(): bool
}

/**
* @return array<string>
* @return array<CreativeUrl>
*/
public function getUrls(): array
{
return $this->urls;
}

/**
* @return array<string>
* @return ?array<string>
*/
public function getOkveds(): array
public function getOkveds(): ?array
{
return $this->okveds;
}
Expand All @@ -109,11 +106,6 @@ public function getTargetAudienceDescription(): ?string
return $this->targetAudienceDescription;
}

public function getIsReadyForErir(): bool
{
return $this->isReadyForErir;
}

public function getInitialContractId(): int
{
return $this->initialContractId;
Expand All @@ -136,7 +128,7 @@ protected static function required(): array
{
return array_merge(
method_exists(parent::class, "required") ? parent::required() : [],
['type', 'form', 'description', 'isSocial', 'isNative', 'urls', 'isReadyForErir', 'initialContractId']
['type', 'form', 'description', 'isSocial', 'isNative', 'urls', 'initialContractId']
);
}

Expand All @@ -161,11 +153,16 @@ protected static function importers(string $key): iterable

case "isSocial":
case "isNative":
case "isReadyForErir":
yield \Closure::fromCallable('boolval');
break;

case "urls":
yield fn ($array) => array_map(
fn ($data) => call_user_func([ '\BeelineOrd\Data\Creative\CreativeUrl', 'create' ], $data),
(array)$array
);
break;

case "okveds":
yield fn ($array) => array_map(
\Closure::fromCallable('strval'),
Expand Down Expand Up @@ -219,12 +216,11 @@ public static function create(array $data): self
$constructorParams["form"],
$constructorParams["isSocial"],
$constructorParams["isNative"],
$constructorParams["isReadyForErir"],
$constructorParams["initialContractId"],
$constructorParams["erirExportedOn"] ?? null,
$constructorParams["erirPlannedExportDate"] ?? null,
$constructorParams["urls"],
$constructorParams["okveds"],
$constructorParams["okveds"] ?? null,
$constructorParams["targetAudienceDescription"] ?? null,
$constructorParams["organizationId"] ?? null
);
Expand Down
14 changes: 7 additions & 7 deletions src/Data/CreativeContent/CreativeContentUploadResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ class CreativeContentUploadResult implements \JsonSerializable
protected ?int $filesCount;
protected ?int $uploadedFilesCount;

/** @var array<CreativeContentUploadResultFileError> $fileErrors */
protected array $fileErrors;
/** @var ?array<CreativeContentUploadResultFileError> $fileErrors */
protected ?array $fileErrors;

public function __construct(?int $filesCount = null, ?int $uploadedFilesCount = null, array $fileErrors = [])
public function __construct(?int $filesCount = null, ?int $uploadedFilesCount = null, ?array $fileErrors = [])
{
$this->filesCount = $filesCount;
$this->uploadedFilesCount = $uploadedFilesCount;
(function(CreativeContentUploadResultFileError ...$_) {})( ...$fileErrors);
$fileErrors && (function(CreativeContentUploadResultFileError ...$_) {})( ...$fileErrors);
$this->fileErrors = $fileErrors;
}

Expand All @@ -39,9 +39,9 @@ public function getUploadedFilesCount(): ?int
}

/**
* @return array<CreativeContentUploadResultFileError>
* @return ?array<CreativeContentUploadResultFileError>
*/
public function getFileErrors(): array
public function getFileErrors(): ?array
{
return $this->fileErrors;
}
Expand Down Expand Up @@ -87,7 +87,7 @@ public static function create(array $data): self
return new static(
$constructorParams["filesCount"] ?? null,
$constructorParams["uploadedFilesCount"] ?? null,
$constructorParams["fileErrors"]
$constructorParams["fileErrors"] ?? null
);
}

Expand Down
14 changes: 7 additions & 7 deletions src/Data/User/UserViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class UserViewModel implements \JsonSerializable
protected ?string $email;
protected ?string $role;

/** @var array<string> $permissions */
protected array $permissions;
/** @var ?array<string> $permissions */
protected ?array $permissions;

public function __construct(
?int $id = null,
Expand All @@ -31,15 +31,15 @@ public function __construct(
?string $inn = null,
?string $email = null,
?string $role = null,
array $permissions = []
?array $permissions = []
) {
$this->id = $id;
$this->organizationId = $organizationId;
$this->username = $username;
$this->inn = $inn;
$this->email = $email;
$this->role = $role;
(function(string ...$_) {})( ...$permissions);
$permissions && (function(string ...$_) {})( ...$permissions);
$this->permissions = $permissions;
}

Expand Down Expand Up @@ -74,9 +74,9 @@ public function getRole(): ?string
}

/**
* @return array<string>
* @return ?array<string>
*/
public function getPermissions(): array
public function getPermissions(): ?array
{
return $this->permissions;
}
Expand Down Expand Up @@ -133,7 +133,7 @@ public static function create(array $data): self
$constructorParams["inn"] ?? null,
$constructorParams["email"] ?? null,
$constructorParams["role"] ?? null,
$constructorParams["permissions"]
$constructorParams["permissions"] ?? null
);
}

Expand Down
23 changes: 8 additions & 15 deletions src/Data/dto.schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@
dto\field('erid', t\string(), true),
]
),
dto\object(
name: 'Creative\\CreativeUrl',
fields: [
dto\field('url', t\string(), true),
]
),

dto\object(
name: 'Creative\\CreativeEditModel',
Expand All @@ -152,19 +158,7 @@
dto\field('description', t\string(), true),
dto\field('isSocial', t\bool(), true),
dto\field('isNative', t\bool(), true),
dto\field('urls', t\list_(t\mixed()), true,
validators: [
'should be at least one' => fn (array $urls) => count($urls) > 0,
'should be [ {url: ...}, ]' => function (array $urls) {
foreach ($urls as $url) {
if (!is_array($url) || !isset($url['url'])) {
return false;
}
}
return true;
}
]
),
dto\field('urls', t\list_(t\object('Creative\\CreativeUrl')), true),
dto\field('okveds', t\list_(t\string())),
dto\field('targetAudienceDescription', t\string()),
dto\field('isReadyForErir', t\bool(), true),
Expand Down Expand Up @@ -197,10 +191,9 @@
dto\field('description', t\string(), true),
dto\field('isSocial', t\bool(), true),
dto\field('isNative', t\bool(), true),
dto\field('urls', t\list_(t\string()), true),
dto\field('urls', t\list_(t\object('Creative\\CreativeUrl')), true),
dto\field('okveds', t\list_(t\string())),
dto\field('targetAudienceDescription', t\string()),
dto\field('isReadyForErir', t\bool(), true),
dto\field('initialContractId', t\int(), true),
dto\field('organizationId', t\int()),
]
Expand Down

0 comments on commit 7915c2c

Please sign in to comment.