-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
41 lines (34 loc) · 815 Bytes
/
Copy pathexample.php
File metadata and controls
41 lines (34 loc) · 815 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
require_once 'vendor/autoload.php';
use DuckType\assertDuckType;
use DuckType\Exceptions\DuckTypeException;
// Define your types and classes
interface Renderable
{
public function render(): string;
}
class PDFDocument
{
public function render(): string
{
return "PDF content";
}
}
class InvalidDocument
{
public function render($extraParam): string
{
return "Invalid content";
}
}
try {
$doc = new PDFDocument();
assertDuckType($doc, Renderable::class); // Should pass
$invalidDoc = new InvalidDocument();
assertDuckType($invalidDoc, Renderable::class); // Should throw DuckTypeException
} catch (DuckTypeException $e) {
echo "Duck typing assertion failed:\n";
foreach ($e->getErrors() as $error) {
echo "- $error\n";
}
}