Skip to content

Commit

Permalink
improved list initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoJF committed Nov 4, 2019
1 parent 9be4ef0 commit a28a90c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 10 deletions.
18 changes: 14 additions & 4 deletions src/Classes/Lists/BaseList.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,33 @@

namespace hugojf\CsgoServerApi\Classes\Lists;

use Traversable;

abstract class BaseList
{
protected $list = [];

protected $itemClass = null;

abstract function buildItem($item);
protected abstract function buildItem(...$params);

public function addItem($items)
public function addItem($items, ...$rest)
{
// Always wrap argument in array
if (!is_array($items))
$items = [$items];
$items = [func_get_args()];

// Instantiate Server classes if needed
$items = array_map(function ($item) {
return (is_object($item) && get_class($item) === $this->itemClass) ? $item : $this->buildItem($item);
// Make sure item is unpackable (when just a string is given as parameter)
if (!is_array($item) && !$item instanceof Traversable)
$item = [$item];

// Check if item is already instantiated
if (count($item) === 1 && is_object($item[0]) && get_class($item[0]) === $this->itemClass)
return $item[0];

return $this->buildItem(...$item);
}, $items);

// Merge with current list
Expand Down
6 changes: 3 additions & 3 deletions src/Classes/Lists/CommandList.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ class CommandList extends BaseList
protected $itemClass = Command::class;

/**
* @param $item
* @param array $params
*
* @return Command
*/
function buildItem($item)
protected function buildItem(...$params)
{
return new Command($item);
return new Command(...func_get_args());
}
}
6 changes: 3 additions & 3 deletions src/Classes/Lists/ServerList.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ class ServerList extends BaseList
protected $itemClass = Server::class;

/**
* @param $item
* @param array $params
*
* @return Server
* @throws InvalidAddressException
*/
function buildItem($item)
protected function buildItem(...$params)
{
return new Server($item);
return new Server(...func_get_args());
}
}
47 changes: 47 additions & 0 deletions tests/ExecTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Exception;
use hugojf\CsgoServerApi\Classes\Api;
use hugojf\CsgoServerApi\Classes\Command;
use hugojf\CsgoServerApi\Classes\Lists\CommandList;
use hugojf\CsgoServerApi\Classes\Lists\ServerList;
use hugojf\CsgoServerApi\Classes\Senders\BroadcastSender;
use hugojf\CsgoServerApi\Classes\Senders\DirectSender;
use hugojf\CsgoServerApi\Classes\Server;
Expand Down Expand Up @@ -207,4 +209,49 @@ public function testServerWillRaiseExceptionOnInvalidAddress()
$this->assertEquals('27001', $server->getPort());
}

public function testCommandList()
{
$commandList = new CommandList();

$commandList->addItem(new Command('stats', 1000, false));
$commandList->addItem([
new Command('stats', 1500, false),
new Command('status', 1500, false),
]);

$commandList->addItem('stats', 1500, false);

$commandList->addItem([
['stats', '1500', false],
['status', '1500', false],
]);

$this->assertEquals(6, count($commandList->getList()));
}

public function testServerList()
{
$serverList = new ServerList();

$serverList->addItem(new Server('177.54.150.15:27001'));
$serverList->addItem([
new Server('177.54.150.15:27001'),
new Server('177.54.150.15:27002'),
]);

$serverList->addItem('177.54.150.15:27002');
$serverList->addItem([
'177.54.150.15:27001',
'177.54.150.15:27002',
]);
$serverList->addItem('177.54.150.15', 27002);
$serverList->addItem([
['177.54.150.15', 27001],
['177.54.150.15', 27002],
]);

$list = $serverList->getList();

$this->assertEquals(9, count($list));
}
}

0 comments on commit a28a90c

Please sign in to comment.