SOLID/1-single-responsibility-principle.php

64 lines
1.0 KiB
PHP
Raw Permalink Normal View History

2016-04-20 13:01:31 +00:00
<?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());
}
}