-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample.phpt
More file actions
77 lines (65 loc) · 1.95 KB
/
example.phpt
File metadata and controls
77 lines (65 loc) · 1.95 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
* example from loyalty program domain
* presented by Jimmy Bogard - https://vimeo.com/43598193
*/
namespace ValuesWithBehaviourExample;
require __DIR__ . '/../../bootstrap.php';
use Tester\Assert;
/**
* Type of offer - e.g. "Summer sale in Shop A"
*/
interface OfferType {
public function name(): string;
public function beginDate(): ?\DateTimeImmutable; // when offer starts
public function expirationType(): ExpirationType;
public function daysValid(): int;
}
/**
* Loyalty program member
*/
interface Member {
public function id(): int;
}
/**
* Each offer must be namely assigned to member
*/
interface Offer {
public function assignedAt(): \DateTimeImmutable; // when ticket is assigned to client
public function type(): OfferType;
public function assignedTo(): Member;
}
/**
* Type of offer expiration.
* - FIXED - expires for all member at once,
* after days set in offer type (counting from
* - ASSIGNMENT - expires after
*
* @method static ExpirationType ASSIGNMENT()
* @method static ExpirationType FIXED()
*/
abstract class ExpirationType extends \Grifart\Enum\Enum
{
protected const ASSIGNMENT = 'assignment';
protected const FIXED = 'fixed';
abstract public function computeExpiration(Offer $offer): \DateTimeImmutable;
protected static function provideInstances() : array {
return [
new class(self::ASSIGNMENT) extends ExpirationType {
public function computeExpiration(Offer $offer): \DateTimeImmutable {
return $offer->assignedAt()
->modify('+' . $offer->type()->daysValid() . ' days');
}
},
new class(self::FIXED) extends ExpirationType {
public function computeExpiration(Offer $offer): \DateTimeImmutable {
$beginDate = $offer->type()->beginDate();
\assert($beginDate !== NULL);
return $beginDate->modify('+' . $offer->type()->daysValid() . ' days');
}
},
];
}
}
// just checking if it compiles
Assert::type(ExpirationType::class, ExpirationType::ASSIGNMENT());