Skip to content

Commit 777b5ac

Browse files
author
kuba
committed
Added Visitor pattern
1 parent 668ef01 commit 777b5ac

File tree

9 files changed

+174
-8
lines changed

9 files changed

+174
-8
lines changed

behavioral/visitor/SickLeave.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace behavioral\visitor;
4+
5+
class SickLeave
6+
{
7+
private $start;
8+
private $end;
9+
10+
public function __construct(\DateTime $start, \DateTime $end)
11+
{
12+
$this->start = $start;
13+
$this->end = $end;
14+
}
15+
16+
public function getStart(): \DateTime
17+
{
18+
return $this->start;
19+
}
20+
21+
public function getEnd(): \DateTime
22+
{
23+
return $this->end;
24+
}
25+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace behavioral\visitor;
4+
5+
class SickLeaveReport implements Visitor
6+
{
7+
public function visitUniversity(University $university): array
8+
{
9+
$msgs = [];
10+
$msgs[] = "Generating sick leave report for: \"{$university->getName()}\"";
11+
12+
foreach ($university->getStudents() as $student) {
13+
$msgs[] = $this->visitStudent($student);
14+
}
15+
16+
return $msgs;
17+
}
18+
19+
public function visitStudent(Student $student): string
20+
{
21+
$daysMissed = 0;
22+
23+
foreach ($student->getSickLeaves() as $sickLeave) {
24+
$daysMissed = $sickLeave->getStart()->diff($sickLeave->getEnd())->days;
25+
}
26+
27+
return "Student: {$student->getName()} missed {$daysMissed} days";
28+
}
29+
}

behavioral/visitor/Student.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace behavioral\visitor;
4+
5+
class Student implements Visitable
6+
{
7+
private $name;
8+
private $sickLeaves = [];
9+
10+
public function __construct(string $name)
11+
{
12+
$this->name = $name;
13+
}
14+
15+
public function addSickLeave(\DateTime $start, \DateTime $end) : void
16+
{
17+
$this->sickLeaves[] = new SickLeave($start, $end);
18+
}
19+
20+
public function getName(): string
21+
{
22+
return $this->name;
23+
}
24+
25+
public function getSickLeaves(): array
26+
{
27+
return $this->sickLeaves;
28+
}
29+
30+
public function accept(Visitor $visitor): string
31+
{
32+
return $visitor->visitStudent($this);
33+
}
34+
}

behavioral/visitor/University.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace behavioral\visitor;
4+
5+
class University implements Visitable
6+
{
7+
private $name;
8+
private $students;
9+
10+
public function __construct(string $name, array $students)
11+
{
12+
$this->name = $name;
13+
$this->students = $students;
14+
}
15+
16+
public function getName(): string
17+
{
18+
return $this->name;
19+
}
20+
21+
public function getStudents(): array
22+
{
23+
return $this->students;
24+
}
25+
26+
public function accept(Visitor $visitor): array
27+
{
28+
return $visitor->visitUniversity($this);
29+
}
30+
}

behavioral/visitor/Visitable.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace behavioral\visitor;
4+
5+
interface Visitable
6+
{
7+
public function accept(Visitor $visitor);
8+
}

behavioral/visitor/Visitor.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace behavioral\visitor;
4+
5+
interface Visitor
6+
{
7+
public function visitUniversity(University $university): array;
8+
9+
public function visitStudent(Student $student): string;
10+
}
11+

behavioral/visitor/demo.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace behavioral\visitor;
4+
5+
require(__DIR__.'/../../autoloader.php');
6+
7+
$john = new Student("John");
8+
$john->addSickLeave(new \DateTime("2019-10-01"), new \DateTime("2019-10-21"));
9+
$john->addSickLeave(new \DateTime("2019-11-02"), new \DateTime("2019-11-10"));
10+
11+
$jan = new Student("Jan");
12+
$jan->addSickLeave(new \DateTime("2019-11-01"), new \DateTime("2019-11-15"));
13+
14+
$ann = new Student("Ann");
15+
16+
$university = new University("Visitor University", [$john, $jan, $ann]);
17+
$sickLeaveReport = new SickLeaveReport();
18+
$results = $sickLeaveReport->visitUniversity($university);
19+
20+
function show($data)
21+
{
22+
foreach ($data as $datum) {
23+
echo $datum.PHP_EOL;
24+
}
25+
}
26+
27+
show($results);

structural/facade/Image.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@
77

88
class Image {
99

10-
const FILE_THUMBNAIL_WITH = 100;
11-
const FILE_THUMBNAIL_HEIGHT = 100;
10+
private $imagick;
1211

13-
public function thumbnail ($filePath) {
14-
$imagick = new Imagick($filePath);
12+
public function __construct () {
13+
$this->imagick = new Imagick();
14+
}
1515

16-
$imagick->setbackgroundcolor('rgb(0, 0, 0)');
17-
$imagick->thumbnailImage(self::FILE_THUMBNAIL_WITH, self::FILE_THUMBNAIL_HEIGHT, true, true);
16+
public function thumbnail (string $filePath, int $width, int $height) {
17+
$this->imagick->readImage($filePath);
18+
$this->imagick->setbackgroundcolor('rgb(0, 0, 0)');
19+
$this->imagick->thumbnailImage($width, $height, true, true);
1820

1921
$fileInfo = new SplFileInfo($filePath);
2022
$thumbPath = $fileInfo->getBasename('.' . $fileInfo->getExtension()) . '_thumb' . "." . $fileInfo->getExtension();
2123

22-
if (file_put_contents($thumbPath, $imagick)) {
24+
if (file_put_contents($thumbPath, $this->imagick)) {
2325
return true;
2426
} else {
2527
throw new \Exception("Could not create thumbnail.");

structural/facade/demo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
require(__DIR__ . '/../../autoloader.php');
66

77
$image = new Image();
8-
$image->thumbnail("demo.jpg");
8+
$image->thumbnail("demo.jpg", 100, 100);

0 commit comments

Comments
 (0)