Switch to constructor property declaration where possible.

This commit is contained in:
flash 2024-08-01 23:56:53 +00:00
parent 3d0c81541d
commit 7ef130629b
41 changed files with 209 additions and 318 deletions

View file

@ -1 +1 @@
0.2408.12322 0.2408.12356

View file

@ -1,7 +1,7 @@
<?php <?php
// ArrayIterator.php // ArrayIterator.php
// Created: 2022-02-03 // Created: 2022-02-03
// Updated: 2023-11-09 // Updated: 2024-08-01
namespace Index; namespace Index;
@ -11,15 +11,14 @@ use Iterator;
* Provides an Iterator implementation for normal arrays. * Provides an Iterator implementation for normal arrays.
*/ */
class ArrayIterator implements Iterator { class ArrayIterator implements Iterator {
private array $array;
private bool $wasValid = true; private bool $wasValid = true;
/** /**
* @param array $array Array to iterate upon. * @param array $array Array to iterate upon.
*/ */
public function __construct(array $array) { public function __construct(
$this->array = $array; private array $array
} ) {}
/** /**
* Returns the current element. * Returns the current element.

View file

@ -1,7 +1,7 @@
<?php <?php
// CSRFP.php // CSRFP.php
// Created: 2021-06-11 // Created: 2021-06-11
// Updated: 2024-07-31 // Updated: 2024-08-01
namespace Index; namespace Index;
@ -19,10 +19,6 @@ class CSRFP {
private const NONCE_LENGTH = 8; private const NONCE_LENGTH = 8;
private const HASH_LENGTH = 32; private const HASH_LENGTH = 32;
private string $secretKey;
private string $identity;
private int $tolerance;
/** /**
* Creates a new CSRFP instance. * Creates a new CSRFP instance.
* *
@ -32,14 +28,10 @@ class CSRFP {
* @return CSRFP New CSRFP instance. * @return CSRFP New CSRFP instance.
*/ */
public function __construct( public function __construct(
string $secretKey, private string $secretKey,
string $identity, private string $identity,
int $tolerance = self::TOLERANCE private int $tolerance = self::TOLERANCE
) { ) {}
$this->secretKey = $secretKey;
$this->identity = $identity;
$this->tolerance = $tolerance;
}
private static function time(int $time = -1): int { private static function time(int $time = -1): int {
return ($time < 0 ? time() : $time) - self::EPOCH; return ($time < 0 ? time() : $time) - self::EPOCH;

View file

@ -1,7 +1,7 @@
<?php <?php
// ColourHSL.php // ColourHSL.php
// Created: 2023-01-02 // Created: 2023-01-02
// Updated: 2023-11-09 // Updated: 2024-08-01
namespace Index\Colour; namespace Index\Colour;
@ -9,11 +9,6 @@ namespace Index\Colour;
* Represents a colour using Hue, Saturation and Lightness. * Represents a colour using Hue, Saturation and Lightness.
*/ */
class ColourHSL extends Colour { class ColourHSL extends Colour {
private float $hue;
private float $saturation;
private float $lightness;
private float $alpha;
private int $red; private int $red;
private int $green; private int $green;
private int $blue; private int $blue;
@ -24,11 +19,15 @@ class ColourHSL extends Colour {
* @param float $lightness Lightness property. * @param float $lightness Lightness property.
* @param float $alpha Alpha property. * @param float $alpha Alpha property.
*/ */
public function __construct(float $hue, float $saturation, float $lightness, float $alpha = 1.0) { public function __construct(
$this->hue = $hue; private float $hue,
$this->saturation = max(0.0, min(1.0, $saturation)); private float $saturation,
$this->lightness = max(0.0, min(1.0, $lightness)); private float $lightness,
$this->alpha = max(0.0, min(1.0, $alpha)); private float $alpha = 1.0
) {
$saturation = max(0.0, min(1.0, $saturation));
$lightness = max(0.0, min(1.0, $lightness));
$alpha = max(0.0, min(1.0, $alpha));
$c = (1 - abs(2 * $lightness - 1)) * $saturation; $c = (1 - abs(2 * $lightness - 1)) * $saturation;
$x = $c * (1 - abs(fmod($hue / 60, 2) - 1)); $x = $c * (1 - abs(fmod($hue / 60, 2) - 1));

View file

@ -1,7 +1,7 @@
<?php <?php
// ColourNamed.php // ColourNamed.php
// Created: 2023-01-02 // Created: 2023-01-02
// Updated: 2023-11-09 // Updated: 2024-08-01
namespace Index\Colour; namespace Index\Colour;
@ -9,7 +9,6 @@ namespace Index\Colour;
* Represents a named CSS colour. * Represents a named CSS colour.
*/ */
class ColourNamed extends Colour { class ColourNamed extends Colour {
private string $name;
private int $red = 0; private int $red = 0;
private int $green = 0; private int $green = 0;
private int $blue = 0; private int $blue = 0;
@ -18,8 +17,10 @@ class ColourNamed extends Colour {
/** /**
* @param string $name CSS colour name. * @param string $name CSS colour name.
*/ */
public function __construct(string $name) { public function __construct(
$this->name = strtolower($name); private string $name
) {
$name = strtolower($name);
$raw = self::COLOURS[$name] ?? -1; $raw = self::COLOURS[$name] ?? -1;
$this->transparent = $raw === -1; $this->transparent = $raw === -1;

View file

@ -1,7 +1,7 @@
<?php <?php
// ColourRGB.php // ColourRGB.php
// Created: 2023-01-02 // Created: 2023-01-02
// Updated: 2023-11-09 // Updated: 2024-08-01
namespace Index\Colour; namespace Index\Colour;
@ -9,22 +9,22 @@ namespace Index\Colour;
* Represents an RGB colour. * Represents an RGB colour.
*/ */
class ColourRGB extends Colour { class ColourRGB extends Colour {
private int $red;
private int $green;
private int $blue;
private float $alpha;
/** /**
* @param int $red Red property. * @param int $red Red property.
* @param int $green Green property. * @param int $green Green property.
* @param int $blue Blue property. * @param int $blue Blue property.
* @param float $alpha Alpha property. * @param float $alpha Alpha property.
*/ */
public function __construct(int $red, int $green, int $blue, float $alpha = 1.0) { public function __construct(
$this->red = max(0, min(255, $red)); private int $red,
$this->green = max(0, min(255, $green)); private int $green,
$this->blue = max(0, min(255, $blue)); private int $blue,
$this->alpha = max(0.0, min(1.0, $alpha)); private float $alpha = 1.0
) {
$red = max(0, min(255, $red));
$green = max(0, min(255, $green));
$blue = max(0, min(255, $blue));
$alpha = max(0.0, min(1.0, $alpha));
} }
public function getRed(): int { public function getRed(): int {

View file

@ -9,15 +9,14 @@ namespace Index\Data;
* Container to avoid having many prepared instances of the same query. * Container to avoid having many prepared instances of the same query.
*/ */
class DbStatementCache { class DbStatementCache {
private IDbConnection $dbConn;
private array $stmts = []; private array $stmts = [];
/** /**
* @param IDbConnection $dbConn Connection to use with this cache. * @param IDbConnection $dbConn Connection to use with this cache.
*/ */
public function __construct(IDbConnection $dbConn) { public function __construct(
$this->dbConn = $dbConn; private IDbConnection $dbConn
} ) {}
private static function hash(string $query): string { private static function hash(string $query): string {
return hash('xxh3', $query, true); return hash('xxh3', $query, true);

View file

@ -1,7 +1,7 @@
<?php <?php
// MariaDBCharacterSetInfo.php // MariaDBCharacterSetInfo.php
// Created: 2021-05-02 // Created: 2021-05-02
// Updated: 2022-02-02 // Updated: 2024-08-01
namespace Index\Data\MariaDB; namespace Index\Data\MariaDB;
@ -13,17 +13,15 @@ use stdClass;
* @see https://www.php.net/manual/en/mysqli.get-charset * @see https://www.php.net/manual/en/mysqli.get-charset
*/ */
class MariaDBCharacterSetInfo { class MariaDBCharacterSetInfo {
private stdClass $charSet;
/** /**
* Creates a new character set info instance. * Creates a new character set info instance.
* *
* @param stdClass $charSet Anonymous object containing the information. * @param stdClass $charSet Anonymous object containing the information.
* @return MariaDBCharacterSetInfo Character set information class. * @return MariaDBCharacterSetInfo Character set information class.
*/ */
public function __construct(stdClass $charSet) { public function __construct(
$this->charSet = $charSet; private stdClass $charSet
} ) {}
/** /**
* Returns the name of the current character set. * Returns the name of the current character set.

View file

@ -13,20 +13,6 @@ use Index\Net\{EndPoint,DnsEndPoint,IPAddress,IPEndPoint,UnixEndPoint};
* Describes a MariaDB or MySQL connection. * Describes a MariaDB or MySQL connection.
*/ */
class MariaDBConnectionInfo implements IDbConnectionInfo { class MariaDBConnectionInfo implements IDbConnectionInfo {
private EndPoint $endPoint;
private string $userName;
private string $password;
private string $dbName;
private ?string $charSet;
private ?string $initCommand;
private ?string $keyPath;
private ?string $certPath;
private ?string $certAuthPath;
private ?string $trustedCertsPath;
private ?string $cipherAlgos;
private bool $verifyCert;
private bool $useCompression;
/** /**
* Creates an instance of MariaDBConnectionInfo. * Creates an instance of MariaDBConnectionInfo.
* *
@ -46,37 +32,39 @@ class MariaDBConnectionInfo implements IDbConnectionInfo {
* @return MariaDBConnectionInfo A connection info instance representing the given information. * @return MariaDBConnectionInfo A connection info instance representing the given information.
*/ */
public function __construct( public function __construct(
EndPoint $endPoint, private EndPoint $endPoint,
string $userName, private string $userName,
string $password, private string $password,
string $dbName, private string $dbName,
string|null $charSet, private string|null $charSet,
string|null $initCommand, private string|null $initCommand,
string|null $keyPath, private string|null $keyPath,
string|null $certPath, private string|null $certPath,
string|null $certAuthPath, private string|null $certAuthPath,
string|null $trustedCertsPath, private string|null $trustedCertsPath,
string|null $cipherAlgos, private string|null $cipherAlgos,
bool $verifyCert, private bool $verifyCert,
bool $useCompression private bool $useCompression
) { ) {
if(!($endPoint instanceof IPEndPoint) if(!($endPoint instanceof IPEndPoint)
&& !($endPoint instanceof DnsEndPoint) && !($endPoint instanceof DnsEndPoint)
&& !($endPoint instanceof UnixEndPoint)) && !($endPoint instanceof UnixEndPoint))
throw new InvalidArgumentException('$endPoint must be of type IPEndPoint, DnsEndPoint or UnixEndPoint.'); throw new InvalidArgumentException('$endPoint must be of type IPEndPoint, DnsEndPoint or UnixEndPoint.');
$this->endPoint = $endPoint;
$this->userName = $userName; if(empty($charSet))
$this->password = $password; $charSet = null;
$this->dbName = $dbName; if(empty($initCommand))
$this->charSet = empty($charSet) ? null : $charSet; $initCommand = null;
$this->initCommand = empty($initCommand) ? null : $initCommand; if(empty($keyPath))
$this->keyPath = empty($keyPath) ? null : $keyPath; $keyPath = null;
$this->certPath = empty($certPath) ? null : $certPath; if(empty($certPath))
$this->certAuthPath = empty($certAuthPath) ? null : $certAuthPath; $certPath = null;
$this->trustedCertsPath = empty($trustedCertsPath) ? null : $trustedCertsPath; if(empty($certAuthPath))
$this->cipherAlgos = empty($cipherAlgos) ? null : $cipherAlgos; $certAuthPath = null;
$this->verifyCert = $verifyCert; if(empty($trustedCertsPath))
$this->useCompression = $useCompression; $trustedCertsPath = null;
if(empty($cipherAlgos))
$cipherAlgos = null;
} }
/** /**

View file

@ -11,10 +11,6 @@ use Index\Data\{DbTools,DbType};
* Represents a bound parameter. * Represents a bound parameter.
*/ */
class MariaDBParameter { class MariaDBParameter {
private int $ordinal;
private int $type;
private mixed $value;
/** /**
* Create a new MariaDBParameter. * Create a new MariaDBParameter.
* *
@ -23,14 +19,15 @@ class MariaDBParameter {
* @param mixed $value Value to bind. * @param mixed $value Value to bind.
* @return MariaDBParameter A new parameter instance. * @return MariaDBParameter A new parameter instance.
*/ */
public function __construct(int $ordinal, int $type, mixed $value) { public function __construct(
$this->ordinal = $ordinal; private int $ordinal,
private int $type,
private mixed $value
) {
if($type == DbType::AUTO) if($type == DbType::AUTO)
$type = DbTools::detectType($value); $type = DbTools::detectType($value);
if($type === DbType::NULL)
$this->type = $type; $value = null;
$this->value = $type === DbType::NULL ? null : $value;
} }
/** /**

View file

@ -17,7 +17,6 @@ use Index\IO\{Stream,TempFileStream};
abstract class MariaDBResult implements IDbResult { abstract class MariaDBResult implements IDbResult {
use DbResultTrait; use DbResultTrait;
protected mysqli_stmt|mysqli_result $result;
protected array $currentRow = []; protected array $currentRow = [];
/** /**
@ -26,9 +25,9 @@ abstract class MariaDBResult implements IDbResult {
* @param mysqli_stmt|mysqli_result $result A result to work with. * @param mysqli_stmt|mysqli_result $result A result to work with.
* @return MariaDBResult A result instance. * @return MariaDBResult A result instance.
*/ */
public function __construct(mysqli_stmt|mysqli_result $result) { public function __construct(
$this->result = $result; protected mysqli_stmt|mysqli_result $result
} ) {}
/** /**
* Gets the number of available rows in this result. * Gets the number of available rows in this result.

View file

@ -15,7 +15,6 @@ use Index\IO\Stream;
* Represents a MariaDB/MySQL prepared statement. * Represents a MariaDB/MySQL prepared statement.
*/ */
class MariaDBStatement implements IDbStatement { class MariaDBStatement implements IDbStatement {
private mysqli_stmt $statement;
private array $params = []; private array $params = [];
/** /**
@ -24,9 +23,9 @@ class MariaDBStatement implements IDbStatement {
* @param mysqli_stmt $statement Underlying statement object. * @param mysqli_stmt $statement Underlying statement object.
* @return MariaDBStatement A new statement instance. * @return MariaDBStatement A new statement instance.
*/ */
public function __construct(mysqli_stmt $statement) { public function __construct(
$this->statement = $statement; private mysqli_stmt $statement
} ) {}
private static bool $constructed = false; private static bool $constructed = false;
private static string $resultImplementation; private static string $resultImplementation;

View file

@ -1,7 +1,7 @@
<?php <?php
// MariaDBWarning.php // MariaDBWarning.php
// Created: 2021-05-02 // Created: 2021-05-02
// Updated: 2022-02-27 // Updated: 2024-08-01
namespace Index\Data\MariaDB; namespace Index\Data\MariaDB;
@ -13,10 +13,6 @@ use Index\XArray;
* Represents a MariaDB/MySQL warning. * Represents a MariaDB/MySQL warning.
*/ */
class MariaDBWarning implements Stringable { class MariaDBWarning implements Stringable {
private string $message;
private string $sqlState;
private int $code;
/** /**
* Creates a new MariaDBWarning object. * Creates a new MariaDBWarning object.
* *
@ -25,11 +21,11 @@ class MariaDBWarning implements Stringable {
* @param int $code Error code of the warning. * @param int $code Error code of the warning.
* @return MariaDBWarning A new warning instance. * @return MariaDBWarning A new warning instance.
*/ */
public function __construct(string $message, string $sqlState, int $code) { public function __construct(
$this->message = $message; private string $message,
$this->sqlState = $sqlState; private string $sqlState,
$this->code = $code; private int $code
} ) {}
/** /**
* Gets the warning message string. * Gets the warning message string.

View file

@ -8,15 +8,15 @@ namespace Index\Data\Migration;
use Index\Data\IDbConnection; use Index\Data\IDbConnection;
class FsDbMigrationInfo implements IDbMigrationInfo { class FsDbMigrationInfo implements IDbMigrationInfo {
private string $path;
private string $name; private string $name;
private string $className; private string $className;
/** /**
* @param string $path Filesystem path to the migration file. * @param string $path Filesystem path to the migration file.
*/ */
public function __construct(string $path) { public function __construct(
$this->path = $path; private string $path
) {
$this->name = $name = pathinfo($path, PATHINFO_FILENAME); $this->name = $name = pathinfo($path, PATHINFO_FILENAME);
$dateTime = substr($name, 0, 17); $dateTime = substr($name, 0, 17);

View file

@ -1,7 +1,7 @@
<?php <?php
// SQLiteConnectionInfo.php // SQLiteConnectionInfo.php
// Created: 2021-05-02 // Created: 2021-05-02
// Updated: 2022-02-28 // Updated: 2024-08-01
namespace Index\Data\SQLite; namespace Index\Data\SQLite;
@ -11,11 +11,6 @@ use Index\Data\IDbConnectionInfo;
* Represents information about a SQLite Client. * Represents information about a SQLite Client.
*/ */
class SQLiteConnectionInfo implements IDbConnectionInfo { class SQLiteConnectionInfo implements IDbConnectionInfo {
private string $fileName;
private bool $readOnly;
private bool $create;
private string $encryptionKey;
/** /**
* Creates a new SQLiteConnectionInfo instance. * Creates a new SQLiteConnectionInfo instance.
* *
@ -26,16 +21,11 @@ class SQLiteConnectionInfo implements IDbConnectionInfo {
* @return SQLiteConnectionInfo Information to create an SQLite database client. * @return SQLiteConnectionInfo Information to create an SQLite database client.
*/ */
public function __construct( public function __construct(
string $fileName, private string $fileName,
string $encryptionKey, private string $encryptionKey,
bool $readOnly, private bool $readOnly,
bool $create private bool $create
) { ) {}
$this->fileName = $fileName;
$this->encryptionKey = $encryptionKey;
$this->readOnly = $readOnly;
$this->create = $create;
}
/** /**
* Gets the path of the SQLite database. * Gets the path of the SQLite database.

View file

@ -16,7 +16,6 @@ use Index\IO\{Stream,TempFileStream};
class SQLiteResult implements IDbResult { class SQLiteResult implements IDbResult {
use DbResultTrait; use DbResultTrait;
private SQLite3Result $result;
private array $currentRow = []; private array $currentRow = [];
/** /**
@ -25,9 +24,9 @@ class SQLiteResult implements IDbResult {
* @param SQLite3Result $result Raw underlying result class. * @param SQLite3Result $result Raw underlying result class.
* @return SQLiteResult A new SQLiteResult instance. * @return SQLiteResult A new SQLiteResult instance.
*/ */
public function __construct(SQLite3Result $result) { public function __construct(
$this->result = $result; private SQLite3Result $result
} ) {}
/** /**
* Gets the number of columns per row in the result. * Gets the number of columns per row in the result.

View file

@ -16,8 +16,6 @@ use Index\IO\Stream;
* Represents a prepared SQLite SQL statement. * Represents a prepared SQLite SQL statement.
*/ */
class SQLiteStatement implements IDbStatement { class SQLiteStatement implements IDbStatement {
private SQLiteConnection $connection;
private SQLite3Stmt $statement;
private ?SQLite3Result $result = null; private ?SQLite3Result $result = null;
/** /**
@ -27,10 +25,10 @@ class SQLiteStatement implements IDbStatement {
* @param SQLite3Stmt $statement The raw statement instance. * @param SQLite3Stmt $statement The raw statement instance.
* @return SQLiteStatement A new statement instance. * @return SQLiteStatement A new statement instance.
*/ */
public function __construct(SQLiteConnection $connection, SQLite3Stmt $statement) { public function __construct(
$this->connection = $connection; private SQLiteConnection $connection,
$this->statement = $statement; private SQLite3Stmt $statement
} ) {}
public function getParameterCount(): int { public function getParameterCount(): int {
return $this->statement->paramCount(); return $this->statement->paramCount();

View file

@ -12,14 +12,12 @@ use Index\IO\{Stream,FileStream};
* Represents Bencoded body content for a HTTP message. * Represents Bencoded body content for a HTTP message.
*/ */
class BencodedContent implements IHttpContent, IBencodeSerialisable { class BencodedContent implements IHttpContent, IBencodeSerialisable {
private mixed $content;
/** /**
* @param mixed $content Content to be bencoded. * @param mixed $content Content to be bencoded.
*/ */
public function __construct(mixed $content) { public function __construct(
$this->content = $content; private mixed $content
} ) {}
/** /**
* Retrieves unencoded content. * Retrieves unencoded content.

View file

@ -12,17 +12,14 @@ use Index\Http\HttpUploadedFile;
* Represents form body content for a HTTP message. * Represents form body content for a HTTP message.
*/ */
class FormContent implements IHttpContent { class FormContent implements IHttpContent {
private array $postFields;
private array $uploadedFiles;
/** /**
* @param array<string, mixed> $postFields Form fields. * @param array<string, mixed> $postFields Form fields.
* @param array<string, HttpUploadedFile> $uploadedFiles Uploaded files. * @param array<string, HttpUploadedFile> $uploadedFiles Uploaded files.
*/ */
public function __construct(array $postFields, array $uploadedFiles) { public function __construct(
$this->postFields = $postFields; private array $postFields,
$this->uploadedFiles = $uploadedFiles; private array $uploadedFiles
} ) {}
/** /**
* Retrieves a form field with filtering. * Retrieves a form field with filtering.

View file

@ -12,14 +12,12 @@ use Index\IO\{Stream,FileStream};
* Represents JSON body content for a HTTP message. * Represents JSON body content for a HTTP message.
*/ */
class JsonContent implements IHttpContent, JsonSerializable { class JsonContent implements IHttpContent, JsonSerializable {
private mixed $content;
/** /**
* @param mixed $content Content to be JSON encoded. * @param mixed $content Content to be JSON encoded.
*/ */
public function __construct(mixed $content) { public function __construct(
$this->content = $content; private mixed $content
} ) {}
/** /**
* Retrieves unencoded content. * Retrieves unencoded content.

View file

@ -11,14 +11,12 @@ use Index\IO\{Stream,FileStream};
* Represents Stream body content for a HTTP message. * Represents Stream body content for a HTTP message.
*/ */
class StreamContent implements IHttpContent { class StreamContent implements IHttpContent {
private Stream $stream;
/** /**
* @param Stream $stream Stream that represents this message body. * @param Stream $stream Stream that represents this message body.
*/ */
public function __construct(Stream $stream) { public function __construct(
$this->stream = $stream; private Stream $stream
} ) {}
/** /**
* Retrieves the underlying stream. * Retrieves the underlying stream.

View file

@ -11,14 +11,12 @@ use Stringable;
* Represents string body content for a HTTP message. * Represents string body content for a HTTP message.
*/ */
class StringContent implements IHttpContent { class StringContent implements IHttpContent {
private string $string;
/** /**
* @param string $string String that represents this message body. * @param string $string String that represents this message body.
*/ */
public function __construct(string $string) { public function __construct(
$this->string = $string; private string $string
} ) {}
/** /**
* Retrieves the underlying string. * Retrieves the underlying string.

View file

@ -11,15 +11,16 @@ use Stringable;
* Represents a generic HTTP header. * Represents a generic HTTP header.
*/ */
class HttpHeader implements Stringable { class HttpHeader implements Stringable {
private string $name;
private array $lines; private array $lines;
/** /**
* @param string $name Name of the header. * @param string $name Name of the header.
* @param string ...$lines Lines of the header. * @param string ...$lines Lines of the header.
*/ */
public function __construct(string $name, string ...$lines) { public function __construct(
$this->name = $name; private string $name,
string ...$lines
) {
$this->lines = $lines; $this->lines = $lines;
} }

View file

@ -11,12 +11,12 @@ use RuntimeException;
* Represents a collection of HTTP headers. * Represents a collection of HTTP headers.
*/ */
class HttpHeaders { class HttpHeaders {
private array $headers;
/** /**
* @param HttpHeader[] $headers HTTP header instances. * @param HttpHeader[] $headers HTTP header instances.
*/ */
public function __construct(array $headers) { public function __construct(
private array $headers
) {
$real = []; $real = [];
foreach($headers as $header) foreach($headers as $header)

View file

@ -13,20 +13,16 @@ use Index\Http\Content\{IHttpContent,BencodedContent,FormContent,JsonContent,Str
* Represents a base HTTP message. * Represents a base HTTP message.
*/ */
abstract class HttpMessage { abstract class HttpMessage {
private string $version;
private HttpHeaders $headers;
private ?IHttpContent $content;
/** /**
* @param string $version HTTP message version. * @param string $version HTTP message version.
* @param HttpHeaders $headers HTTP message headers. * @param HttpHeaders $headers HTTP message headers.
* @param ?IHttpContent $content Body contents. * @param ?IHttpContent $content Body contents.
*/ */
public function __construct(string $version, HttpHeaders $headers, ?IHttpContent $content) { public function __construct(
$this->version = $version; private string $version,
$this->headers = $headers; private HttpHeaders $headers,
$this->content = $content; private ?IHttpContent $content
} ) {}
/** /**
* Retrieves the HTTP version of this message. * Retrieves the HTTP version of this message.

View file

@ -14,11 +14,6 @@ use Index\Http\Content\{IHttpContent,JsonContent,StreamContent,FormContent};
* Represents a HTTP request message. * Represents a HTTP request message.
*/ */
class HttpRequest extends HttpMessage { class HttpRequest extends HttpMessage {
private string $method;
private string $path;
private array $params;
private array $cookies;
/** /**
* @param string $version HTTP message version. * @param string $version HTTP message version.
* @param string $method HTTP request method. * @param string $method HTTP request method.
@ -30,18 +25,13 @@ class HttpRequest extends HttpMessage {
*/ */
public function __construct( public function __construct(
string $version, string $version,
string $method, private string $method,
string $path, private string $path,
array $params, private array $params,
array $cookies, private array $cookies,
HttpHeaders $headers, HttpHeaders $headers,
?IHttpContent $content ?IHttpContent $content
) { ) {
$this->method = $method;
$this->path = $path;
$this->params = $params;
$this->cookies = $cookies;
parent::__construct($version, $headers, $content); parent::__construct($version, $headers, $content);
} }
@ -93,7 +83,7 @@ class HttpRequest extends HttpMessage {
public function getParam(string $name, int $filter = FILTER_DEFAULT, array|int $options = 0): mixed { public function getParam(string $name, int $filter = FILTER_DEFAULT, array|int $options = 0): mixed {
if(!isset($this->params[$name])) if(!isset($this->params[$name]))
return null; return null;
return filter_var($this->params[$name] ?? null, $filter, $options); return filter_var($this->params[$name], $filter, $options);
} }
/** /**
@ -126,7 +116,7 @@ class HttpRequest extends HttpMessage {
public function getCookie(string $name, int $filter = FILTER_DEFAULT, array|int $options = 0): mixed { public function getCookie(string $name, int $filter = FILTER_DEFAULT, array|int $options = 0): mixed {
if(!isset($this->cookies[$name])) if(!isset($this->cookies[$name]))
return null; return null;
return filter_var($this->cookies[$name] ?? null, $filter, $options); return filter_var($this->cookies[$name], $filter, $options);
} }
/** /**

View file

@ -11,9 +11,6 @@ use Index\Http\Content\IHttpContent;
* Represents a HTTP response message. * Represents a HTTP response message.
*/ */
class HttpResponse extends HttpMessage { class HttpResponse extends HttpMessage {
private int $statusCode;
private string $statusText;
/** /**
* @param string $version HTTP message version. * @param string $version HTTP message version.
* @param int $statusCode HTTP response status code. * @param int $statusCode HTTP response status code.
@ -23,14 +20,11 @@ class HttpResponse extends HttpMessage {
*/ */
public function __construct( public function __construct(
string $version, string $version,
int $statusCode, private int $statusCode,
string $statusText, private string $statusText,
HttpHeaders $headers, HttpHeaders $headers,
?IHttpContent $content ?IHttpContent $content
) { ) {
$this->statusCode = $statusCode;
$this->statusText = $statusText;
parent::__construct($version, $headers, $content); parent::__construct($version, $headers, $content);
} }

View file

@ -14,11 +14,6 @@ use Index\IO\{Stream,FileStream};
* Represents an uploaded file in a multipart/form-data request. * Represents an uploaded file in a multipart/form-data request.
*/ */
class HttpUploadedFile implements ICloseable { class HttpUploadedFile implements ICloseable {
private int $errorCode;
private int $size;
private string $localFileName;
private string $suggestedFileName;
private MediaType $suggestedMediaType;
private bool $hasMoved = false; private bool $hasMoved = false;
private ?Stream $stream = null; private ?Stream $stream = null;
@ -30,20 +25,14 @@ class HttpUploadedFile implements ICloseable {
* @param MediaType|string $suggestedMediaType Mediatype included by the client. * @param MediaType|string $suggestedMediaType Mediatype included by the client.
*/ */
public function __construct( public function __construct(
int $errorCode, private int $errorCode,
int $size, private int $size,
string $localFileName, private string $localFileName,
string $suggestedFileName, private string $suggestedFileName,
MediaType|string $suggestedMediaType private MediaType|string $suggestedMediaType
) { ) {
if(!($suggestedMediaType instanceof MediaType)) if(!($suggestedMediaType instanceof MediaType))
$suggestedMediaType = MediaType::parse($suggestedMediaType); $suggestedMediaType = MediaType::parse($suggestedMediaType);
$this->errorCode = $errorCode;
$this->size = $size;
$this->localFileName = $localFileName;
$this->suggestedFileName = $suggestedFileName;
$this->suggestedMediaType = $suggestedMediaType;
} }
/** /**

View file

@ -1,7 +1,7 @@
<?php <?php
// HandlerAttribute.php // HandlerAttribute.php
// Created: 2024-03-28 // Created: 2024-03-28
// Updated: 2024-03-28 // Updated: 2024-08-01
namespace Index\Http\Routing; namespace Index\Http\Routing;
@ -12,7 +12,12 @@ use ReflectionObject;
* Provides base for attributes that mark methods in a class as handlers. * Provides base for attributes that mark methods in a class as handlers.
*/ */
abstract class HandlerAttribute { abstract class HandlerAttribute {
public function __construct(private string $path) {} /**
* @param string $path Target path.
*/
public function __construct(
private string $path
) {}
/** /**
* Returns the target path. * Returns the target path.

View file

@ -1,7 +1,7 @@
<?php <?php
// HttpRoute.php // HttpRoute.php
// Created: 2024-03-28 // Created: 2024-03-28
// Updated: 2024-03-28 // Updated: 2024-08-01
namespace Index\Http\Routing; namespace Index\Http\Routing;
@ -12,15 +12,15 @@ use Attribute;
*/ */
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)] #[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
class HttpRoute extends HandlerAttribute { class HttpRoute extends HandlerAttribute {
private string $method;
/** /**
* @param string $method * @param string $method
* @param string $path * @param string $path
*/ */
public function __construct(string $method, string $path) { public function __construct(
private string $method,
string $path
) {
parent::__construct($path); parent::__construct($path);
$this->method = $method;
} }
/** /**

View file

@ -22,8 +22,6 @@ class HttpRouter implements IRouter {
private array $contentHandlers = []; private array $contentHandlers = [];
private string $defaultCharSet;
private IErrorHandler $errorHandler; private IErrorHandler $errorHandler;
/** /**
@ -32,11 +30,10 @@ class HttpRouter implements IRouter {
* @param bool $registerDefaultContentHandlers true to register default content handlers for JSON, Bencode, etc. * @param bool $registerDefaultContentHandlers true to register default content handlers for JSON, Bencode, etc.
*/ */
public function __construct( public function __construct(
string $charSet = '', private string $charSet = '',
IErrorHandler|string $errorHandler = 'html', IErrorHandler|string $errorHandler = 'html',
bool $registerDefaultContentHandlers = true, bool $registerDefaultContentHandlers = true,
) { ) {
$this->defaultCharSet = $charSet;
$this->setErrorHandler($errorHandler); $this->setErrorHandler($errorHandler);
if($registerDefaultContentHandlers) if($registerDefaultContentHandlers)
@ -49,9 +46,9 @@ class HttpRouter implements IRouter {
* @return string Normalised character set name. * @return string Normalised character set name.
*/ */
public function getCharSet(): string { public function getCharSet(): string {
if($this->defaultCharSet === '') if($this->charSet === '')
return strtolower(mb_preferred_mime_name(mb_internal_encoding())); return strtolower(mb_preferred_mime_name(mb_internal_encoding()));
return $this->defaultCharSet; return $this->charSet;
} }
/** /**

View file

@ -11,20 +11,18 @@ namespace Index\Http\Routing;
class ScopedRouter implements IRouter { class ScopedRouter implements IRouter {
use RouterTrait; use RouterTrait;
private IRouter $router;
private string $prefix;
/** /**
* @param IRouter $router Underlying router. * @param IRouter $router Underlying router.
* @param string $prefix Base path to use as a prefix. * @param string $prefix Base path to use as a prefix.
*/ */
public function __construct(IRouter $router, string $prefix) { public function __construct(
private IRouter $router,
private string $prefix
) {
if($router instanceof ScopedRouter) if($router instanceof ScopedRouter)
$router = $router->getParentRouter(); $router = $router->getParentRouter();
$this->router = $router;
// todo: cleanup // TODO: cleanup prefix
$this->prefix = $prefix;
} }
private function getParentRouter(): IRouter { private function getParentRouter(): IRouter {

View file

@ -110,17 +110,17 @@ class FileStream extends GenericStream {
*/ */
public const LOCK_NON_BLOCKING = LOCK_NB; public const LOCK_NON_BLOCKING = LOCK_NB;
private int $lock;
/** /**
* @param string $path Path to the file. * @param string $path Path to the file.
* @param string $mode Stream mode to use to open the file. * @param string $mode Stream mode to use to open the file.
* @param int $lock Locking method to use. * @param int $lock Locking method to use.
* @throws RuntimeException If the file could not be opened. * @throws RuntimeException If the file could not be opened.
*/ */
public function __construct(string $path, string $mode, int $lock) { public function __construct(
$this->lock = $lock; string $path,
string $mode,
private int $lock
) {
try { try {
$stream = fopen($path, $mode); $stream = fopen($path, $mode);
} catch(ErrorException $ex) { } catch(ErrorException $ex) {

View file

@ -32,7 +32,9 @@ class GenericStream extends Stream {
'x+', 'x+b', 'c', 'cb', 'c+', 'c+b', 'x+', 'x+b', 'c', 'cb', 'c+', 'c+b',
]; ];
protected $stream; // resource // resource
// can't seem to turn this one into a constructor property thing?
protected $stream;
private bool $canRead; private bool $canRead;
private bool $canWrite; private bool $canWrite;

View file

@ -12,17 +12,12 @@ use Stringable;
* Implements a structure for representing and comparing media/mime types. * Implements a structure for representing and comparing media/mime types.
*/ */
class MediaType implements Stringable, IComparable, IEquatable { class MediaType implements Stringable, IComparable, IEquatable {
private string $category = ''; public function __construct(
private string $kind = ''; private string $category,
private string $suffix = ''; private string $kind,
private array $params = []; private string $suffix,
private array $params
public function __construct(string $category, string $kind, string $suffix, array $params) { ) {}
$this->category = $category;
$this->kind = $kind;
$this->suffix = $suffix;
$this->params = $params;
}
public function getCategory(): string { public function getCategory(): string {
return $this->category; return $this->category;

View file

@ -11,19 +11,17 @@ use InvalidArgumentException;
* A DNS end point. * A DNS end point.
*/ */
class DnsEndPoint extends EndPoint { class DnsEndPoint extends EndPoint {
private string $host;
private int $port;
/** /**
* @param string $host DNS host name. * @param string $host DNS host name.
* @param int $port Network port, 0 to leave default. * @param int $port Network port, 0 to leave default.
* @throws InvalidArgumentException If $port is less than 0 or greater than 65535. * @throws InvalidArgumentException If $port is less than 0 or greater than 65535.
*/ */
public function __construct(string $host, int $port) { public function __construct(
private string $host,
private int $port
) {
if($port < 0 || $port > 0xFFFF) if($port < 0 || $port > 0xFFFF)
throw new InvalidArgumentException('$port is not a valid port number.'); throw new InvalidArgumentException('$port is not a valid port number.');
$this->host = $host;
$this->port = $port;
} }
/** /**

View file

@ -35,14 +35,14 @@ final class IPAddress implements JsonSerializable, Stringable, IEquatable {
*/ */
public const V6 = 6; public const V6 = 6;
private string $raw;
private int $width; private int $width;
/** /**
* @param string $raw Raw IP address data. * @param string $raw Raw IP address data.
*/ */
public function __construct(string $raw) { public function __construct(
$this->raw = $raw; private string $raw
) {
$this->width = strlen($raw); $this->width = strlen($raw);
} }

View file

@ -14,17 +14,14 @@ use Index\IEquatable;
* Represents a CIDR range of IP addresses. * Represents a CIDR range of IP addresses.
*/ */
final class IPAddressRange implements JsonSerializable, Stringable, IEquatable { final class IPAddressRange implements JsonSerializable, Stringable, IEquatable {
private IPAddress $base;
private int $mask;
/** /**
* @param IPAddress $base Base IP address. * @param IPAddress $base Base IP address.
* @param int $mask CIDR mask. * @param int $mask CIDR mask.
*/ */
public function __construct(IPAddress $base, int $mask) { public function __construct(
$this->base = $base; private IPAddress $base,
$this->mask = $mask; private int $mask
} ) {}
/** /**
* Retrieves the base IP address. * Retrieves the base IP address.

View file

@ -12,19 +12,17 @@ use Index\XString;
* Represents an IP address end point. * Represents an IP address end point.
*/ */
class IPEndPoint extends EndPoint { class IPEndPoint extends EndPoint {
private IPAddress $address;
private int $port;
/** /**
* @param IPAddress $address IP address. * @param IPAddress $address IP address.
* @param int $port Network port, 0 to leave default. * @param int $port Network port, 0 to leave default.
* @throws InvalidArgumentException If $port is less than 0 or greater than 65535. * @throws InvalidArgumentException If $port is less than 0 or greater than 65535.
*/ */
public function __construct(IPAddress $address, int $port) { public function __construct(
private IPAddress $address,
private int $port
) {
if($port < 0 || $port > 0xFFFF) if($port < 0 || $port > 0xFFFF)
throw new InvalidArgumentException('$port is not a valid port number.'); throw new InvalidArgumentException('$port is not a valid port number.');
$this->address = $address;
$this->port = $port;
} }
/** /**

View file

@ -9,14 +9,12 @@ namespace Index\Net;
* Represents a UNIX socket path end point. * Represents a UNIX socket path end point.
*/ */
class UnixEndPoint extends EndPoint { class UnixEndPoint extends EndPoint {
private string $path;
/** /**
* @param string $socketPath Path to the socket file. * @param string $socketPath Path to the socket file.
*/ */
public function __construct(string $socketPath) { public function __construct(
$this->path = $socketPath; private string $socketPath
} ) {}
/** /**
* Gets path to the socket file. * Gets path to the socket file.
@ -24,20 +22,20 @@ class UnixEndPoint extends EndPoint {
* @return string Path to the socket file. * @return string Path to the socket file.
*/ */
public function getPath(): string { public function getPath(): string {
return $this->path; return $this->socketPath;
} }
public function __serialize(): array { public function __serialize(): array {
return [$this->path]; return [$this->socketPath];
} }
public function __unserialize(array $serialized): void { public function __unserialize(array $serialized): void {
$this->path = $serialized[0]; $this->socketPath = $serialized[0];
} }
public function equals(mixed $other): bool { public function equals(mixed $other): bool {
return $other instanceof UnixEndPoint return $other instanceof UnixEndPoint
&& $this->path === $other->path; && $this->socketPath === $other->socketPath;
} }
/** /**
@ -56,6 +54,6 @@ class UnixEndPoint extends EndPoint {
} }
public function __toString(): string { public function __toString(): string {
return $this->path; return $this->socketPath;
} }
} }

View file

@ -9,11 +9,6 @@ namespace Index\Performance;
* Represents a timing point. * Represents a timing point.
*/ */
class TimingPoint { class TimingPoint {
private int $timePoint;
private int $duration;
private string $name;
private string $comment;
/** /**
* @param int $timePoint Starting tick count. * @param int $timePoint Starting tick count.
* @param int $duration Duration ticks count. * @param int $duration Duration ticks count.
@ -21,16 +16,11 @@ class TimingPoint {
* @param string $comment Timing point comment. * @param string $comment Timing point comment.
*/ */
public function __construct( public function __construct(
int $timePoint, private int $timePoint,
int $duration, private int $duration,
string $name, private string $name,
string $comment private string $comment
) { ) {}
$this->timePoint = $timePoint;
$this->duration = $duration;
$this->name = $name;
$this->comment = $comment;
}
/** /**
* Gets the starting ticks count. * Gets the starting ticks count.