Глава 37: черновик

This commit is contained in:
Igor Simdyanov
2022-05-27 22:54:03 +03:00
parent 575b5deb24
commit b0a9f99860
9 changed files with 170 additions and 0 deletions

View File

@ -0,0 +1,14 @@
<?php
namespace Factory\Models;
class Collection extends \Factory\Router
{
public function __construct(public ?string $collection = null) {}
public function render() : array
{
return array_map(
fn($item) => $item->render(),
$this->collection);
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Factory\Models;
class Page extends \Factory\Router
{
public function __construct(
public string $title,
public string $content)
{}
public function render() : string
{
return '<h1>' . htmlspecialchars($this->title) . '</h1>' .
'<p>' . htmlspecialchars($this->content) . '</p>';
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace Factory\Models;
class Pages extends Collection
{
public function __construct(public ?array $pages = null)
{
if(is_null($pages))
{
$pages = [
new Page(
'Первая статья',
'Содержимое первой статьи'),
new Page(
'Вторая статья',
'Содержимое второй статьи')
];
}
parent::__construct($pages);
}
public function render() : string
{
return implode('', parent::render());
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace Factory\Models;
class User extends \Factory\Router
{
public function __construct(
public string $email,
private string $password,
public string $first_name = null,
public string $last_name = null)
{}
public function render() : string
{
$name = implode(' ', [$this->first_name, $this->last_name]);
return '<strong>' . htmlspecialchars($name) . '</strong> ' .
'(' . htmlspecialchars($this->email) . ')';
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace Factory\Models;
class Users extends Collection
{
public function __construct(public ?array $users = null)
{
if(is_null($users))
{
$users = [
new User(
'makkuz@yandex.ru',
'password',
'Максим',
'Кузнецов'),
new User(
'igorsimdyanov@gmail.com',
'password',
'Игорь',
'Симдянов')
];
}
parent::__construct($users);
}
public function render() : string
{
return implode('<br />', parent::render());
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace Factory;
abstract class Router
{
public static function parse(string $url) : mixed
{
$arr = explode('/', $url);
$class = 'Factory\\Models\\' . ucfirst($arr[0]);
$id = count($arr) > 1 ? $arr[1] : null;
$obj = new $class();
if(is_null($id)) {
return $obj;
} else {
return $obj->collection[$id];
}
}
abstract public function render();
}

7
patterns/factory_use.php Normal file
View File

@ -0,0 +1,7 @@
<?php
spl_autoload_register();
use Factory\Router;
$obj = Router::parse('users');
echo $obj->render();

View File

@ -0,0 +1,11 @@
<?php
namespace MVC\models;
class User
{
public function __construct(
public string $email,
private string $password,
public string $first_name = null,
public string $last_name = null) {}
}

View File

@ -0,0 +1,27 @@
<?php
namespace MVC\models;
class Users
{
public $collection;
public function __construct(public ?array $users = null)
{
if(is_null($users))
{
$users = [
new User(
'makkuz@yandex.ru',
'password',
'Максим',
'Кузнецов'),
new User(
'igorsimdyanov@gmail.com',
'password',
'Игорь',
'Симдянов')
];
}
$this->collection = $users;
}
}