Skip to content

Commit 32cf3e7

Browse files
author
Thorsten Suckow-Homberg
committed
fix: empty array is considered as no-match
add Equatable to Template for comparing two templates for equality refs #8
1 parent 8590db7 commit 32cf3e7

File tree

3 files changed

+94
-5
lines changed

3 files changed

+94
-5
lines changed

src/Net/Uri/Component/Path/Template.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929

3030
namespace Conjoon\Net\Uri\Component\Path;
3131

32+
use Conjoon\Core\Contract\Equatable;
33+
use Conjoon\Core\Contract\Stringable;
34+
use Conjoon\Core\Contract\StringStrategy;
35+
use Conjoon\Core\Exception\InvalidTypeException;
3236
use Conjoon\Net\Uri;
3337

3438
/**
@@ -51,7 +55,7 @@
5155
* // ["mailFolderId" => "2"]
5256
*
5357
*/
54-
class Template
58+
class Template implements Stringable, Equatable
5559
{
5660
/**
5761
* @var string
@@ -97,7 +101,7 @@ public function match(Uri $uri): ?array
97101
$uriRegex = $this->getUriTemplateRegex();
98102
$matches = $uriRegex->match($uri->getPath());
99103

100-
if (!$matches) {
104+
if ($matches === null) {
101105
return null;
102106
}
103107

@@ -146,4 +150,30 @@ private function getUriTemplateRegex(): TemplateRegex
146150
}
147151
return $this->uriTemplateRegex;
148152
}
153+
154+
155+
/**
156+
* @param StringStrategy|null $stringStrategy
157+
* @return string
158+
*/
159+
public function toString(StringStrategy $stringStrategy = null): string
160+
{
161+
return $stringStrategy ? $stringStrategy->toString($this) : $this->templateString;
162+
}
163+
164+
165+
/**
166+
* @param object $target
167+
* @return bool
168+
*/
169+
public function equals(object $target): bool
170+
{
171+
if (!($target instanceof Template)) {
172+
throw new InvalidTypeException(
173+
"\"target\" must be an instance of " . static::class . ", is " . get_class($target)
174+
);
175+
}
176+
177+
return strtolower($this->toString()) === strtolower($target->toString());
178+
}
149179
}

tests/Net/Uri/Component/Path/TemplateRegexTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ public function testMatch(): void
120120
"resourceId" => "123",
121121
"subItemId" => "456"
122122
],
123+
], [
124+
"cArg" => "tpl/resource",
125+
"input" => "tpl/resource",
126+
"output" => [],
123127
]];
124128

125129
foreach ($tests as $test) {

tests/Net/Uri/Component/Path/TemplateTest.php

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929

3030
namespace Tests\Conjoon\Net\Uri\Component\Path;
3131

32+
use Conjoon\Core\Contract\Equatable;
33+
use Conjoon\Core\Contract\Stringable;
34+
use Conjoon\Core\Exception\InvalidTypeException;
3235
use Conjoon\Net\Uri\Component\Path\Template;
3336
use Conjoon\Net\Uri;
3437
use Tests\TestCase;
@@ -38,6 +41,52 @@
3841
*/
3942
class TemplateTest extends TestCase
4043
{
44+
/**
45+
* Tests class & types
46+
* @return void
47+
*/
48+
public function testClass(): void
49+
{
50+
$tpl = new Template("/MailFolder/{mailFolderId}");
51+
$this->assertInstanceOf(Stringable::class, $tpl);
52+
$this->assertInstanceOf(Equatable::class, $tpl);
53+
}
54+
55+
/**
56+
* Tests equals() with InvalidTypeException
57+
* @return void
58+
*/
59+
public function testEqualsWithException(): void
60+
{
61+
$this->expectException(InvalidTypeException::class);
62+
63+
$tplLft = new Template("/MailFolder/{mailFolderId}");
64+
65+
$equatable = new class implements Equatable {
66+
public function equals(object $target): bool
67+
{
68+
return true;
69+
}
70+
};
71+
72+
$tplLft->equals($equatable);
73+
}
74+
75+
76+
/**
77+
* Tests equals()
78+
* @return void
79+
*/
80+
public function testEquals(): void
81+
{
82+
$tplLft = new Template("/MailFolder/{mailFolderId}");
83+
$tplYes = new Template("/MAILFOLDER/{mailFolderId}");
84+
$tplNo = new Template("/SnailFolder/{mailFolderId}");
85+
86+
$this->assertTrue($tplLft->equals($tplYes));
87+
$this->assertFalse($tplLft->equals($tplNo));
88+
}
89+
4190
/**
4291
* Tests getTemplateString()
4392
*
@@ -94,18 +143,24 @@ public function testMatch(): void
94143

95144
$this->assertSame(
96145
["mailFolderId" => "2"],
97-
$tpl->match(Uri::create("https://localhost:8080/MailFolder/2?query=value"))
146+
$tpl->match(Uri::make("https://localhost:8080/MailFolder/2?query=value"))
98147
);
99148

100149
$this->assertNull(
101-
$tpl->match(Uri::create("https://localhost:8080/path/MailFolder/2?query=value"))
150+
$tpl->match(Uri::make("https://localhost:8080/path/MailFolder/2?query=value"))
102151
);
103152

104153
// relative path
105154
$tpl = new Template("MailFolder/{mailFolderId}");
106155
$this->assertSame(
107156
["mailFolderId" => "2"],
108-
$tpl->match(Uri::create("https://localhost:8080/pathMailFolder/2?query=value"))
157+
$tpl->match(Uri::make("https://localhost:8080/pathMailFolder/2?query=value"))
158+
);
159+
160+
$tpl = new Template("/employees");
161+
$this->assertSame(
162+
[],
163+
$tpl->match(Uri::make("https://localhost:8080/employees"))
109164
);
110165
}
111166
}

0 commit comments

Comments
 (0)