Initial commit
This commit is contained in:
49
src/Spinner.php
Normal file
49
src/Spinner.php
Normal 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];
|
||||
}
|
||||
}
|
18
src/Style/BrailleDotsStyle.php
Normal file
18
src/Style/BrailleDotsStyle.php
Normal 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),
|
||||
];
|
||||
}
|
||||
}
|
16
src/Style/RollingBallStyle.php
Normal file
16
src/Style/RollingBallStyle.php
Normal 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),
|
||||
];
|
||||
}
|
||||
}
|
16
src/Style/SpinnerStyle.php
Normal file
16
src/Style/SpinnerStyle.php
Normal 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).' ',
|
||||
];
|
||||
}
|
||||
}
|
8
src/Style/StyleInterface.php
Normal file
8
src/Style/StyleInterface.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\Spinner\Style;
|
||||
|
||||
interface StyleInterface
|
||||
{
|
||||
public function getFrames(): array;
|
||||
}
|
Reference in New Issue
Block a user