Skip to content

Instantly share code, notes, and snippets.

@aotd1
Last active May 22, 2019 05:24
Show Gist options
  • Save aotd1/09c813dd9a77d5f7873d3729f068d0b1 to your computer and use it in GitHub Desktop.
Save aotd1/09c813dd9a77d5f7873d3729f068d0b1 to your computer and use it in GitHub Desktop.
Простой генератор названий версий при недельных релизах
<?php
class VersionGenerator {
protected $adjective = [];
protected $noun = [];
protected $seed;
public function __construct($seed)
{
$this->seed = $seed;
}
/**
* @param array $template
* @return string
*/
public function generate($template = ['adjective'])
{
mt_srand($this->seed);
$result = [];
foreach ($template as $segment) {
$rand = mt_rand(0, count($this->$segment));
$result[] = self::mb_ucfirst($this->$segment[$rand]);
}
return implode(" ", $result);
}
public function loadAdjectives($fileName)
{
$this->adjective = $this->loadFile($fileName);
return $this;
}
public function loadNouns($fileName)
{
$this->noun = $this->loadFile($fileName);
return $this;
}
/**
* @param $fileName
* @return string[]
*/
protected function loadFile($fileName)
{
return file($fileName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
}
protected static function mb_ucfirst($string, $enc = 'UTF-8')
{
return mb_strtoupper(mb_substr($string, 0, 1, $enc), $enc) .
mb_substr($string, 1, mb_strlen($string, $enc), $enc);
}
}
if (!empty($_GET['date'])) {
$time = strtotime($_GET['date']);
if ($time) {
$_GET['seed'] = (int)date("YW", $time);
}
}
$seed = empty($_GET['seed']) ? (int)date("YW") : (int)$_GET['seed'];
$version = (new VersionGenerator($seed))
->loadAdjectives(__DIR__ . '/adjectives.txt')
->loadNouns(__DIR__ . '/nouns.txt')
->generate(['adjective', 'adjective', 'noun']);
if ($_GET['format'] === 'plain'):
header('Content-Type: text/plain');
echo $version;
elseif ($_GET['format']==='json'):
header('Content-Type: application/json');
echo json_encode($version, JSON_UNESCAPED_UNICODE);
else: ?>
<html>
<title><?=$version?></title>
<body>
<h1><small>Астрологи объявили неделю</small> <?=$version?></h1>
<!-- Список query-параметров:
seed integer - инициализатор псевдослучайной последовательности, по умолчанию - номер недели в году
date string (YYY-mm-dd) - альтернативный способ задания seed. Из переданной даты извлекается год и порядковый номер недели.
format string (plain|json|html) - способ отображения информации
Примеры:
/index.php - версия на текущую неделю
/index.php?date=2018-09-04 - версия на дату 2018-09-04
/index.php?date=2018-09-04&format=json - версия на дату 2018-09-04 в виде json-строки
-->
<form method="get">
Узнать версию на дату:
<input type="date" name="date"/>
<input type="submit" value="Узнать"/>
</form>
</body>
</html>
<?php endif?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment