Skip to content

Commit

Permalink
Add more casting methods for parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
dantleech committed Jul 27, 2024
1 parent 6bd2ff8 commit 406add6
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
3.0.0
=====

- Add more casting methods on `parameter`
- Update to PSR container ^2.0
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"require": {
"php": "^8.1",
"phpactor/map-resolver": "^1.4",
"psr/container": "^2.0"
"psr/container": "^1.0||^2.0"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.0",
Expand Down
91 changes: 91 additions & 0 deletions lib/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,95 @@ public function float(): float
}
return $this->value;
}

/**
* @return list<string>
*/
public function listOfString(): array
{
return array_map(function (mixed $value) {
$this->assertScalar($value);
return (string)$value;
}, (array)$this->value);
}

/**
* @return list<int>
*/
public function listOfInt(): array
{
return array_map(function (mixed $value) {
$this->assertScalar($value);
return (int)$value;
}, (array)$this->value);
}

/**
* @return list<float>
*/
public function listOfFloat(): array
{
return array_map(function (mixed $value) {
$this->assertScalar($value);
return (float)$value;
}, (array)$this->value);
}

/**
* @return list<bool>
*/
public function listOfBool(): array
{
return array_map(function (mixed $value) {
$this->assertScalar($value);
return (bool)$value;
}, (array)$this->value);
}

public function intOrNull(): ?int
{
if (null === $this->value) {
return null;
}

return $this->int();
}

public function floatOrNull(): ?float
{
if (null === $this->value) {
return null;
}

return $this->float();
}

public function stringOrNull(): ?string
{
if (null === $this->value) {
return null;
}

return $this->string();
}

public function boolOrNull(): ?bool
{
if (null === $this->value) {
return null;
}

return $this->bool();
}

/**
* @phpstan-assert scalar $value
*/
private function assertScalar(mixed $value): void
{
if (!is_scalar($value)) {
throw new RuntimeException(sprintf('Value in list is not a scalar, it is an "%s"', gettype($this->value)));
}
}

}
24 changes: 23 additions & 1 deletion lib/PhpactorContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,31 @@ public function getTags(): array
return $this->tags;
}

/**
* @template TClass of object
* @param class-string<TClass> $expected
* @return TClass
*/
public function expect(string $id, string $expected): object
{
return $this->get($id);
$service = $this->get($id);
if (!is_object($service)) {
throw new RuntimeException(sprintf(
'Expected service `%s` to be an object but got `%s`',
$id,
get_debug_type($service),
));
}
if (!$service instanceof $expected) {
throw new RuntimeException(sprintf(
'Expected service `%s` to be an instance of `%s` but got `%s`',
$id,
$expected,
$service::class
));
}

return $service;
}

public function parameter(string $name): Parameter
Expand Down
5 changes: 1 addition & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
parameters:
inferPrivatePropertyTypeFromConstructor: true
paths:
- lib
level: 7
level: max
32 changes: 32 additions & 0 deletions tests/Unit/ParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,32 @@ public function testCast(): void
self::assertSame(true, (new Parameter(true))->bool());
self::assertSame(12.2, (new Parameter(12.2))->float());
self::assertSame(12.0, (new Parameter(12))->float());

self::assertSame(null, (new Parameter(null))->intOrNull());
self::assertSame(null, (new Parameter(null))->floatOrNull());
self::assertSame(null, (new Parameter(null))->stringOrNull());
self::assertSame(null, (new Parameter(null))->boolOrNull());

self::assertSame(1, (new Parameter(1))->intOrNull());
self::assertSame(1.1, (new Parameter(1.1))->floatOrNull());
self::assertSame('one', (new Parameter('one'))->stringOrNull());
self::assertSame(true, (new Parameter(true))->boolOrNull());

self::assertSame(['foo'], (new Parameter(['foo']))->listOfString());
self::assertSame(['foo'], (new Parameter('foo'))->listOfString());
self::assertSame(['1'], (new Parameter(1))->listOfString());

self::assertSame([1.2], (new Parameter(['1.2']))->listOfFloat());
self::assertSame([1.4], (new Parameter(1.4))->listOfFloat());
self::assertSame([1.2], (new Parameter([1.2]))->listOfFloat());

self::assertSame([1], (new Parameter(['1']))->listOfInt());
self::assertSame([2], (new Parameter(2))->listOfInt());
self::assertSame([2], (new Parameter([2]))->listOfInt());

self::assertSame([true], (new Parameter(['1']))->listOfBool());
self::assertSame([false], (new Parameter(false))->listOfBool());
self::assertSame([true], (new Parameter([true]))->listOfBool());
}

/**
Expand Down Expand Up @@ -58,5 +84,11 @@ public static function provideCastException(): Generator
fn (Parameter $p) => $p->bool(),
'Value is not a bool, it is "string"'
];

yield 'list of string with list of string' => [
[[]],
fn (Parameter $p) => $p->listOfString(),
'Value in list is not a scalar, it is an "array"'
];
}
}

0 comments on commit 406add6

Please sign in to comment.