API/library/Deals.php

96 lines
2.0 KiB
PHP

<?php
namespace Shikiryu\LBCReposter;
class Deals extends \ArrayObject
{
protected $account;
/**
* @param Deal $deal
*
* @return $this
*/
public function addDeal(Deal $deal)
{
$this->append($deal);
return $this;
}
/**
* @param string[]|Deal[] $deals
*
* @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;
}
/**
* @param string[]|Deal[] $deals
*
* @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;
}
/**
* @param mixed $value
*/
public function append($value)
{
if (is_array($value)) {
parent::append($this->parseArray($value));
}
if ($value instanceof Deal) {
parent::append($value);
}
}
/**
* @param array $array
*
* @return Deal
*/
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;
}
}