Created
April 26, 2018 04:00
-
-
Save gusrub/06a194bfa2c4ecd6339426a1c1d64a0c to your computer and use it in GitHub Desktop.
Encapsulating translation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once('i18n.php'); | |
use function Helpers\I18n\translate; | |
class Person | |
{ | |
const VERSION = '1.0.0'; | |
public $firstName; | |
public $lastName; | |
public $dob; | |
function __construct($firstName, $lastName, $dob) | |
{ | |
$this->firstName = $firstName; | |
$this->lastName = $lastName; | |
$this->dob = $dob; | |
} | |
public function printInfo() | |
{ | |
$age = $this->calculateAge(); | |
echo translate("Hello my name is %s %s and I'm %s\n", $this->firstName, $this->lastName, $age); | |
} | |
public function goodBye() | |
{ | |
echo translate("That's it, good bye!\n"); | |
} | |
public function printVersion() | |
{ | |
echo translate("\n(running version %s)\n", self::VERSION); | |
} | |
private function calculateAge() | |
{ | |
$dob = date_create($this->dob); | |
$now = date_create(); | |
$age = date_diff($dob, $now)->format("%y"); | |
$response = null; | |
if($age < 18) { | |
$response = translate("%s years which means I'm a teen", $age); | |
} else { | |
$response = translate("%s years which means I'm a grown up", $age); | |
} | |
return $response; | |
} | |
} | |
$p = new Person( | |
'John', | |
'Wayne', | |
'1980-01-20' | |
); | |
$p->printInfo(); | |
$p->goodBye(); | |
$p->printVersion(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment