Initial commit

This commit is contained in:
2022-09-22 01:49:58 +02:00
commit e58591a874
10 changed files with 819 additions and 0 deletions

49
src/Spinner.php Normal file
View File

@ -0,0 +1,49 @@
<?php
namespace NoccyLabs\Spinner;
class Spinner
{
private static $defaultStyle = Style\BrailleDotsStyle::class;
private static $defaultFps = 10;
private array $frames = [];
private int $frame = 0;
private float $lastAdvance = 0;
private float $advanceIv = 0;
public static function setDefaultStyle(string $class)
{
$inst = new $class;
if (!($inst instanceof Style\StyleInterface)) {
throw new \DomainException();
}
self::$defaultStyle = $class;
}
public function __construct(?string $style=null, ?int $fps=null)
{
$style = $style??self::$defaultStyle;
$fps = $fps??self::$defaultFps;
$this->advanceIv = 1.0 / $fps;
$style = new $style();
$this->frames = $style->getFrames();
}
public function __toString()
{
$now = microtime(true);
if ($this->lastAdvance + $this->advanceIv < $now) {
$this->frame = ($this->frame + 1) % count($this->frames);
$this->lastAdvance = $now;
}
return $this->frames[$this->frame];
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace NoccyLabs\Spinner\Style;
class BrailleDotsStyle implements StyleInterface
{
public function getFrames(): array
{
return [
mb_chr(0x2839),
mb_chr(0x283c),
mb_chr(0x2836),
mb_chr(0x2827),
mb_chr(0x280f),
mb_chr(0x281b),
];
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace NoccyLabs\Spinner\Style;
class RollingBallStyle implements StyleInterface
{
public function getFrames(): array
{
return [
mb_chr(0x25d0),
mb_chr(0x25d3),
mb_chr(0x25d1),
mb_chr(0x25d2),
];
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace NoccyLabs\Spinner\Style;
class SpinnerStyle implements StyleInterface
{
public function getFrames(): array
{
return [
mb_chr(0x25dc).' ',
' '.mb_chr(0x25dd),
' '.mb_chr(0x25de),
mb_chr(0x25df).' ',
];
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace NoccyLabs\Spinner\Style;
interface StyleInterface
{
public function getFrames(): array;
}