Compare commits

...

6 commits

Author SHA1 Message Date
flash 5bd40795a0 Updated TODO list. 2023-11-09 13:55:30 +00:00
flash 987ce5ce52 Added ~OrNull methods to IDbResult. 2023-11-09 13:52:12 +00:00
flash 96700f7044 Added documentation comments to the Colour classes. 2023-11-09 13:36:45 +00:00
flash 41ac00e3e0 Cleanups in XArray. 2023-11-09 13:12:34 +00:00
flash 06f0ba27bb Removed Type class. 2023-11-09 12:59:28 +00:00
flash 1c2058e199 Deprecated IString. 2023-11-09 12:58:04 +00:00
28 changed files with 420 additions and 391 deletions

11
TODO.md
View file

@ -4,15 +4,6 @@
- Create tests for everything testable.
- Draw out a plan for the templating backend.
- Will probably just construct using the HTML abstraction stuff, would make AJAX'ing page updates trivial also.
- Reinvestigate the semi-isolated PHP script template system I used in some other project, I forgot which.
- Determine structure for markup parsing abstraction.
- Writing a bbcode and markdown parser outside of Index, will implement a structure when done.
- Create HTML construction utilities.
- Create similarly address GD2/Imagick wrappers.
- Figure out the SQL Query builder.
@ -22,6 +13,8 @@
- Add RSS/Atom feed construction utilities.
- Verify structure of the DateTime types.
## Low Prio
- Get guides working on phpdoc.

View file

@ -1 +1 @@
0.2309.152244
0.2311.91351

137
composer.lock generated
View file

@ -68,16 +68,16 @@
},
{
"name": "nikic/php-parser",
"version": "v4.16.0",
"version": "v4.17.1",
"source": {
"type": "git",
"url": "https://github.com/nikic/PHP-Parser.git",
"reference": "19526a33fb561ef417e822e85f08a00db4059c17"
"reference": "a6303e50c90c355c7eeee2c4a8b27fe8dc8fef1d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/19526a33fb561ef417e822e85f08a00db4059c17",
"reference": "19526a33fb561ef417e822e85f08a00db4059c17",
"url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/a6303e50c90c355c7eeee2c4a8b27fe8dc8fef1d",
"reference": "a6303e50c90c355c7eeee2c4a8b27fe8dc8fef1d",
"shasum": ""
},
"require": {
@ -118,9 +118,9 @@
],
"support": {
"issues": "https://github.com/nikic/PHP-Parser/issues",
"source": "https://github.com/nikic/PHP-Parser/tree/v4.16.0"
"source": "https://github.com/nikic/PHP-Parser/tree/v4.17.1"
},
"time": "2023-06-25T14:52:30+00:00"
"time": "2023-08-13T19:53:39+00:00"
},
{
"name": "phar-io/manifest",
@ -235,16 +235,16 @@
},
{
"name": "phpstan/phpstan",
"version": "1.10.26",
"version": "1.10.41",
"source": {
"type": "git",
"url": "https://github.com/phpstan/phpstan.git",
"reference": "5d660cbb7e1b89253a47147ae44044f49832351f"
"reference": "c6174523c2a69231df55bdc65b61655e72876d76"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/5d660cbb7e1b89253a47147ae44044f49832351f",
"reference": "5d660cbb7e1b89253a47147ae44044f49832351f",
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/c6174523c2a69231df55bdc65b61655e72876d76",
"reference": "c6174523c2a69231df55bdc65b61655e72876d76",
"shasum": ""
},
"require": {
@ -293,20 +293,20 @@
"type": "tidelift"
}
],
"time": "2023-07-19T12:44:37+00:00"
"time": "2023-11-05T12:57:57+00:00"
},
{
"name": "phpunit/php-code-coverage",
"version": "10.1.2",
"version": "10.1.7",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/php-code-coverage.git",
"reference": "db1497ec8dd382e82c962f7abbe0320e4882ee4e"
"reference": "355324ca4980b8916c18b9db29f3ef484078f26e"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/db1497ec8dd382e82c962f7abbe0320e4882ee4e",
"reference": "db1497ec8dd382e82c962f7abbe0320e4882ee4e",
"url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/355324ca4980b8916c18b9db29f3ef484078f26e",
"reference": "355324ca4980b8916c18b9db29f3ef484078f26e",
"shasum": ""
},
"require": {
@ -363,7 +363,7 @@
"support": {
"issues": "https://github.com/sebastianbergmann/php-code-coverage/issues",
"security": "https://github.com/sebastianbergmann/php-code-coverage/security/policy",
"source": "https://github.com/sebastianbergmann/php-code-coverage/tree/10.1.2"
"source": "https://github.com/sebastianbergmann/php-code-coverage/tree/10.1.7"
},
"funding": [
{
@ -371,20 +371,20 @@
"type": "github"
}
],
"time": "2023-05-22T09:04:27+00:00"
"time": "2023-10-04T15:34:17+00:00"
},
{
"name": "phpunit/php-file-iterator",
"version": "4.0.2",
"version": "4.1.0",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/php-file-iterator.git",
"reference": "5647d65443818959172645e7ed999217360654b6"
"reference": "a95037b6d9e608ba092da1b23931e537cadc3c3c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/5647d65443818959172645e7ed999217360654b6",
"reference": "5647d65443818959172645e7ed999217360654b6",
"url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/a95037b6d9e608ba092da1b23931e537cadc3c3c",
"reference": "a95037b6d9e608ba092da1b23931e537cadc3c3c",
"shasum": ""
},
"require": {
@ -424,7 +424,7 @@
"support": {
"issues": "https://github.com/sebastianbergmann/php-file-iterator/issues",
"security": "https://github.com/sebastianbergmann/php-file-iterator/security/policy",
"source": "https://github.com/sebastianbergmann/php-file-iterator/tree/4.0.2"
"source": "https://github.com/sebastianbergmann/php-file-iterator/tree/4.1.0"
},
"funding": [
{
@ -432,7 +432,7 @@
"type": "github"
}
],
"time": "2023-05-07T09:13:23+00:00"
"time": "2023-08-31T06:24:48+00:00"
},
{
"name": "phpunit/php-invoker",
@ -499,16 +499,16 @@
},
{
"name": "phpunit/php-text-template",
"version": "3.0.0",
"version": "3.0.1",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/php-text-template.git",
"reference": "9f3d3709577a527025f55bcf0f7ab8052c8bb37d"
"reference": "0c7b06ff49e3d5072f057eb1fa59258bf287a748"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/php-text-template/zipball/9f3d3709577a527025f55bcf0f7ab8052c8bb37d",
"reference": "9f3d3709577a527025f55bcf0f7ab8052c8bb37d",
"url": "https://api.github.com/repos/sebastianbergmann/php-text-template/zipball/0c7b06ff49e3d5072f057eb1fa59258bf287a748",
"reference": "0c7b06ff49e3d5072f057eb1fa59258bf287a748",
"shasum": ""
},
"require": {
@ -546,7 +546,8 @@
],
"support": {
"issues": "https://github.com/sebastianbergmann/php-text-template/issues",
"source": "https://github.com/sebastianbergmann/php-text-template/tree/3.0.0"
"security": "https://github.com/sebastianbergmann/php-text-template/security/policy",
"source": "https://github.com/sebastianbergmann/php-text-template/tree/3.0.1"
},
"funding": [
{
@ -554,7 +555,7 @@
"type": "github"
}
],
"time": "2023-02-03T06:56:46+00:00"
"time": "2023-08-31T14:07:24+00:00"
},
{
"name": "phpunit/php-timer",
@ -617,16 +618,16 @@
},
{
"name": "phpunit/phpunit",
"version": "10.2.6",
"version": "10.4.2",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/phpunit.git",
"reference": "1c17815c129f133f3019cc18e8d0c8622e6d9bcd"
"reference": "cacd8b9dd224efa8eb28beb69004126c7ca1a1a1"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/1c17815c129f133f3019cc18e8d0c8622e6d9bcd",
"reference": "1c17815c129f133f3019cc18e8d0c8622e6d9bcd",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/cacd8b9dd224efa8eb28beb69004126c7ca1a1a1",
"reference": "cacd8b9dd224efa8eb28beb69004126c7ca1a1a1",
"shasum": ""
},
"require": {
@ -640,7 +641,7 @@
"phar-io/manifest": "^2.0.3",
"phar-io/version": "^3.0.2",
"php": ">=8.1",
"phpunit/php-code-coverage": "^10.1.1",
"phpunit/php-code-coverage": "^10.1.5",
"phpunit/php-file-iterator": "^4.0",
"phpunit/php-invoker": "^4.0",
"phpunit/php-text-template": "^3.0",
@ -650,8 +651,8 @@
"sebastian/comparator": "^5.0",
"sebastian/diff": "^5.0",
"sebastian/environment": "^6.0",
"sebastian/exporter": "^5.0",
"sebastian/global-state": "^6.0",
"sebastian/exporter": "^5.1",
"sebastian/global-state": "^6.0.1",
"sebastian/object-enumerator": "^5.0",
"sebastian/recursion-context": "^5.0",
"sebastian/type": "^4.0",
@ -666,7 +667,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-main": "10.2-dev"
"dev-main": "10.4-dev"
}
},
"autoload": {
@ -698,7 +699,7 @@
"support": {
"issues": "https://github.com/sebastianbergmann/phpunit/issues",
"security": "https://github.com/sebastianbergmann/phpunit/security/policy",
"source": "https://github.com/sebastianbergmann/phpunit/tree/10.2.6"
"source": "https://github.com/sebastianbergmann/phpunit/tree/10.4.2"
},
"funding": [
{
@ -714,7 +715,7 @@
"type": "tidelift"
}
],
"time": "2023-07-17T12:08:28+00:00"
"time": "2023-10-26T07:21:45+00:00"
},
{
"name": "sebastian/cli-parser",
@ -885,16 +886,16 @@
},
{
"name": "sebastian/comparator",
"version": "5.0.0",
"version": "5.0.1",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/comparator.git",
"reference": "72f01e6586e0caf6af81297897bd112eb7e9627c"
"reference": "2db5010a484d53ebf536087a70b4a5423c102372"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/72f01e6586e0caf6af81297897bd112eb7e9627c",
"reference": "72f01e6586e0caf6af81297897bd112eb7e9627c",
"url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/2db5010a484d53ebf536087a70b4a5423c102372",
"reference": "2db5010a484d53ebf536087a70b4a5423c102372",
"shasum": ""
},
"require": {
@ -905,7 +906,7 @@
"sebastian/exporter": "^5.0"
},
"require-dev": {
"phpunit/phpunit": "^10.0"
"phpunit/phpunit": "^10.3"
},
"type": "library",
"extra": {
@ -949,7 +950,8 @@
],
"support": {
"issues": "https://github.com/sebastianbergmann/comparator/issues",
"source": "https://github.com/sebastianbergmann/comparator/tree/5.0.0"
"security": "https://github.com/sebastianbergmann/comparator/security/policy",
"source": "https://github.com/sebastianbergmann/comparator/tree/5.0.1"
},
"funding": [
{
@ -957,20 +959,20 @@
"type": "github"
}
],
"time": "2023-02-03T07:07:16+00:00"
"time": "2023-08-14T13:18:12+00:00"
},
{
"name": "sebastian/complexity",
"version": "3.0.0",
"version": "3.1.0",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/complexity.git",
"reference": "e67d240970c9dc7ea7b2123a6d520e334dd61dc6"
"reference": "68cfb347a44871f01e33ab0ef8215966432f6957"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/complexity/zipball/e67d240970c9dc7ea7b2123a6d520e334dd61dc6",
"reference": "e67d240970c9dc7ea7b2123a6d520e334dd61dc6",
"url": "https://api.github.com/repos/sebastianbergmann/complexity/zipball/68cfb347a44871f01e33ab0ef8215966432f6957",
"reference": "68cfb347a44871f01e33ab0ef8215966432f6957",
"shasum": ""
},
"require": {
@ -983,7 +985,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-main": "3.0-dev"
"dev-main": "3.1-dev"
}
},
"autoload": {
@ -1006,7 +1008,8 @@
"homepage": "https://github.com/sebastianbergmann/complexity",
"support": {
"issues": "https://github.com/sebastianbergmann/complexity/issues",
"source": "https://github.com/sebastianbergmann/complexity/tree/3.0.0"
"security": "https://github.com/sebastianbergmann/complexity/security/policy",
"source": "https://github.com/sebastianbergmann/complexity/tree/3.1.0"
},
"funding": [
{
@ -1014,7 +1017,7 @@
"type": "github"
}
],
"time": "2023-02-03T06:59:47+00:00"
"time": "2023-09-28T11:50:59+00:00"
},
{
"name": "sebastian/diff",
@ -1149,16 +1152,16 @@
},
{
"name": "sebastian/exporter",
"version": "5.0.0",
"version": "5.1.1",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/exporter.git",
"reference": "f3ec4bf931c0b31e5b413f5b4fc970a7d03338c0"
"reference": "64f51654862e0f5e318db7e9dcc2292c63cdbddc"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/f3ec4bf931c0b31e5b413f5b4fc970a7d03338c0",
"reference": "f3ec4bf931c0b31e5b413f5b4fc970a7d03338c0",
"url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/64f51654862e0f5e318db7e9dcc2292c63cdbddc",
"reference": "64f51654862e0f5e318db7e9dcc2292c63cdbddc",
"shasum": ""
},
"require": {
@ -1172,7 +1175,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-main": "5.0-dev"
"dev-main": "5.1-dev"
}
},
"autoload": {
@ -1214,7 +1217,8 @@
],
"support": {
"issues": "https://github.com/sebastianbergmann/exporter/issues",
"source": "https://github.com/sebastianbergmann/exporter/tree/5.0.0"
"security": "https://github.com/sebastianbergmann/exporter/security/policy",
"source": "https://github.com/sebastianbergmann/exporter/tree/5.1.1"
},
"funding": [
{
@ -1222,7 +1226,7 @@
"type": "github"
}
],
"time": "2023-02-03T07:06:49+00:00"
"time": "2023-09-24T13:22:09+00:00"
},
{
"name": "sebastian/global-state",
@ -1288,16 +1292,16 @@
},
{
"name": "sebastian/lines-of-code",
"version": "2.0.0",
"version": "2.0.1",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/lines-of-code.git",
"reference": "17c4d940ecafb3d15d2cf916f4108f664e28b130"
"reference": "649e40d279e243d985aa8fb6e74dd5bb28dc185d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/lines-of-code/zipball/17c4d940ecafb3d15d2cf916f4108f664e28b130",
"reference": "17c4d940ecafb3d15d2cf916f4108f664e28b130",
"url": "https://api.github.com/repos/sebastianbergmann/lines-of-code/zipball/649e40d279e243d985aa8fb6e74dd5bb28dc185d",
"reference": "649e40d279e243d985aa8fb6e74dd5bb28dc185d",
"shasum": ""
},
"require": {
@ -1333,7 +1337,8 @@
"homepage": "https://github.com/sebastianbergmann/lines-of-code",
"support": {
"issues": "https://github.com/sebastianbergmann/lines-of-code/issues",
"source": "https://github.com/sebastianbergmann/lines-of-code/tree/2.0.0"
"security": "https://github.com/sebastianbergmann/lines-of-code/security/policy",
"source": "https://github.com/sebastianbergmann/lines-of-code/tree/2.0.1"
},
"funding": [
{
@ -1341,7 +1346,7 @@
"type": "github"
}
],
"time": "2023-02-03T07:08:02+00:00"
"time": "2023-08-31T09:25:50+00:00"
},
{
"name": "sebastian/object-enumerator",

View file

@ -1,7 +1,7 @@
<?php
// AString.php
// Created: 2021-04-26
// Updated: 2023-07-05
// Updated: 2023-11-09
namespace Index;
@ -11,7 +11,7 @@ use InvalidArgumentException;
/**
* Provides an immutable ASCII string with arrow methods.
*
* Normal PHP strings should be used for buffers/byte arrays.
* @deprecated IString, AString and WString are deprecated and will be removed. Use XString methods instead.
*/
final class AString implements IString {
use XStringTrait;

View file

@ -1,36 +1,63 @@
<?php
// ArrayIterator.php
// Created: 2022-02-03
// Updated: 2022-02-03
// Updated: 2023-11-09
namespace Index\Collections;
namespace Index;
use Iterator;
/**
* Provides an Iterator implementation for normal arrays.
*/
class ArrayIterator implements Iterator {
private array $array;
private bool $wasValid = true;
/**
* @param array $array Array to iterate upon.
*/
public function __construct(array $array) {
$this->array = $array;
}
/**
* Returns the current element.
*
* @return mixed Can return any type.
*/
public function current(): mixed {
return current($this->array);
}
/**
* Returns the key of the current element.
*
* @return mixed Returns scalar on success, or null on failure.
*/
public function key(): mixed {
return key($this->array);
}
/**
* Moves forward to next element.
*/
public function next(): void {
$this->wasValid = next($this->array) !== false;
}
/**
* Rewind the Iterator to the first element.
*/
public function rewind(): void {
$this->wasValid = reset($this->array) !== false;
}
/**
* Checks if current position is valid.
*
* @return bool Returns true on success or false on failure.
*/
public function valid(): bool {
return $this->wasValid;
}

View file

@ -1,10 +0,0 @@
<?php
// IArrayable.php
// Created: 2022-02-03
// Updated: 2022-02-03
namespace Index\Collections;
interface IArrayable {
function toArray(): array;
}

View file

@ -1,21 +1,57 @@
<?php
// Colour.php
// Created: 2023-01-02
// Updated: 2023-09-15
// Updated: 2023-11-09
namespace Index\Colour;
use Stringable;
use Index\XNumber;
/**
* Abstract class for representing colours.
*/
abstract class Colour implements Stringable {
/**
* Retrieves the Red RGB byte.
*
* @return int A number ranging from 0 to 255.
*/
public abstract function getRed(): int;
/**
* Retrieves the Green RGB byte.
*
* @return int A number ranging from 0 to 255.
*/
public abstract function getGreen(): int;
/**
* Retrieves the Blue RGB byte.
*
* @return int A number ranging from 0 to 255.
*/
public abstract function getBlue(): int;
/**
* Retrieves the alpha component.
*
* @return float A number ranging from 0.0 to 1.0.
*/
public abstract function getAlpha(): float;
/**
* Whether this colour should be ignored and another should be used instead.
*
* @return bool true if this colour should be ignored.
*/
public abstract function shouldInherit(): bool;
/**
* Returns this colour in a format CSS understands.
*
* @return string CSS compatible colour.
*/
public abstract function __toString(): string;
private const READABILITY_THRESHOLD = 186.0;
@ -25,26 +61,57 @@ abstract class Colour implements Stringable {
// luminance shit might need further review
/**
* Calculates the luminance value of this colour.
*
* @return float Luminance value for this colour.
*/
public function getLuminance(): float {
return self::LUMINANCE_WEIGHT_RED * $this->getRed()
+ self::LUMINANCE_WEIGHT_GREEN * $this->getGreen()
+ self::LUMINANCE_WEIGHT_BLUE * $this->getBlue();
}
/**
* Checks whether this colour is on the brighter side of things.
*
* @return bool true if this colour would not be very legible on a light background.
*/
public function isLight(): bool {
return $this->getLuminance() > self::READABILITY_THRESHOLD;
}
/**
* Checks whether this colour is on the darker side of things.
*
* @return bool true if this colour would not be very legible on a dark background.
*/
public function isDark(): bool {
return $this->getLuminance() <= self::READABILITY_THRESHOLD;
}
private static ColourNull $none;
/**
* Gets an empty colour.
*
* @return Colour A global instance of ColourNull.
*/
public static function none(): Colour {
return self::$none;
}
/**
* Mixes two colours.
* 0.0 -> Entirely $colour1
* 0.5 -> Midpoint between $colour1 and $colour2
* 1.0 -> Entirely $colour2
*
* @param Colour $colour1 Starting colour.
* @param Colour $colour2 Ending colour.
* @param float $weight Weight of $colour2.
* @return Colour Mixed colour.
*/
public static function mix(Colour $colour1, Colour $colour2, float $weight): Colour {
$weight = min(1, max(0, $weight));
@ -65,18 +132,42 @@ abstract class Colour implements Stringable {
private const MSZ_INHERIT = 0x40000000;
/**
* Creates a Colour object from raw Misuzu format.
*
* If bit 31 is set the global instance of ColourNull will always be returned,
* otherwise an instance of ColourRGB will be created using the fromRawRGB method (top 8 bits ignored).
*
* @param int $raw Raw RGB colour in Misuzu format.
* @return Colour Colour instance representing the Misuzu colour.
*/
public static function fromMisuzu(int $raw): Colour {
if($raw & self::MSZ_INHERIT)
return self::$none;
return ColourRGB::fromRawRGB($raw);
}
/**
* Converts a Colour object to raw Misuzu format.
*
* If shouldInherit is true, an integer with only bit 31 will be returned,
* otherwise a raw RGB value will be returned.
*
* @param Colour $colour Colour to be converted to raw Misuzu format.
* @return int Raw Misuzu format colour.
*/
public static function toMisuzu(Colour $colour): int {
if($colour->shouldInherit())
return self::MSZ_INHERIT;
return ($colour->getRed() << 16) | ($colour->getGreen() << 8) | $colour->getBlue();
}
/**
* Attempts to parse a CSS format colour.
*
* @param ?string $value CSS format colour.
* @return Colour Parsed CSS colour.
*/
public static function parse(?string $value): Colour {
if($value === null)
return self::$none;
@ -205,6 +296,7 @@ abstract class Colour implements Stringable {
return self::$none;
}
/** @internal */
public static function init(): void {
self::$none = new ColourNull;
}

View file

@ -1,10 +1,13 @@
<?php
// ColourHSL.php
// Created: 2023-01-02
// Updated: 2023-01-02
// Updated: 2023-11-09
namespace Index\Colour;
/**
* Represents a colour using Hue, Saturation and Lightness.
*/
class ColourHSL extends Colour {
private float $hue;
private float $saturation;
@ -15,6 +18,12 @@ class ColourHSL extends Colour {
private int $green;
private int $blue;
/**
* @param float $hue Hue property.
* @param float $saturation Saturation property.
* @param float $lightness Lightness property.
* @param float $alpha Alpha property.
*/
public function __construct(float $hue, float $saturation, float $lightness, float $alpha = 1.0) {
$this->hue = $hue;
$this->saturation = max(0.0, min(1.0, $saturation));
@ -64,14 +73,29 @@ class ColourHSL extends Colour {
return $this->blue;
}
/**
* Retrieves the Hue component.
*
* @return float Hue rotation value.
*/
public function getHue(): float {
return $this->hue;
}
/**
* Retrieves the Saturation component.
*
* @return float Saturation value.
*/
public function getSaturation(): float {
return $this->saturation;
}
/**
* Retrieves the lightness component.
*
* @return float Lightness value.
*/
public function getLightness(): float {
return $this->lightness;
}
@ -97,6 +121,12 @@ class ColourHSL extends Colour {
return sprintf('hsl(%sdeg, %s%%, %s%%)', $hue, $sat, $lig);
}
/**
* Converts any other colour to a ColourHSL.
*
* @param Colour $colour Original colour.
* @return ColourHSL Converted colour.
*/
public static function convert(Colour $colour): ColourHSL {
if($colour instanceof ColourHSL)
return $colour;

View file

@ -1,10 +1,13 @@
<?php
// ColourNamed.php
// Created: 2023-01-02
// Updated: 2023-01-02
// Updated: 2023-11-09
namespace Index\Colour;
/**
* Represents a named CSS colour.
*/
class ColourNamed extends Colour {
private string $name;
private int $red = 0;
@ -12,6 +15,9 @@ class ColourNamed extends Colour {
private int $blue = 0;
private bool $transparent;
/**
* @param string $name CSS colour name.
*/
public function __construct(string $name) {
$this->name = strtolower($name);
@ -28,10 +34,20 @@ class ColourNamed extends Colour {
}
}
/**
* Retrieves the CSS name of this colour.
*
* @return string CSS name.
*/
public function getName(): string {
return $this->name;
}
/**
* Retrieves whether this CSS colour is transparent.
*
* @return bool true if the colour is transparent.
*/
public function isTransparent(): bool {
return $this->transparent;
}
@ -60,14 +76,26 @@ class ColourNamed extends Colour {
return $this->name;
}
/**
* Returns all supported CSS colour names.
*
* @return string[] All supported CSS colour names.
*/
public static function getNames(): array {
return array_keys(self::COLOURS);
}
/**
* Checks whether a CSS colour name is supported.
*
* @param string $name CSS colour name.
* @return bool true if the colour name is supported.
*/
public static function isValidName(string $name): bool {
return array_key_exists($name, self::COLOURS);
}
/** @var array<string, int> */
private const COLOURS = [
// CSS Level 1
'black' => 0x000000,

View file

@ -1,10 +1,15 @@
<?php
// ColourNull.php
// Created: 2023-01-02
// Updated: 2023-01-02
// Updated: 2023-11-09
namespace Index\Colour;
/**
* Represents an empty colour.
*
* If possible, use Colour::empty() to get the global instance of this class.
*/
class ColourNull extends Colour {
public function getRed(): int {
return 0;

View file

@ -1,16 +1,25 @@
<?php
// ColourRGB.php
// Created: 2023-01-02
// Updated: 2023-01-02
// Updated: 2023-11-09
namespace Index\Colour;
/**
* Represents an RGB colour.
*/
class ColourRGB extends Colour {
private int $red;
private int $green;
private int $blue;
private float $alpha;
/**
* @param int $red Red property.
* @param int $green Green property.
* @param int $blue Blue property.
* @param float $alpha Alpha property.
*/
public function __construct(int $red, int $green, int $blue, float $alpha = 1.0) {
$this->red = max(0, min(255, $red));
$this->green = max(0, min(255, $green));
@ -46,6 +55,12 @@ class ColourRGB extends Colour {
return sprintf('#%02x%02x%02x', $this->red, $this->green, $this->blue);
}
/**
* Create a ColourRGB instance from a raw RGB value.
*
* @param int $raw A raw RGB colour in 0xRRGGBB format.
* @return ColourRGB An instance of ColourRGB.
*/
public static function fromRawRGB(int $raw): ColourRGB {
return new ColourRGB(
(($raw >> 16) & 0xFF),
@ -55,6 +70,12 @@ class ColourRGB extends Colour {
);
}
/**
* Create a ColourRGB instance from a raw ARGB value.
*
* @param int $raw A raw ARGB colour in 0xAARRGGBB format.
* @return ColourRGB An instance of ColourRGB.
*/
public static function fromRawARGB(int $raw): ColourRGB {
return new ColourRGB(
(($raw >> 16) & 0xFF),
@ -64,6 +85,12 @@ class ColourRGB extends Colour {
);
}
/**
* Create a ColourRGB instance from a raw RGBA value.
*
* @param int $raw A raw RGBA colour in 0xRRGGBBAA format.
* @return ColourRGB An instance of ColourRGB.
*/
public static function fromRawRGBA(int $raw): ColourRGB {
return new ColourRGB(
(($raw >> 24) & 0xFF),
@ -73,6 +100,12 @@ class ColourRGB extends Colour {
);
}
/**
* Converts any other colour to a ColourRGB.
*
* @param Colour $colour Original colour.
* @return ColourRGB Converted colour.
*/
public static function convert(Colour $colour): ColourRGB {
if($colour instanceof ColourRGB)
return $colour;

View file

@ -0,0 +1,32 @@
<?php
// DbResultTrait.php
// Created: 2023-11-09
// Updated: 2023-11-09
namespace Index\Data;
trait DbResultTrait {
public function getString(int|string $index): string {
return (string)$this->getValue($index);
}
public function getStringOrNull(int|string $index): ?string {
return $this->isNull($index) ? null : (string)$this->getValue($index);
}
public function getInteger(int|string $index): int {
return (int)$this->getValue($index);
}
public function getIntegerOrNull(int|string $index): ?int {
return $this->isNull($index) ? null : (int)$this->getValue($index);
}
public function getFloat(int|string $index): float {
return (float)$this->getValue($index);
}
public function getFloatOrNull(int|string $index): ?float {
return $this->isNull($index) ? null : (float)$this->getValue($index);
}
}

View file

@ -1,13 +1,11 @@
<?php
// IDbResult.php
// Created: 2021-05-02
// Updated: 2022-02-16
// Updated: 2023-11-09
namespace Index\Data;
use Index\AString;
use Index\ICloseable;
use Index\WString;
use Index\IO\Stream;
/**
@ -46,21 +44,12 @@ interface IDbResult extends ICloseable {
function getString(int|string $index): string;
/**
* Gets the value from the target index cast as an ASCII string.
* Checks if the field is null, then gets the value from the target index cast as a native string.
*
* @param int|string $index Target index.
* @return AString Returns an AString of the value.
* @return ?string Returns a string of the value or null.
*/
function getAString(int|string $index): AString;
/**
* Gets the value from the target index cast as a multi-byte string.
*
* @param int|string $index Target index.
* @param string $encoding Encoding of the string.
* @return WString Returns an WString of the value.
*/
function getWString(int|string $index, string $encoding): WString;
function getStringOrNull(int|string $index): ?string;
/**
* Gets the value from the target index cast as an integer.
@ -70,6 +59,14 @@ interface IDbResult extends ICloseable {
*/
function getInteger(int|string $index): int;
/**
* Checks if the field is null, then gets the value from the target index cast as an integer.
*
* @param int|string $index Target index.
* @return int Returns the value cast to an integer or null.
*/
function getIntegerOrNull(int|string $index): ?int;
/**
* Gets the value from the target index cast as a floating point number.
*
@ -78,6 +75,14 @@ interface IDbResult extends ICloseable {
*/
function getFloat(int|string $index): float;
/**
* Checks if the field is null, then gets the value from the target index cast as a floating point number.
*
* @param int|string $index Target index.
* @return float Returns the value cast to a floating point number or null.
*/
function getFloatOrNull(int|string $index): ?float;
/**
* Gets the value from the target index as a Stream.
*

View file

@ -1,7 +1,7 @@
<?php
// MariaDBConnection.php
// Created: 2021-04-30
// Updated: 2023-07-10
// Updated: 2023-11-09
namespace Index\Data\MariaDB;
@ -9,7 +9,6 @@ use mysqli;
use mysqli_sql_exception;
use InvalidArgumentException;
use RuntimeException;
use Index\AString;
use Index\Version;
use Index\Data\BeginTransactionFailedException;
use Index\Data\DataException;

View file

@ -1,15 +1,14 @@
<?php
// MariaDBResult.php
// Created: 2021-05-02
// Updated: 2023-01-01
// Updated: 2023-11-09
namespace Index\Data\MariaDB;
use mysqli_result;
use mysqli_stmt;
use InvalidArgumentException;
use Index\AString;
use Index\WString;
use Index\Data\DbResultTrait;
use Index\Data\IDbResult;
use Index\IO\Stream;
use Index\IO\TempFileStream;
@ -18,6 +17,8 @@ use Index\IO\TempFileStream;
* Represents a MariaDB/MySQL database result.
*/
abstract class MariaDBResult implements IDbResult {
use DbResultTrait;
protected mysqli_stmt|mysqli_result $result;
protected array $currentRow = [];
@ -69,26 +70,6 @@ abstract class MariaDBResult implements IDbResult {
return $this->currentRow[$index] ?? null;
}
public function getString(int|string $index): string {
return (string)$this->getValue($index);
}
public function getAString(int|string $index): AString {
return new AString($this->getString($index));
}
public function getWString(int|string $index, string $encoding): WString {
return new WString($this->getString($index), $encoding);
}
public function getInteger(int|string $index): int {
return (int)$this->getValue($index);
}
public function getFloat(int|string $index): float {
return (float)$this->getValue($index);
}
public function getStream(int|string $index): ?Stream {
if($this->isNull($index))
return null;

View file

@ -1,12 +1,10 @@
<?php
// NullDbResult.php
// Created: 2021-05-02
// Updated: 2022-02-16
// Updated: 2023-11-09
namespace Index\Data\NullDb;
use Index\AString;
use Index\WString;
use Index\Data\IDbResult;
use Index\IO\Stream;
@ -30,22 +28,26 @@ class NullDbResult implements IDbResult {
return '';
}
public function getAString(int|string $index): AString {
return AString::empty();
}
public function getWString(int|string $index, string $encoding): WString {
return WString::empty();
public function getStringOrNull(int|string $index): ?string {
return null;
}
public function getInteger(int|string $index): int {
return 0;
}
public function getIntegerOrNull(int|string $index): ?int {
return null;
}
public function getFloat(int|string $index): float {
return 0.0;
}
public function getFloatOrNull(int|string $index): ?float {
return null;
}
public function getStream(int|string $index): ?Stream {
return null;
}

View file

@ -1,14 +1,13 @@
<?php
// SQLiteResult.php
// Created: 2021-05-02
// Updated: 2023-01-01
// Updated: 2023-11-09
namespace Index\Data\SQLite;
use SQLite3Result;
use InvalidArgumentException;
use Index\AString;
use Index\WString;
use Index\Data\DbResultTrait;
use Index\Data\IDbResult;
use Index\IO\Stream;
use Index\IO\TempFileStream;
@ -17,6 +16,8 @@ use Index\IO\TempFileStream;
* Represents an SQLite result set.
*/
class SQLiteResult implements IDbResult {
use DbResultTrait;
private SQLite3Result $result;
private array $currentRow = [];
@ -55,26 +56,6 @@ class SQLiteResult implements IDbResult {
return $this->currentRow[$index] ?? null;
}
public function getString(int|string $index): string {
return (string)$this->getValue($index);
}
public function getAString(int|string $index): AString {
return new AString($this->getString($index));
}
public function getWString(int|string $index, string $encoding): WString {
return new WString($this->getString($index), $encoding);
}
public function getInteger(int|string $index): int {
return (int)$this->getValue($index);
}
public function getFloat(int|string $index): float {
return (float)$this->getValue($index);
}
/**
* Gets the value from the target index as a Stream.
* If you're aware that you're using SQLite it may make more sense to use SQLiteConnection::getBlobStream instead.

View file

@ -1,7 +1,7 @@
<?php
// HttpFx.php
// Created: 2022-02-15
// Updated: 2023-09-11
// Updated: 2023-11-09
namespace Index\Http;
@ -9,8 +9,6 @@ use stdClass;
use Exception;
use JsonSerializable;
use Index\Environment;
use Index\IString;
use Index\WString;
use Index\IO\Stream;
use Index\Http\Content\BencodedContent;
use Index\Http\Content\JsonContent;
@ -115,7 +113,7 @@ class HttpFx implements IRouter {
if(!$responseBuilder->hasContentType())
$responseBuilder->setTypeJson();
$responseBuilder->setContent(new JsonContent($result));
} elseif(is_object($result) && !($result instanceof IString)) {
} elseif(is_object($result)) {
foreach($this->objectHandlers as $info)
if($info[0]($result)) {
$info[1]($responseBuilder, $result);
@ -124,17 +122,18 @@ class HttpFx implements IRouter {
}
if(!$responseBuilder->hasContent() && $result !== null) {
$charset = ($result instanceof WString) ? $result->getEncoding() : null;
$result = (string)$result;
$responseBuilder->setContent(new StringContent($result));
if(!$responseBuilder->hasContentType()) {
$charset = strtolower(mb_preferred_mime_name(mb_detect_encoding($result)));
if(strtolower(substr($result, 0, 5)) === '<?xml')
$responseBuilder->setTypeXML($charset ?? WString::getDefaultEncoding());
$responseBuilder->setTypeXML($charset);
elseif(strtolower(substr($result, 0, 14)) === '<!doctype html')
$responseBuilder->setTypeHTML($charset ?? WString::getDefaultEncoding());
$responseBuilder->setTypeHTML($charset);
else
$responseBuilder->setTypePlain($charset ?? 'us-ascii');
$responseBuilder->setTypePlain($charset);
}
}
}
@ -154,7 +153,7 @@ class HttpFx implements IRouter {
'<!doctype html><html><head><meta charset="%3$s"/><title>%1$03d %2$s</title></head><body><center><h1>%1$03d %2$s</h1></center><hr/><center>Index</center></body></html>',
$code,
$message,
WString::getDefaultEncoding()
strtolower(mb_preferred_mime_name(mb_internal_encoding()))
)));
}

View file

@ -1,7 +1,7 @@
<?php
// IString.php
// Created: 2021-06-20
// Updated: 2023-07-05
// Updated: 2023-11-09
namespace Index;
@ -12,6 +12,9 @@ use JsonSerializable;
use Stringable;
use Index\Serialisation\IBencodeSerialisable;
/**
* @deprecated IString, AString and WString are deprecated and will be removed. Use XString methods instead.
*/
interface IString extends ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Stringable, IComparable, ICloneable, IEquatable, IBencodeSerialisable {
/**
* Default trim characters.

View file

@ -1,7 +1,7 @@
<?php
// StringIterator.php
// Created: 2022-02-02
// Updated: 2022-02-02
// Updated: 2023-11-09
namespace Index;
@ -9,6 +9,8 @@ use Iterator;
/**
* Provides an iterator for IString types.
*
* @deprecated Will be removed along with IString, AString and WString.
*/
class StringIterator implements Iterator {
private IString $value;

View file

@ -1,126 +0,0 @@
<?php
// Type.php
// Created: 2021-04-26
// Updated: 2022-02-28
namespace Index;
use ReflectionClass;
use RuntimeException;
final class Type {
/**
* Boolean type id.
*
* @var string
*/
public const BOOL = 'boolean';
/**
* Integer type id.
*
* @var string
*/
public const INTEGER = 'integer';
/**
* Floating point type id.
*
* @var string
*/
public const FLOAT = 'double';
/**
* String type id.
*
* @var string
*/
public const STRING = 'string';
/**
* Array type id.
*
* @var string
*/
public const ARRAY = 'array';
/**
* Object type id.
*
* @var string
*/
public const OBJECT = 'object';
/**
* Resource type id.
*
* @var string
*/
public const RESOURCE = 'resource';
/**
* Closed resource type id.
*
* @var string
*/
public const CLOSED_RESOURCE = 'resource (closed)';
/**
* NULL type id.
*
* @var string
*/
public const NULL = 'NULL';
/**
* Unknown type id.
*
* @var string
*/
public const UNKNOWN = 'unknown type';
/**
* Gets the default value for this type.
*
* @return mixed Default value for the given base type.
*/
public static function default(string $type): mixed {
switch($type) {
case self::BOOL:
return false;
case self::INTEGER:
return 0;
case self::FLOAT:
return 0.0;
case self::STRING:
return '';
case self::ARRAY:
return [];
default:
return null;
}
}
/**
* Gets a list of all class names this Type inherits.
*
* @param string|object $target Target object.
* @return array Array containing the names.
*/
public static function classTree(string|object $target): array {
if(is_object($target))
$target = get_class($target);
$tree = [];
$classInfo = new ReflectionClass($target);
foreach($classInfo->getInterfaceNames() as $interface)
$tree[] = $interface;
do {
$tree[] = $classInfo->getName();
} while(($classInfo = $classInfo->getParentClass()) !== false);
return $tree;
}
}

View file

@ -1,7 +1,7 @@
<?php
// WString.php
// Created: 2021-06-22
// Updated: 2023-07-05
// Updated: 2023-11-09
namespace Index;
@ -11,6 +11,8 @@ use ValueError;
/**
* Provides an immutable multi-byte string with arrow methods.
*
* @deprecated IString, AString and WString are deprecated and will be removed. Use XString methods instead.
*/
final class WString implements IString {
use XStringTrait;

View file

@ -1,7 +1,7 @@
<?php
// XArray.php
// Created: 2022-02-02
// Updated: 2022-02-14
// Updated: 2023-11-09
namespace Index;
@ -10,8 +10,6 @@ use InvalidArgumentException;
use Countable;
use Iterator;
use IteratorAggregate;
use Index\Collections\IArrayable;
use Index\Collections\ArrayIterator;
/**
* Provides various helper methods for collections.
@ -289,8 +287,6 @@ final class XArray {
public static function toArray(iterable $iterable): array {
if(is_array($iterable))
return $iterable;
if($iterable instanceof IArrayable)
return $iterable->toArray();
$items = [];

View file

@ -1,11 +1,12 @@
<?php
// XNumber.php
// Created: 2023-09-15
// Updated: 2023-09-15
// Updated: 2023-11-09
namespace Index;
use InvalidArgumentException;
/**
* Provides various helper methods for numbers.
*/

View file

@ -1,11 +1,12 @@
<?php
// XString.php
// Created: 2022-02-03
// Updated: 2023-07-05
// Updated: 2023-11-09
namespace Index;
use InvalidArgumentException;
use Stringable;
/**
* Provides various helper methods for strings.
@ -18,35 +19,34 @@ final class XString {
*/
public const RANDOM_CHARS = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789';
public static function toBool(string $value): bool {
return boolval($value);
}
public static function toInt(string $value, int $base = 10): int {
return $base === 10 ? (int)$value : intval($value, $base);
}
public static function toFloat(string $value): float {
return (float)$value;
}
/**
* Converts special characters to HTML entities.
*
* Uses htmlspecialchars with alternate defaults.
*
* @param Stringable|string $string The string being converted.
* @param int $flags A bitmask consisting of flags found in the documentation for htmlspecialchars.
* @param ?string $encoding Optional argument defining the encoding used when converting characters.
* @param bool $doubleEncoding Also reencode existing HTML entities.
* @return string The converted string.
*/
public static function escape(
string $value,
Stringable|string $string,
int $flags = ENT_COMPAT | ENT_HTML5,
?string $encoding = null,
bool $doubleEncoding = true
): string {
return htmlspecialchars($value, $flags, $encoding, $doubleEncoding);
return htmlspecialchars((string)$string, $flags, $encoding, $doubleEncoding);
}
/**
* Counts unique characters in a string.
*
* @param string $string String to count unique characters of.
* @param Stringable|string $string String to count unique characters of.
* @return int Unique character count.
*/
public static function countUnique(string $string): int {
$string = str_split($string);
public static function countUnique(Stringable|string $string): int {
$string = str_split((string)$string);
$chars = [];
foreach($string as $char)
@ -59,24 +59,22 @@ final class XString {
/**
* Check if a string is null or empty.
*
* @param IString|string|null $string String ot check for emptiness.
* @param Stringable|string|null $string String ot check for emptiness.
* @return bool true if the string is empty, false if not.
*/
public static function nullOrEmpty(IString|string|null $string): bool {
public static function nullOrEmpty(Stringable|string|null $string): bool {
if($string === null)
return true;
if($string instanceof IString)
return $string->isEmpty();
return empty($string);
return empty((string)$string);
}
/**
* Check if a string is null or whitespace.
*
* @param IString|string|null $string String to check for whitespace.
* @param Stringable|string|null $string String to check for whitespace.
* @return bool true if the string is whitespace, false if not.
*/
public static function nullOrWhitespace(IString|string|null $string): bool {
public static function nullOrWhitespace(Stringable|string|null $string): bool {
if($string === null)
return true;
return empty(trim((string)$string));

View file

@ -1,7 +1,7 @@
<?php
// XStringTrait.php
// Created: 2021-06-22
// Updated: 2022-02-27
// Updated: 2023-11-09
namespace Index;
@ -10,6 +10,7 @@ use BadMethodCallException;
/**
* Provides method implementations that are common between all string types.
* @internal
* @deprecated Will be removed along with IString, AString and WString.
*/
trait XStringTrait {
/**
@ -43,15 +44,15 @@ trait XStringTrait {
}
public function toBool(): bool {
return XString::toBool($this);
return boolval((string)$this);
}
public function toInt(int $base = 10): int {
return XString::toInt($this, $base);
return $base === 10 ? (int)(string)$this : intval((string)$this, $base);
}
public function toFloat(): float {
return XString::toFloat($this);
return (float)(string)$this;
}
public function escape(

View file

@ -1,12 +1,11 @@
<?php
// EnvironmentTest.php
// Created: 2021-05-02
// Updated: 2023-01-01
// Updated: 2023-11-09
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use Index\AString;
use Index\Environment;
use Index\Version;

View file

@ -1,49 +0,0 @@
<?php
// TypeTest.php
// Created: 2021-04-27
// Updated: 2022-02-28
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use Index\AString;
use Index\Type;
use Index\XArray;
/**
* @covers Type
*/
final class TypeTest extends TestCase {
public function testDefaults(): void {
$this->assertEquals(Type::default(Type::BOOL), false);
$this->assertEquals(Type::default(Type::INTEGER), 0);
$this->assertEquals(Type::default(Type::FLOAT), 0.0);
$this->assertEquals(Type::default(Type::STRING), '');
$this->assertEquals(Type::default(Type::ARRAY), []);
$this->assertEquals(Type::default(Type::OBJECT), null);
$this->assertEquals(Type::default(Type::RESOURCE), null);
$this->assertEquals(Type::default(Type::CLOSED_RESOURCE), null);
$this->assertEquals(Type::default(Type::NULL), null);
$this->assertEquals(Type::default(Type::UNKNOWN), null);
}
public function testClassTree(): void {
$array1 = Type::classTree(new AString('the'));
$array2 = [
'Index\\IString',
'Stringable',
'Traversable',
'Index\\Serialisation\\IBencodeSerialisable',
'Index\\IEquatable',
'Index\\ICloneable',
'Index\\IComparable',
'JsonSerializable',
'IteratorAggregate',
'Countable',
'ArrayAccess',
'Index\\AString',
];
$this->assertTrue(XArray::sequenceEquals($array1, $array2));
}
}