2016-08-20 23:02:05 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Shikiryu\LBCReposter;
|
|
|
|
|
|
|
|
class Deals extends \ArrayObject
|
|
|
|
{
|
|
|
|
protected $account;
|
|
|
|
|
2017-09-27 21:42:22 +02:00
|
|
|
/**
|
|
|
|
* @param Deal $deal
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2017-09-06 06:29:28 +02:00
|
|
|
public function addDeal(Deal $deal)
|
2016-08-20 23:02:05 +02:00
|
|
|
{
|
2017-09-06 06:29:28 +02:00
|
|
|
$this->append($deal);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-09-27 21:42:22 +02:00
|
|
|
/**
|
|
|
|
* @param string[]|Deal[] $deals
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2017-09-06 06:29:28 +02:00
|
|
|
public function addDeals(array $deals)
|
|
|
|
{
|
|
|
|
array_walk($deals, function(&$deal) {
|
|
|
|
if (is_string($deal)) {
|
|
|
|
$deal = Deal::fromURL($this->account, $deal);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
foreach ($deals as $deal) {
|
|
|
|
$this->append($deal);
|
2016-08-20 23:02:05 +02:00
|
|
|
}
|
2017-09-06 06:29:28 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-09-27 21:42:22 +02:00
|
|
|
/**
|
|
|
|
* @param string[]|Deal[] $deals
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2017-09-06 06:29:28 +02:00
|
|
|
public function setDeals(array $deals)
|
|
|
|
{
|
|
|
|
array_walk($deals, function(&$deal) {
|
|
|
|
if (is_string($deal)) {
|
|
|
|
$deal = Deal::fromURL($this->account, $deal);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$this->exchangeArray($deals);
|
|
|
|
return $this;
|
2016-08-20 23:02:05 +02:00
|
|
|
}
|
|
|
|
|
2016-08-24 23:37:29 +02:00
|
|
|
/**
|
|
|
|
* @param Account $account
|
2017-09-27 21:42:22 +02:00
|
|
|
*
|
2017-09-06 06:29:28 +02:00
|
|
|
* @return Deals
|
2016-08-24 23:37:29 +02:00
|
|
|
*/
|
2017-09-06 06:29:28 +02:00
|
|
|
public function setAccount($account)
|
2016-08-20 23:02:05 +02:00
|
|
|
{
|
|
|
|
$this->account = $account;
|
2017-09-06 06:29:28 +02:00
|
|
|
return $this;
|
2016-08-20 23:02:05 +02:00
|
|
|
}
|
|
|
|
|
2017-09-27 21:42:22 +02:00
|
|
|
/**
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
2016-08-20 23:02:05 +02:00
|
|
|
public function append($value)
|
|
|
|
{
|
|
|
|
if (is_array($value)) {
|
|
|
|
parent::append($this->parseArray($value));
|
|
|
|
}
|
|
|
|
if ($value instanceof Deal) {
|
|
|
|
parent::append($value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-27 21:42:22 +02:00
|
|
|
/**
|
|
|
|
* @param array $array
|
|
|
|
*
|
|
|
|
* @return Deal
|
|
|
|
*/
|
2017-09-06 06:29:28 +02:00
|
|
|
private function parseArray(array $array = [])
|
|
|
|
{
|
|
|
|
if (count($array) == 1) {
|
|
|
|
return Deal::fromURL($this->account, current(array_values($array)));
|
|
|
|
}
|
|
|
|
$deal = new Deal();
|
|
|
|
foreach ($array as $name => $value) {
|
|
|
|
$method = sprintf('set%s', ucfirst($name));
|
|
|
|
$deal->$method($value);
|
|
|
|
}
|
|
|
|
return $deal;
|
|
|
|
}
|
|
|
|
|
2016-08-19 23:02:07 +02:00
|
|
|
}
|