mirror of
https://github.com/igorsimdyanov/php8.git
synced 2025-08-15 23:31:10 +00:00
Глава 37: черновик
This commit is contained in:
14
patterns/factory/models/collection.php
Normal file
14
patterns/factory/models/collection.php
Normal 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);
|
||||
}
|
||||
}
|
16
patterns/factory/models/page.php
Normal file
16
patterns/factory/models/page.php
Normal 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>';
|
||||
}
|
||||
}
|
26
patterns/factory/models/pages.php
Normal file
26
patterns/factory/models/pages.php
Normal 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());
|
||||
}
|
||||
}
|
19
patterns/factory/models/user.php
Normal file
19
patterns/factory/models/user.php
Normal 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) . ')';
|
||||
}
|
||||
}
|
30
patterns/factory/models/users.php
Normal file
30
patterns/factory/models/users.php
Normal 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());
|
||||
}
|
||||
}
|
20
patterns/factory/router.php
Normal file
20
patterns/factory/router.php
Normal 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
7
patterns/factory_use.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
spl_autoload_register();
|
||||
|
||||
use Factory\Router;
|
||||
|
||||
$obj = Router::parse('users');
|
||||
echo $obj->render();
|
11
patterns/mvc/models/user.php
Normal file
11
patterns/mvc/models/user.php
Normal 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) {}
|
||||
}
|
27
patterns/mvc/models/users.php
Normal file
27
patterns/mvc/models/users.php
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user