phar-creator/src/model/Person.php

50 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace PHP_PHAR\model;
/**
* Person model
*/
class Person {
// Variables
private string $firstname;
private string $lastname;
private int $age; // not pertinent but okay
// Constructor
public function __construct(string $firstname, string $lastname, ?int $age = null)
{
$this->firstname = $firstname;
$this->lastname = $lastname;
$this->age = isset($age) ? $age : -1;
}
// Magic getter
public function __get(string $val): mixed
{
return isset($this->$val) ? $this->$val : "Unknown attribut";
}
// Magic setter
public function __set(string $val, mixed $value): void
{
if (!(isset($this->$val) && gettype($this->$val) == gettype($value))) return;
switch ($val) {
case 'age':
$this->$val = $value > -1 ? $value : $this->$val;
break;
default:
$this->$val = $value;
break;
}
}
/**
* Retrieve the formated name
*/
public function getFormattedName(): string
{
return sprintf("%s %s", strtoupper($this->lastname), ucfirst(strtolower($this->firstname)));
}
}