linkmobility-php-client/src/RecipientsList/RecipientsList.php

135 lines
2.5 KiB
PHP
Raw Normal View History

2022-06-20 14:47:33 +02:00
<?php
/**
2022-07-13 10:50:45 +02:00
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
2022-06-20 14:47:33 +02:00
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
2022-07-13 10:50:45 +02:00
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @link https://www.oxidmodule.com
2022-06-20 14:47:33 +02:00
*/
declare(strict_types=1);
namespace D3\LinkmobilityClient\RecipientsList;
use D3\LinkmobilityClient\Client;
2022-06-20 14:47:33 +02:00
use D3\LinkmobilityClient\ValueObject\Recipient;
2022-06-24 16:00:56 +02:00
use Iterator;
2022-06-20 14:47:33 +02:00
2022-06-24 16:00:56 +02:00
class RecipientsList implements RecipientsListInterface, Iterator
2022-06-20 14:47:33 +02:00
{
/**
* @var Client
*/
private $client;
2022-06-20 14:47:33 +02:00
/**
* @var array
*/
private $recipients = [];
2022-07-13 10:41:23 +02:00
public function __construct(Client $client)
{
$this->setClient($client);
}
2022-07-11 15:06:18 +02:00
/**
* @param Recipient $recipient
*
* @return RecipientsListInterface
*/
2022-07-13 10:41:23 +02:00
public function add(Recipient $recipient): RecipientsListInterface
2022-06-20 14:47:33 +02:00
{
$this->recipients[ md5(serialize($recipient)) ] = $recipient;
2022-07-13 10:41:23 +02:00
return $this;
2022-06-20 14:47:33 +02:00
}
2022-07-11 15:06:18 +02:00
/**
* @return RecipientsListInterface
*/
2022-07-13 10:41:23 +02:00
public function clearRecipents(): RecipientsListInterface
2022-06-20 14:47:33 +02:00
{
$this->recipients = [];
return $this;
2022-06-20 14:47:33 +02:00
}
/**
* @return array
*/
2022-07-13 10:41:23 +02:00
public function getRecipients(): array
2022-06-20 14:47:33 +02:00
{
return array_values(
array_map(
function (Recipient $recipient) {
return $recipient->getFormatted();
2022-06-20 14:47:33 +02:00
},
$this->recipients
)
);
}
2022-06-24 16:00:56 +02:00
/**
* @return array
*/
2022-07-13 10:41:23 +02:00
public function getRecipientsList(): array
2022-06-20 14:47:33 +02:00
{
return $this->recipients;
}
2022-06-24 16:00:56 +02:00
/**
* @return false|mixed
*/
2022-06-20 14:47:33 +02:00
public function current()
{
return current($this->recipients);
}
2022-06-24 16:00:56 +02:00
/**
* @return false|mixed|void
*/
2022-06-20 14:47:33 +02:00
public function next()
{
return next($this->recipients);
}
public function key()
{
return key($this->recipients);
}
public function rewind()
{
2022-07-11 15:06:18 +02:00
reset($this->recipients);
2022-06-20 14:47:33 +02:00
}
2022-06-24 16:00:56 +02:00
public function valid(): bool
2022-06-20 14:47:33 +02:00
{
2022-06-24 16:00:56 +02:00
return current($this->recipients) instanceof Recipient;
2022-06-20 14:47:33 +02:00
}
/**
* @return Client
*/
public function getClient(): Client
{
return $this->client;
}
/**
* @param Client $client
2022-07-11 15:06:18 +02:00
*
* @return RecipientsList
*/
2022-07-13 10:41:23 +02:00
public function setClient(Client $client): RecipientsList
{
$this->client = $client;
2022-07-11 15:06:18 +02:00
return $this;
}
2022-07-13 10:41:23 +02:00
}