Skip to content

Commit

Permalink
static, composite
Browse files Browse the repository at this point in the history
  • Loading branch information
brenoroosevelt committed Feb 18, 2022
1 parent fbdc967 commit 9004392
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/CompositeContainer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);

namespace BrenoRoosevelt\Psr11;

use Exception;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;

class CompositeContainer implements ContainerInterface
{
/** @var ContainerInterface[] */
private array $containers;

public function __construct(ContainerInterface ...$containers)
{
$this->containers = $containers;
}

/** @inheritDoc */
public function get(string $id)
{
foreach ($this->containers as $container) {
if ($container->has($id)) {
return $container->get($id);
}
}

$message = sprintf('Composite Container Exception: no entry was found for %s', $id);
throw new class($message) extends Exception implements NotFoundExceptionInterface {
};
}

/** @inheritDoc */
public function has(string $id): bool
{
foreach ($this->containers as $container) {
if ($container->has($id)) {
return true;
}
}

return false;
}
}
39 changes: 39 additions & 0 deletions src/StaticContainer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);

namespace BrenoRoosevelt\Psr11;

use Exception;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;

class StaticContainer implements ContainerInterface
{
private array $services;

/**
* @param array<string, mixed> $services
*/
public function __construct(array $services = [])
{
$this->services = $services;
}

/** @inheritDoc */
public function get(string $id)
{
if (! array_key_exists($id, $this->services)) {
$message = sprintf('Static Container Exception: no entry was found for %s', $id);
throw new class($message) extends Exception implements NotFoundExceptionInterface {
};
}

return $this->services[$id];
}

/** @inheritDoc */
public function has(string $id): bool
{
return array_key_exists($id, $this->services);
}
}
46 changes: 46 additions & 0 deletions tests/CompositeContainerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);

namespace BrenoRoosevelt\Psr11\Tests;

use BrenoRoosevelt\Psr11\CompositeContainer;
use BrenoRoosevelt\Psr11\StaticContainer;
use PHPUnit\Framework\TestCase;
use Psr\Container\NotFoundExceptionInterface;

class CompositeContainerTest extends TestCase
{
public function testGet(): void
{
$container = new CompositeContainer(
new StaticContainer(['id1' => 'value1']),
new StaticContainer(['id2' => 'value2']),
);

$this->assertEquals('value1', $container->get('id1'));
$this->assertEquals('value2', $container->get('id2'));
}

public function testGetException(): void
{
$container = new CompositeContainer(
new StaticContainer(['id1' => 'value1']),
new StaticContainer(['id2' => 'value2']),
);

$this->expectException(NotFoundExceptionInterface::class);
$container->get('invalid');
}

public function testHas(): void
{
$container = new CompositeContainer(
new StaticContainer(['id1' => 'value1']),
new StaticContainer(['id2' => 'value2']),
);

$this->assertTrue($container->has('id1'));
$this->assertTrue($container->has('id2'));
$this->assertFalse($container->has('id3'));
}
}
30 changes: 30 additions & 0 deletions tests/StaticContainerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);

namespace BrenoRoosevelt\Psr11\Tests;

use BrenoRoosevelt\Psr11\StaticContainer;
use PHPUnit\Framework\TestCase;
use Psr\Container\NotFoundExceptionInterface;

class StaticContainerTest extends TestCase
{
public function testGet(): void
{
$container = new StaticContainer(['id' => 'value']);
$this->assertEquals('value', $container->get('id'));
}

public function testGetException(): void
{
$this->expectException(NotFoundExceptionInterface::class);
(new StaticContainer)->get('invalid');
}

public function testHas(): void
{
$container = new StaticContainer(['id' => 'value']);
$this->assertTrue($container->has('id'));
$this->assertFalse($container->has('invalid'));
}
}

0 comments on commit 9004392

Please sign in to comment.