222 lines
5.2 KiB
PHP
222 lines
5.2 KiB
PHP
<?php
|
||
|
||
$arrMusic = [
|
||
'Открываем',
|
||
'набор',
|
||
'на',
|
||
'оплачиваемую',
|
||
'стажировку',
|
||
'Вы',
|
||
'спрашивали',
|
||
'вы',
|
||
'ждали',
|
||
'и',
|
||
'вот',
|
||
'мы',
|
||
'возвращаемся',
|
||
'с',
|
||
'анонсом',
|
||
'стажировки',
|
||
'для',
|
||
'разработчиков!',
|
||
'6 месяцев',
|
||
];
|
||
|
||
class Page
|
||
{
|
||
public string $html;
|
||
public string $feed;
|
||
public string $paginator;
|
||
public string $button;
|
||
// ... другие свойства
|
||
|
||
public function __construct() {}
|
||
|
||
public function __toString(): string
|
||
{
|
||
return "Модель: {$this->feed}, Цвет: {$this->paginator}, Двигатель: {$this->button}";
|
||
}
|
||
}
|
||
interface PageBuilder
|
||
{
|
||
public function setFeed(): PageBuilder;
|
||
public function setPaginator(): PageBuilder;
|
||
// public function setBuild(string $engine): PageBuilder;
|
||
// ... другие методы для установки свойств
|
||
public function build(): Page;
|
||
}
|
||
// ConcreteBuilder
|
||
class MusicPageBuilder implements PageBuilder
|
||
{
|
||
private Car $car;
|
||
|
||
public function __construct()
|
||
{
|
||
$this->car = new Car();
|
||
}
|
||
|
||
public function setModel(string $model): CarBuilder
|
||
{
|
||
$this->car->model = $model;
|
||
return $this;
|
||
}
|
||
|
||
public function setColor(string $color): CarBuilder
|
||
{
|
||
$this->car->color = $color;
|
||
return $this;
|
||
}
|
||
|
||
public function setEngine(string $engine): CarBuilder
|
||
{
|
||
$this->car->engine = $engine;
|
||
return $this;
|
||
}
|
||
|
||
// ... другие методы для установки свойств
|
||
|
||
public function build(): Car
|
||
{
|
||
return $this->car;
|
||
}
|
||
}
|
||
|
||
// Director (необязательный)
|
||
class CarDirector
|
||
{
|
||
public function constructCar(CarBuilder $builder): Car
|
||
{
|
||
return $builder->setModel('BMW X5')->setColor('Черный')->setEngine('V8')->build();
|
||
}
|
||
}
|
||
|
||
|
||
// Клиентский код
|
||
$builder = new ConcreteCarBuilder();
|
||
$car = $builder->setModel('Audi A6')->setColor('Синий')->setEngine('V6')->build();
|
||
echo $car . PHP_EOL;
|
||
|
||
$director = new CarDirector();
|
||
$car2 = $director->constructCar($builder);
|
||
echo $car2;
|
||
|
||
|
||
exit();
|
||
|
||
define('LIMIT', 5);
|
||
define('OFFSET', 0);
|
||
|
||
|
||
|
||
// class Paginator
|
||
// {
|
||
const LIMIT = 5;
|
||
const OFFSET = 0;
|
||
private $data = [];
|
||
private $limit = null;
|
||
private $offset = null;
|
||
private $feed = "";
|
||
private $paginator = "";
|
||
public function __construct($data)
|
||
{
|
||
$this->data = $data;
|
||
$this->limit = isset($_GET['limit']) ? $_GET['limit'] : self::LIMIT;
|
||
$this->offset = isset($_GET['offset']) ? $_GET['offset'] : self::OFFSET;
|
||
}
|
||
|
||
public function setFeed()
|
||
{
|
||
$portial = array_slice($this->data, $this->offset, $this->limit);
|
||
$this->feed = '<ul id="feed">';
|
||
foreach ($portial as $value) {
|
||
$this->feed .= "<li>$value</li>";
|
||
}
|
||
$this->feed .= '';
|
||
return $this;
|
||
}
|
||
|
||
public function setPaginator()
|
||
{
|
||
$len = sizeof($this->data);
|
||
$countPage = intdiv($len, $this->limit);
|
||
$this->paginator = '';
|
||
for ($i = 0; $i < $countPage; $i++) {
|
||
$this->paginator .= '<a href= "./portial.php?offset=' . self::LIMIT * $i . '&limit=' . self::LIMIT . '">' . $i . '</a> ';
|
||
}
|
||
return $this;
|
||
}
|
||
|
||
public function build()
|
||
{
|
||
echo <<<abyr
|
||
<!DOCTYPE html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>Pangination</title>
|
||
</head>
|
||
<body>
|
||
$this->feed
|
||
<div id="paginator">
|
||
$this->paginator
|
||
</div>
|
||
<button type="button" click="getPage">Пам-пам</button>
|
||
|
||
</body>
|
||
|
||
</html>
|
||
<script>
|
||
console.log('start script')
|
||
let parent = document.getElementById('paginator')
|
||
parent.addEventListener('click', getPage)
|
||
async function getFetch(url) {
|
||
const response = await fetch(url, { method:'get'} )
|
||
// return await response.json();
|
||
return await response.text();
|
||
|
||
}
|
||
|
||
// parent = document.getElementById('paginator')
|
||
// console.log(parent)
|
||
parent.addEventListener('click' , getPage )
|
||
|
||
async function getPage(event) {
|
||
event.stopPropagation()
|
||
event.preventDefault()
|
||
// let num_page = event.target.getAttribute('data-id')
|
||
let limit = 5
|
||
let url = "page.php?offset" + num_page + "&limit=" + limit
|
||
console.log(url)
|
||
let data = await getFetch(url)
|
||
feed = document.getElementById('feed')
|
||
feed.innerHTML = feed.innerHTML + data
|
||
console.log(data)
|
||
}
|
||
|
||
|
||
</script>
|
||
abyr;
|
||
}
|
||
|
||
|
||
|
||
// define('LIMIT', 5);
|
||
// define('OFFSET', 0);
|
||
|
||
|
||
|
||
// $len = sizeof($arrMusic);
|
||
|
||
// $portial = array_slice($arrMusic, $offset, $limit);
|
||
// print($len);
|
||
|
||
|
||
|
||
|
||
// var_dump($countPage);
|
||
$p = new Paginator($arrMusic);
|
||
$p->renderFeed();
|
||
$p->renderPaginator();
|
||
$p->rederPage();
|