initial commit
This commit is contained in:
21
src/factory/personFactory.php
Normal file
21
src/factory/personFactory.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PHP_PHAR\factory;
|
||||
|
||||
use PHP_PHAR\model\Person;
|
||||
|
||||
/**
|
||||
* Generate Person
|
||||
* @return array<Person>
|
||||
*/
|
||||
function makePersons(): array
|
||||
{
|
||||
return [
|
||||
new Person("Jérémi", "Nihart", 20),
|
||||
new Person("Coralie", "Florquin", 21),
|
||||
new Person("Guillaume", "Vander"),
|
||||
new Person("Samuel", "Nihart", 17),
|
||||
new Person("Michel", "Laroux", null)
|
||||
];
|
||||
}
|
||||
36
src/main.php
Normal file
36
src/main.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PHP_PHAR;
|
||||
|
||||
use function PHP_PHAR\factory\makePersons;
|
||||
use function PHP_PHAR\utils\sayHelloToAll;
|
||||
|
||||
// load -> Model, Factory, Utils; then run closure
|
||||
require_once __DIR__ . '/model/Person.php';
|
||||
require_once __DIR__ . '/factory/personFactory.php';
|
||||
require_once __DIR__ . '/utils/func.php';
|
||||
|
||||
// Entry point
|
||||
(function()
|
||||
{
|
||||
$persons = makePersons(); // make demo persons
|
||||
|
||||
echo "==> To beging I will print the data generated by the Person Factory.\n"; // first print
|
||||
sayHelloToAll($persons); // display hello message
|
||||
|
||||
echo "\n==> Now I will edit some data with magic method...\n"; // edit some data
|
||||
$persons[0]->lastname = 'New Last Name Very Long LMAO';
|
||||
$persons[3]->lastname = 'An Other Very Long Last Name';
|
||||
$persons[4]->lastname = 'An New FirstName';
|
||||
sayHelloToAll($persons); // display hello message
|
||||
|
||||
echo "\n==> Now I will edit the age and show it with magic methods...\n"; // edit age, reboot lastname and display
|
||||
$persons[0]->lastname = 'Nihart';
|
||||
echo "BEFORE: " . $persons[0]->getFormattedName() . " & age=" . $persons[0]->age . "\n";
|
||||
$persons[0]->age = 10;
|
||||
echo "AFTER: " . $persons[0]->getFormattedName() . " & age=" . $persons[0]->age . "\n";
|
||||
$persons[0]->age = -20;
|
||||
echo "AFTER 2 WITH NO EFFECT: " . $persons[0]->getFormattedName() . " & age=" . $persons[0]->age . "\n";
|
||||
}
|
||||
)();
|
||||
50
src/model/Person.php
Normal file
50
src/model/Person.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?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)));
|
||||
}
|
||||
}
|
||||
22
src/utils/func.php
Normal file
22
src/utils/func.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PHP_PHAR\utils;
|
||||
|
||||
use PHP_PHAR\model\Person;
|
||||
|
||||
/**
|
||||
* Retrieve the hello world message per person
|
||||
*/
|
||||
function getHelloTo(Person $person): string
|
||||
{
|
||||
return "Hello to our World " . $person->getFormattedName() . " !";
|
||||
}
|
||||
|
||||
/**
|
||||
* Say hello world to a person list
|
||||
*/
|
||||
function sayHelloToAll(array $persons): void
|
||||
{
|
||||
foreach ($persons as $person) echo(getHelloTo($person) . "\n");
|
||||
}
|
||||
Reference in New Issue
Block a user