72 lines
1.6 KiB
PHP
72 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace Shikiryu\LBCReposter;
|
|
|
|
class Deals extends \ArrayObject
|
|
{
|
|
protected $account;
|
|
|
|
public function addDeal(Deal $deal)
|
|
{
|
|
$this->append($deal);
|
|
return $this;
|
|
}
|
|
|
|
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);
|
|
}
|
|
return $this;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* @param Account $account
|
|
* @return Deals
|
|
*/
|
|
public function setAccount($account)
|
|
{
|
|
$this->account = $account;
|
|
return $this;
|
|
}
|
|
|
|
public function append($value)
|
|
{
|
|
if (is_array($value)) {
|
|
parent::append($this->parseArray($value));
|
|
}
|
|
if ($value instanceof Deal) {
|
|
parent::append($value);
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
} |