Skip to content

Commit

Permalink
Fixed contactlist status id when read from response
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinMystikJonas committed May 30, 2023
1 parent ef271ef commit 8eba7c0
Show file tree
Hide file tree
Showing 24 changed files with 806 additions and 59 deletions.
2 changes: 1 addition & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
- If you still want to use these deprecated endpoints change `SmartEmailing\v3\Request\Eshops\Model\Order` to `SmartEmailing\v3\Models\OrderWithFeedItems`
- For `Search` requests use `setPage($page, $limit)` instead of `setPage($page)->limit($limit)`
- For `Search` requests use getters `getPage()`, `getOffset()` and `getLimit()` instead of properties

- Array `CustomFieldDefinition->options` is no longer available. USe `->options()` instead to get proper `Holder` object

- `CustomRequest` now returns `CustomResponse` with parsed data using `->data()`

Expand Down
16 changes: 1 addition & 15 deletions src/Endpoints/AbstractBasicSearchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(Api $api, ?int $page = null, ?int $limit = null)
*/
public function select(array $select): self
{
$this->select = $select;
$this->select = array_unique(array_merge($this->select ?? [], $select));
return $this;
}

Expand Down Expand Up @@ -98,18 +98,4 @@ protected function options(): array
'query' => $this->query(),
];
}

/**
* Sets the value into array if not valid
*
* @param mixed $value
*/
protected function setQuery(array &$array, string $key, $value): void
{
if ($value === null) {
return;
}

$array[$key] = is_array($value) ? implode(',', $value) : $value;
}
}
35 changes: 35 additions & 0 deletions src/Endpoints/AbstractExpandableGetRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace SmartEmailing\v3\Endpoints;

/**
* @template TResponse of AbstractItemResponse
* @extends AbstractGetRequest<TResponse>
*/
abstract class AbstractExpandableGetRequest extends AbstractGetRequest
{
public ?array $expand = null;

/**
* List of expanded fields
*
* @return $this
*/
public function expandBy(array $expand): self
{
$this->expand = array_unique(array_merge($this->expand ?? [], $expand));
return $this;
}

/**
* Builds a GET query
*/
public function query(): array
{
$query = parent::query();
$this->setQuery($query, 'expand', $this->expand);
return $query;
}
}
21 changes: 18 additions & 3 deletions src/Endpoints/AbstractGetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,32 @@ public function getItemId(): int
*/
public function select(array $select): self
{
$this->select = $select;
$this->select = array_unique(array_merge($this->select ?? [], $select));
return $this;
}

/**
* @return array{select: string}
* Builds a GET query
*/
public function query(): array
{
$query = parent::query();
$this->setQuery($query, 'select', $this->select);
return $query;
}

public function toArray(): array
{
return [];
}

/**
* @return array{query: mixed[]}
*/
protected function options(): array
{
return [
'select' => implode(',', $this->select),
'query' => $this->query(),
];
}
}
26 changes: 25 additions & 1 deletion src/Endpoints/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ public function jsonSerialize(): array
return $this->toArray();
}

/**
* Builds a GET query
*/
public function query(): array
{
return [];
}

/**
* Returns the request method
*/
Expand All @@ -81,13 +89,29 @@ abstract protected function endpoint(): string;
protected function options(): array
{
if ($this->method() === 'GET') {
return [];
return [
'query' => $this->query(),
];
}
return [
'json' => $this->jsonSerialize(),
];
}

/**
* Sets the value into array if not valid
*
* @param mixed $value
*/
protected function setQuery(array &$array, string $key, $value): void
{
if ($value === null) {
return;
}

$array[$key] = is_array($value) ? implode(',', $value) : $value;
}

/**
* Converts the Guzzles RequestException into internal exception
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Endpoints/AbstractSearchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function filter(): AbstractFilters
*/
public function expandBy(array $expand): self
{
$this->expand = $expand;
$this->expand = array_unique(array_merge($this->expand ?? [], $expand));
return $this;
}

Expand Down
17 changes: 14 additions & 3 deletions src/Endpoints/Contacts/Get/ContactsGetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,32 @@
namespace SmartEmailing\v3\Endpoints\Contacts\Get;

use Psr\Http\Message\ResponseInterface;
use SmartEmailing\v3\Endpoints\AbstractGetRequest;
use SmartEmailing\v3\Endpoints\AbstractExpandableGetRequest;
use SmartEmailing\v3\Exceptions\InvalidFormatException;
use SmartEmailing\v3\Models\Contact;

/**
* @extends AbstractGetRequest<ContactsGetResponse>
* @extends AbstractExpandableGetRequest<ContactsGetResponse>
*/
class ContactsGetRequest extends AbstractGetRequest
class ContactsGetRequest extends AbstractExpandableGetRequest
{
public function select(array $select): self
{
InvalidFormatException::checkAllowedValues($select, Contact::SELECT_FIELDS);
return parent::select($select);
}

public function expandBy(array $expand): self
{
InvalidFormatException::checkAllowedValues($expand, Contact::EXPAND_FIELDS);
return parent::expandBy($expand);
}

public function expandCustomFields(): self
{
return $this->expandBy(['customfields']);
}

protected function endpoint(): string
{
return 'contacts/' . $this->getItemId();
Expand Down
5 changes: 5 additions & 0 deletions src/Endpoints/Contacts/Search/ContactsSearchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public function expandBy(array $expand): self
return parent::expandBy($expand);
}

public function expandCustomFields(): self
{
return $this->expandBy(['customfields']);
}

public function select(array $select): self
{
InvalidFormatException::checkAllowedValues($select, Contact::SELECT_FIELDS);
Expand Down
17 changes: 14 additions & 3 deletions src/Endpoints/CustomFields/Get/CustomFieldsGetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,32 @@
namespace SmartEmailing\v3\Endpoints\CustomFields\Get;

use Psr\Http\Message\ResponseInterface;
use SmartEmailing\v3\Endpoints\AbstractGetRequest;
use SmartEmailing\v3\Endpoints\AbstractExpandableGetRequest;
use SmartEmailing\v3\Exceptions\InvalidFormatException;
use SmartEmailing\v3\Models\CustomFieldDefinition;

/**
* @extends AbstractGetRequest<CustomFieldsGetResponse>
* @extends AbstractExpandableGetRequest<CustomFieldsGetResponse>
*/
class CustomFieldsGetRequest extends AbstractGetRequest
class CustomFieldsGetRequest extends AbstractExpandableGetRequest
{
public function select(array $select): self
{
InvalidFormatException::checkAllowedValues($select, CustomFieldDefinition::SELECT_FIELDS);
return parent::select($select);
}

public function expandBy(array $expand): self
{
InvalidFormatException::checkAllowedValues($expand, CustomFieldDefinition::EXPAND_FIELDS);
return parent::expandBy($expand);
}

public function expandCustomFieldOptions(): self
{
return $this->expandBy(['customfield_options']);
}

protected function endpoint(): string
{
return 'customfield/' . $this->getItemId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public function expandBy(array $expand): self
return parent::expandBy($expand);
}

public function expandCustomFieldOptions(): self
{
return $this->expandBy(['customfield_options']);
}

/**
* Comma separated list of properties to select. eg. "?select=id,name" If not provided, all fields are selected.
*
Expand Down
8 changes: 8 additions & 0 deletions src/Models/AbstractHolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public function get($index): Model
return $this->items[$index];
}

/**
* @param int|string $index
*/
public function has($index): bool
{
return isset($this->items[$index]);
}

public function isEmpty(): bool
{
return $this->items === [];
Expand Down
20 changes: 20 additions & 0 deletions src/Models/AbstractMapHolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@ abstract class AbstractMapHolder extends AbstractHolder
*/
protected array $idMap = [];

/**
* @param int|string $id
* @return TEntry
*/
public function getById($id): Model
{
if (isset($this->idMap[$id]) === false) {
throw new \InvalidArgumentException(sprintf('Id %s does not exist', $id));
}
return $this->idMap[$id];
}

/**
* @param int|string $id
*/
public function hasId($id): bool
{
return isset($this->idMap[$id]);
}

/**
* Adds an entry model into items list. Only unique items are added (represented by the id property)
*
Expand Down
11 changes: 10 additions & 1 deletion src/Models/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,16 @@ public static function fromJSON(\stdClass $json): object

if (isset($json->customfields)) {
foreach ($json->customfields as $value) {
$item->customFields->add(CustomFieldValue::fromJSON($value));
$customField = CustomFieldValue::fromJSON($value);
if ($customField->id === null) {
continue;
}
if ($item->customFields->hasId($customField->id)) {
$original = $item->customFields->getById($customField->id);
$original->setOptions(array_merge($original->options, $customField->options));
} else {
$item->customFields->add($customField);
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/Models/ContactListStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,16 @@ public function jsonSerialize(): array
{
return $this->removeEmptyValues($this->toArray());
}

/**
* @return static
*/
public static function fromJSON(\stdClass $json): object
{
$item = parent::fromJSON($json);
if (isset($json->contactlist_id)) {
$item->id = (int) $json->contactlist_id;
}
return $item;
}
}
Loading

0 comments on commit 8eba7c0

Please sign in to comment.