Last active
December 22, 2023 23:23
-
-
Save MiguelMateoTavarez/a1bde3173a6e4b61114f4af4e583975b to your computer and use it in GitHub Desktop.
Ejemplo responsabilidad única
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 | |
enum Gender: int | |
{ | |
case MALE = 1; | |
case FEMALE = 2; | |
case OTHER = 3; | |
static public function getName($value): string | |
{ | |
return match ($value) { | |
self::MALE => 'Male', | |
self::FEMALE => 'Female', | |
self::OTHER => 'Other', | |
default => 'Gender not allowed' | |
}; | |
} | |
} | |
class Person | |
{ | |
public function __construct( | |
public string $name, | |
public Gender $gender, | |
public string $birthday | |
) { | |
$this->name = $name; | |
$this->gender = $gender; | |
$this->birthday = $birthday; | |
} | |
} | |
interface UserInterface | |
{ | |
public function checkCredentials(): bool; | |
} | |
class User implements UserInterface | |
{ | |
public function __construct( | |
public string $email, | |
public string $role, | |
) { | |
$this->email = $email; | |
$this->role = $role; | |
$this->lastAccess = date('Y-m-d'); | |
} | |
public function checkCredentials(): bool | |
{ | |
return true; | |
} | |
} | |
class SettingsProps | |
{ | |
public function __construct( | |
public string $workingDirectory, | |
public string $lastOpenFolder, | |
) { | |
$this->workingDirectory = $workingDirectory; | |
$this->lastOpenFolder = $lastOpenFolder; | |
} | |
} | |
class UserSettings | |
{ | |
public Person $person; | |
public User $user; | |
public SettingsProps $settingsProps; | |
public function __construct( | |
string $workingDirectory, | |
string $lastOpenFolder, | |
string $email, | |
string $role, | |
string $lastAccess, | |
string $name, | |
Gender $gender, | |
string $birthday | |
) | |
{ | |
$this->person = new Person($name, $gender, $birthday); | |
$this->user = new User($email, $role, $lastAccess); | |
$this->settingsProps = new SettingsProps($workingDirectory, $lastOpenFolder); | |
} | |
} | |
$now = date('Y-m-d'); | |
$userSettings = new UserSettings( | |
'/usr/miguel', | |
'/home', | |
'[email protected]', | |
'Admin', | |
$now, | |
'Miguel Mateo', | |
Gender::MALE, | |
'1990-10-12' | |
); | |
extract(get_object_vars($userSettings)); | |
echo json_encode( | |
[ | |
$settingsProps->workingDirectory, | |
$settingsProps->lastOpenFolder, | |
$person->name, | |
"gender" => Gender::getName($person->gender), | |
$person->birthday, | |
$user->email, | |
$user->role, | |
$user->lastAccess | |
] | |
) . "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment