SOLID/1-single-responsibility-principle.php
thangtd90 c509e66670 Init
2016-04-20 21:15:00 +07:00

64 lines
1.0 KiB
PHP

<?php
// Single Responsibility Principle Violation
class Report
{
public function getTitle()
{
return 'Report Title';
}
public function getDate()
{
return '2016-04-21';
}
public function getContents()
{
return [
'title' => $this->getTitle(),
'date' => $this->getDate(),
];
}
public function formatJson()
{
return json_encode($this->getContents());
}
}
// Refactored
class Report
{
public function getTitle()
{
return 'Report Title';
}
public function getDate()
{
return '2016-04-21';
}
public function getContents()
{
return [
'title' => $this->getTitle(),
'date' => $this->getDate(),
];
}
}
interface ReportFormattable
{
public function format(Report $report);
}
class JsonReportFormatter implements ReportFormattable
{
public function format(Report $report)
{
return json_encode($report->getContents());
}
}