Skip to content

Commit b9a25fc

Browse files
committed
Add parameter value object
1 parent 0ff1ec3 commit b9a25fc

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed

lib/Container.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public function getServiceIdsForTag(string $tag): array;
3232
*/
3333
public function getParameter(string $name);
3434

35+
public function parameter(string $name): Parameter;
36+
3537
/**
3638
* @return array<string,mixed>
3739
*/

lib/Parameter.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Phpactor\Container;
4+
5+
use RuntimeException;
6+
7+
final class Parameter
8+
{
9+
public function __construct(private mixed $value)
10+
{
11+
}
12+
13+
public function value(): mixed
14+
{
15+
return $this->value;
16+
}
17+
18+
public function int(): int
19+
{
20+
if (!is_int($this->value)) {
21+
throw new RuntimeException(sprintf('Value is not an int, it is "%s"', gettype($this->value)));
22+
}
23+
return $this->value;
24+
}
25+
26+
public function string(): string
27+
{
28+
if (!is_string($this->value)) {
29+
throw new RuntimeException(sprintf('Value is not a string, it is "%s"', gettype($this->value)));
30+
}
31+
return $this->value;
32+
}
33+
34+
public function bool(): bool
35+
{
36+
if (!is_bool($this->value)) {
37+
throw new RuntimeException(sprintf('Value is not a bool, it is "%s"', gettype($this->value)));
38+
}
39+
return $this->value;
40+
}
41+
42+
public function float(): float
43+
{
44+
if (!is_float($this->value)) {
45+
throw new RuntimeException(sprintf('Value is not a float, it is "%s"', gettype($this->value)));
46+
}
47+
return $this->value;
48+
}
49+
}

lib/PhpactorContainer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,9 @@ public function expect(string $id, string $expected): object
152152
{
153153
return $this->get($id);
154154
}
155+
156+
public function parameter(string $name): Parameter
157+
{
158+
return new Parameter($this->getParameter($name));
159+
}
155160
}

tests/Unit/ParameterTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Phpactor\Container\Tests\Unit;
4+
5+
use Closure;
6+
use Generator;
7+
use PHPUnit\Framework\TestCase;
8+
use Phpactor\Container\Parameter;
9+
use RuntimeException;
10+
11+
class ParameterTest extends TestCase
12+
{
13+
public function testCast(): void
14+
{
15+
self::assertSame(12, (new Parameter(12))->value());
16+
self::assertSame(12, (new Parameter(12))->int());
17+
self::assertSame('12', (new Parameter('12'))->string());
18+
self::assertSame(true, (new Parameter(true))->bool());
19+
self::assertSame(12.2, (new Parameter(12.2))->float());
20+
}
21+
22+
/**
23+
* @dataProvider provideCastException
24+
* @param Closure(Parameter): mixed $invoker
25+
*/
26+
public function testCastException(mixed $value, Closure $invoker, string $message): void
27+
{
28+
$this->expectException(RuntimeException::class);
29+
$this->expectExceptionMessage($message);
30+
$invoker(
31+
new Parameter($value)
32+
);
33+
}
34+
/**
35+
* @return Generator<string,array{mixed,Closure(Parameter): mixed,string}>
36+
*/
37+
public function provideCastException(): Generator
38+
{
39+
yield 'not an int' => [
40+
'12',
41+
fn (Parameter $p) => $p->int(),
42+
'Value is not an int, it is "string"'
43+
];
44+
yield 'not an string' => [
45+
12,
46+
fn (Parameter $p) => $p->string(),
47+
'Value is not a string, it is "integer"'
48+
];
49+
yield 'not a float' => [
50+
'12',
51+
fn (Parameter $p) => $p->float(),
52+
'Value is not a float, it is "string"'
53+
];
54+
yield 'not a bool' => [
55+
'12',
56+
fn (Parameter $p) => $p->bool(),
57+
'Value is not a bool, it is "string"'
58+
];
59+
}
60+
}

tests/Unit/PhpactorContainerTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ public function testReturnsParameter(): void
130130
$this->assertEquals('value1', $result);
131131
}
132132

133+
public function testReturnsParameterObject(): void
134+
{
135+
$result = $this->container->parameter('configKey1');
136+
$this->assertEquals('value1', $result->value());
137+
}
138+
133139
public function testReturnsNullParameter(): void
134140
{
135141
$result = $this->container->getParameter('nullKey');

0 commit comments

Comments
 (0)