Skip to content

Commit bc7fe1f

Browse files
author
Greg Bowler
authored
Callback interface (#5)
* test: implement unit tests * feature: introduce callable interface
1 parent 23d0093 commit bc7fe1f

File tree

6 files changed

+50
-27
lines changed

6 files changed

+50
-27
lines changed

src/CallbackTypeSafeGetter.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
namespace Gt\TypeSafeGetter;
3+
4+
use DateTimeInterface;
5+
6+
interface CallbackTypeSafeGetter {
7+
public function get(string $name, ?callable $callback = null):mixed;
8+
public function getString(string $name, ?callable $callback = null):?string;
9+
public function getInt(string $name, ?callable $callback = null):?int;
10+
public function getFloat(string $name, ?callable $callback = null):?float;
11+
public function getBool(string $name, ?callable $callback = null):?bool;
12+
public function getDateTime(string $name, ?callable $callback = null):?DateTimeInterface;
13+
}

src/NullableTypeSafeGetter.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ function(string|int|DateTimeInterface $value) {
3535
);
3636
}
3737

38+
/**
39+
* @template T
40+
* @param class-string<T> $className
41+
* @param string $name
42+
* @return T
43+
*/
44+
public function getClass(string $className, string $name):?object {
45+
return new $className;
46+
}
47+
3848
protected function getNullableType(string $name, string|callable $type):mixed {
3949
$value = $this->get($name);
4050
if(is_null($value)) {
@@ -59,16 +69,7 @@ protected function getNullableType(string $name, string|callable $type):mixed {
5969
return call_user_func($type, $value);
6070
}
6171

62-
$ucType = ucfirst($type);
63-
if(class_exists($type)) {
64-
return new $type($value);
65-
}
66-
elseif(method_exists($this, "get$ucType")) {
67-
return call_user_func(
68-
[$this, "get$ucType"],
69-
$value
70-
);
71-
}
72+
7273

7374
return null;
7475
}

src/TypeSafeGetter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ public function getInt(string $name):?int;
1010
public function getFloat(string $name):?float;
1111
public function getBool(string $name):?bool;
1212
public function getDateTime(string $name):?DateTimeInterface;
13-
}
13+
}

test/phpunit/ExampleClass.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
namespace Gt\TypeSafeGetter\Test;
3+
4+
class ExampleClass {
5+
public function __construct(
6+
public string $name
7+
) {}
8+
}

test/phpunit/Example.php renamed to test/phpunit/TestGetter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<?php
22
namespace Gt\TypeSafeGetter\Test;
33

4+
use Gt\TypeSafeGetter\TypeSafeGetter;
45
use Gt\TypeSafeGetter\NullableTypeSafeGetter;
56

6-
class Example {
7+
class TestGetter implements TypeSafeGetter {
78
use NullableTypeSafeGetter;
89

910
public function __construct(private array $kvp = []) {}

test/phpunit/TypeSafeGetterTest.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,72 +7,72 @@
77
class TypeSafeGetterTest extends TestCase {
88
public function testGetString():void {
99
$value = uniqid();
10-
$sut = new Example([
10+
$sut = new TestGetter([
1111
"test" => $value,
1212
]);
1313
self::assertSame($value, $sut->getString("test"));
1414
}
1515

1616
public function testGetString_null():void {
17-
$sut = new Example();
17+
$sut = new TestGetter();
1818
self::assertNull($sut->getString("test"));
1919
}
2020

2121
public function testGetString_castFromInt():void {
22-
$sut = new Example([
22+
$sut = new TestGetter([
2323
"test" => 123,
2424
]);
2525
self::assertSame("123", $sut->getString("test"));
2626
}
2727

2828
public function testGetInt():void {
2929
$value = rand(1, 999);
30-
$sut = new Example([
30+
$sut = new TestGetter([
3131
"test" => $value,
3232
]);
3333
self::assertSame($value, $sut->getInt("test"));
3434
}
3535

3636
public function testGetInt_null():void {
37-
$sut = new Example();
37+
$sut = new TestGetter();
3838
self::assertNull($sut->getInt("test"));
3939
}
4040

4141
public function testGetInt_castFromFloat():void {
4242
$value = 123.456;
43-
$sut = new Example([
43+
$sut = new TestGetter([
4444
"test" => $value,
4545
]);
4646
self::assertSame(123, $sut->getInt("test"));
4747
}
4848

4949
public function testGetFloat():void {
5050
$value = 123.456;
51-
$sut = new Example([
51+
$sut = new TestGetter([
5252
"test" => $value,
5353
]);
5454
self::assertSame($value, $sut->getFloat("test"));
5555
}
5656

5757
public function testGetFloat_null():void {
58-
$sut = new Example();
58+
$sut = new TestGetter();
5959
self::assertNull($sut->getFloat("test"));
6060
}
6161

6262
public function testGetBool():void {
63-
$sut = new Example([
63+
$sut = new TestGetter([
6464
"test" => true,
6565
]);
6666
self::assertTrue($sut->getBool("test"));
6767
}
6868

6969
public function testGetBool_null():void {
70-
$sut = new Example();
70+
$sut = new TestGetter();
7171
self::assertNull($sut->getBool("test"));
7272
}
7373

7474
public function testGetBool_castFromString():void {
75-
$sut = new Example([
75+
$sut = new TestGetter([
7676
"yes" => "truthy",
7777
"no" => "",
7878
]);
@@ -82,20 +82,20 @@ public function testGetBool_castFromString():void {
8282

8383
public function testGetDateTime():void {
8484
$value = new DateTime();
85-
$sut = new Example([
85+
$sut = new TestGetter([
8686
"test" => $value,
8787
]);
8888
self::assertSame($value, $sut->getDateTime("test"));
8989
}
9090

9191
public function testGetDateTime_null():void {
92-
$sut = new Example();
92+
$sut = new TestGetter();
9393
self::assertNull($sut->getDateTime("test"));
9494
}
9595

9696
public function testGetDateTime_castFromString():void {
9797
$dateString = "1988-04-05";
98-
$sut = new Example([
98+
$sut = new TestGetter([
9999
"test" => $dateString,
100100
]);
101101
$dateTime = $sut->getDateTime("test");
@@ -105,7 +105,7 @@ public function testGetDateTime_castFromString():void {
105105
public function testGetDateTime_castFromInt():void {
106106
$dateString = "1900-04-05";
107107
$timestamp = strtotime($dateString);
108-
$sut = new Example([
108+
$sut = new TestGetter([
109109
"test" => $timestamp,
110110
]);
111111
$dateTime = $sut->getDateTime("test");

0 commit comments

Comments
 (0)