Apply fixes from StyleCI
This commit is contained in:
parent
41c23b5e8b
commit
c41efce5bb
4 changed files with 116 additions and 65 deletions
|
@ -1,12 +1,14 @@
|
||||||
<?php
|
<?php
|
||||||
namespace Misuzu;
|
namespace Misuzu;
|
||||||
|
|
||||||
class Colour {
|
class Colour
|
||||||
|
{
|
||||||
private const INHERIT = 0x40000000;
|
private const INHERIT = 0x40000000;
|
||||||
|
|
||||||
private $rawValue = 0;
|
private $rawValue = 0;
|
||||||
|
|
||||||
public function __get(string $name) {
|
public function __get(string $name)
|
||||||
|
{
|
||||||
switch ($name) {
|
switch ($name) {
|
||||||
case 'raw':
|
case 'raw':
|
||||||
return $this->rawValue;
|
return $this->rawValue;
|
||||||
|
@ -30,44 +32,51 @@ class Colour {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __set(string $name, $value): void {
|
public function __set(string $name, $value): void
|
||||||
|
{
|
||||||
switch ($name) {
|
switch ($name) {
|
||||||
case 'raw':
|
case 'raw':
|
||||||
if (!is_int32($value) && !is_uint32($value))
|
if (!is_int32($value) && !is_uint32($value)) {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
$this->rawValue = $value;
|
$this->rawValue = $value;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'inherit':
|
case 'inherit':
|
||||||
if (!is_bool($value))
|
if (!is_bool($value)) {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if ($value)
|
if ($value) {
|
||||||
$this->rawValue |= self::INHERIT;
|
$this->rawValue |= self::INHERIT;
|
||||||
else
|
} else {
|
||||||
$this->rawValue &= ~self::INHERIT;
|
$this->rawValue &= ~self::INHERIT;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'red':
|
case 'red':
|
||||||
if (!is_byte($value))
|
if (!is_byte($value)) {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
$this->rawValue &= ~0xFF0000;
|
$this->rawValue &= ~0xFF0000;
|
||||||
$this->rawValue |= $value << 16;
|
$this->rawValue |= $value << 16;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'green':
|
case 'green':
|
||||||
if (!is_byte($value))
|
if (!is_byte($value)) {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
$this->rawValue &= ~0xFF00;
|
$this->rawValue &= ~0xFF00;
|
||||||
$this->rawValue |= $value << 8;
|
$this->rawValue |= $value << 8;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'blue':
|
case 'blue':
|
||||||
if (!is_byte($value))
|
if (!is_byte($value)) {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
$this->rawValue &= ~0xFF;
|
$this->rawValue &= ~0xFF;
|
||||||
$this->rawValue |= $value;
|
$this->rawValue |= $value;
|
||||||
|
@ -76,11 +85,13 @@ class Colour {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __construct(int $raw) {
|
public function __construct(int $raw)
|
||||||
|
{
|
||||||
$this->rawValue = $raw;
|
$this->rawValue = $raw;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function fromRGB(int $red, int $green, int $blue): Colour {
|
public static function fromRGB(int $red, int $green, int $blue): Colour
|
||||||
|
{
|
||||||
$raw = 0;
|
$raw = 0;
|
||||||
$raw |= ($red & 0xFF) << 16;
|
$raw |= ($red & 0xFF) << 16;
|
||||||
$raw |= ($green & 0xFF) << 8;
|
$raw |= ($green & 0xFF) << 8;
|
||||||
|
@ -88,14 +99,16 @@ class Colour {
|
||||||
return new static($raw);
|
return new static($raw);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function fromHex(string $hex): Colour {
|
public static function fromHex(string $hex): Colour
|
||||||
|
{
|
||||||
$hex = ltrim(strtolower($hex), '#');
|
$hex = ltrim(strtolower($hex), '#');
|
||||||
$hex_length = strlen($hex);
|
$hex_length = strlen($hex);
|
||||||
|
|
||||||
if ($hex_length === 3)
|
if ($hex_length === 3) {
|
||||||
$hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
|
$hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
|
||||||
elseif ($hex_length != 6)
|
} elseif ($hex_length != 6) {
|
||||||
throw new \Exception('Invalid hex colour format! (find a more appropiate exception type)');
|
throw new \Exception('Invalid hex colour format! (find a more appropiate exception type)');
|
||||||
|
}
|
||||||
|
|
||||||
return static::fromRGB(
|
return static::fromRGB(
|
||||||
hexdec(substr($hex, 0, 2)),
|
hexdec(substr($hex, 0, 2)),
|
||||||
|
@ -104,11 +117,13 @@ class Colour {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function none(): Colour {
|
public static function none(): Colour
|
||||||
|
{
|
||||||
return new static(static::INHERIT);
|
return new static(static::INHERIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __toString() {
|
public function __toString()
|
||||||
|
{
|
||||||
return "#{$this->hex}";
|
return "#{$this->hex}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
<?php
|
<?php
|
||||||
namespace Misuzu;
|
namespace Misuzu;
|
||||||
|
|
||||||
class Zalgo {
|
class Zalgo
|
||||||
|
{
|
||||||
private const ZALGO_CHARS_UP = [
|
private const ZALGO_CHARS_UP = [
|
||||||
"\u{030d}", "\u{030e}", "\u{0304}", "\u{0305}", "\u{033f}",
|
"\u{030d}", "\u{030e}", "\u{0304}", "\u{0305}", "\u{033f}",
|
||||||
"\u{0311}", "\u{0306}", "\u{0310}", "\u{0352}", "\u{0357}",
|
"\u{0311}", "\u{0306}", "\u{0310}", "\u{0352}", "\u{0357}",
|
||||||
|
@ -48,7 +49,8 @@ class Zalgo {
|
||||||
public const ZALGO_DIR_MID = 0x2;
|
public const ZALGO_DIR_MID = 0x2;
|
||||||
public const ZALGO_DIR_DOWN = 0x4;
|
public const ZALGO_DIR_DOWN = 0x4;
|
||||||
|
|
||||||
public static function strip(string $text): string {
|
public static function strip(string $text): string
|
||||||
|
{
|
||||||
$text = str_replace(self::ZALGO_CHARS_UP, '', $text);
|
$text = str_replace(self::ZALGO_CHARS_UP, '', $text);
|
||||||
$text = str_replace(self::ZALGO_CHARS_MIDDLE, '', $text);
|
$text = str_replace(self::ZALGO_CHARS_MIDDLE, '', $text);
|
||||||
$text = str_replace(self::ZALGO_CHARS_DOWN, '', $text);
|
$text = str_replace(self::ZALGO_CHARS_DOWN, '', $text);
|
||||||
|
@ -56,45 +58,52 @@ class Zalgo {
|
||||||
return $text;
|
return $text;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function isZalgo(string $char): bool {
|
private static function isZalgo(string $char): bool
|
||||||
|
{
|
||||||
return in_array($char, self::ZALGO_CHARS_UP)
|
return in_array($char, self::ZALGO_CHARS_UP)
|
||||||
|| in_array($char, self::ZALGO_CHARS_MIDDLE)
|
|| in_array($char, self::ZALGO_CHARS_MIDDLE)
|
||||||
|| in_array($char, self::ZALGO_CHARS_DOWN);
|
|| in_array($char, self::ZALGO_CHARS_DOWN);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function getZalgo(array $array, int $length): string {
|
private static function getZalgo(array $array, int $length): string
|
||||||
|
{
|
||||||
$string = '';
|
$string = '';
|
||||||
|
|
||||||
for ($i = 0; $i < $length; $i++)
|
for ($i = 0; $i < $length; $i++) {
|
||||||
$string .= array_rand_value($array, '');
|
$string .= array_rand_value($array, '');
|
||||||
|
}
|
||||||
|
|
||||||
return $string;
|
return $string;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function run(string $text, int $mode = self::ZALGO_MODE_MINI, int $direction = self::ZALGO_DIR_MID | self::ZALGO_DIR_DOWN): string {
|
public static function run(string $text, int $mode = self::ZALGO_MODE_MINI, int $direction = self::ZALGO_DIR_MID | self::ZALGO_DIR_DOWN): string
|
||||||
$text_length = strlen($text);
|
{
|
||||||
|
$text_length = strlen($text);
|
||||||
|
|
||||||
if (!$text_length || !$mode || !$direction || !in_array($mode, self::ZALGO_MODES))
|
if (!$text_length || !$mode || !$direction || !in_array($mode, self::ZALGO_MODES)) {
|
||||||
return $text;
|
return $text;
|
||||||
|
}
|
||||||
|
|
||||||
$going_up = has_flag($direction, self::ZALGO_DIR_UP);
|
$going_up = has_flag($direction, self::ZALGO_DIR_UP);
|
||||||
$going_mid = has_flag($direction, self::ZALGO_DIR_MID);
|
$going_mid = has_flag($direction, self::ZALGO_DIR_MID);
|
||||||
$going_down = has_flag($direction, self::ZALGO_DIR_DOWN);
|
$going_down = has_flag($direction, self::ZALGO_DIR_DOWN);
|
||||||
|
|
||||||
if (!$going_up || !$going_mid || !$going_down)
|
if (!$going_up || !$going_mid || !$going_down) {
|
||||||
return $text;
|
return $text;
|
||||||
|
}
|
||||||
|
|
||||||
$str = '';
|
$str = '';
|
||||||
|
|
||||||
for ($i = 0; $i < $text_length; $i++) {
|
for ($i = 0; $i < $text_length; $i++) {
|
||||||
$char = $text[$i];
|
$char = $text[$i];
|
||||||
|
|
||||||
if (self::isZalgo($char))
|
if (self::isZalgo($char)) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
$str .= $char;
|
$str .= $char;
|
||||||
|
|
||||||
switch ($mode) {
|
switch ($mode) {
|
||||||
case self::ZALGO_MODE_MINI:
|
case self::ZALGO_MODE_MINI:
|
||||||
$num_up = mt_rand(0, 8);
|
$num_up = mt_rand(0, 8);
|
||||||
$num_mid = mt_rand(0, 2);
|
$num_mid = mt_rand(0, 2);
|
||||||
|
@ -103,8 +112,8 @@ class Zalgo {
|
||||||
|
|
||||||
case self::ZALGO_MODE_NORMAL:
|
case self::ZALGO_MODE_NORMAL:
|
||||||
$num_up = mt_rand(0, 16) / 2 + 1;
|
$num_up = mt_rand(0, 16) / 2 + 1;
|
||||||
$num_mid = mt_rand(0, 6) / 2;
|
$num_mid = mt_rand(0, 6) / 2;
|
||||||
$num_down = mt_rand(0, 8) / 2 + 1;
|
$num_down = mt_rand(0, 8) / 2 + 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case self::ZALGO_MODE_MAX:
|
case self::ZALGO_MODE_MAX:
|
||||||
|
@ -114,11 +123,17 @@ class Zalgo {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($going_up) $str .= self::getZalgo(self::ZALGO_CHARS_UP, $num_up);
|
if ($going_up) {
|
||||||
if ($going_mid) $str .= self::getZalgo(self::ZALGO_CHARS_MIDDLE, $num_mid);
|
$str .= self::getZalgo(self::ZALGO_CHARS_UP, $num_up);
|
||||||
if ($going_down) $str .= self::getZalgo(self::ZALGO_CHARS_DOWN, $num_down);
|
|
||||||
}
|
}
|
||||||
|
if ($going_mid) {
|
||||||
|
$str .= self::getZalgo(self::ZALGO_CHARS_MIDDLE, $num_mid);
|
||||||
|
}
|
||||||
|
if ($going_down) {
|
||||||
|
$str .= self::getZalgo(self::ZALGO_CHARS_DOWN, $num_down);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $str;
|
return $str;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,8 @@ use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
use Misuzu\Colour;
|
use Misuzu\Colour;
|
||||||
|
|
||||||
class ColourTest extends TestCase {
|
class ColourTest extends TestCase
|
||||||
|
{
|
||||||
public const RED_HEX6 = 67;
|
public const RED_HEX6 = 67;
|
||||||
public const GREEN_HEX6 = 45;
|
public const GREEN_HEX6 = 45;
|
||||||
public const BLUE_HEX6 = 23;
|
public const BLUE_HEX6 = 23;
|
||||||
|
@ -18,7 +19,8 @@ class ColourTest extends TestCase {
|
||||||
public const STR_HEX3 = '3388dd';
|
public const STR_HEX3 = '3388dd';
|
||||||
public const RAW_HEX3 = 3377373;
|
public const RAW_HEX3 = 3377373;
|
||||||
|
|
||||||
public function testNone() {
|
public function testNone()
|
||||||
|
{
|
||||||
$colour = Colour::none();
|
$colour = Colour::none();
|
||||||
|
|
||||||
$this->assertTrue($colour->inherit);
|
$this->assertTrue($colour->inherit);
|
||||||
|
@ -29,7 +31,8 @@ class ColourTest extends TestCase {
|
||||||
$this->assertEquals($colour->hex, '000000');
|
$this->assertEquals($colour->hex, '000000');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFromRaw() {
|
public function testFromRaw()
|
||||||
|
{
|
||||||
$colour = new Colour(static::RAW_HEX6);
|
$colour = new Colour(static::RAW_HEX6);
|
||||||
|
|
||||||
$this->assertEquals($colour->hex, static::STR_HEX6);
|
$this->assertEquals($colour->hex, static::STR_HEX6);
|
||||||
|
@ -40,7 +43,8 @@ class ColourTest extends TestCase {
|
||||||
$this->assertFalse($colour->inherit);
|
$this->assertFalse($colour->inherit);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFromRGB() {
|
public function testFromRGB()
|
||||||
|
{
|
||||||
$colour = Colour::fromRGB(static::RED_HEX6, static::GREEN_HEX6, static::BLUE_HEX6);
|
$colour = Colour::fromRGB(static::RED_HEX6, static::GREEN_HEX6, static::BLUE_HEX6);
|
||||||
|
|
||||||
$this->assertEquals($colour->hex, static::STR_HEX6);
|
$this->assertEquals($colour->hex, static::STR_HEX6);
|
||||||
|
@ -51,7 +55,8 @@ class ColourTest extends TestCase {
|
||||||
$this->assertFalse($colour->inherit);
|
$this->assertFalse($colour->inherit);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFromHex() {
|
public function testFromHex()
|
||||||
|
{
|
||||||
$colour = Colour::fromHex(static::SSTR_HEX6);
|
$colour = Colour::fromHex(static::SSTR_HEX6);
|
||||||
|
|
||||||
$this->assertEquals($colour->hex, static::STR_HEX6);
|
$this->assertEquals($colour->hex, static::STR_HEX6);
|
||||||
|
@ -62,7 +67,8 @@ class ColourTest extends TestCase {
|
||||||
$this->assertFalse($colour->inherit);
|
$this->assertFalse($colour->inherit);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFromHex3() {
|
public function testFromHex3()
|
||||||
|
{
|
||||||
$colour = Colour::fromHex(static::SSTR_HEX3);
|
$colour = Colour::fromHex(static::SSTR_HEX3);
|
||||||
|
|
||||||
$this->assertEquals($colour->hex, static::STR_HEX3);
|
$this->assertEquals($colour->hex, static::STR_HEX3);
|
||||||
|
|
45
utility.php
45
utility.php
|
@ -2,64 +2,79 @@
|
||||||
// both of these are provided by illuminate/database already but i feel like it makes sense to add these definitions regardless
|
// both of these are provided by illuminate/database already but i feel like it makes sense to add these definitions regardless
|
||||||
|
|
||||||
if (!function_exists('starts_with')) {
|
if (!function_exists('starts_with')) {
|
||||||
function starts_with(string $string, string $text): bool {
|
function starts_with(string $string, string $text): bool
|
||||||
|
{
|
||||||
return substr($string, 0, strlen($text)) === $text;
|
return substr($string, 0, strlen($text)) === $text;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!function_exists('ends_with')) {
|
if (!function_exists('ends_with')) {
|
||||||
function ends_with(string $string, string $text): bool {
|
function ends_with(string $string, string $text): bool
|
||||||
|
{
|
||||||
return substr($string, 0 - strlen($text)) === $text;
|
return substr($string, 0 - strlen($text)) === $text;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function dechex_pad(int $value, int $padding = 2): string {
|
function dechex_pad(int $value, int $padding = 2): string
|
||||||
|
{
|
||||||
return str_pad(dechex($value), $padding, '0', STR_PAD_LEFT);
|
return str_pad(dechex($value), $padding, '0', STR_PAD_LEFT);
|
||||||
}
|
}
|
||||||
|
|
||||||
function array_rand_value(array $array, $fallback = null) {
|
function array_rand_value(array $array, $fallback = null)
|
||||||
if (!$array)
|
{
|
||||||
|
if (!$array) {
|
||||||
return $fallback;
|
return $fallback;
|
||||||
|
}
|
||||||
|
|
||||||
return $array[array_rand($array)];
|
return $array[array_rand($array)];
|
||||||
}
|
}
|
||||||
|
|
||||||
function has_flag(int $flags, int $flag): bool {
|
function has_flag(int $flags, int $flag): bool
|
||||||
|
{
|
||||||
return ($flags & $flag) > 0;
|
return ($flags & $flag) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
function is_int_ex($value, int $boundary_low, int $boundary_high): bool {
|
function is_int_ex($value, int $boundary_low, int $boundary_high): bool
|
||||||
|
{
|
||||||
return is_int($value) && $value >= $boundary_low && $value <= $boundary_high;
|
return is_int($value) && $value >= $boundary_low && $value <= $boundary_high;
|
||||||
}
|
}
|
||||||
|
|
||||||
function is_sbyte($value): bool {
|
function is_sbyte($value): bool
|
||||||
|
{
|
||||||
return is_int_ex($value, -0x80, 0x7F);
|
return is_int_ex($value, -0x80, 0x7F);
|
||||||
}
|
}
|
||||||
|
|
||||||
function is_byte($value): bool {
|
function is_byte($value): bool
|
||||||
|
{
|
||||||
return is_int_ex($value, 0x0, 0xFF);
|
return is_int_ex($value, 0x0, 0xFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
function is_int16($value): bool {
|
function is_int16($value): bool
|
||||||
|
{
|
||||||
return is_int_ex($value, -0x8000, 0x7FFF);
|
return is_int_ex($value, -0x8000, 0x7FFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
function is_uint16($value): bool {
|
function is_uint16($value): bool
|
||||||
|
{
|
||||||
return is_int_ex($value, 0x0, 0xFFFF);
|
return is_int_ex($value, 0x0, 0xFFFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
function is_int32($value): bool {
|
function is_int32($value): bool
|
||||||
|
{
|
||||||
return is_int_ex($value, -0x80000000, 0x7FFFFFFF);
|
return is_int_ex($value, -0x80000000, 0x7FFFFFFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
function is_uint32($value): bool {
|
function is_uint32($value): bool
|
||||||
|
{
|
||||||
return is_int_ex($value, 0x0, 0xFFFFFFFF);
|
return is_int_ex($value, 0x0, 0xFFFFFFFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
function is_int64($value): bool {
|
function is_int64($value): bool
|
||||||
|
{
|
||||||
return is_int_ex($value, -0x8000000000000000, 0x7FFFFFFFFFFFFFFF);
|
return is_int_ex($value, -0x8000000000000000, 0x7FFFFFFFFFFFFFFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
function is_uint64($value): bool {
|
function is_uint64($value): bool
|
||||||
|
{
|
||||||
return is_int_ex($value, 0x0, 0xFFFFFFFFFFFFFFFF);
|
return is_int_ex($value, 0x0, 0xFFFFFFFFFFFFFFFF);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue