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
|
||||
namespace Misuzu;
|
||||
|
||||
class Colour {
|
||||
class Colour
|
||||
{
|
||||
private const INHERIT = 0x40000000;
|
||||
|
||||
private $rawValue = 0;
|
||||
|
||||
public function __get(string $name) {
|
||||
public function __get(string $name)
|
||||
{
|
||||
switch ($name) {
|
||||
case 'raw':
|
||||
return $this->rawValue;
|
||||
|
@ -30,44 +32,51 @@ class Colour {
|
|||
return null;
|
||||
}
|
||||
|
||||
public function __set(string $name, $value): void {
|
||||
public function __set(string $name, $value): void
|
||||
{
|
||||
switch ($name) {
|
||||
case 'raw':
|
||||
if (!is_int32($value) && !is_uint32($value))
|
||||
if (!is_int32($value) && !is_uint32($value)) {
|
||||
break;
|
||||
}
|
||||
|
||||
$this->rawValue = $value;
|
||||
break;
|
||||
|
||||
case 'inherit':
|
||||
if (!is_bool($value))
|
||||
if (!is_bool($value)) {
|
||||
break;
|
||||
}
|
||||
|
||||
if ($value)
|
||||
if ($value) {
|
||||
$this->rawValue |= self::INHERIT;
|
||||
else
|
||||
} else {
|
||||
$this->rawValue &= ~self::INHERIT;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'red':
|
||||
if (!is_byte($value))
|
||||
if (!is_byte($value)) {
|
||||
break;
|
||||
}
|
||||
|
||||
$this->rawValue &= ~0xFF0000;
|
||||
$this->rawValue |= $value << 16;
|
||||
break;
|
||||
|
||||
case 'green':
|
||||
if (!is_byte($value))
|
||||
if (!is_byte($value)) {
|
||||
break;
|
||||
}
|
||||
|
||||
$this->rawValue &= ~0xFF00;
|
||||
$this->rawValue |= $value << 8;
|
||||
break;
|
||||
|
||||
case 'blue':
|
||||
if (!is_byte($value))
|
||||
if (!is_byte($value)) {
|
||||
break;
|
||||
}
|
||||
|
||||
$this->rawValue &= ~0xFF;
|
||||
$this->rawValue |= $value;
|
||||
|
@ -76,11 +85,13 @@ class Colour {
|
|||
}
|
||||
}
|
||||
|
||||
public function __construct(int $raw) {
|
||||
public function __construct(int $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 |= ($red & 0xFF) << 16;
|
||||
$raw |= ($green & 0xFF) << 8;
|
||||
|
@ -88,14 +99,16 @@ class Colour {
|
|||
return new static($raw);
|
||||
}
|
||||
|
||||
public static function fromHex(string $hex): Colour {
|
||||
public static function fromHex(string $hex): Colour
|
||||
{
|
||||
$hex = ltrim(strtolower($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];
|
||||
elseif ($hex_length != 6)
|
||||
} elseif ($hex_length != 6) {
|
||||
throw new \Exception('Invalid hex colour format! (find a more appropiate exception type)');
|
||||
}
|
||||
|
||||
return static::fromRGB(
|
||||
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);
|
||||
}
|
||||
|
||||
public function __toString() {
|
||||
public function __toString()
|
||||
{
|
||||
return "#{$this->hex}";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
<?php
|
||||
namespace Misuzu;
|
||||
|
||||
class Zalgo {
|
||||
class Zalgo
|
||||
{
|
||||
private const ZALGO_CHARS_UP = [
|
||||
"\u{030d}", "\u{030e}", "\u{0304}", "\u{0305}", "\u{033f}",
|
||||
"\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_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_MIDDLE, '', $text);
|
||||
$text = str_replace(self::ZALGO_CHARS_DOWN, '', $text);
|
||||
|
@ -56,41 +58,48 @@ class Zalgo {
|
|||
return $text;
|
||||
}
|
||||
|
||||
private static function isZalgo(string $char): bool {
|
||||
private static function isZalgo(string $char): bool
|
||||
{
|
||||
return in_array($char, self::ZALGO_CHARS_UP)
|
||||
|| in_array($char, self::ZALGO_CHARS_MIDDLE)
|
||||
|| 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 = '';
|
||||
|
||||
for ($i = 0; $i < $length; $i++)
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$string .= array_rand_value($array, '');
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if (!$text_length || !$mode || !$direction || !in_array($mode, self::ZALGO_MODES))
|
||||
if (!$text_length || !$mode || !$direction || !in_array($mode, self::ZALGO_MODES)) {
|
||||
return $text;
|
||||
}
|
||||
|
||||
$going_up = has_flag($direction, self::ZALGO_DIR_UP);
|
||||
$going_mid = has_flag($direction, self::ZALGO_DIR_MID);
|
||||
$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;
|
||||
}
|
||||
|
||||
$str = '';
|
||||
|
||||
for ($i = 0; $i < $text_length; $i++) {
|
||||
$char = $text[$i];
|
||||
|
||||
if (self::isZalgo($char))
|
||||
if (self::isZalgo($char)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$str .= $char;
|
||||
|
||||
|
@ -114,9 +123,15 @@ class Zalgo {
|
|||
break;
|
||||
}
|
||||
|
||||
if ($going_up) $str .= self::getZalgo(self::ZALGO_CHARS_UP, $num_up);
|
||||
if ($going_mid) $str .= self::getZalgo(self::ZALGO_CHARS_MIDDLE, $num_mid);
|
||||
if ($going_down) $str .= self::getZalgo(self::ZALGO_CHARS_DOWN, $num_down);
|
||||
if ($going_up) {
|
||||
$str .= self::getZalgo(self::ZALGO_CHARS_UP, $num_up);
|
||||
}
|
||||
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;
|
||||
|
|
|
@ -3,7 +3,8 @@ use PHPUnit\Framework\TestCase;
|
|||
|
||||
use Misuzu\Colour;
|
||||
|
||||
class ColourTest extends TestCase {
|
||||
class ColourTest extends TestCase
|
||||
{
|
||||
public const RED_HEX6 = 67;
|
||||
public const GREEN_HEX6 = 45;
|
||||
public const BLUE_HEX6 = 23;
|
||||
|
@ -18,7 +19,8 @@ class ColourTest extends TestCase {
|
|||
public const STR_HEX3 = '3388dd';
|
||||
public const RAW_HEX3 = 3377373;
|
||||
|
||||
public function testNone() {
|
||||
public function testNone()
|
||||
{
|
||||
$colour = Colour::none();
|
||||
|
||||
$this->assertTrue($colour->inherit);
|
||||
|
@ -29,7 +31,8 @@ class ColourTest extends TestCase {
|
|||
$this->assertEquals($colour->hex, '000000');
|
||||
}
|
||||
|
||||
public function testFromRaw() {
|
||||
public function testFromRaw()
|
||||
{
|
||||
$colour = new Colour(static::RAW_HEX6);
|
||||
|
||||
$this->assertEquals($colour->hex, static::STR_HEX6);
|
||||
|
@ -40,7 +43,8 @@ class ColourTest extends TestCase {
|
|||
$this->assertFalse($colour->inherit);
|
||||
}
|
||||
|
||||
public function testFromRGB() {
|
||||
public function testFromRGB()
|
||||
{
|
||||
$colour = Colour::fromRGB(static::RED_HEX6, static::GREEN_HEX6, static::BLUE_HEX6);
|
||||
|
||||
$this->assertEquals($colour->hex, static::STR_HEX6);
|
||||
|
@ -51,7 +55,8 @@ class ColourTest extends TestCase {
|
|||
$this->assertFalse($colour->inherit);
|
||||
}
|
||||
|
||||
public function testFromHex() {
|
||||
public function testFromHex()
|
||||
{
|
||||
$colour = Colour::fromHex(static::SSTR_HEX6);
|
||||
|
||||
$this->assertEquals($colour->hex, static::STR_HEX6);
|
||||
|
@ -62,7 +67,8 @@ class ColourTest extends TestCase {
|
|||
$this->assertFalse($colour->inherit);
|
||||
}
|
||||
|
||||
public function testFromHex3() {
|
||||
public function testFromHex3()
|
||||
{
|
||||
$colour = Colour::fromHex(static::SSTR_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
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
function array_rand_value(array $array, $fallback = null) {
|
||||
if (!$array)
|
||||
function array_rand_value(array $array, $fallback = null)
|
||||
{
|
||||
if (!$array) {
|
||||
return $fallback;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
function is_sbyte($value): bool {
|
||||
function is_sbyte($value): bool
|
||||
{
|
||||
return is_int_ex($value, -0x80, 0x7F);
|
||||
}
|
||||
|
||||
function is_byte($value): bool {
|
||||
function is_byte($value): bool
|
||||
{
|
||||
return is_int_ex($value, 0x0, 0xFF);
|
||||
}
|
||||
|
||||
function is_int16($value): bool {
|
||||
function is_int16($value): bool
|
||||
{
|
||||
return is_int_ex($value, -0x8000, 0x7FFF);
|
||||
}
|
||||
|
||||
function is_uint16($value): bool {
|
||||
function is_uint16($value): bool
|
||||
{
|
||||
return is_int_ex($value, 0x0, 0xFFFF);
|
||||
}
|
||||
|
||||
function is_int32($value): bool {
|
||||
function is_int32($value): bool
|
||||
{
|
||||
return is_int_ex($value, -0x80000000, 0x7FFFFFFF);
|
||||
}
|
||||
|
||||
function is_uint32($value): bool {
|
||||
function is_uint32($value): bool
|
||||
{
|
||||
return is_int_ex($value, 0x0, 0xFFFFFFFF);
|
||||
}
|
||||
|
||||
function is_int64($value): bool {
|
||||
function is_int64($value): bool
|
||||
{
|
||||
return is_int_ex($value, -0x8000000000000000, 0x7FFFFFFFFFFFFFFF);
|
||||
}
|
||||
|
||||
function is_uint64($value): bool {
|
||||
function is_uint64($value): bool
|
||||
{
|
||||
return is_int_ex($value, 0x0, 0xFFFFFFFFFFFFFFFF);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue