Created
October 26, 2016 20:10
-
-
Save ahoulgrave/836070fb6aa26db423932c900a8a0619 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
class User | |
{ | |
/** | |
* @var string | |
*/ | |
private $name; | |
/** | |
* @var integer | |
*/ | |
private $age; | |
/** | |
* @var float | |
*/ | |
private $weight; | |
/** | |
* @var float | |
*/ | |
private $size; | |
/** | |
* User constructor. | |
* @param string $name | |
* @param int $age | |
* @param float $weight | |
* @param float $size | |
*/ | |
public function __construct($name, $age, $weight, $size) | |
{ | |
$this->setName($name); | |
$this->setAge($age); | |
$this->setWeight($weight); | |
$this->setSize($size); | |
} | |
/** | |
* @return string | |
*/ | |
public function getName() | |
{ | |
return $this->name; | |
} | |
/** | |
* @param string $name | |
*/ | |
public function setName($name) | |
{ | |
$this->name = $name; | |
} | |
/** | |
* @return int | |
*/ | |
public function getAge() | |
{ | |
return $this->age; | |
} | |
/** | |
* @param int $age | |
*/ | |
public function setAge($age) | |
{ | |
$this->age = $age; | |
} | |
/** | |
* @return float | |
*/ | |
public function getWeight() | |
{ | |
return $this->weight; | |
} | |
/** | |
* @param float $weight | |
*/ | |
public function setWeight($weight) | |
{ | |
$this->weight = $weight; | |
} | |
/** | |
* @return float | |
*/ | |
public function getSize() | |
{ | |
return $this->size; | |
} | |
/** | |
* @param float $size | |
*/ | |
public function setSize($size) | |
{ | |
$this->size = $size; | |
} | |
} | |
class UserManager | |
{ | |
const SEPARATOR = '|'; | |
/** | |
* @var string | |
*/ | |
private $file; | |
public function __construct($file = 'users.txt') | |
{ | |
$this->setFile($file); | |
} | |
/** | |
* @param User $user | |
* @return string | |
*/ | |
public function displayUserInfo(User $user) | |
{ | |
$template = " | |
--- Name: " . $user->getName() . " ---<br /> | |
Age: " . $user->getAge() . "<br /> | |
Weight: " . $user->getWeight() . " Size: " . $user->getSize() . "<br />"; | |
return $template; | |
} | |
/** | |
* @return resource | |
*/ | |
public function addUser(User $user) | |
{ | |
$fileName = $this->getFile(); | |
$content = file_get_contents($fileName); | |
$encodedUser = $this->encodeUser($user); | |
$content.= "\n" . $encodedUser; | |
file_put_contents($fileName, $content); | |
} | |
/** | |
* @return User[] | |
*/ | |
public function getUsers() | |
{ | |
$fileName = $this->getFile(); | |
$content = file_get_contents($fileName); | |
$userLines = explode("\n", $content); | |
$users = array(); | |
foreach ($userLines as $userLine) | |
{ | |
if ($userLine) | |
$users[] = $this->decodeUser($userLine); | |
} | |
return $users; | |
} | |
/** | |
* @param $string | |
* @return User | |
*/ | |
public function decodeUser($string) | |
{ | |
$parts = explode(self::SEPARATOR, $string); | |
$user = new User($parts[0], $parts[1], $parts[2], $parts[3]); | |
return $user; | |
} | |
/** | |
* @param User $user | |
* @return string | |
*/ | |
public function encodeUser(User $user) | |
{ | |
return join(self::SEPARATOR, array( | |
$user->getName(), | |
$user->getAge(), | |
$user->getWeight(), | |
$user->getSize() | |
)); | |
} | |
/** | |
* @return string | |
*/ | |
public function getFile() | |
{ | |
return $this->file; | |
} | |
/** | |
* @param string $file | |
*/ | |
public function setFile($file) | |
{ | |
$this->file = $file; | |
} | |
} | |
$userManager = new UserManager(); | |
if (isset($_POST['submit']) && $_POST['submit']) | |
{ | |
$newUser = new User($_POST['name'], $_POST['age'], $_POST['weight'], $_POST['size']); | |
$userManager->addUser($newUser); | |
} | |
$users = $userManager->getUsers(); | |
?> | |
<html> | |
<head> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> | |
<title>Test</title> | |
</head> | |
<body> | |
<div class="container"> | |
<h1>Test</h1> | |
<hr> | |
<?php if (empty($users)) { ?> | |
<div class="alert alert-info">No users</div> | |
<?php } else { ?> | |
<?php foreach ($users as $user) { ?> | |
<?php echo $userManager->displayUserInfo($user); ?> | |
<?php } ?> | |
<?php } ?> | |
<hr> | |
<form action="" method="post"> | |
<input type="text" name="name" placeholder="Name" required> | |
<input type="text" name="age" placeholder="Age" required> | |
<input type="text" name="weight" placeholder="Weight" required> | |
<input type="text" name="size" placeholder="Size" required> | |
<input type="submit" name="submit" value="Create user"> | |
</form> | |
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> | |
</div> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment