API/library/Deals.php

96 lines
2.0 KiB
PHP
Raw Permalink Normal View History

2016-08-20 23:02:05 +02:00
<?php
namespace Shikiryu\LBCReposter;
class Deals extends \ArrayObject
{
protected $account;
/**
* @param Deal $deal
*
* @return $this
*/
public function addDeal(Deal $deal)
2016-08-20 23:02:05 +02:00
{
$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);
2016-08-20 23:02:05 +02:00
}
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;
2016-08-20 23:02:05 +02:00
}
/**
* @param Account $account
*
* @return Deals
*/
public function setAccount($account)
2016-08-20 23:02:05 +02:00
{
$this->account = $account;
return $this;
2016-08-20 23:02:05 +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);
}
}
/**
* @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;
}
2016-08-19 23:02:07 +02:00
}