Compare commits
68 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
83738adcb6 | ||
|
|
5454133239 | ||
|
|
87f35f23c3 | ||
|
|
3c4c5d3106 | ||
|
|
b080727ff3 | ||
|
|
ae044bee6f | ||
|
|
529b6a75ba | ||
|
|
8de112ba7e | ||
|
|
ad070ebdf4 | ||
|
|
9edfc2acb6 | ||
|
|
35cc6dbf91 | ||
|
|
cb3d5e1f27 | ||
|
|
0a45300c21 | ||
|
|
54ce378ae2 | ||
|
|
4ac659f7d9 | ||
|
|
497833ca71 | ||
|
|
e5a9b149b1 | ||
|
|
5213805a58 | ||
|
|
a9f1d878f7 | ||
|
|
3845bc7ff5 | ||
|
|
32c192a362 | ||
|
|
2bd68f32ac | ||
|
|
f5964fed02 | ||
|
|
625272198d | ||
|
|
00821bd5ea | ||
|
|
921b9cb3d9 | ||
|
|
720b78b687 | ||
|
|
565014e1e2 | ||
|
|
d9bcb577d7 | ||
|
|
8613e8977b | ||
|
|
0c51a3be87 | ||
|
|
f9cf36524e | ||
|
|
bacb9881ac | ||
|
|
f0fae1f76d | ||
|
|
1653e6b684 | ||
|
|
c8bc0062ad | ||
|
|
5c8a2ef8da | ||
|
|
d8379a10d9 | ||
|
|
30e2f33620 | ||
|
|
a4f16f4ca9 | ||
|
|
6e7b9cd033 | ||
|
|
4bc2ad8fa0 | ||
|
|
0d4e959f39 | ||
|
|
95d567545a | ||
|
|
d89c6d1bde | ||
|
|
337ebb9032 | ||
|
|
9538ebce7b | ||
|
|
1bff19f4b6 | ||
|
|
66dc72ec67 | ||
|
|
f781b5e55f | ||
|
|
934db50b3a | ||
|
|
573588ad3c | ||
|
|
d04addba81 | ||
|
|
a50a38fd40 | ||
|
|
3c5200cd99 | ||
|
|
50a4b88f55 | ||
|
|
e82929f512 | ||
|
|
5fc55c53b8 | ||
|
|
47da4d02ff | ||
|
|
9d131cf6dd | ||
|
|
dfcae20f64 | ||
|
|
61e489ee4c | ||
|
|
29982f90bc | ||
|
|
7cced63c4b | ||
|
|
06c2ea5e0d | ||
|
|
2e9239ec23 | ||
|
|
0c89840dba | ||
|
|
db144493f3 |
6
.gitignore
vendored
6
.gitignore
vendored
@@ -1 +1,7 @@
|
||||
.libs
|
||||
node_modules/
|
||||
composer.lock
|
||||
vendor/
|
||||
tools/
|
||||
www/composer.lock
|
||||
www/vendor
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phive xmlns="https://phar.io/phive">
|
||||
<phar name="phpunit" version="^9.6" installed="9.6.19" location="./tools/phpunit" copy="false"/>
|
||||
<phar name="phpcbf" version="^3.7.2" installed="3.10.0" location="./tools/phpcbf" copy="false"/>
|
||||
<phar name="phpcs" version="^3.7.2" installed="3.10.0" location="./tools/phpcs" copy="false"/>
|
||||
<phar name="phpstan" version="^1.10.37" installed="1.11.1" location="./tools/phpstan" copy="false"/>
|
||||
<phar name="phpunit" version="^9.6" installed="9.6.21" location="./tools/phpunit" copy="false"/>
|
||||
<phar name="phpcbf" version="^3.7.2" installed="3.10.3" location="./tools/phpcbf" copy="false"/>
|
||||
<phar name="phpcs" version="^3.7.2" installed="3.10.3" location="./tools/phpcs" copy="false"/>
|
||||
<phar name="phpstan" version="^1.10.37" installed="1.12.4" location="./tools/phpstan" copy="false"/>
|
||||
<phar name="phan" version="^5.4.2" installed="5.4.3" location="./tools/phan" copy="false"/>
|
||||
<phar name="psalm" version="^5.15.0" installed="5.24.0" location="./tools/psalm" copy="false"/>
|
||||
<phar name="phpdox" version="^0.12.0" installed="0.12.0" location="./tools/phpdox" copy="false"/>
|
||||
|
||||
@@ -36,7 +36,7 @@ if [ -n "${2}" ] && [ -z "${php_bin}" ]; then
|
||||
fi;
|
||||
|
||||
# Note 4dev/tests/bootstrap.php has to be set as bootstrap file in phpunit.xml
|
||||
phpunit_call="${php_bin}${base}tools/phpunit ${opt_testdox} -c ${base}phpunit.xml ${base}4dev/tests/";
|
||||
phpunit_call="${php_bin}${base}vendor/bin/phpunit ${opt_testdox} -c ${base}phpunit.xml ${base}4dev/tests/";
|
||||
|
||||
${phpunit_call};
|
||||
|
||||
|
||||
105
4dev/database/function/upgrade_serial_to_identity.sql
Normal file
105
4dev/database/function/upgrade_serial_to_identity.sql
Normal file
@@ -0,0 +1,105 @@
|
||||
-- Upgrae serial to identity type
|
||||
--
|
||||
-- @param reclass tbl The table where the column is located
|
||||
-- @param name col The column to be changed
|
||||
-- @param varchar identity_type [default=a] Allowed a, d, assigned, default
|
||||
-- @param varchar col_type [default=''] Allowed smallint, int, bigint, int2, int4, int8
|
||||
-- @raises EXCEPTON on column not found, no linked sequence, more than one linked sequence found
|
||||
--
|
||||
CREATE OR REPLACE FUNCTION upgrade_serial_to_identity(
|
||||
tbl regclass,
|
||||
col name,
|
||||
identity_type varchar = 'a',
|
||||
col_type varchar = ''
|
||||
)
|
||||
RETURNS void
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
DECLARE
|
||||
colnum smallint;
|
||||
seqid oid;
|
||||
count int;
|
||||
col_type_oid int;
|
||||
col_type_len int;
|
||||
current_col_atttypid oid;
|
||||
current_col_attlen int;
|
||||
BEGIN
|
||||
-- switch between always (default) or default identiy type
|
||||
IF identity_type NOT IN ('a', 'd', 'assigned', 'default') THEN
|
||||
identity_type := 'a';
|
||||
ELSE
|
||||
IF identity_type = 'default' THEN
|
||||
identity_type := 'd';
|
||||
ELSIF identity_type = 'assigned' THEN
|
||||
identity_type := 'a';
|
||||
END IF;
|
||||
END IF;
|
||||
-- find column number, attribute oid and attribute len
|
||||
SELECT attnum, atttypid, attlen
|
||||
INTO colnum, current_col_atttypid, current_col_attlen
|
||||
FROM pg_attribute
|
||||
WHERE attrelid = tbl AND attname = col;
|
||||
IF NOT FOUND THEN
|
||||
RAISE EXCEPTION 'column does not exist';
|
||||
END IF;
|
||||
|
||||
-- find sequence
|
||||
SELECT INTO seqid objid
|
||||
FROM pg_depend
|
||||
WHERE (refclassid, refobjid, refobjsubid) = ('pg_class'::regclass, tbl, colnum)
|
||||
AND classid = 'pg_class'::regclass AND objsubid = 0
|
||||
AND deptype = 'a';
|
||||
|
||||
GET DIAGNOSTICS count = ROW_COUNT;
|
||||
IF count < 1 THEN
|
||||
RAISE EXCEPTION 'no linked sequence found';
|
||||
ELSIF count > 1 THEN
|
||||
RAISE EXCEPTION 'more than one linked sequence found';
|
||||
END IF;
|
||||
|
||||
-- drop the default
|
||||
EXECUTE 'ALTER TABLE ' || tbl || ' ALTER COLUMN ' || quote_ident(col) || ' DROP DEFAULT';
|
||||
|
||||
-- change the dependency between column and sequence to internal
|
||||
UPDATE pg_depend
|
||||
SET deptype = 'i'
|
||||
WHERE (classid, objid, objsubid) = ('pg_class'::regclass, seqid, 0)
|
||||
AND deptype = 'a';
|
||||
|
||||
-- mark the column as identity column
|
||||
UPDATE pg_attribute
|
||||
-- set to 'd' for default
|
||||
SET attidentity = identity_type
|
||||
WHERE attrelid = tbl
|
||||
AND attname = col;
|
||||
RAISE NOTICE 'Update to identity for table "%" and columen "%" with type "%"', tbl, col, identity_type;
|
||||
|
||||
-- set type if requested and not empty
|
||||
IF col_type <> '' THEN
|
||||
IF col_type IN ('smallint', 'int', 'bigint', 'int2', 'int4', 'int8') THEN
|
||||
-- rewrite smallint, int, bigint
|
||||
IF col_type = 'smallint' THEN
|
||||
col_type := 'int2';
|
||||
ELSIF col_type = 'int' THEN
|
||||
col_type := 'int4';
|
||||
ELSIF col_type = 'bigint' THEN
|
||||
col_type := 'int8';
|
||||
END IF;
|
||||
-- get the length and oid for selected
|
||||
SELECT oid, typlen INTO col_type_oid, col_type_len FROM pg_type WHERE typname = col_type;
|
||||
-- set only if diff or hight
|
||||
IF current_col_atttypid <> col_type_oid AND col_type_len > current_col_attlen THEN
|
||||
RAISE NOTICE 'Change col type: %', col_type;
|
||||
-- update type
|
||||
UPDATE pg_attribute
|
||||
SET
|
||||
atttypid = col_type_oid, attlen = col_type_len
|
||||
WHERE attrelid = tbl
|
||||
AND attname = col;
|
||||
END IF;
|
||||
ELSE
|
||||
RAISE NOTICE 'Invalid col type: %', col_type;
|
||||
END IF;
|
||||
END IF;
|
||||
END;
|
||||
$$;
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
-- DROP TABLE edit_access;
|
||||
CREATE TABLE edit_access (
|
||||
edit_access_id SERIAL PRIMARY KEY,
|
||||
edit_access_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
enabled SMALLINT NOT NULL DEFAULT 0,
|
||||
protected SMALLINT DEFAULT 0,
|
||||
deleted SMALLINT DEFAULT 0,
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
-- DROP TABLE edit_access_data;
|
||||
CREATE TABLE edit_access_data (
|
||||
edit_access_data_id SERIAL PRIMARY KEY,
|
||||
edit_access_data_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
edit_access_id INT NOT NULL,
|
||||
FOREIGN KEY (edit_access_id) REFERENCES edit_access (edit_access_id) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
enabled SMALLINT NOT NULL DEFAULT 0,
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
-- DROP TABLE edit_access_right;
|
||||
CREATE TABLE edit_access_right (
|
||||
edit_access_right_id SERIAL PRIMARY KEY,
|
||||
edit_access_right_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
name VARCHAR,
|
||||
level SMALLINT,
|
||||
type VARCHAR,
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
-- DROP TABLE edit_access_user;
|
||||
CREATE TABLE edit_access_user (
|
||||
edit_access_user_id SERIAL PRIMARY KEY,
|
||||
edit_access_user_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
edit_access_id INT NOT NULL,
|
||||
FOREIGN KEY (edit_access_id) REFERENCES edit_access (edit_access_id) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
edit_user_id INT NOT NULL,
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
-- DROP TABLE edit_group;
|
||||
CREATE TABLE edit_group (
|
||||
edit_group_id SERIAL PRIMARY KEY,
|
||||
edit_group_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
edit_scheme_id INT,
|
||||
FOREIGN KEY (edit_scheme_id) REFERENCES edit_scheme (edit_scheme_id) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
edit_access_right_id INT NOT NULL,
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
-- DROP TABLE edit_language;
|
||||
CREATE TABLE edit_language (
|
||||
edit_language_id SERIAL PRIMARY KEY,
|
||||
edit_language_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
enabled SMALLINT NOT NULL DEFAULT 0,
|
||||
lang_default SMALLINT NOT NULL DEFAULT 0,
|
||||
long_name VARCHAR,
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
-- DROP TABLE edit_log;
|
||||
CREATE TABLE edit_log (
|
||||
edit_log_id SERIAL PRIMARY KEY,
|
||||
edit_log_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
euid INT, -- this is a foreign key, but I don't nedd to reference to it
|
||||
FOREIGN KEY (euid) REFERENCES edit_user (edit_user_id) MATCH FULL ON UPDATE CASCADE ON DELETE SET NULL,
|
||||
username VARCHAR,
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
-- DROP TABLE edit_menu_group;
|
||||
CREATE TABLE edit_menu_group (
|
||||
edit_menu_group_id SERIAL PRIMARY KEY,
|
||||
edit_menu_group_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
name VARCHAR,
|
||||
flag VARCHAR,
|
||||
order_number INT NOT NULL
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
-- DROP TABLE edit_page;
|
||||
CREATE TABLE edit_page (
|
||||
edit_page_id SERIAL PRIMARY KEY,
|
||||
edit_page_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
content_alias_edit_page_id INT, -- alias for page content, if the page content is defined on a different page, ege for ajax backend pages
|
||||
FOREIGN KEY (content_alias_edit_page_id) REFERENCES edit_page (edit_page_id) MATCH FULL ON DELETE RESTRICT ON UPDATE CASCADE,
|
||||
filename VARCHAR,
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
-- DROP TABLE edit_page_access;
|
||||
CREATE TABLE edit_page_access (
|
||||
edit_page_access_id SERIAL PRIMARY KEY,
|
||||
edit_page_access_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
edit_group_id INT NOT NULL,
|
||||
FOREIGN KEY (edit_group_id) REFERENCES edit_group (edit_group_id) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
edit_page_id INT NOT NULL,
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
-- DROP TABLE edit_page_content;
|
||||
CREATE TABLE edit_page_content (
|
||||
edit_page_content_id SERIAL PRIMARY KEY,
|
||||
edit_page_content_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
edit_page_id INT NOT NULL,
|
||||
FOREIGN KEY (edit_page_id) REFERENCES edit_page (edit_page_id) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
edit_access_right_id INT NOT NULL,
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
-- DROP TABLE edit_query_string;
|
||||
CREATE TABLE edit_query_string (
|
||||
edit_query_string_id SERIAL PRIMARY KEY,
|
||||
edit_query_string_id SERIAINT GENERATED ALWAYS AS IDENTITYL PRIMARY KEY,
|
||||
edit_page_id INT NOT NULL,
|
||||
FOREIGN KEY (edit_page_id) REFERENCES edit_page (edit_page_id) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
enabled SMALLINT NOT NULL DEFAULT 0,
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
-- DROP TABLE edit_scheme;
|
||||
CREATE TABLE edit_scheme (
|
||||
edit_scheme_id SERIAL PRIMARY KEY,
|
||||
edit_scheme_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
enabled SMALLINT NOT NULL DEFAULT 0,
|
||||
name VARCHAR,
|
||||
header_color VARCHAR,
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
-- DROP TABLE edit_user;
|
||||
CREATE TABLE edit_user (
|
||||
edit_user_id SERIAL PRIMARY KEY,
|
||||
edit_user_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
connect_edit_user_id INT, -- possible reference to other user
|
||||
FOREIGN KEY (connect_edit_user_id) REFERENCES edit_user (edit_user_id) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
edit_language_id INT NOT NULL,
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
-- DROP TABLE edit_visible_group;
|
||||
CREATE TABLE edit_visible_group (
|
||||
edit_visible_group_id SERIAL PRIMARY KEY,
|
||||
edit_visible_group_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
name VARCHAR,
|
||||
flag VARCHAR
|
||||
) INHERITS (edit_generic) WITHOUT OIDS;
|
||||
|
||||
65
4dev/tests/AAASetupData/requests/http_requests.php
Normal file
65
4dev/tests/AAASetupData/requests/http_requests.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php // phpcs:ignore PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* AUTHOR: Clemens Schwaighofer
|
||||
* CREATED: Ymd
|
||||
* DESCRIPTION:
|
||||
* DescriptionHere
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* build return json
|
||||
*
|
||||
* @param array<string,mixed> $http_headers
|
||||
* @param ?string $body
|
||||
* @return string
|
||||
*/
|
||||
function buildContent(array $http_headers, ?string $body): string
|
||||
{
|
||||
if (is_string($body) && !empty($body)) {
|
||||
$_body = json_decode($body, true);
|
||||
if (!is_array($_body)) {
|
||||
$body = [$body];
|
||||
} else {
|
||||
$body = $_body;
|
||||
}
|
||||
} elseif (is_string($body)) {
|
||||
$body = [];
|
||||
}
|
||||
return json_encode([
|
||||
'HEADERS' => $http_headers,
|
||||
"REQUEST_TYPE" => $_SERVER['REQUEST_METHOD'],
|
||||
"PARAMS" => $_GET,
|
||||
"BODY" => $body,
|
||||
]);
|
||||
}
|
||||
|
||||
$http_headers = array_filter($_SERVER, function ($value, $key) {
|
||||
if (str_starts_with($key, 'HTTP_')) {
|
||||
return true;
|
||||
}
|
||||
}, ARRAY_FILTER_USE_BOTH);
|
||||
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
|
||||
// if the header has Authorization and RunAuthTest then exit with 401
|
||||
if (!empty($http_headers['HTTP_AUTHORIZATION']) && !empty($http_headers['HTTP_RUNAUTHTEST'])) {
|
||||
header("HTTP/1.1 401 Unauthorized");
|
||||
print buildContent($http_headers, '{"code": 401, "content": {"Error": "Not Authorized"}}');
|
||||
exit;
|
||||
}
|
||||
|
||||
// if server request type is get set file_get to null -> no body
|
||||
if ($_SERVER['REQUEST_METHOD'] == "GET") {
|
||||
$file_get = null;
|
||||
} elseif (($file_get = file_get_contents('php://input')) === false) {
|
||||
header("HTTP/1.1 404 Not Found");
|
||||
print buildContent($http_headers, '{"code": 404, "content": {"Error": "file_get_contents failed"}}');
|
||||
exit;
|
||||
}
|
||||
|
||||
print buildContent($http_headers, $file_get);
|
||||
|
||||
// __END__
|
||||
1186
4dev/tests/Convert/CoreLibsConvertColorTest.php
Normal file
1186
4dev/tests/Convert/CoreLibsConvertColorTest.php
Normal file
File diff suppressed because it is too large
Load Diff
@@ -9,7 +9,7 @@ use PHPUnit\Framework\TestCase;
|
||||
/**
|
||||
* Test class for Convert\Colors
|
||||
* @coversDefaultClass \CoreLibs\Convert\Colors
|
||||
* @testdox \CoreLibs\Convert\Colors method tests
|
||||
* @testdox \CoreLibs\Convert\Colors legacy method tests
|
||||
*/
|
||||
final class CoreLibsConvertColorsTest extends TestCase
|
||||
{
|
||||
@@ -21,7 +21,7 @@ final class CoreLibsConvertColorsTest extends TestCase
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rgb2hexColorProvider(): array
|
||||
public function providerRgb2hexColor(): array
|
||||
{
|
||||
return [
|
||||
'color' => [
|
||||
@@ -88,7 +88,7 @@ final class CoreLibsConvertColorsTest extends TestCase
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function hex2rgbColorProvider(): array
|
||||
public function providerHex2rgbColor(): array
|
||||
{
|
||||
return [
|
||||
'color' => [
|
||||
@@ -215,7 +215,7 @@ final class CoreLibsConvertColorsTest extends TestCase
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rgb2hsbColorProvider(): array
|
||||
public function providerRgb2hsbColor(): array
|
||||
{
|
||||
$list = [];
|
||||
foreach ($this->rgb2hslAndhsbList() as $name => $values) {
|
||||
@@ -234,7 +234,7 @@ final class CoreLibsConvertColorsTest extends TestCase
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function hsb2rgbColorProvider(): array
|
||||
public function providerHsb2rgbColor(): array
|
||||
{
|
||||
$list = [];
|
||||
foreach ($this->rgb2hslAndhsbList() as $name => $values) {
|
||||
@@ -253,7 +253,7 @@ final class CoreLibsConvertColorsTest extends TestCase
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rgb2hslColorProvider(): array
|
||||
public function providerRgb2hslColor(): array
|
||||
{
|
||||
$list = [];
|
||||
foreach ($this->rgb2hslAndhsbList() as $name => $values) {
|
||||
@@ -272,7 +272,7 @@ final class CoreLibsConvertColorsTest extends TestCase
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function hsl2rgbColorProvider(): array
|
||||
public function providerHsl2rgbColor(): array
|
||||
{
|
||||
$list = [];
|
||||
foreach ($this->rgb2hslAndhsbList() as $name => $values) {
|
||||
@@ -291,7 +291,7 @@ final class CoreLibsConvertColorsTest extends TestCase
|
||||
* TODO: add cross convert check
|
||||
*
|
||||
* @covers ::rgb2hex
|
||||
* @dataProvider rgb2hexColorProvider
|
||||
* @dataProvider providerRgb2hexColor
|
||||
* @testdox rgb2hex $input_r,$input_g,$input_b will be $expected [$_dataName]
|
||||
*
|
||||
* @param int $input_r
|
||||
@@ -342,7 +342,7 @@ final class CoreLibsConvertColorsTest extends TestCase
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::hex2rgb
|
||||
* @dataProvider hex2rgbColorProvider
|
||||
* @dataProvider providerHex2rgbColor
|
||||
* @testdox hex2rgb $input will be $expected, $expected_str str[,], $expected_str_sep str[$separator] [$_dataName]
|
||||
*
|
||||
* @param string $input
|
||||
@@ -385,7 +385,7 @@ final class CoreLibsConvertColorsTest extends TestCase
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::rgb2hsb
|
||||
* @dataProvider rgb2hsbColorProvider
|
||||
* @dataProvider providerRgb2hsbColor
|
||||
* @testdox rgb2hsb $input_r,$input_g,$input_b will be $expected [$_dataName]
|
||||
*
|
||||
* @param integer $input_r
|
||||
@@ -409,7 +409,7 @@ final class CoreLibsConvertColorsTest extends TestCase
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::hsb2rgb
|
||||
* @dataProvider hsb2rgbColorProvider
|
||||
* @dataProvider providerHsb2rgbColor
|
||||
* @testdox hsb2rgb $input_h,$input_s,$input_b will be $expected [$_dataName]
|
||||
*
|
||||
* @param float $input_h
|
||||
@@ -434,7 +434,7 @@ final class CoreLibsConvertColorsTest extends TestCase
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::rgb2hsl
|
||||
* @dataProvider rgb2hslColorProvider
|
||||
* @dataProvider providerRgb2hslColor
|
||||
* @testdox rgb2hsl $input_r,$input_g,$input_b will be $expected [$_dataName]
|
||||
*
|
||||
* @param integer $input_r
|
||||
@@ -458,7 +458,7 @@ final class CoreLibsConvertColorsTest extends TestCase
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::hsl2rgb
|
||||
* @dataProvider hsl2rgbColorProvider
|
||||
* @dataProvider providerHsl2rgbColor
|
||||
* @testdox hsl2rgb $input_h,$input_s,$input_l will be $expected [$_dataName]
|
||||
*
|
||||
* @param integer|float $input_h
|
||||
|
||||
@@ -18,7 +18,7 @@ final class CoreLibsConvertMathTest extends TestCase
|
||||
*
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function fceilProvider(): array
|
||||
public function providerFceil(): array
|
||||
{
|
||||
return [
|
||||
'5.5 must be 6' => [5.5, 6],
|
||||
@@ -31,7 +31,7 @@ final class CoreLibsConvertMathTest extends TestCase
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::fceil
|
||||
* @dataProvider fceilProvider
|
||||
* @dataProvider providerFceil
|
||||
* @testdox fceil: Input $input must be $expected
|
||||
*
|
||||
* @param float $input
|
||||
@@ -51,7 +51,7 @@ final class CoreLibsConvertMathTest extends TestCase
|
||||
*
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function floorProvider(): array
|
||||
public function providerFloor(): array
|
||||
{
|
||||
return [
|
||||
'5123456 with -3 must be 5123000' => [5123456, -3, 5123000],
|
||||
@@ -63,7 +63,7 @@ final class CoreLibsConvertMathTest extends TestCase
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::floorp
|
||||
* @dataProvider floorProvider
|
||||
* @dataProvider providerFloor
|
||||
* @testdox floor: Input $input with cutoff $cutoff must be $expected
|
||||
*
|
||||
* @param int $input
|
||||
@@ -84,7 +84,7 @@ final class CoreLibsConvertMathTest extends TestCase
|
||||
*
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function initNumericProvider(): array
|
||||
public function providerInitNumeric(): array
|
||||
{
|
||||
return [
|
||||
'5 must be 5' => [5, 5, 'int'],
|
||||
@@ -98,7 +98,7 @@ final class CoreLibsConvertMathTest extends TestCase
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::initNumeric
|
||||
* @dataProvider initNumericProvider
|
||||
* @dataProvider providerInitNumeric
|
||||
* @testdox initNumeric: Input $info $input must match $expected [$_dataName]
|
||||
*
|
||||
* @param int|float|string $input
|
||||
@@ -113,6 +113,388 @@ final class CoreLibsConvertMathTest extends TestCase
|
||||
\CoreLibs\Convert\Math::initNumeric($input)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function providerCbrt(): array
|
||||
{
|
||||
return [
|
||||
'cube root of 2' => [2, 1.25992, 5],
|
||||
'cube root of 3' => [3, 1.44225, 5],
|
||||
'cube root of -1' => [-1, 'NAN', 0],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::cbrt
|
||||
* @dataProvider providerCbrt
|
||||
* @testdox initNumeric: Input $input must match $expected [$_dataName]
|
||||
*
|
||||
* @param float|int $number
|
||||
* @param float $expected
|
||||
* @param int $round_to
|
||||
* @return void
|
||||
*/
|
||||
public function testCbrt(float|int $number, float|string $expected, int $round_to): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
round(\CoreLibs\Convert\Math::cbrt($number), $round_to)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function providerMultiplyMatrices(): array
|
||||
{
|
||||
return [
|
||||
'[3] x [3] => [3x1]' => [
|
||||
[1, 2, 3],
|
||||
[1, 2, 3],
|
||||
[14]
|
||||
],
|
||||
'[3] x [3x1]' => [
|
||||
[1, 2, 3],
|
||||
[[1], [2], [3]],
|
||||
[14]
|
||||
],
|
||||
'[3] x [3x1]' => [
|
||||
[1, 2, 3],
|
||||
[[1], [2], [3]],
|
||||
[14]
|
||||
],
|
||||
'[1x3L] x [3x1]' => [
|
||||
[[1, 2, 3]],
|
||||
[[1], [2], [3]],
|
||||
[14]
|
||||
],
|
||||
'[1x3] x [3x1]' => [
|
||||
[[1], [2], [3]],
|
||||
[[1], [2], [3]],
|
||||
[1, 2, 3]
|
||||
],
|
||||
'[2x3] x [3] => [3x1]' => [
|
||||
[
|
||||
[1, 2, 3],
|
||||
[1, 2, 3]
|
||||
],
|
||||
[1, 2, 3],
|
||||
[
|
||||
14,
|
||||
14
|
||||
]
|
||||
],
|
||||
'[2x3] x [3x1]' => [
|
||||
[
|
||||
[1, 2, 3],
|
||||
[1, 2, 3]
|
||||
],
|
||||
[[1], [2], [3]],
|
||||
[
|
||||
14,
|
||||
14
|
||||
]
|
||||
],
|
||||
'[2x3] x [2x3] => [3x3]' => [
|
||||
[
|
||||
[1, 2, 3],
|
||||
[1, 2, 3],
|
||||
],
|
||||
[
|
||||
[1, 2, 3],
|
||||
[1, 2, 3],
|
||||
],
|
||||
[
|
||||
[3, 6, 9],
|
||||
[3, 6, 9]
|
||||
]
|
||||
],
|
||||
'[2x3] x [3x3]' => [
|
||||
[
|
||||
[1, 2, 3],
|
||||
[1, 2, 3],
|
||||
],
|
||||
[
|
||||
[1, 2, 3],
|
||||
[1, 2, 3],
|
||||
[0, 0, 0],
|
||||
],
|
||||
[
|
||||
[3, 6, 9],
|
||||
[3, 6, 9]
|
||||
]
|
||||
],
|
||||
'[2x3] x [3x2]' => [
|
||||
'a' => [
|
||||
[1, 2, 3],
|
||||
[1, 2, 3],
|
||||
],
|
||||
'b' => [
|
||||
[1, 1],
|
||||
[2, 2],
|
||||
[3, 3],
|
||||
],
|
||||
'prod' => [
|
||||
[14, 14],
|
||||
[14, 14],
|
||||
]
|
||||
],
|
||||
'[3x3] x [3] => [1x3]' => [
|
||||
[
|
||||
[1, 2, 3],
|
||||
[1, 2, 3],
|
||||
[1, 2, 3],
|
||||
],
|
||||
[1, 2, 3],
|
||||
[
|
||||
14,
|
||||
14,
|
||||
14
|
||||
]
|
||||
],
|
||||
'[3x3] x [2x3] => [3x3]' => [
|
||||
[
|
||||
[1, 2, 3],
|
||||
[1, 2, 3],
|
||||
[1, 2, 3],
|
||||
],
|
||||
[
|
||||
[1, 2, 3],
|
||||
[1, 2, 3],
|
||||
],
|
||||
[
|
||||
[3, 6, 9],
|
||||
[3, 6, 9],
|
||||
[3, 6, 9],
|
||||
]
|
||||
],
|
||||
'[3x3] x [3x3]' => [
|
||||
[
|
||||
[1, 2, 3],
|
||||
[1, 2, 3],
|
||||
[1, 2, 3],
|
||||
],
|
||||
[
|
||||
[1, 2, 3],
|
||||
[1, 2, 3],
|
||||
// [0, 0, 0],
|
||||
],
|
||||
[
|
||||
[3, 6, 9],
|
||||
[3, 6, 9],
|
||||
[3, 6, 9],
|
||||
]
|
||||
],
|
||||
'[3] x [3x3]' => [
|
||||
[1, 2, 3],
|
||||
[
|
||||
[1, 2, 3],
|
||||
[1, 2, 3],
|
||||
[1, 2, 3],
|
||||
],
|
||||
[
|
||||
[6, 12, 18],
|
||||
]
|
||||
],
|
||||
'[2x3] x [3x3]' => [
|
||||
[
|
||||
[1, 2, 3],
|
||||
[1, 2, 3],
|
||||
],
|
||||
[
|
||||
[1, 2, 3],
|
||||
[1, 2, 3],
|
||||
[1, 2, 3],
|
||||
],
|
||||
[
|
||||
[6, 12, 18],
|
||||
[6, 12, 18],
|
||||
]
|
||||
],
|
||||
'inblanaced [2x2,3] x [3x2]' => [
|
||||
'a' => [
|
||||
[1, 2, 3],
|
||||
[4, 5]
|
||||
],
|
||||
'b' => [
|
||||
[6, 7],
|
||||
[8, 9],
|
||||
[10, 11]
|
||||
],
|
||||
'result' => [
|
||||
[52, 58],
|
||||
[64, 73],
|
||||
]
|
||||
],
|
||||
'inblanaced [2x3] x [3x1,2]' => [
|
||||
'a' => [
|
||||
[1, 2, 3],
|
||||
[4, 5, 7]
|
||||
],
|
||||
'b' => [
|
||||
[7, 8],
|
||||
[9, 10],
|
||||
[11]
|
||||
],
|
||||
'result' => [
|
||||
[58, 28],
|
||||
[150, 82],
|
||||
]
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::multiplyMatrices
|
||||
* @dataProvider providerMultiplyMatrices
|
||||
* @testdox initNumeric: Input $input_a x $input_b must match $expected [$_dataName]
|
||||
*
|
||||
* @param array $input_a
|
||||
* @param array $input_b
|
||||
* @param array $expected
|
||||
* @return void
|
||||
*/
|
||||
public function testMultiplyMatrices(array $input_a, array $input_b, array $expected): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
\CoreLibs\Convert\Math::multiplyMatrices($input_a, $input_b)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function providerEqualWithEpsilon(): array
|
||||
{
|
||||
return [
|
||||
'equal' => [
|
||||
'a' => 0.000000000000000222,
|
||||
'b' => 0.000000000000000222,
|
||||
'epsilon' => PHP_FLOAT_EPSILON,
|
||||
'equal' => true,
|
||||
],
|
||||
'almost equal' => [
|
||||
'a' => 0.000000000000000222,
|
||||
'b' => 0.000000000000000232,
|
||||
'epsilon' => PHP_FLOAT_EPSILON,
|
||||
'equal' => true,
|
||||
],
|
||||
'not equal' => [
|
||||
'a' => 0.000000000000000222,
|
||||
'b' => 0.000000000000004222,
|
||||
'epsilon' => PHP_FLOAT_EPSILON,
|
||||
'equal' => false,
|
||||
],
|
||||
'equal, different epsilon' => [
|
||||
'a' => 0.000000000000000222,
|
||||
'b' => 0.000000000000004222,
|
||||
'epsilon' => 0.0001,
|
||||
'equal' => true,
|
||||
],
|
||||
'not equal, different epsilon' => [
|
||||
'a' => 0.0001,
|
||||
'b' => 0.0002,
|
||||
'epsilon' => 0.0001,
|
||||
'equal' => false,
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::equalWithEpsilon
|
||||
* @dataProvider providerEqualWithEpsilon
|
||||
* @testdox equalWithEpsilon with $a and $b and Epsilon: $epsilon must be equal: $equal [$_dataName]
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testEqualWithEpsilon(float $a, float $b, float $epsilon, bool $equal): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$equal,
|
||||
\CoreLibs\Convert\Math::equalWithEpsilon($a, $b, $epsilon)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function providerCompareWithEpsilon(): array
|
||||
{
|
||||
return [
|
||||
'smaller, true' => [
|
||||
'value' => 0.0001,
|
||||
'compare' => '<',
|
||||
'limit' => 0.0002,
|
||||
'epsilon' => 0.00001,
|
||||
'match' => true,
|
||||
],
|
||||
'smaller, false' => [
|
||||
'value' => 0.0001,
|
||||
'compare' => '<',
|
||||
'limit' => 0.0001,
|
||||
'epsilon' => 0.00001,
|
||||
'match' => false,
|
||||
],
|
||||
'bigger, true' => [
|
||||
'value' => 0.0002,
|
||||
'compare' => '>',
|
||||
'limit' => 0.0001,
|
||||
'epsilon' => 0.00001,
|
||||
'match' => true,
|
||||
],
|
||||
'bigger, false' => [
|
||||
'value' => 0.0001,
|
||||
'compare' => '>',
|
||||
'limit' => 0.0001,
|
||||
'epsilon' => 0.00001,
|
||||
'match' => false,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @covers ::compareWithEpsilon
|
||||
* @dataProvider providerCompareWithEpsilon
|
||||
* @testdox compareWithEpsilon $value $compare $limit with $epsilon must match: $match [$_dataName]
|
||||
*
|
||||
* @param float $value
|
||||
* @param string $compare
|
||||
* @param float $limit
|
||||
* @param float $epslion
|
||||
* @param bool $match
|
||||
* @return void
|
||||
*/
|
||||
public function testCompareWithEpsilon(
|
||||
float $value,
|
||||
string $compare,
|
||||
float $limit,
|
||||
float $epsilon,
|
||||
bool $match
|
||||
): void {
|
||||
$this->assertEquals(
|
||||
$match,
|
||||
\CoreLibs\Convert\Math::compareWithEpsilon($value, $compare, $limit, $epsilon)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
|
||||
@@ -10,7 +10,6 @@ use PHPUnit\Framework\TestCase;
|
||||
* Test class for DB\Extended\ArrayIO
|
||||
* This will only test the PgSQL parts
|
||||
* @coversDefaultClass \CoreLibs\DB\Extended\ArrayIO
|
||||
* @coversDefaultClass \CoreLibs\DB\Extended\ArrayIO
|
||||
* @testdox \CoreLibs\Extended\ArrayIO method tests for extended DB interface
|
||||
*/
|
||||
final class CoreLibsDBExtendedArrayIOTest extends TestCase
|
||||
|
||||
@@ -37,8 +37,9 @@ namespace tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use CoreLibs\Logging\Logger\Level;
|
||||
use CoreLibs\Logging;
|
||||
use CoreLibs\DB\Options\Convert;
|
||||
use CoreLibs\DB\Support\ConvertPlaceholder;
|
||||
|
||||
/**
|
||||
* Test class for DB\IO + DB\SQL\PgSQL
|
||||
@@ -117,7 +118,7 @@ final class CoreLibsDBIOTest extends TestCase
|
||||
);
|
||||
}
|
||||
// define basic connection set valid and one invalid
|
||||
self::$log = new \CoreLibs\Logging\Logging([
|
||||
self::$log = new Logging\Logging([
|
||||
// 'log_folder' => __DIR__ . DIRECTORY_SEPARATOR . 'log',
|
||||
'log_folder' => DIRECTORY_SEPARATOR . 'tmp',
|
||||
'log_file_id' => 'CoreLibs-DB-IO-Test',
|
||||
@@ -159,15 +160,12 @@ final class CoreLibsDBIOTest extends TestCase
|
||||
// create the tables
|
||||
$db->dbExec(
|
||||
// primary key name is table + '_id'
|
||||
// table_with_primary_key_id SERIAL PRIMARY KEY,
|
||||
<<<SQL
|
||||
CREATE TABLE table_with_primary_key (
|
||||
table_with_primary_key_id SERIAL PRIMARY KEY,
|
||||
table_with_primary_key_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
$base_table
|
||||
SQL
|
||||
/* "CREATE TABLE table_with_primary_key ("
|
||||
// primary key name is table + '_id'
|
||||
. "table_with_primary_key_id SERIAL PRIMARY KEY, "
|
||||
. $base_table */
|
||||
);
|
||||
$db->dbExec(
|
||||
<<<SQL
|
||||
@@ -570,11 +568,11 @@ final class CoreLibsDBIOTest extends TestCase
|
||||
);
|
||||
$db->dbClose();
|
||||
// second conenction with log set NOT debug
|
||||
$log = new \CoreLibs\Logging\Logging([
|
||||
$log = new Logging\Logging([
|
||||
// 'log_folder' => __DIR__ . DIRECTORY_SEPARATOR . 'log',
|
||||
'log_folder' => DIRECTORY_SEPARATOR . 'tmp',
|
||||
'log_file_id' => 'CoreLibs-DB-IO-Test',
|
||||
'log_level' => \CoreLibs\Logging\Logger\Level::Notice,
|
||||
'log_level' => Logging\Logger\Level::Notice,
|
||||
]);
|
||||
$db = new \CoreLibs\DB\IO(
|
||||
self::$db_config[$connection],
|
||||
@@ -3293,6 +3291,7 @@ final class CoreLibsDBIOTest extends TestCase
|
||||
'query' => 'INSERT INTO table_with_primary_key (row_int, uid) '
|
||||
. 'VALUES ($1, $2) RETURNING table_with_primary_key_id',
|
||||
'returning_id' => true,
|
||||
'placeholder_converted' => [],
|
||||
],
|
||||
],
|
||||
// update
|
||||
@@ -3327,6 +3326,7 @@ final class CoreLibsDBIOTest extends TestCase
|
||||
'query' => 'UPDATE table_with_primary_key SET row_int = $1, '
|
||||
. 'row_varchar = $2 WHERE uid = $3',
|
||||
'returning_id' => false,
|
||||
'placeholder_converted' => [],
|
||||
],
|
||||
],
|
||||
// select
|
||||
@@ -3356,6 +3356,7 @@ final class CoreLibsDBIOTest extends TestCase
|
||||
'count' => 1,
|
||||
'query' => 'SELECT row_int, uid FROM table_with_primary_key WHERE uid = $1',
|
||||
'returning_id' => false,
|
||||
'placeholder_converted' => [],
|
||||
],
|
||||
],
|
||||
// any query but with no parameters
|
||||
@@ -3388,6 +3389,7 @@ final class CoreLibsDBIOTest extends TestCase
|
||||
'count' => 0,
|
||||
'query' => 'SELECT row_int, uid FROM table_with_primary_key',
|
||||
'returning_id' => false,
|
||||
'placeholder_converted' => [],
|
||||
],
|
||||
],
|
||||
// no statement name (25)
|
||||
@@ -3411,6 +3413,7 @@ final class CoreLibsDBIOTest extends TestCase
|
||||
'count' => 0,
|
||||
'query' => '',
|
||||
'returning_id' => false,
|
||||
'placeholder_converted' => [],
|
||||
],
|
||||
],
|
||||
// no query (prepare 11)
|
||||
@@ -3435,6 +3438,7 @@ final class CoreLibsDBIOTest extends TestCase
|
||||
'count' => 0,
|
||||
'query' => '',
|
||||
'returning_id' => false,
|
||||
'placeholder_converted' => [],
|
||||
],
|
||||
],
|
||||
// no db connection (prepare/execute 16)
|
||||
@@ -3464,6 +3468,7 @@ final class CoreLibsDBIOTest extends TestCase
|
||||
'count' => 0,
|
||||
'query' => 'SELECT row_int, uid FROM table_with_primary_key',
|
||||
'returning_id' => false,
|
||||
'placeholder_converted' => [],
|
||||
],
|
||||
],
|
||||
// prepare with different statement name
|
||||
@@ -3489,6 +3494,7 @@ final class CoreLibsDBIOTest extends TestCase
|
||||
'count' => 0,
|
||||
'query' => 'SELECT row_int, uid FROM table_with_primary_key',
|
||||
'returning_id' => false,
|
||||
'placeholder_converted' => [],
|
||||
],
|
||||
],
|
||||
// insert wrong data count compared to needed (execute 23)
|
||||
@@ -3514,10 +3520,12 @@ final class CoreLibsDBIOTest extends TestCase
|
||||
'query' => 'INSERT INTO table_with_primary_key (row_int, uid) VALUES '
|
||||
. '($1, $2) RETURNING table_with_primary_key_id',
|
||||
'returning_id' => true,
|
||||
'placeholder_converted' => [],
|
||||
],
|
||||
],
|
||||
// execute does not return a result (22)
|
||||
// TODO execute does not return a result
|
||||
// TODO prepared statement with placeholder params auto convert
|
||||
];
|
||||
}
|
||||
|
||||
@@ -3662,7 +3670,7 @@ final class CoreLibsDBIOTest extends TestCase
|
||||
}
|
||||
|
||||
// check dbGetPrepareCursorValue
|
||||
foreach (['pk_name', 'count', 'query', 'returning_id'] as $key) {
|
||||
foreach (['pk_name', 'count', 'query', 'returning_id', 'placeholder_converted'] as $key) {
|
||||
$this->assertEquals(
|
||||
$prepare_cursor[$key],
|
||||
$db->dbGetPrepareCursorValue($stm_name, $key),
|
||||
@@ -5031,8 +5039,151 @@ final class CoreLibsDBIOTest extends TestCase
|
||||
$db->dbClose();
|
||||
}
|
||||
|
||||
// query placeholder convert
|
||||
// MARK: QUERY PLACEHOLDERS
|
||||
|
||||
// test query placeholder detection for all possible sets
|
||||
// ::dbPrepare
|
||||
|
||||
/**
|
||||
* placeholder sql
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function providerDbCountQueryParams(): array
|
||||
{
|
||||
return [
|
||||
'one place holder' => [
|
||||
'query' => 'SELECT row_varchar FROM table_with_primary_key WHERE row_varchar = $1',
|
||||
'count' => 1,
|
||||
'convert' => false,
|
||||
],
|
||||
'one place holder, json call' => [
|
||||
'query' => "SELECT row_varchar FROM table_with_primary_key WHERE row_jsonb->>'data' = $1",
|
||||
'count' => 1,
|
||||
'convert' => false,
|
||||
],
|
||||
'one place holder, <> compare' => [
|
||||
'query' => "SELECT row_varchar FROM table_with_primary_key WHERE row_varchar <> $1",
|
||||
'count' => 1,
|
||||
'convert' => false,
|
||||
],
|
||||
'one place holder, named' => [
|
||||
'query' => "SELECT row_varchar FROM table_with_primary_key WHERE row_varchar <> :row_varchar",
|
||||
'count' => 1,
|
||||
'convert' => true,
|
||||
],
|
||||
'no replacement' => [
|
||||
'query' => "SELECT row_varchar FROM table_with_primary_key WHERE row_varchar = '$1'",
|
||||
'count' => 0,
|
||||
'convert' => false,
|
||||
],
|
||||
'insert' => [
|
||||
'query' => "INSERT INTO table_with_primary_key (row_varchar, row_jsonb, row_int) VALUES ($1, $2, $3)",
|
||||
'count' => 3,
|
||||
'convert' => false,
|
||||
],
|
||||
'update' => [
|
||||
'query' => "UPDATE table_with_primary_key SET row_varchar = $1, row_jsonb = $2, row_int = $3 WHERE row_numeric = $4",
|
||||
'count' => 4,
|
||||
'convert' => false,
|
||||
],
|
||||
'multiple, multline' => [
|
||||
'query' => <<<SQL
|
||||
SELECT
|
||||
row_varchar
|
||||
FROM
|
||||
table_with_primary_key
|
||||
WHERE
|
||||
row_varchar = $1 AND row_int = $2
|
||||
AND row_numeric = ANY($3)
|
||||
SQL,
|
||||
'count' => 3,
|
||||
'convert' => false,
|
||||
],
|
||||
'two digit numbers' => [
|
||||
'query' => <<<SQL
|
||||
INSERT INTO table_with_primary_key (
|
||||
row_int, row_numeric, row_varchar, row_varchar_literal, row_json,
|
||||
row_jsonb, row_bytea, row_timestamp, row_date, row_interval
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $5,
|
||||
$6, $7, $8, $9, $10
|
||||
)
|
||||
SQL,
|
||||
'count' => 10,
|
||||
'convert' => false,
|
||||
],
|
||||
'things in brackets' => [
|
||||
'query' => <<<SQL
|
||||
SELECT row_varchar
|
||||
FROM table_with_primary_key
|
||||
WHERE
|
||||
row_varchar = $1 AND
|
||||
(row_int = ANY($2) OR row_int = $3)
|
||||
AND row_varchar_literal = $4
|
||||
SQL,
|
||||
'count' => 4,
|
||||
'convert' => false,
|
||||
],
|
||||
'number compare' => [
|
||||
'query' => <<<SQL
|
||||
SELECT row_varchar
|
||||
FROM table_with_primary_key
|
||||
WHERE
|
||||
row_int >= $1 OR row_int <= $2 OR
|
||||
row_int > $3 OR row_int < $4
|
||||
OR row_int = $5 OR row_int <> $6
|
||||
SQL,
|
||||
'count' => 6,
|
||||
'convert' => false,
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Placeholder check and convert tests
|
||||
*
|
||||
* @covers ::dbPrepare
|
||||
* @covers ::__dbCountQueryParams
|
||||
* @onvers ::convertPlaceholderInQuery
|
||||
* @dataProvider providerDbCountQueryParams
|
||||
* @testdox Query replacement count test [$_dataName]
|
||||
*
|
||||
* @param string $query
|
||||
* @param int $count
|
||||
* @return void
|
||||
*/
|
||||
public function testDbCountQueryParams(string $query, int $count, bool $convert): void
|
||||
{
|
||||
$db = new \CoreLibs\DB\IO(
|
||||
self::$db_config['valid'],
|
||||
self::$log
|
||||
);
|
||||
$id = sha1($query);
|
||||
$db->dbSetConvertPlaceholder($convert);
|
||||
$db->dbPrepare($id, $query);
|
||||
// print "\n**\n";
|
||||
// print "PCount: " . $db->dbGetPrepareCursorValue($id, 'count') . "\n";
|
||||
// print "\n**\n";
|
||||
$this->assertEquals(
|
||||
$count,
|
||||
$db->dbGetPrepareCursorValue($id, 'count'),
|
||||
'DB count params'
|
||||
);
|
||||
$placeholder = ConvertPlaceholder::convertPlaceholderInQuery($query, null, 'pg');
|
||||
// print "RES: " . print_r($placeholder, true) . "\n";
|
||||
$this->assertEquals(
|
||||
$count,
|
||||
$placeholder['needed'],
|
||||
'convert params'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* query placeholder convert
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function queryPlaceholderReplaceProvider(): array
|
||||
{
|
||||
// WHERE row_varchar = $1
|
||||
@@ -5076,7 +5227,9 @@ final class CoreLibsDBIOTest extends TestCase
|
||||
WHERE row_varchar = $1
|
||||
SQL,
|
||||
'expected_params' => ['string a'],
|
||||
]
|
||||
],
|
||||
// TODO: test with multiple entries
|
||||
// TODO: test with same entry ($1, $1, :var, :var)
|
||||
];
|
||||
}
|
||||
|
||||
@@ -5178,6 +5331,8 @@ final class CoreLibsDBIOTest extends TestCase
|
||||
// - data debug
|
||||
// dbDumpData
|
||||
|
||||
// MARK: ASYNC
|
||||
|
||||
// ASYNC at the end because it has 1s timeout
|
||||
// - asynchronous executions
|
||||
// dbExecAsync, dbCheckAsync
|
||||
|
||||
@@ -230,6 +230,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
|
||||
'str' => 'ERROR MESSAGE',
|
||||
'message' => null,
|
||||
'log_error' => null,
|
||||
'log_warning' => null,
|
||||
'expected' => '<ERROR> ERROR MESSAGE',
|
||||
],
|
||||
'error, logged' => [
|
||||
@@ -238,6 +239,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
|
||||
'str' => 'ERROR MESSAGE',
|
||||
'message' => null,
|
||||
'log_error' => true,
|
||||
'log_warning' => null,
|
||||
'expected' => '<ERROR> ERROR MESSAGE',
|
||||
],
|
||||
'error, logged, message' => [
|
||||
@@ -246,14 +248,43 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
|
||||
'str' => 'ERROR MESSAGE',
|
||||
'message' => 'OTHER ERROR MESSAGE',
|
||||
'log_error' => true,
|
||||
'log_warning' => null,
|
||||
'expected' => '<ERROR> OTHER ERROR MESSAGE',
|
||||
],
|
||||
'warn, not logged' => [
|
||||
'id' => '300',
|
||||
'level' => 'warn',
|
||||
'str' => 'WARNING MESSAGE',
|
||||
'message' => null,
|
||||
'log_error' => null,
|
||||
'log_warning' => null,
|
||||
'expected' => '<WARNING> WARNING MESSAGE',
|
||||
],
|
||||
'warn, logged' => [
|
||||
'id' => '300',
|
||||
'level' => 'warn',
|
||||
'str' => 'WARNING MESSAGE',
|
||||
'message' => null,
|
||||
'log_error' => null,
|
||||
'log_warning' => true,
|
||||
'expected' => '<WARNING> WARNING MESSAGE',
|
||||
],
|
||||
'warn, logged, message' => [
|
||||
'id' => '300',
|
||||
'level' => 'warn',
|
||||
'str' => 'WARNING MESSAGE',
|
||||
'message' => 'OTHER WARNING MESSAGE',
|
||||
'log_error' => null,
|
||||
'log_warning' => true,
|
||||
'expected' => '<WARNING> OTHER WARNING MESSAGE',
|
||||
],
|
||||
'notice' => [
|
||||
'id' => '100',
|
||||
'level' => 'notice',
|
||||
'str' => 'NOTICE MESSAGE',
|
||||
'message' => null,
|
||||
'log_error' => null,
|
||||
'log_warning' => null,
|
||||
'expected' => '<NOTICE> NOTICE MESSAGE',
|
||||
],
|
||||
'notice, message' => [
|
||||
@@ -262,6 +293,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
|
||||
'str' => 'NOTICE MESSAGE',
|
||||
'message' => 'OTHER NOTICE MESSAGE',
|
||||
'log_error' => null,
|
||||
'log_warning' => null,
|
||||
'expected' => '<NOTICE> OTHER NOTICE MESSAGE',
|
||||
],
|
||||
'crash' => [
|
||||
@@ -270,6 +302,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
|
||||
'str' => 'CRASH MESSAGE',
|
||||
'message' => null,
|
||||
'log_error' => null,
|
||||
'log_warning' => null,
|
||||
'expected' => '<ALERT> CRASH MESSAGE',
|
||||
],
|
||||
'crash, message' => [
|
||||
@@ -278,6 +311,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
|
||||
'str' => 'CRASH MESSAGE',
|
||||
'message' => 'OTHER CRASH MESSAGE',
|
||||
'log_error' => null,
|
||||
'log_warning' => null,
|
||||
'expected' => '<ALERT> OTHER CRASH MESSAGE',
|
||||
],
|
||||
'abort' => [
|
||||
@@ -286,6 +320,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
|
||||
'str' => 'ABORT MESSAGE',
|
||||
'message' => null,
|
||||
'log_error' => null,
|
||||
'log_warning' => null,
|
||||
'expected' => '<CRITICAL> ABORT MESSAGE',
|
||||
],
|
||||
'abort, message' => [
|
||||
@@ -294,6 +329,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
|
||||
'str' => 'ABORT MESSAGE',
|
||||
'message' => 'OTHER ABORT MESSAGE',
|
||||
'log_error' => null,
|
||||
'log_warning' => null,
|
||||
'expected' => '<CRITICAL> OTHER ABORT MESSAGE',
|
||||
],
|
||||
'unknown' => [
|
||||
@@ -302,6 +338,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
|
||||
'str' => 'WRONG LEVEL MESSAGE',
|
||||
'message' => null,
|
||||
'log_error' => null,
|
||||
'log_warning' => null,
|
||||
'expected' => '<EMERGENCY> WRONG LEVEL MESSAGE',
|
||||
],
|
||||
'unknown, message' => [
|
||||
@@ -310,6 +347,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
|
||||
'str' => 'WRONG LEVEL MESSAGE',
|
||||
'message' => 'OTHER WRONG LEVEL MESSAGE',
|
||||
'log_error' => null,
|
||||
'log_warning' => null,
|
||||
'expected' => '<EMERGENCY> OTHER WRONG LEVEL MESSAGE',
|
||||
],
|
||||
];
|
||||
@@ -326,6 +364,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
|
||||
* @param string $str
|
||||
* @param string|null $message
|
||||
* @param bool|null $log_error
|
||||
* @param bool|null $log_warning
|
||||
* @param string $expected
|
||||
* @return void
|
||||
*/
|
||||
@@ -335,6 +374,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
|
||||
string $str,
|
||||
?string $message,
|
||||
?bool $log_error,
|
||||
?bool $log_warning,
|
||||
string $expected
|
||||
): void {
|
||||
$log = new \CoreLibs\Logging\Logging([
|
||||
@@ -349,7 +389,8 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
|
||||
$level,
|
||||
$str,
|
||||
message: $message,
|
||||
log_error: $log_error
|
||||
log_error: $log_error,
|
||||
log_warning: $log_warning
|
||||
);
|
||||
$file_content = '';
|
||||
if (is_file($log->getLogFolder() . $log->getLogFile())) {
|
||||
@@ -363,6 +404,11 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
|
||||
$expected,
|
||||
$file_content
|
||||
);
|
||||
} elseif ($level == 'warn' && ($log_warning === null || $log_warning === false)) {
|
||||
$this->assertStringNotContainsString(
|
||||
$expected,
|
||||
$file_content
|
||||
);
|
||||
} else {
|
||||
$this->assertStringContainsString(
|
||||
$expected,
|
||||
@@ -382,6 +428,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
|
||||
* @param string $str
|
||||
* @param string|null $message
|
||||
* @param bool|null $log_error
|
||||
* @param bool|null $log_warning
|
||||
* @param string $expected
|
||||
* @return void
|
||||
*/
|
||||
@@ -391,6 +438,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
|
||||
string $str,
|
||||
?string $message,
|
||||
?bool $log_error,
|
||||
?bool $log_warning,
|
||||
string $expected
|
||||
): void {
|
||||
$log = new \CoreLibs\Logging\Logging([
|
||||
@@ -405,7 +453,8 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
|
||||
$level,
|
||||
$str,
|
||||
message: $message,
|
||||
log_error: $log_error
|
||||
log_error: $log_error,
|
||||
log_warning: $log_warning
|
||||
);
|
||||
$file_content = '';
|
||||
if (is_file($log->getLogFolder() . $log->getLogFile())) {
|
||||
@@ -419,6 +468,11 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
|
||||
$expected,
|
||||
$file_content
|
||||
);
|
||||
} elseif ($level == 'warn' && $log_warning === false) {
|
||||
$this->assertStringNotContainsString(
|
||||
$expected,
|
||||
$file_content
|
||||
);
|
||||
} else {
|
||||
$this->assertStringContainsString(
|
||||
$expected,
|
||||
|
||||
1232
4dev/tests/UrlRequests/CoreLibsUrlRequestsCurlTest.php
Normal file
1232
4dev/tests/UrlRequests/CoreLibsUrlRequestsCurlTest.php
Normal file
File diff suppressed because it is too large
Load Diff
@@ -3,9 +3,20 @@
|
||||
"version": "dev-master",
|
||||
"description": "CoreLibs: Development package",
|
||||
"type": "library",
|
||||
"config": {
|
||||
},
|
||||
"require": {
|
||||
"php": ">=8.1"
|
||||
"php": ">=8.3"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpstan/phpstan": "^2.0",
|
||||
"phpstan/phpstan-deprecation-rules": "^2.0",
|
||||
"phpstan/extension-installer": "^1.4",
|
||||
"phan/phan": "^5.4",
|
||||
"phpunit/phpunit": "^9",
|
||||
"yamadashy/phpstan-friendly-formatter": "^1.1"
|
||||
},
|
||||
"config": {
|
||||
"allow-plugins": {
|
||||
"phpstan/extension-installer": true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
20
composer.lock
generated
20
composer.lock
generated
@@ -1,20 +0,0 @@
|
||||
{
|
||||
"_readme": [
|
||||
"This file locks the dependencies of your project to a known state",
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "3c37bd2878b371840fc0d7d4a249ea4c",
|
||||
"packages": [],
|
||||
"packages-dev": [],
|
||||
"aliases": [],
|
||||
"minimum-stability": "stable",
|
||||
"stability-flags": [],
|
||||
"prefer-stable": false,
|
||||
"prefer-lowest": false,
|
||||
"platform": {
|
||||
"php": ">=8.1"
|
||||
},
|
||||
"platform-dev": [],
|
||||
"plugin-api-version": "2.3.0"
|
||||
}
|
||||
14
phpstan.neon
14
phpstan.neon
@@ -1,9 +1,17 @@
|
||||
# PHP Stan Config
|
||||
includes:
|
||||
- phpstan-conditional.php
|
||||
#- ./vendor/yamadashy/phpstan-friendly-formatter/extension.neon
|
||||
# - phar://phpstan.phar/conf/bleedingEdge.neon
|
||||
parameters:
|
||||
tmpDir: %currentWorkingDirectory%/tmp/phpstan-corelibs
|
||||
#errorFormat: friendly
|
||||
#friendly:
|
||||
# lineBefore: 3
|
||||
# lineAfter: 3
|
||||
level: 8 # max is now 9
|
||||
# strictRules:
|
||||
# allRules: false
|
||||
checkMissingCallableSignature: true
|
||||
treatPhpDocTypesAsCertain: false
|
||||
paths:
|
||||
@@ -53,6 +61,6 @@ parameters:
|
||||
# paths:
|
||||
# - ...
|
||||
# - ...
|
||||
#-
|
||||
# message: "#^Call to deprecated method #"
|
||||
# path: www/admin/class_test*.php
|
||||
# -
|
||||
# message: "#^Call to deprecated method #"
|
||||
# path: www/admin/class_test*.php
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
/home/clemens/.phive/phars/phan-5.4.3.phar
|
||||
@@ -1 +0,0 @@
|
||||
/home/clemens/.phive/phars/php-cs-fixer-3.57.2.phar
|
||||
@@ -1 +0,0 @@
|
||||
/home/clemens/.phive/phars/phpdocumentor-3.4.3.phar
|
||||
@@ -1 +0,0 @@
|
||||
/home/clemens/.phive/phars/phpcbf-3.10.0.phar
|
||||
@@ -1 +0,0 @@
|
||||
/home/clemens/.phive/phars/phpcs-3.10.0.phar
|
||||
@@ -1 +0,0 @@
|
||||
/home/clemens/.phive/phars/phpdox-0.12.0.phar
|
||||
@@ -1 +0,0 @@
|
||||
/home/clemens/.phive/phars/phpstan-1.11.1.phar
|
||||
@@ -1 +0,0 @@
|
||||
/home/clemens/.phive/phars/phpunit-9.6.19.phar
|
||||
@@ -1 +0,0 @@
|
||||
/home/clemens/.phive/phars/psalm-5.24.0.phar
|
||||
25
vendor/autoload.php
vendored
25
vendor/autoload.php
vendored
@@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
// autoload.php @generated by Composer
|
||||
|
||||
if (PHP_VERSION_ID < 50600) {
|
||||
if (!headers_sent()) {
|
||||
header('HTTP/1.1 500 Internal Server Error');
|
||||
}
|
||||
$err = 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL;
|
||||
if (!ini_get('display_errors')) {
|
||||
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
|
||||
fwrite(STDERR, $err);
|
||||
} elseif (!headers_sent()) {
|
||||
echo $err;
|
||||
}
|
||||
}
|
||||
trigger_error(
|
||||
$err,
|
||||
E_USER_ERROR
|
||||
);
|
||||
}
|
||||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInitdd705c6e8ab22e0d642372dec7767718::getLoader();
|
||||
581
vendor/composer/ClassLoader.php
vendored
581
vendor/composer/ClassLoader.php
vendored
@@ -1,581 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Composer.
|
||||
*
|
||||
* (c) Nils Adermann <naderman@naderman.de>
|
||||
* Jordi Boggiano <j.boggiano@seld.be>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
/**
|
||||
* ClassLoader implements a PSR-0, PSR-4 and classmap class loader.
|
||||
*
|
||||
* $loader = new \Composer\Autoload\ClassLoader();
|
||||
*
|
||||
* // register classes with namespaces
|
||||
* $loader->add('Symfony\Component', __DIR__.'/component');
|
||||
* $loader->add('Symfony', __DIR__.'/framework');
|
||||
*
|
||||
* // activate the autoloader
|
||||
* $loader->register();
|
||||
*
|
||||
* // to enable searching the include path (eg. for PEAR packages)
|
||||
* $loader->setUseIncludePath(true);
|
||||
*
|
||||
* In this example, if you try to use a class in the Symfony\Component
|
||||
* namespace or one of its children (Symfony\Component\Console for instance),
|
||||
* the autoloader will first look for the class under the component/
|
||||
* directory, and it will then fallback to the framework/ directory if not
|
||||
* found before giving up.
|
||||
*
|
||||
* This class is loosely based on the Symfony UniversalClassLoader.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||
* @see https://www.php-fig.org/psr/psr-0/
|
||||
* @see https://www.php-fig.org/psr/psr-4/
|
||||
*/
|
||||
class ClassLoader
|
||||
{
|
||||
/** @var \Closure(string):void */
|
||||
private static $includeFile;
|
||||
|
||||
/** @var ?string */
|
||||
private $vendorDir;
|
||||
|
||||
// PSR-4
|
||||
/**
|
||||
* @var array[]
|
||||
* @psalm-var array<string, array<string, int>>
|
||||
*/
|
||||
private $prefixLengthsPsr4 = array();
|
||||
/**
|
||||
* @var array[]
|
||||
* @psalm-var array<string, array<int, string>>
|
||||
*/
|
||||
private $prefixDirsPsr4 = array();
|
||||
/**
|
||||
* @var array[]
|
||||
* @psalm-var array<string, string>
|
||||
*/
|
||||
private $fallbackDirsPsr4 = array();
|
||||
|
||||
// PSR-0
|
||||
/**
|
||||
* @var array[]
|
||||
* @psalm-var array<string, array<string, string[]>>
|
||||
*/
|
||||
private $prefixesPsr0 = array();
|
||||
/**
|
||||
* @var array[]
|
||||
* @psalm-var array<string, string>
|
||||
*/
|
||||
private $fallbackDirsPsr0 = array();
|
||||
|
||||
/** @var bool */
|
||||
private $useIncludePath = false;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
* @psalm-var array<string, string>
|
||||
*/
|
||||
private $classMap = array();
|
||||
|
||||
/** @var bool */
|
||||
private $classMapAuthoritative = false;
|
||||
|
||||
/**
|
||||
* @var bool[]
|
||||
* @psalm-var array<string, bool>
|
||||
*/
|
||||
private $missingClasses = array();
|
||||
|
||||
/** @var ?string */
|
||||
private $apcuPrefix;
|
||||
|
||||
/**
|
||||
* @var self[]
|
||||
*/
|
||||
private static $registeredLoaders = array();
|
||||
|
||||
/**
|
||||
* @param ?string $vendorDir
|
||||
*/
|
||||
public function __construct($vendorDir = null)
|
||||
{
|
||||
$this->vendorDir = $vendorDir;
|
||||
self::initializeIncludeClosure();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getPrefixes()
|
||||
{
|
||||
if (!empty($this->prefixesPsr0)) {
|
||||
return call_user_func_array('array_merge', array_values($this->prefixesPsr0));
|
||||
}
|
||||
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array[]
|
||||
* @psalm-return array<string, array<int, string>>
|
||||
*/
|
||||
public function getPrefixesPsr4()
|
||||
{
|
||||
return $this->prefixDirsPsr4;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array[]
|
||||
* @psalm-return array<string, string>
|
||||
*/
|
||||
public function getFallbackDirs()
|
||||
{
|
||||
return $this->fallbackDirsPsr0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array[]
|
||||
* @psalm-return array<string, string>
|
||||
*/
|
||||
public function getFallbackDirsPsr4()
|
||||
{
|
||||
return $this->fallbackDirsPsr4;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[] Array of classname => path
|
||||
* @psalm-return array<string, string>
|
||||
*/
|
||||
public function getClassMap()
|
||||
{
|
||||
return $this->classMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $classMap Class to filename map
|
||||
* @psalm-param array<string, string> $classMap
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function addClassMap(array $classMap)
|
||||
{
|
||||
if ($this->classMap) {
|
||||
$this->classMap = array_merge($this->classMap, $classMap);
|
||||
} else {
|
||||
$this->classMap = $classMap;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a set of PSR-0 directories for a given prefix, either
|
||||
* appending or prepending to the ones previously set for this prefix.
|
||||
*
|
||||
* @param string $prefix The prefix
|
||||
* @param string[]|string $paths The PSR-0 root directories
|
||||
* @param bool $prepend Whether to prepend the directories
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function add($prefix, $paths, $prepend = false)
|
||||
{
|
||||
if (!$prefix) {
|
||||
if ($prepend) {
|
||||
$this->fallbackDirsPsr0 = array_merge(
|
||||
(array) $paths,
|
||||
$this->fallbackDirsPsr0
|
||||
);
|
||||
} else {
|
||||
$this->fallbackDirsPsr0 = array_merge(
|
||||
$this->fallbackDirsPsr0,
|
||||
(array) $paths
|
||||
);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$first = $prefix[0];
|
||||
if (!isset($this->prefixesPsr0[$first][$prefix])) {
|
||||
$this->prefixesPsr0[$first][$prefix] = (array) $paths;
|
||||
|
||||
return;
|
||||
}
|
||||
if ($prepend) {
|
||||
$this->prefixesPsr0[$first][$prefix] = array_merge(
|
||||
(array) $paths,
|
||||
$this->prefixesPsr0[$first][$prefix]
|
||||
);
|
||||
} else {
|
||||
$this->prefixesPsr0[$first][$prefix] = array_merge(
|
||||
$this->prefixesPsr0[$first][$prefix],
|
||||
(array) $paths
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a set of PSR-4 directories for a given namespace, either
|
||||
* appending or prepending to the ones previously set for this namespace.
|
||||
*
|
||||
* @param string $prefix The prefix/namespace, with trailing '\\'
|
||||
* @param string[]|string $paths The PSR-4 base directories
|
||||
* @param bool $prepend Whether to prepend the directories
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function addPsr4($prefix, $paths, $prepend = false)
|
||||
{
|
||||
if (!$prefix) {
|
||||
// Register directories for the root namespace.
|
||||
if ($prepend) {
|
||||
$this->fallbackDirsPsr4 = array_merge(
|
||||
(array) $paths,
|
||||
$this->fallbackDirsPsr4
|
||||
);
|
||||
} else {
|
||||
$this->fallbackDirsPsr4 = array_merge(
|
||||
$this->fallbackDirsPsr4,
|
||||
(array) $paths
|
||||
);
|
||||
}
|
||||
} elseif (!isset($this->prefixDirsPsr4[$prefix])) {
|
||||
// Register directories for a new namespace.
|
||||
$length = strlen($prefix);
|
||||
if ('\\' !== $prefix[$length - 1]) {
|
||||
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
|
||||
}
|
||||
$this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
|
||||
$this->prefixDirsPsr4[$prefix] = (array) $paths;
|
||||
} elseif ($prepend) {
|
||||
// Prepend directories for an already registered namespace.
|
||||
$this->prefixDirsPsr4[$prefix] = array_merge(
|
||||
(array) $paths,
|
||||
$this->prefixDirsPsr4[$prefix]
|
||||
);
|
||||
} else {
|
||||
// Append directories for an already registered namespace.
|
||||
$this->prefixDirsPsr4[$prefix] = array_merge(
|
||||
$this->prefixDirsPsr4[$prefix],
|
||||
(array) $paths
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a set of PSR-0 directories for a given prefix,
|
||||
* replacing any others previously set for this prefix.
|
||||
*
|
||||
* @param string $prefix The prefix
|
||||
* @param string[]|string $paths The PSR-0 base directories
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function set($prefix, $paths)
|
||||
{
|
||||
if (!$prefix) {
|
||||
$this->fallbackDirsPsr0 = (array) $paths;
|
||||
} else {
|
||||
$this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a set of PSR-4 directories for a given namespace,
|
||||
* replacing any others previously set for this namespace.
|
||||
*
|
||||
* @param string $prefix The prefix/namespace, with trailing '\\'
|
||||
* @param string[]|string $paths The PSR-4 base directories
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setPsr4($prefix, $paths)
|
||||
{
|
||||
if (!$prefix) {
|
||||
$this->fallbackDirsPsr4 = (array) $paths;
|
||||
} else {
|
||||
$length = strlen($prefix);
|
||||
if ('\\' !== $prefix[$length - 1]) {
|
||||
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
|
||||
}
|
||||
$this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
|
||||
$this->prefixDirsPsr4[$prefix] = (array) $paths;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns on searching the include path for class files.
|
||||
*
|
||||
* @param bool $useIncludePath
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setUseIncludePath($useIncludePath)
|
||||
{
|
||||
$this->useIncludePath = $useIncludePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Can be used to check if the autoloader uses the include path to check
|
||||
* for classes.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getUseIncludePath()
|
||||
{
|
||||
return $this->useIncludePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns off searching the prefix and fallback directories for classes
|
||||
* that have not been registered with the class map.
|
||||
*
|
||||
* @param bool $classMapAuthoritative
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setClassMapAuthoritative($classMapAuthoritative)
|
||||
{
|
||||
$this->classMapAuthoritative = $classMapAuthoritative;
|
||||
}
|
||||
|
||||
/**
|
||||
* Should class lookup fail if not found in the current class map?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isClassMapAuthoritative()
|
||||
{
|
||||
return $this->classMapAuthoritative;
|
||||
}
|
||||
|
||||
/**
|
||||
* APCu prefix to use to cache found/not-found classes, if the extension is enabled.
|
||||
*
|
||||
* @param string|null $apcuPrefix
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setApcuPrefix($apcuPrefix)
|
||||
{
|
||||
$this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The APCu prefix in use, or null if APCu caching is not enabled.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getApcuPrefix()
|
||||
{
|
||||
return $this->apcuPrefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers this instance as an autoloader.
|
||||
*
|
||||
* @param bool $prepend Whether to prepend the autoloader or not
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register($prepend = false)
|
||||
{
|
||||
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
|
||||
|
||||
if (null === $this->vendorDir) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($prepend) {
|
||||
self::$registeredLoaders = array($this->vendorDir => $this) + self::$registeredLoaders;
|
||||
} else {
|
||||
unset(self::$registeredLoaders[$this->vendorDir]);
|
||||
self::$registeredLoaders[$this->vendorDir] = $this;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregisters this instance as an autoloader.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function unregister()
|
||||
{
|
||||
spl_autoload_unregister(array($this, 'loadClass'));
|
||||
|
||||
if (null !== $this->vendorDir) {
|
||||
unset(self::$registeredLoaders[$this->vendorDir]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the given class or interface.
|
||||
*
|
||||
* @param string $class The name of the class
|
||||
* @return true|null True if loaded, null otherwise
|
||||
*/
|
||||
public function loadClass($class)
|
||||
{
|
||||
if ($file = $this->findFile($class)) {
|
||||
(self::$includeFile)($file);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the path to the file where the class is defined.
|
||||
*
|
||||
* @param string $class The name of the class
|
||||
*
|
||||
* @return string|false The path if found, false otherwise
|
||||
*/
|
||||
public function findFile($class)
|
||||
{
|
||||
// class map lookup
|
||||
if (isset($this->classMap[$class])) {
|
||||
return $this->classMap[$class];
|
||||
}
|
||||
if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) {
|
||||
return false;
|
||||
}
|
||||
if (null !== $this->apcuPrefix) {
|
||||
$file = apcu_fetch($this->apcuPrefix.$class, $hit);
|
||||
if ($hit) {
|
||||
return $file;
|
||||
}
|
||||
}
|
||||
|
||||
$file = $this->findFileWithExtension($class, '.php');
|
||||
|
||||
// Search for Hack files if we are running on HHVM
|
||||
if (false === $file && defined('HHVM_VERSION')) {
|
||||
$file = $this->findFileWithExtension($class, '.hh');
|
||||
}
|
||||
|
||||
if (null !== $this->apcuPrefix) {
|
||||
apcu_add($this->apcuPrefix.$class, $file);
|
||||
}
|
||||
|
||||
if (false === $file) {
|
||||
// Remember that this class does not exist.
|
||||
$this->missingClasses[$class] = true;
|
||||
}
|
||||
|
||||
return $file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the currently registered loaders indexed by their corresponding vendor directories.
|
||||
*
|
||||
* @return self[]
|
||||
*/
|
||||
public static function getRegisteredLoaders()
|
||||
{
|
||||
return self::$registeredLoaders;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @param string $ext
|
||||
* @return string|false
|
||||
*/
|
||||
private function findFileWithExtension($class, $ext)
|
||||
{
|
||||
// PSR-4 lookup
|
||||
$logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;
|
||||
|
||||
$first = $class[0];
|
||||
if (isset($this->prefixLengthsPsr4[$first])) {
|
||||
$subPath = $class;
|
||||
while (false !== $lastPos = strrpos($subPath, '\\')) {
|
||||
$subPath = substr($subPath, 0, $lastPos);
|
||||
$search = $subPath . '\\';
|
||||
if (isset($this->prefixDirsPsr4[$search])) {
|
||||
$pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1);
|
||||
foreach ($this->prefixDirsPsr4[$search] as $dir) {
|
||||
if (file_exists($file = $dir . $pathEnd)) {
|
||||
return $file;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// PSR-4 fallback dirs
|
||||
foreach ($this->fallbackDirsPsr4 as $dir) {
|
||||
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
|
||||
return $file;
|
||||
}
|
||||
}
|
||||
|
||||
// PSR-0 lookup
|
||||
if (false !== $pos = strrpos($class, '\\')) {
|
||||
// namespaced class name
|
||||
$logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
|
||||
. strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
|
||||
} else {
|
||||
// PEAR-like class name
|
||||
$logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext;
|
||||
}
|
||||
|
||||
if (isset($this->prefixesPsr0[$first])) {
|
||||
foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
|
||||
if (0 === strpos($class, $prefix)) {
|
||||
foreach ($dirs as $dir) {
|
||||
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
|
||||
return $file;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// PSR-0 fallback dirs
|
||||
foreach ($this->fallbackDirsPsr0 as $dir) {
|
||||
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
|
||||
return $file;
|
||||
}
|
||||
}
|
||||
|
||||
// PSR-0 include paths.
|
||||
if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) {
|
||||
return $file;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static function initializeIncludeClosure(): void
|
||||
{
|
||||
if (self::$includeFile !== null) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope isolated include.
|
||||
*
|
||||
* Prevents access to $this/self from included files.
|
||||
*
|
||||
* @param string $file
|
||||
* @return void
|
||||
*/
|
||||
self::$includeFile = static function($file) {
|
||||
include $file;
|
||||
};
|
||||
}
|
||||
}
|
||||
352
vendor/composer/InstalledVersions.php
vendored
352
vendor/composer/InstalledVersions.php
vendored
@@ -1,352 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Composer.
|
||||
*
|
||||
* (c) Nils Adermann <naderman@naderman.de>
|
||||
* Jordi Boggiano <j.boggiano@seld.be>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Composer;
|
||||
|
||||
use Composer\Autoload\ClassLoader;
|
||||
use Composer\Semver\VersionParser;
|
||||
|
||||
/**
|
||||
* This class is copied in every Composer installed project and available to all
|
||||
*
|
||||
* See also https://getcomposer.org/doc/07-runtime.md#installed-versions
|
||||
*
|
||||
* To require its presence, you can require `composer-runtime-api ^2.0`
|
||||
*
|
||||
* @final
|
||||
*/
|
||||
class InstalledVersions
|
||||
{
|
||||
/**
|
||||
* @var mixed[]|null
|
||||
* @psalm-var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}|array{}|null
|
||||
*/
|
||||
private static $installed;
|
||||
|
||||
/**
|
||||
* @var bool|null
|
||||
*/
|
||||
private static $canGetVendors;
|
||||
|
||||
/**
|
||||
* @var array[]
|
||||
* @psalm-var array<string, array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}>
|
||||
*/
|
||||
private static $installedByVendor = array();
|
||||
|
||||
/**
|
||||
* Returns a list of all package names which are present, either by being installed, replaced or provided
|
||||
*
|
||||
* @return string[]
|
||||
* @psalm-return list<string>
|
||||
*/
|
||||
public static function getInstalledPackages()
|
||||
{
|
||||
$packages = array();
|
||||
foreach (self::getInstalled() as $installed) {
|
||||
$packages[] = array_keys($installed['versions']);
|
||||
}
|
||||
|
||||
if (1 === \count($packages)) {
|
||||
return $packages[0];
|
||||
}
|
||||
|
||||
return array_keys(array_flip(\call_user_func_array('array_merge', $packages)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all package names with a specific type e.g. 'library'
|
||||
*
|
||||
* @param string $type
|
||||
* @return string[]
|
||||
* @psalm-return list<string>
|
||||
*/
|
||||
public static function getInstalledPackagesByType($type)
|
||||
{
|
||||
$packagesByType = array();
|
||||
|
||||
foreach (self::getInstalled() as $installed) {
|
||||
foreach ($installed['versions'] as $name => $package) {
|
||||
if (isset($package['type']) && $package['type'] === $type) {
|
||||
$packagesByType[] = $name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $packagesByType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the given package is installed
|
||||
*
|
||||
* This also returns true if the package name is provided or replaced by another package
|
||||
*
|
||||
* @param string $packageName
|
||||
* @param bool $includeDevRequirements
|
||||
* @return bool
|
||||
*/
|
||||
public static function isInstalled($packageName, $includeDevRequirements = true)
|
||||
{
|
||||
foreach (self::getInstalled() as $installed) {
|
||||
if (isset($installed['versions'][$packageName])) {
|
||||
return $includeDevRequirements || empty($installed['versions'][$packageName]['dev_requirement']);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the given package satisfies a version constraint
|
||||
*
|
||||
* e.g. If you want to know whether version 2.3+ of package foo/bar is installed, you would call:
|
||||
*
|
||||
* Composer\InstalledVersions::satisfies(new VersionParser, 'foo/bar', '^2.3')
|
||||
*
|
||||
* @param VersionParser $parser Install composer/semver to have access to this class and functionality
|
||||
* @param string $packageName
|
||||
* @param string|null $constraint A version constraint to check for, if you pass one you have to make sure composer/semver is required by your package
|
||||
* @return bool
|
||||
*/
|
||||
public static function satisfies(VersionParser $parser, $packageName, $constraint)
|
||||
{
|
||||
$constraint = $parser->parseConstraints($constraint);
|
||||
$provided = $parser->parseConstraints(self::getVersionRanges($packageName));
|
||||
|
||||
return $provided->matches($constraint);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a version constraint representing all the range(s) which are installed for a given package
|
||||
*
|
||||
* It is easier to use this via isInstalled() with the $constraint argument if you need to check
|
||||
* whether a given version of a package is installed, and not just whether it exists
|
||||
*
|
||||
* @param string $packageName
|
||||
* @return string Version constraint usable with composer/semver
|
||||
*/
|
||||
public static function getVersionRanges($packageName)
|
||||
{
|
||||
foreach (self::getInstalled() as $installed) {
|
||||
if (!isset($installed['versions'][$packageName])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$ranges = array();
|
||||
if (isset($installed['versions'][$packageName]['pretty_version'])) {
|
||||
$ranges[] = $installed['versions'][$packageName]['pretty_version'];
|
||||
}
|
||||
if (array_key_exists('aliases', $installed['versions'][$packageName])) {
|
||||
$ranges = array_merge($ranges, $installed['versions'][$packageName]['aliases']);
|
||||
}
|
||||
if (array_key_exists('replaced', $installed['versions'][$packageName])) {
|
||||
$ranges = array_merge($ranges, $installed['versions'][$packageName]['replaced']);
|
||||
}
|
||||
if (array_key_exists('provided', $installed['versions'][$packageName])) {
|
||||
$ranges = array_merge($ranges, $installed['versions'][$packageName]['provided']);
|
||||
}
|
||||
|
||||
return implode(' || ', $ranges);
|
||||
}
|
||||
|
||||
throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $packageName
|
||||
* @return string|null If the package is being replaced or provided but is not really installed, null will be returned as version, use satisfies or getVersionRanges if you need to know if a given version is present
|
||||
*/
|
||||
public static function getVersion($packageName)
|
||||
{
|
||||
foreach (self::getInstalled() as $installed) {
|
||||
if (!isset($installed['versions'][$packageName])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!isset($installed['versions'][$packageName]['version'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $installed['versions'][$packageName]['version'];
|
||||
}
|
||||
|
||||
throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $packageName
|
||||
* @return string|null If the package is being replaced or provided but is not really installed, null will be returned as version, use satisfies or getVersionRanges if you need to know if a given version is present
|
||||
*/
|
||||
public static function getPrettyVersion($packageName)
|
||||
{
|
||||
foreach (self::getInstalled() as $installed) {
|
||||
if (!isset($installed['versions'][$packageName])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!isset($installed['versions'][$packageName]['pretty_version'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $installed['versions'][$packageName]['pretty_version'];
|
||||
}
|
||||
|
||||
throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $packageName
|
||||
* @return string|null If the package is being replaced or provided but is not really installed, null will be returned as reference
|
||||
*/
|
||||
public static function getReference($packageName)
|
||||
{
|
||||
foreach (self::getInstalled() as $installed) {
|
||||
if (!isset($installed['versions'][$packageName])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!isset($installed['versions'][$packageName]['reference'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $installed['versions'][$packageName]['reference'];
|
||||
}
|
||||
|
||||
throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $packageName
|
||||
* @return string|null If the package is being replaced or provided but is not really installed, null will be returned as install path. Packages of type metapackages also have a null install path.
|
||||
*/
|
||||
public static function getInstallPath($packageName)
|
||||
{
|
||||
foreach (self::getInstalled() as $installed) {
|
||||
if (!isset($installed['versions'][$packageName])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return isset($installed['versions'][$packageName]['install_path']) ? $installed['versions'][$packageName]['install_path'] : null;
|
||||
}
|
||||
|
||||
throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @psalm-return array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}
|
||||
*/
|
||||
public static function getRootPackage()
|
||||
{
|
||||
$installed = self::getInstalled();
|
||||
|
||||
return $installed[0]['root'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the raw installed.php data for custom implementations
|
||||
*
|
||||
* @deprecated Use getAllRawData() instead which returns all datasets for all autoloaders present in the process. getRawData only returns the first dataset loaded, which may not be what you expect.
|
||||
* @return array[]
|
||||
* @psalm-return array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}
|
||||
*/
|
||||
public static function getRawData()
|
||||
{
|
||||
@trigger_error('getRawData only returns the first dataset loaded, which may not be what you expect. Use getAllRawData() instead which returns all datasets for all autoloaders present in the process.', E_USER_DEPRECATED);
|
||||
|
||||
if (null === self::$installed) {
|
||||
// only require the installed.php file if this file is loaded from its dumped location,
|
||||
// and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937
|
||||
if (substr(__DIR__, -8, 1) !== 'C') {
|
||||
self::$installed = include __DIR__ . '/installed.php';
|
||||
} else {
|
||||
self::$installed = array();
|
||||
}
|
||||
}
|
||||
|
||||
return self::$installed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the raw data of all installed.php which are currently loaded for custom implementations
|
||||
*
|
||||
* @return array[]
|
||||
* @psalm-return list<array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}>
|
||||
*/
|
||||
public static function getAllRawData()
|
||||
{
|
||||
return self::getInstalled();
|
||||
}
|
||||
|
||||
/**
|
||||
* Lets you reload the static array from another file
|
||||
*
|
||||
* This is only useful for complex integrations in which a project needs to use
|
||||
* this class but then also needs to execute another project's autoloader in process,
|
||||
* and wants to ensure both projects have access to their version of installed.php.
|
||||
*
|
||||
* A typical case would be PHPUnit, where it would need to make sure it reads all
|
||||
* the data it needs from this class, then call reload() with
|
||||
* `require $CWD/vendor/composer/installed.php` (or similar) as input to make sure
|
||||
* the project in which it runs can then also use this class safely, without
|
||||
* interference between PHPUnit's dependencies and the project's dependencies.
|
||||
*
|
||||
* @param array[] $data A vendor/composer/installed.php data set
|
||||
* @return void
|
||||
*
|
||||
* @psalm-param array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>} $data
|
||||
*/
|
||||
public static function reload($data)
|
||||
{
|
||||
self::$installed = $data;
|
||||
self::$installedByVendor = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array[]
|
||||
* @psalm-return list<array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}>
|
||||
*/
|
||||
private static function getInstalled()
|
||||
{
|
||||
if (null === self::$canGetVendors) {
|
||||
self::$canGetVendors = method_exists('Composer\Autoload\ClassLoader', 'getRegisteredLoaders');
|
||||
}
|
||||
|
||||
$installed = array();
|
||||
|
||||
if (self::$canGetVendors) {
|
||||
foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) {
|
||||
if (isset(self::$installedByVendor[$vendorDir])) {
|
||||
$installed[] = self::$installedByVendor[$vendorDir];
|
||||
} elseif (is_file($vendorDir.'/composer/installed.php')) {
|
||||
$installed[] = self::$installedByVendor[$vendorDir] = require $vendorDir.'/composer/installed.php';
|
||||
if (null === self::$installed && strtr($vendorDir.'/composer', '\\', '/') === strtr(__DIR__, '\\', '/')) {
|
||||
self::$installed = $installed[count($installed) - 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (null === self::$installed) {
|
||||
// only require the installed.php file if this file is loaded from its dumped location,
|
||||
// and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937
|
||||
if (substr(__DIR__, -8, 1) !== 'C') {
|
||||
self::$installed = require __DIR__ . '/installed.php';
|
||||
} else {
|
||||
self::$installed = array();
|
||||
}
|
||||
}
|
||||
$installed[] = self::$installed;
|
||||
|
||||
return $installed;
|
||||
}
|
||||
}
|
||||
21
vendor/composer/LICENSE
vendored
21
vendor/composer/LICENSE
vendored
@@ -1,21 +0,0 @@
|
||||
|
||||
Copyright (c) Nils Adermann, Jordi Boggiano
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is furnished
|
||||
to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
10
vendor/composer/autoload_classmap.php
vendored
10
vendor/composer/autoload_classmap.php
vendored
@@ -1,10 +0,0 @@
|
||||
<?php
|
||||
|
||||
// autoload_classmap.php @generated by Composer
|
||||
|
||||
$vendorDir = dirname(__DIR__);
|
||||
$baseDir = dirname($vendorDir);
|
||||
|
||||
return array(
|
||||
'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php',
|
||||
);
|
||||
9
vendor/composer/autoload_namespaces.php
vendored
9
vendor/composer/autoload_namespaces.php
vendored
@@ -1,9 +0,0 @@
|
||||
<?php
|
||||
|
||||
// autoload_namespaces.php @generated by Composer
|
||||
|
||||
$vendorDir = dirname(__DIR__);
|
||||
$baseDir = dirname($vendorDir);
|
||||
|
||||
return array(
|
||||
);
|
||||
9
vendor/composer/autoload_psr4.php
vendored
9
vendor/composer/autoload_psr4.php
vendored
@@ -1,9 +0,0 @@
|
||||
<?php
|
||||
|
||||
// autoload_psr4.php @generated by Composer
|
||||
|
||||
$vendorDir = dirname(__DIR__);
|
||||
$baseDir = dirname($vendorDir);
|
||||
|
||||
return array(
|
||||
);
|
||||
38
vendor/composer/autoload_real.php
vendored
38
vendor/composer/autoload_real.php
vendored
@@ -1,38 +0,0 @@
|
||||
<?php
|
||||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInitdd705c6e8ab22e0d642372dec7767718
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
public static function loadClassLoader($class)
|
||||
{
|
||||
if ('Composer\Autoload\ClassLoader' === $class) {
|
||||
require __DIR__ . '/ClassLoader.php';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Composer\Autoload\ClassLoader
|
||||
*/
|
||||
public static function getLoader()
|
||||
{
|
||||
if (null !== self::$loader) {
|
||||
return self::$loader;
|
||||
}
|
||||
|
||||
require __DIR__ . '/platform_check.php';
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInitdd705c6e8ab22e0d642372dec7767718', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitdd705c6e8ab22e0d642372dec7767718', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitdd705c6e8ab22e0d642372dec7767718::getInitializer($loader));
|
||||
|
||||
$loader->register(true);
|
||||
|
||||
return $loader;
|
||||
}
|
||||
}
|
||||
20
vendor/composer/autoload_static.php
vendored
20
vendor/composer/autoload_static.php
vendored
@@ -1,20 +0,0 @@
|
||||
<?php
|
||||
|
||||
// autoload_static.php @generated by Composer
|
||||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInitdd705c6e8ab22e0d642372dec7767718
|
||||
{
|
||||
public static $classMap = array (
|
||||
'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php',
|
||||
);
|
||||
|
||||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->classMap = ComposerStaticInitdd705c6e8ab22e0d642372dec7767718::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
}
|
||||
5
vendor/composer/installed.json
vendored
5
vendor/composer/installed.json
vendored
@@ -1,5 +0,0 @@
|
||||
{
|
||||
"packages": [],
|
||||
"dev": true,
|
||||
"dev-package-names": []
|
||||
}
|
||||
23
vendor/composer/installed.php
vendored
23
vendor/composer/installed.php
vendored
@@ -1,23 +0,0 @@
|
||||
<?php return array(
|
||||
'root' => array(
|
||||
'name' => 'egrajp/development-corelibs-dev',
|
||||
'pretty_version' => 'dev-master',
|
||||
'version' => 'dev-master',
|
||||
'reference' => NULL,
|
||||
'type' => 'library',
|
||||
'install_path' => __DIR__ . '/../../',
|
||||
'aliases' => array(),
|
||||
'dev' => true,
|
||||
),
|
||||
'versions' => array(
|
||||
'egrajp/development-corelibs-dev' => array(
|
||||
'pretty_version' => 'dev-master',
|
||||
'version' => 'dev-master',
|
||||
'reference' => NULL,
|
||||
'type' => 'library',
|
||||
'install_path' => __DIR__ . '/../../',
|
||||
'aliases' => array(),
|
||||
'dev_requirement' => false,
|
||||
),
|
||||
),
|
||||
);
|
||||
26
vendor/composer/platform_check.php
vendored
26
vendor/composer/platform_check.php
vendored
@@ -1,26 +0,0 @@
|
||||
<?php
|
||||
|
||||
// platform_check.php @generated by Composer
|
||||
|
||||
$issues = array();
|
||||
|
||||
if (!(PHP_VERSION_ID >= 80100)) {
|
||||
$issues[] = 'Your Composer dependencies require a PHP version ">= 8.1.0". You are running ' . PHP_VERSION . '.';
|
||||
}
|
||||
|
||||
if ($issues) {
|
||||
if (!headers_sent()) {
|
||||
header('HTTP/1.1 500 Internal Server Error');
|
||||
}
|
||||
if (!ini_get('display_errors')) {
|
||||
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
|
||||
fwrite(STDERR, 'Composer detected issues in your platform:' . PHP_EOL.PHP_EOL . implode(PHP_EOL, $issues) . PHP_EOL.PHP_EOL);
|
||||
} elseif (!headers_sent()) {
|
||||
echo 'Composer detected issues in your platform:' . PHP_EOL.PHP_EOL . str_replace('You are running '.PHP_VERSION.'.', '', implode(PHP_EOL, $issues)) . PHP_EOL.PHP_EOL;
|
||||
}
|
||||
}
|
||||
trigger_error(
|
||||
'Composer detected issues in your platform: ' . implode(' ', $issues),
|
||||
E_USER_ERROR
|
||||
);
|
||||
}
|
||||
79
www/admin/UrlRequests.target.php
Normal file
79
www/admin/UrlRequests.target.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php // phpcs:ignore PSR1.Files.SideEffects
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
// url requests target test
|
||||
require 'config.php';
|
||||
use CoreLibs\Convert\Json;
|
||||
$LOG_FILE_ID = 'classTest-urlrequests-target';
|
||||
$log = new CoreLibs\Logging\Logging([
|
||||
'log_folder' => BASE . LOG,
|
||||
'log_file_id' => $LOG_FILE_ID,
|
||||
'log_per_date' => true,
|
||||
]);
|
||||
|
||||
/**
|
||||
* build return json
|
||||
*
|
||||
* @param array<string,mixed> $http_headers
|
||||
* @param ?string $body
|
||||
* @return string
|
||||
*/
|
||||
function buildContent(array $http_headers, ?string $body): string
|
||||
{
|
||||
if (is_string($body) && !empty($body)) {
|
||||
$_body = Json::jsonConvertToArray($body);
|
||||
if (Json::jsonGetLastError()) {
|
||||
$body = [$body];
|
||||
} else {
|
||||
$body = $_body;
|
||||
}
|
||||
} elseif (is_string($body)) {
|
||||
$body = [];
|
||||
}
|
||||
return Json::jsonConvertArrayTo([
|
||||
'HEADERS' => $http_headers,
|
||||
"REQUEST_TYPE" => $_SERVER['REQUEST_METHOD'],
|
||||
"PARAMS" => $_GET,
|
||||
"BODY" => $body,
|
||||
// "STRING_BODY" => $body,
|
||||
]);
|
||||
}
|
||||
|
||||
$http_headers = array_filter($_SERVER, function ($value, $key) {
|
||||
if (str_starts_with($key, 'HTTP_')) {
|
||||
return true;
|
||||
}
|
||||
}, ARRAY_FILTER_USE_BOTH);
|
||||
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
|
||||
// if the header has Authorization and RunAuthTest then exit with 401
|
||||
if (!empty($http_headers['HTTP_AUTHORIZATION']) && !empty($http_headers['HTTP_RUNAUTHTEST'])) {
|
||||
header("HTTP/1.1 401 Unauthorized");
|
||||
print buildContent($http_headers, '{"code": 401, "content": {"Error": "Not Authorized"}}');
|
||||
exit;
|
||||
}
|
||||
|
||||
// if server request type is get set file_get to null -> no body
|
||||
if ($_SERVER['REQUEST_METHOD'] == "GET") {
|
||||
$file_get = null;
|
||||
} elseif (($file_get = file_get_contents('php://input')) === false) {
|
||||
header("HTTP/1.1 404 Not Found");
|
||||
print buildContent($http_headers, '{"code": 404, "content": {"Error": "file_get_contents failed"}}');
|
||||
exit;
|
||||
}
|
||||
// str_replace('\"', '"', trim($file_get, '"'));
|
||||
|
||||
$log->debug('SERVER', $log->prAr($_SERVER));
|
||||
$log->debug('HEADERS', $log->prAr($http_headers));
|
||||
$log->debug('REQUEST TYPE', $_SERVER['REQUEST_METHOD']);
|
||||
$log->debug('GET', $log->prAr($_GET));
|
||||
$log->debug('POST', $log->prAr($_POST));
|
||||
$log->debug('PHP-INPUT', $log->prAr($file_get));
|
||||
|
||||
print buildContent($http_headers, $file_get);
|
||||
|
||||
$log->debug('[END]', '=========================================>');
|
||||
|
||||
// __END__
|
||||
@@ -55,7 +55,25 @@ print '<div><h1>' . $PAGE_NAME . '</h1></div>';
|
||||
print "SETACL[]: <br>";
|
||||
$backend->setACL(['EMPTY' => 'EMPTY']);
|
||||
print "ADBEDITLOG: <br>";
|
||||
$backend->adbEditLog('CLASSTEST-ADMIN', 'Some info string');
|
||||
$backend->adbEditLog('CLASSTEST-ADMIN-BINARY', 'Some info string', 'BINARY');
|
||||
$backend->adbEditLog('CLASSTEST-ADMIN-ZLIB', 'Some info string', 'ZLIB');
|
||||
$backend->adbEditLog('CLASSTEST-ADMIN-SERIAL', 'Some info string', 'SERIAL');
|
||||
$backend->adbEditLog('CLASSTEST-ADMIN-INVALID', 'Some info string', 'INVALID');
|
||||
// test with various
|
||||
$backend->action = 'TEST ACTION';
|
||||
$backend->action_id = 'TEST ACTION ID';
|
||||
$backend->action_yes = 'TEST ACTION YES';
|
||||
$backend->action_flag = 'TEST ACTION FLAG';
|
||||
$backend->action_menu = 'TEST ACTION MENU';
|
||||
$backend->action_loaded = 'TEST ACTION LOADED';
|
||||
$backend->action_value = 'TEST ACTION VALUE';
|
||||
$backend->action_type = 'TEST ACTION TYPE';
|
||||
$backend->action_error = 'TEST ACTION ERROR';
|
||||
$backend->adbEditLog('CLASSTEST-ADMIN-JSON', [
|
||||
"_GET" => $_GET,
|
||||
"_POST" => $_POST,
|
||||
], 'JSON');
|
||||
|
||||
print "ADBTOPMENU(0): " . Support::printAr($backend->adbTopMenu(CONTENT_PATH)) . "<br>";
|
||||
print "ADBMSG: <br>";
|
||||
$backend->adbMsg('info', 'Message: %1$d', [1]);
|
||||
|
||||
@@ -115,9 +115,6 @@ print "ARRAYFLATFORKEY: " . DgS::printAr(ArrayHandler::arrayFlatForKey($test_arr
|
||||
*/
|
||||
function rec(string $pre, string $cur, array $node = [])
|
||||
{
|
||||
if (!is_array($node)) {
|
||||
$node = [];
|
||||
}
|
||||
print "<div style='color: green;'>#### PRE: " . $pre . ", CUR: " . $cur . ", N-c: "
|
||||
. count($node) . " [" . join('|', array_keys($node)) . "]</div>";
|
||||
if (!$pre) {
|
||||
|
||||
@@ -19,6 +19,8 @@ $LOG_FILE_ID = 'classTest-convert-colors';
|
||||
ob_end_flush();
|
||||
|
||||
use CoreLibs\Convert\Colors;
|
||||
use CoreLibs\Convert\Color\Color;
|
||||
use CoreLibs\Convert\Color\Coordinates;
|
||||
use CoreLibs\Debug\Support as DgS;
|
||||
use CoreLibs\Convert\SetVarType;
|
||||
|
||||
@@ -29,6 +31,36 @@ $log = new CoreLibs\Logging\Logging([
|
||||
]);
|
||||
$color_class = 'CoreLibs\Convert\Colors';
|
||||
|
||||
/**
|
||||
* print out a color block with info
|
||||
*
|
||||
* @param string $color
|
||||
* @param string $text
|
||||
* @param string $text_add
|
||||
* @return string
|
||||
*/
|
||||
function display(string $color, string $text, string $text_add): string
|
||||
{
|
||||
$css = 'margin:5px;padding:50px;'
|
||||
. 'width:10%;'
|
||||
. 'text-align:center;'
|
||||
. 'color:white;text-shadow: 0 0 5px black;font-weight:bold;';
|
||||
$template = <<<HTML
|
||||
<div style="background-color:{COLOR};{CSS}">
|
||||
{TEXT}
|
||||
</div>
|
||||
HTML;
|
||||
return str_replace(
|
||||
["{COLOR}", "{TEXT}", "{CSS}"],
|
||||
[
|
||||
$color,
|
||||
$text . (!empty($text_add) ? '<br>' . $text_add : ''),
|
||||
$css
|
||||
],
|
||||
$template
|
||||
);
|
||||
}
|
||||
|
||||
$PAGE_NAME = 'TEST CLASS: CONVERT COLORS';
|
||||
print "<!DOCTYPE html>";
|
||||
print "<html><head><title>" . $PAGE_NAME . "</title></head>";
|
||||
@@ -36,32 +68,82 @@ print "<body>";
|
||||
print '<div><a href="class_test.php">Class Test Master</a></div>';
|
||||
print '<div><h1>' . $PAGE_NAME . '</h1></div>';
|
||||
|
||||
// out of bounds test
|
||||
|
||||
// define a list of from to color sets for conversion test
|
||||
|
||||
$hwb = Color::hsbToHwb(new Coordinates\HSB([
|
||||
160,
|
||||
0,
|
||||
50,
|
||||
]));
|
||||
print "HWB: " . DgS::printAr($hwb) . "<br>";
|
||||
$hsb = Color::hwbToHsb($hwb);
|
||||
print "HSB: " . DgS::printAr($hsb) . "<br>";
|
||||
|
||||
$oklch = Color::rgbToOkLch(Coordinates\RGB::create([
|
||||
250,
|
||||
0,
|
||||
0
|
||||
]));
|
||||
print "OkLch: " . DgS::printAr($oklch) . "<br>";
|
||||
$rgb = Color::okLchToRgb($oklch);
|
||||
print "OkLch -> RGB: " . DgS::printAr($rgb) . "<br>";
|
||||
|
||||
$oklab = Color::rgbToOkLab(Coordinates\RGB::create([
|
||||
250,
|
||||
0,
|
||||
0
|
||||
]));
|
||||
print "OkLab: " . DgS::printAr($oklab) . "<br>";
|
||||
print display($oklab->toCssString(), $oklab->toCssString(), 'Oklab');
|
||||
$rgb = Color::okLabToRgb($oklab);
|
||||
print "OkLab -> RGB: " . DgS::printAr($rgb) . "<br>";
|
||||
print display($rgb->toCssString(), $rgb->toCssString(), 'OkLab to RGB');
|
||||
|
||||
$rgb = Coordinates\RGB::create([250, 100, 10])->toLinear();
|
||||
print "RGBlinear: " . DgS::printAr($rgb) . "<br>";
|
||||
$rgb = Coordinates\RGB::create([0, 0, 0])->toLinear();
|
||||
print "RGBlinear: " . DgS::printAr($rgb) . "<br>";
|
||||
|
||||
$cie_lab = Color::okLabToLab($oklab);
|
||||
print "CieLab: " . DgS::printAr($cie_lab) . "<br>";
|
||||
print display($cie_lab->toCssString(), $cie_lab->toCssString(), 'OkLab to Cie Lab');
|
||||
|
||||
$rgb = Coordinates\RGB::create([0, 0, 60]);
|
||||
$hsb = Color::rgbToHsb($rgb);
|
||||
$rgb_b = Color::hsbToRgb($hsb);
|
||||
print "RGB: " . DgS::printAr($rgb) . "<br>";
|
||||
print "RGB->HSB: " . DgS::printAr($hsb) . "<br>";
|
||||
print "HSB->RGB: " . DgS::printAr($rgb_b) . "<br>";
|
||||
|
||||
$hsl = Coordinates\HSL::create([0, 20, 0]);
|
||||
$hsb = Coordinates\HSB::create([0, 20, 0]);
|
||||
$hsl_from_hsb = Color::hsbToHsl($hsb);
|
||||
print "HSL from HSB: " . DgS::printAr($hsl_from_hsb) . "<br>";
|
||||
|
||||
print "<hr>";
|
||||
|
||||
// A(out of bounds)
|
||||
try {
|
||||
print "C::S/COLOR invalid rgb->hex (gray 125): -1, -1, -1: "
|
||||
. CoreLibs\Convert\Colors::rgb2hex(-1, -1, -1) . "<br>";
|
||||
. (new Coordinates\RGB([-1, -1, -1]))->returnAsHex() . "<br>";
|
||||
} catch (\LengthException $e) {
|
||||
print "*Exception: " . $e->getMessage() . "<br>" . $e . "<br>";
|
||||
}
|
||||
try {
|
||||
print "\$C::S/COLOR invalid rgb->hex (gray 125): -1, -1, -1: "
|
||||
. $color_class::rgb2hex(-1, -1, -1) . "<br>";
|
||||
} catch (\LengthException $e) {
|
||||
print "**Exception: " . $e->getMessage() . "<br><pre>" . print_r($e, true) . "</pre><br>";
|
||||
print "*Exception: " . $e->getMessage() . "<br><pre>" . print_r($e, true) . "</pre><br>";
|
||||
}
|
||||
print "<hr>";
|
||||
print "<h2>LEGACY</h2>";
|
||||
// B(valid)
|
||||
$rgb = [10, 20, 30];
|
||||
$rgb = [50, 20, 30];
|
||||
$hex = '#0a141e';
|
||||
$hsb = [210, 67, 12];
|
||||
$hsb_f = [210.5, 67.5, 12.5];
|
||||
$hsl = [210, 50, 7.8];
|
||||
$hsb = [210, 50, 7.8];
|
||||
print "S::COLOR rgb->hex: $rgb[0], $rgb[1], $rgb[2]: " . Colors::rgb2hex($rgb[0], $rgb[1], $rgb[2]) . "<br>";
|
||||
print "S::COLOR hex->rgb: $hex: " . DgS::printAr(SetVarType::setArray(
|
||||
Colors::hex2rgb($hex)
|
||||
)) . "<br>";
|
||||
print "C::S/COLOR rgb->hext: $hex: " . DgS::printAr(SetVarType::setArray(
|
||||
print "C::S/COLOR rgb->hex: $hex: " . DgS::printAr(SetVarType::setArray(
|
||||
CoreLibs\Convert\Colors::hex2rgb($hex)
|
||||
)) . "<br>";
|
||||
// C(to hsb/hsl)
|
||||
@@ -82,9 +164,9 @@ print "S::COLOR hsb_f->rgb: $hsb_f[0], $hsb_f[1], $hsb_f[2]: "
|
||||
. DgS::printAr(SetVarType::setArray(
|
||||
Colors::hsb2rgb($hsb_f[0], $hsb_f[1], $hsb_f[2])
|
||||
)) . "<br>";
|
||||
print "S::COLOR hsl->rgb: $hsl[0], $hsl[1], $hsl[2]: "
|
||||
print "S::COLOR hsl->rgb: $hsb[0], $hsb[1], $hsb[2]: "
|
||||
. DgS::printAr(SetVarType::setArray(
|
||||
Colors::hsl2rgb($hsl[0], $hsl[1], $hsl[2])
|
||||
Colors::hsl2rgb($hsb[0], $hsb[1], $hsb[2])
|
||||
)) . "<br>";
|
||||
|
||||
$hsb = [0, 0, 5];
|
||||
@@ -93,16 +175,26 @@ print "S::COLOR hsb->rgb: $hsb[0], $hsb[1], $hsb[2]: "
|
||||
Colors::hsb2rgb($hsb[0], $hsb[1], $hsb[2])
|
||||
)) . "<br>";
|
||||
|
||||
print "<hr>";
|
||||
|
||||
// Random text
|
||||
$h = rand(0, 359);
|
||||
$s = rand(15, 70);
|
||||
$b = 100;
|
||||
$l = 50;
|
||||
print "RANDOM IN: H: " . $h . ", S: " . $s . ", B/L: " . $b . "/" . $l . "<br>";
|
||||
print "RANDOM hsb->rgb: <pre>" . DgS::printAr(SetVarType::setArray(Colors::hsb2rgb($h, $s, $b))) . "</pre><br>";
|
||||
print "RANDOM hsl->rgb: <pre>" . DgS::printAr(SetVarType::setArray(Colors::hsl2rgb($h, $s, $l))) . "</pre><br>";
|
||||
print "RANDOM hsb->rgb: <pre>"
|
||||
. DgS::printAr(SetVarType::setArray(Color::hsbToRgb(new Coordinates\HSB([$h, $s, $b])))) . "</pre><br>";
|
||||
print "RANDOM hsl->rgb: <pre>"
|
||||
. DgS::printAr(SetVarType::setArray(Color::hslToRgb(new Coordinates\HSL([$h, $s, $l])))) . "</pre><br>";
|
||||
|
||||
// TODO: run compare check input must match output
|
||||
print "<hr>";
|
||||
|
||||
$rgb = [0, 0, 0];
|
||||
print "rgb 0,0,0: " . Dgs::printAr($rgb) . " => "
|
||||
. Dgs::printAr(Color::rgbToHsb(new Coordinates\RGB([$rgb[0], $rgb[1], $rgb[2]]))) . "<br>";
|
||||
|
||||
print "<hr>";
|
||||
|
||||
print "</body></html>";
|
||||
|
||||
|
||||
233
www/admin/class_test.db.convert-placeholder.php
Normal file
233
www/admin/class_test.db.convert-placeholder.php
Normal file
@@ -0,0 +1,233 @@
|
||||
<?php // phpcs:ignore warning
|
||||
|
||||
/**
|
||||
* @phan-file-suppress PhanTypeSuspiciousStringExpression
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
// turn on all error reporting
|
||||
error_reporting(E_ALL | E_STRICT | E_ERROR | E_WARNING | E_PARSE | E_COMPILE_ERROR);
|
||||
|
||||
ob_start();
|
||||
|
||||
// basic class test file
|
||||
define('USE_DATABASE', true);
|
||||
// sample config
|
||||
require 'config.php';
|
||||
// define log file id
|
||||
$LOG_FILE_ID = 'classTest-db-convert-placeholder';
|
||||
ob_end_flush();
|
||||
|
||||
use CoreLibs\Debug\Support;
|
||||
use CoreLibs\DB\Support\ConvertPlaceholder;
|
||||
|
||||
$log = new CoreLibs\Logging\Logging([
|
||||
'log_folder' => BASE . LOG,
|
||||
'log_file_id' => $LOG_FILE_ID,
|
||||
'log_per_date' => true,
|
||||
]);
|
||||
|
||||
|
||||
$PAGE_NAME = 'TEST CLASS: DB CONVERT PLACEHOLDER';
|
||||
print "<!DOCTYPE html>";
|
||||
print "<html><head><title>" . $PAGE_NAME . "</title></head>";
|
||||
print "<body>";
|
||||
print '<div><a href="class_test.php">Class Test Master</a></div>';
|
||||
print '<div><h1>' . $PAGE_NAME . '</h1></div>';
|
||||
|
||||
print "LOGFILE NAME: " . $log->getLogFile() . "<br>";
|
||||
print "LOGFILE ID: " . $log->getLogFileId() . "<br>";
|
||||
|
||||
print "Lookup Regex: <pre>" . ConvertPlaceholder::REGEX_LOOKUP_PLACEHOLDERS . "</pre>";
|
||||
print "Replace Named Regex: <pre>" . ConvertPlaceholder::REGEX_REPLACE_NAMED . "</pre>";
|
||||
print "Replace Named Regex: <pre>" . ConvertPlaceholder::REGEX_REPLACE_QUESTION_MARK . "</pre>";
|
||||
print "Replace Named Regex: <pre>" . ConvertPlaceholder::REGEX_REPLACE_NUMBERED . "</pre>";
|
||||
|
||||
$uniqid = \CoreLibs\Create\Uids::uniqIdShort();
|
||||
// $binary_data = $db->dbEscapeBytea(file_get_contents('class_test.db.php') ?: '');
|
||||
// $binary_data = file_get_contents('class_test.db.php') ?: '';
|
||||
$binary_data = '';
|
||||
$params = [
|
||||
$uniqid,
|
||||
true,
|
||||
'STRING A',
|
||||
2,
|
||||
2.5,
|
||||
1,
|
||||
date('H:m:s'),
|
||||
date('Y-m-d H:i:s'),
|
||||
json_encode(['a' => 'string', 'b' => 1, 'c' => 1.5, 'f' => true, 'g' => ['a', 1, 1.5]]),
|
||||
null,
|
||||
'{"a", "b"}',
|
||||
'{1,2}',
|
||||
'{"(array Text A, 5, 8.8)","(array Text B, 10, 15.2)"}',
|
||||
'("Text", 4, 6.3)',
|
||||
$binary_data
|
||||
];
|
||||
|
||||
$query = <<<SQL
|
||||
INSERT INTO test_foo (
|
||||
test, some_bool, string_a, number_a, number_a_numeric, smallint_a,
|
||||
some_time, some_timestamp, json_string, null_var,
|
||||
array_char_1, array_int_1,
|
||||
array_composite,
|
||||
composite_item,
|
||||
some_binary
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $5, $6,
|
||||
$7, $8, $9, $10,
|
||||
$11, $12,
|
||||
$13,
|
||||
$14,
|
||||
$15
|
||||
)
|
||||
RETURNING
|
||||
test_foo_id,
|
||||
test, some_bool, string_a, number_a, number_a_numeric, smallint_a,
|
||||
some_time, some_timestamp, json_string, null_var,
|
||||
array_char_1, array_int_1,
|
||||
array_composite,
|
||||
composite_item,
|
||||
some_binary
|
||||
SQL;
|
||||
|
||||
print "[ALL] Convert: "
|
||||
. Support::printAr(ConvertPlaceholder::convertPlaceholderInQuery($query, $params))
|
||||
. "<br>";
|
||||
echo "<hr>";
|
||||
|
||||
$query = "SELECT foo FROM bar WHERE baz = :baz AND buz = :baz AND biz = :biz AND boz = :bez";
|
||||
$params = [':baz' => 'SETBAZ', ':bez' => 'SETBEZ', ':biz' => 'SETBIZ'];
|
||||
print "[NO PARAMS] Convert: "
|
||||
. Support::printAr(ConvertPlaceholder::convertPlaceholderInQuery($query, $params))
|
||||
. "<br>";
|
||||
echo "<hr>";
|
||||
|
||||
$query = "SELECT foo FROM bar WHERE baz = :baz AND buz = :baz AND biz = :biz AND boz = :bez";
|
||||
$params = null;
|
||||
print "[NO PARAMS] Convert: "
|
||||
. Support::printAr(ConvertPlaceholder::convertPlaceholderInQuery($query, $params))
|
||||
. "<br>";
|
||||
echo "<hr>";
|
||||
|
||||
$query = "SELECT row_varchar FROM table_with_primary_key WHERE row_varchar <> :row_varchar";
|
||||
$params = null;
|
||||
print "[NO PARAMS] Convert: "
|
||||
. Support::printAr(ConvertPlaceholder::convertPlaceholderInQuery($query, $params))
|
||||
. "<br>";
|
||||
echo "<hr>";
|
||||
|
||||
$query = "SELECT row_varchar, row_varchar_literal, row_int, row_date FROM table_with_primary_key";
|
||||
$params = null;
|
||||
print "[NO PARAMS] TEST: "
|
||||
. Support::printAr(ConvertPlaceholder::convertPlaceholderInQuery($query, $params))
|
||||
. "<br>";
|
||||
echo "<hr>";
|
||||
|
||||
print "[P-CONV]: "
|
||||
. Support::printAr(
|
||||
ConvertPlaceholder::updateParamList([
|
||||
'original' => [
|
||||
'query' => 'SELECT foo FROM bar WHERE baz = :baz AND buz = :biz AND biz = :biz AND boz = :bez',
|
||||
'params' => [':baz' => 'SETBAZ', ':bez' => 'SETBEZ', ':biz' => 'SETBIZ'],
|
||||
'empty_params' => false,
|
||||
],
|
||||
'type' => 'named',
|
||||
'found' => 3,
|
||||
// 'matches' => [
|
||||
// ':baz'
|
||||
// ],
|
||||
// 'params_lookup' => [
|
||||
// ':baz' => '$1'
|
||||
// ],
|
||||
// 'query' => "SELECT foo FROM bar WHERE baz = $1",
|
||||
// 'parms' => [
|
||||
// 'SETBAZ'
|
||||
// ],
|
||||
])
|
||||
);
|
||||
|
||||
echo "<hr>";
|
||||
|
||||
// test connectors: = , <> () for query detection
|
||||
|
||||
// convert placeholder tests
|
||||
// ? -> $n
|
||||
// :name -> $n
|
||||
|
||||
// other way around (just visual)
|
||||
$test_queries = [
|
||||
'skip' => [
|
||||
'query' => <<<SQL
|
||||
SELECT test, string_a, number_a
|
||||
FROM test_foo
|
||||
SQL,
|
||||
'params' => [],
|
||||
'direction' => 'pg',
|
||||
],
|
||||
'numbers' => [
|
||||
'query' => <<<SQL
|
||||
SELECT test, string_a, number_a
|
||||
FROM test_foo
|
||||
WHERE
|
||||
foo = $1 AND bar = $1 AND foobar = $2
|
||||
SQL,
|
||||
'params' => [\CoreLibs\Create\Uids::uniqIdShort(), 'string A-1', 1234],
|
||||
'direction' => 'pdo',
|
||||
],
|
||||
'a?' => [
|
||||
'query' => <<<SQL
|
||||
INSERT INTO test_foo (
|
||||
test, string_a, number_a
|
||||
) VALUES (
|
||||
?, ?, ?
|
||||
)
|
||||
SQL,
|
||||
'params' => [\CoreLibs\Create\Uids::uniqIdShort(), 'string A-1', 1234],
|
||||
'direction' => 'pg',
|
||||
],
|
||||
'b:' => [
|
||||
'query' => <<<SQL
|
||||
INSERT INTO test_foo (
|
||||
test, string_a, number_a
|
||||
) VALUES (
|
||||
:test, :string_a, :number_a
|
||||
)
|
||||
SQL,
|
||||
'params' => [
|
||||
':test' => \CoreLibs\Create\Uids::uniqIdShort(),
|
||||
':string_a' => 'string B-1',
|
||||
':number_a' => 5678
|
||||
],
|
||||
'direction' => 'pg',
|
||||
],
|
||||
'select, compare $' => [
|
||||
'query' => <<<SQL
|
||||
SELECT row_varchar
|
||||
FROM table_with_primary_key
|
||||
WHERE
|
||||
row_int >= $1 OR row_int <= $2 OR
|
||||
row_int > $3 OR row_int < $4
|
||||
OR row_int = $5 OR row_int <> $6
|
||||
SQL,
|
||||
'params' => null,
|
||||
'direction' => 'pg'
|
||||
]
|
||||
];
|
||||
|
||||
|
||||
foreach ($test_queries as $info => $data) {
|
||||
$query = $data['query'];
|
||||
$params = $data['params'];
|
||||
$direction = $data['direction'];
|
||||
print "[$info] Convert: "
|
||||
. Support::printAr(ConvertPlaceholder::convertPlaceholderInQuery($query, $params, $direction))
|
||||
. "<br>";
|
||||
echo "<hr>";
|
||||
}
|
||||
|
||||
print "</body></html>";
|
||||
$log->debug('DEBUGEND', '==================================== [END]');
|
||||
|
||||
// __END__
|
||||
@@ -70,8 +70,7 @@ for ($i = 1; $i <= 6; $i++) {
|
||||
print $i . ") " . $cache_flag . ": "
|
||||
. "res: " . (is_bool($res) ?
|
||||
"<b>Bool:</b> " . Support::prBl($res) :
|
||||
(is_array($res) ?
|
||||
"Array: " . Support::prBl(is_array($res)) : '{-}')
|
||||
"Array: Yes"
|
||||
) . ", "
|
||||
. "cursor_ext: <pre>" . Support::printAr(
|
||||
SetVarType::setArray($db->dbGetCursorExt($q_db_ret))
|
||||
@@ -89,8 +88,7 @@ for ($i = 1; $i <= 6; $i++) {
|
||||
print $i . ") " . $cache_flag . ": "
|
||||
. "res: " . (is_bool($res) ?
|
||||
"<b>Bool:</b> " . Support::prBl($res) :
|
||||
(is_array($res) ?
|
||||
"Array: " . Support::prBl(is_array($res)) : '{-}')
|
||||
"Array: Yes"
|
||||
) . ", "
|
||||
. "cursor_ext: <pre>" . Support::printAr(
|
||||
SetVarType::setArray($db->dbGetCursorExt($q_db_ret))
|
||||
@@ -108,8 +106,7 @@ for ($i = 1; $i <= 6; $i++) {
|
||||
print $i . ") " . $cache_flag . ": "
|
||||
. "res: " . (is_bool($res) ?
|
||||
"<b>Bool:</b> " . Support::prBl($res) :
|
||||
(is_array($res) ?
|
||||
"Array: " . Support::prBl(is_array($res)) : '{-}')
|
||||
"Array: Yes"
|
||||
) . ", "
|
||||
. "cursor_ext: <pre>" . Support::printAr(
|
||||
SetVarType::setArray($db->dbGetCursorExt($q_db_ret))
|
||||
@@ -127,8 +124,7 @@ for ($i = 1; $i <= 6; $i++) {
|
||||
print $i . ") " . $cache_flag . ": "
|
||||
. "res: " . (is_bool($res) ?
|
||||
"<b>Bool:</b> " . Support::prBl($res) :
|
||||
(is_array($res) ?
|
||||
"Array: " . Support::prBl(is_array($res)) : '{-}')
|
||||
"Array: Yes"
|
||||
) . ", "
|
||||
. "cursor_ext: <pre>" . Support::printAr(
|
||||
SetVarType::setArray($db->dbGetCursorExt($q_db_ret))
|
||||
@@ -146,8 +142,7 @@ for ($i = 1; $i <= 6; $i++) {
|
||||
print $i . ") " . $cache_flag . ": "
|
||||
. "res: " . (is_bool($res) ?
|
||||
"<b>Bool:</b> " . Support::prBl($res) :
|
||||
(is_array($res) ?
|
||||
"Array: " . Support::prBl(is_array($res)) : '{-}')
|
||||
"Array: Yes"
|
||||
) . ", "
|
||||
. "cursor_ext: <pre>" . Support::printAr(
|
||||
SetVarType::setArray($db->dbGetCursorExt($q_db_ret))
|
||||
|
||||
@@ -316,7 +316,8 @@ print "EOM STRING EXEC RETURN TEST: " . print_r(
|
||||
$db->dbReturnRowParams(
|
||||
$query_select,
|
||||
[$__last_insert_id]
|
||||
)
|
||||
),
|
||||
true
|
||||
) . "<br>";
|
||||
// B
|
||||
$status = $db->dbExecParams(
|
||||
@@ -345,7 +346,8 @@ print "EOM STRING EXEC RETURN TEST: " . print_r(
|
||||
$db->dbReturnRowParams(
|
||||
$query_select,
|
||||
[$__last_insert_id]
|
||||
)
|
||||
),
|
||||
true
|
||||
) . "<br>";
|
||||
// params > 10 for debug
|
||||
// error catcher
|
||||
@@ -674,7 +676,7 @@ echo "<hr>";
|
||||
|
||||
print "COMPOSITE ELEMENT READ<br>";
|
||||
$res = $db->dbReturnRow("SELECT item, count, (item).name, (item).price, (item).supplier_id FROM on_hand");
|
||||
print "ROW: <pre>" . print_r($res) . "</pre>";
|
||||
print "ROW: <pre>" . print_r($res, true) . "</pre>";
|
||||
var_dump($res);
|
||||
print "Field Name/Types: <pre>" . print_r($db->dbGetFieldNameTypes(), true) . "</pre>";
|
||||
echo "<hr>";
|
||||
|
||||
@@ -204,6 +204,20 @@ WHERE string_a = $1
|
||||
SQL, []);
|
||||
print "PL: " . Support::PrAr($db->dbGetPlaceholderConverted()) . "<br>";
|
||||
|
||||
echo "dbReturn read LIKE: <br>";
|
||||
while (
|
||||
is_array($res = $db->dbReturnParams(
|
||||
<<<SQL
|
||||
SELECT test, string_a, number_a
|
||||
FROM test_foo
|
||||
WHERE string_a LIKE ?
|
||||
SQL,
|
||||
['%A-1%']
|
||||
))
|
||||
) {
|
||||
print "RES: " . Support::prAr($res) . "<br>";
|
||||
}
|
||||
|
||||
print "</body></html>";
|
||||
$db->log->debug('DEBUGEND', '==================================== [END]');
|
||||
|
||||
|
||||
@@ -45,6 +45,8 @@ $em->setErrorMsg('123', 'error', 'msg this is bad, auto logged if debug');
|
||||
$em->setErrorMsg('123', 'error', 'msg this is bad, auto logged if debug', 'target-id', 'other-style');
|
||||
$em->setErrorMsg('123', 'error', 'msg this is bad, logged always', log_error:true);
|
||||
$em->setErrorMsg('123', 'error', 'msg this is bad, never logged', log_error:false);
|
||||
$em->setErrorMsg('500', 'warning', 'This is perhaps not super good, logged_always', log_warning:true);
|
||||
$em->setErrorMsg('500', 'warning', 'This is perhaps not super good, logged_never', log_warning:false);
|
||||
$em->setErrorMsg('1000', 'info', 'This is good');
|
||||
$em->setErrorMsg('9999', 'abort', 'BAD: This is critical (abort)');
|
||||
$em->setErrorMsg('10-1000', 'wrong', 'Wrong level: This is emergency');
|
||||
@@ -59,6 +61,8 @@ print "ErrorsIds: <pre>" . $log->prAr($em->getErrorIds()) . "</pre>";
|
||||
print "Errors: <pre>" . $log->prAr($em->getErrorMsg()) . "</pre>";
|
||||
print "JumpTargets: <pre>" . $log->prAr($em->getJumpTarget()) . "</pre>";
|
||||
|
||||
print "IS info > ok: " . ml::fromName('info')->isHigherThan(ml::ok) . "<br>";
|
||||
|
||||
print "</body></html>";
|
||||
|
||||
$log->debug('[END]', '==========================================>');
|
||||
|
||||
@@ -23,7 +23,6 @@ $log = new CoreLibs\Logging\Logging([
|
||||
'log_file_id' => $LOG_FILE_ID,
|
||||
'log_per_date' => true,
|
||||
]);
|
||||
$_math = new CoreLibs\Convert\Math();
|
||||
$math_class = 'CoreLibs\Convert\Math';
|
||||
|
||||
// define a list of from to color sets for conversion test
|
||||
@@ -35,13 +34,9 @@ print "<body>";
|
||||
print '<div><a href="class_test.php">Class Test Master</a></div>';
|
||||
print '<div><h1>' . $PAGE_NAME . '</h1></div>';
|
||||
|
||||
print "FCEIL: " . $_math->fceil(5.1234567890, 5) . "<br>";
|
||||
print "FLOORP: " . $_math->floorp(5123456, -3) . "<br>";
|
||||
print "FLOORP: " . $_math->floorp(5123456, -10) . "<br>";
|
||||
print "INITNUMERIC: " . $_math->initNumeric('123') . "<br>";
|
||||
|
||||
print "S-FCEIL: " . $math_class::fceil(5.1234567890, 5) . "<br>";
|
||||
print "S-FLOORP: " . $math_class::floorp(5123456, -3) . "<br>";
|
||||
print "S-FLOORP: " . $math_class::floorp(5123456, -10) . "<br>";
|
||||
print "S-INITNUMERIC: " . $math_class::initNumeric(123) . "<br>";
|
||||
print "S-INITNUMERIC: " . $math_class::initNumeric(123.456) . "<br>";
|
||||
print "S-INITNUMERIC: " . $math_class::initNumeric('123') . "<br>";
|
||||
|
||||
@@ -73,6 +73,7 @@ $test_files = [
|
||||
'class_test.db.query-placeholder.php' => 'Class Test: DB query placeholder convert',
|
||||
'class_test.db.dbReturn.php' => 'Class Test: DB dbReturn',
|
||||
'class_test.db.single.php' => 'Class Test: DB single query tests',
|
||||
'class_test.db.convert-placeholder.php' => 'Class Test: DB convert placeholder',
|
||||
'class_test.convert.colors.php' => 'Class Test: CONVERT COLORS',
|
||||
'class_test.check.colors.php' => 'Class Test: CHECK COLORS',
|
||||
'class_test.mime.php' => 'Class Test: MIME',
|
||||
@@ -117,6 +118,7 @@ $test_files = [
|
||||
'class_test.config.direct.php' => 'Class Test: CONFIG DIRECT',
|
||||
'class_test.class-calls.php' => 'Class Test: CLASS CALLS',
|
||||
'class_test.error_msg.php' => 'Class Test: ERROR MSG',
|
||||
'class_test.url-requests.curl.php' => 'Class Test: URL REQUESTS: CURL',
|
||||
'subfolder/class_test.config.direct.php' => 'Class Test: CONFIG DIRECT SUB',
|
||||
];
|
||||
|
||||
@@ -133,7 +135,7 @@ print "<div>READ _ENV ARRAY:</div>";
|
||||
print Support::dumpVar(array_map('htmlentities', $_ENV));
|
||||
// set + check edit access id
|
||||
$edit_access_id = 3;
|
||||
if (is_object($login) && isset($login->loginGetAcl()['unit'])) {
|
||||
if (isset($login->loginGetAcl()['unit'])) {
|
||||
print "ACL UNIT: " . print_r(array_keys($login->loginGetAcl()['unit']), true) . "<br>";
|
||||
print "ACCESS CHECK: " . (string)$login->loginCheckEditAccess($edit_access_id) . "<br>";
|
||||
if ($login->loginCheckEditAccess($edit_access_id)) {
|
||||
@@ -176,25 +178,23 @@ $log->debug('SOME MARK', 'Some error output');
|
||||
|
||||
// INTERNAL SET
|
||||
print "EDIT ACCESS ID: " . $backend->edit_access_id . "<br>";
|
||||
if (is_object($login)) {
|
||||
// print "ACL: <br>".$backend->print_ar($login->loginGetAcl())."<br>";
|
||||
// $log->debug('ACL', "ACL: " . \CoreLibs\Debug\Support::dumpVar($login->loginGetAcl()));
|
||||
// print "DEFAULT ACL: <br>".$backend->print_ar($login->default_acl_list)."<br>";
|
||||
// print "DEFAULT ACL: <br>".$backend->print_ar($login->default_acl_list)."<br>";
|
||||
// $result = array_flip(
|
||||
// array_filter(
|
||||
// array_flip($login->default_acl_list),
|
||||
// function ($key) {
|
||||
// if (is_numeric($key)) {
|
||||
// return $key;
|
||||
// }
|
||||
// }
|
||||
// )
|
||||
// );
|
||||
// print "DEFAULT ACL: <br>".$backend->print_ar($result)."<br>";
|
||||
// DEPRICATED CALL
|
||||
// $backend->adbSetACL($login->loginGetAcl());
|
||||
}
|
||||
// print "ACL: <br>".$backend->print_ar($login->loginGetAcl())."<br>";
|
||||
// $log->debug('ACL', "ACL: " . \CoreLibs\Debug\Support::dumpVar($login->loginGetAcl()));
|
||||
// print "DEFAULT ACL: <br>".$backend->print_ar($login->default_acl_list)."<br>";
|
||||
// print "DEFAULT ACL: <br>".$backend->print_ar($login->default_acl_list)."<br>";
|
||||
// $result = array_flip(
|
||||
// array_filter(
|
||||
// array_flip($login->default_acl_list),
|
||||
// function ($key) {
|
||||
// if (is_numeric($key)) {
|
||||
// return $key;
|
||||
// }
|
||||
// }
|
||||
// )
|
||||
// );
|
||||
// print "DEFAULT ACL: <br>".$backend->print_ar($result)."<br>";
|
||||
// DEPRICATED CALL
|
||||
// $backend->adbSetACL($login->loginGetAcl());
|
||||
|
||||
print "THIS HOST: " . HOST_NAME . ", with PROTOCOL: " . HOST_PROTOCOL . " is running SSL: " . HOST_SSL . "<br>";
|
||||
print "DIR: " . DIR . "<br>";
|
||||
|
||||
361
www/admin/class_test.url-requests.curl.php
Normal file
361
www/admin/class_test.url-requests.curl.php
Normal file
@@ -0,0 +1,361 @@
|
||||
<?php // phpcs:ignore warning
|
||||
|
||||
/**
|
||||
* @phan-file-suppress PhanTypeSuspiciousStringExpression
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
error_reporting(E_ALL | E_STRICT | E_ERROR | E_WARNING | E_PARSE | E_COMPILE_ERROR);
|
||||
|
||||
ob_start();
|
||||
|
||||
// basic class test file
|
||||
define('USE_DATABASE', false);
|
||||
// sample config
|
||||
require 'config.php';
|
||||
// define log file id
|
||||
$LOG_FILE_ID = 'classTest-urlrequests';
|
||||
ob_end_flush();
|
||||
|
||||
use CoreLibs\UrlRequests\Curl;
|
||||
|
||||
$log = new CoreLibs\Logging\Logging([
|
||||
'log_folder' => BASE . LOG,
|
||||
'log_file_id' => $LOG_FILE_ID,
|
||||
'log_per_date' => true,
|
||||
]);
|
||||
|
||||
$PAGE_NAME = 'TEST CLASS: URL REQUESTS CURL';
|
||||
print "<!DOCTYPE html>";
|
||||
print "<html><head><title>" . $PAGE_NAME . "</title></head>";
|
||||
print "<body>";
|
||||
print '<div><a href="class_test.php">Class Test Master</a></div>';
|
||||
print '<div><h1>' . $PAGE_NAME . '</h1></div>';
|
||||
|
||||
$client = new Curl();
|
||||
|
||||
print "<hr>";
|
||||
$data = $client->get(
|
||||
'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlRequests.target.php'
|
||||
. '?other=get_a',
|
||||
[
|
||||
'headers' => [
|
||||
'test-header' => 'ABC',
|
||||
'info-request-type' => '_GET',
|
||||
'Funk-pop' => 'Semlly god'
|
||||
],
|
||||
'query' => ['foo' => 'BAR']
|
||||
]
|
||||
);
|
||||
print "_GET RESPONSE: <pre>" . print_r($data, true) . "</pre>";
|
||||
|
||||
print "<hr>";
|
||||
$data = $client->request(
|
||||
'get',
|
||||
'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlRequests.target.php'
|
||||
. '?other=get_a',
|
||||
);
|
||||
print "_GET RESPONSE, nothing set: <pre>" . print_r($data, true) . "</pre>";
|
||||
|
||||
print "<hr>";
|
||||
try {
|
||||
$data = $client->request(
|
||||
'get',
|
||||
'soba54.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlRequests.target.php'
|
||||
. '?other=get_a',
|
||||
);
|
||||
print "_GET RESPONSE, nothing set, invalid URL: <pre>" . print_r($data, true) . "</pre>";
|
||||
} catch (Exception $e) {
|
||||
print "Exception: <pre>" . print_r($e, true) . "</pre><br>";
|
||||
}
|
||||
|
||||
|
||||
print "<hr>";
|
||||
$data = $client->request(
|
||||
"get",
|
||||
'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/'
|
||||
. 'trunk/www/admin/UrlRequests.target.php'
|
||||
. '?other=get_a',
|
||||
[
|
||||
"headers" => [
|
||||
'test-header' => 'ABC',
|
||||
'info-request-type' => '_GET',
|
||||
'Funk-pop' => 'Semlly god'
|
||||
],
|
||||
"query" => ['foo' => 'BAR'],
|
||||
],
|
||||
);
|
||||
print "[request] _GET RESPONSE: <pre>" . print_r($data, true) . "</pre>";
|
||||
|
||||
print "<hr>";
|
||||
$data = $client->post(
|
||||
'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlRequests.target.php'
|
||||
. '?other=post_a',
|
||||
[
|
||||
'body' => ['payload' => 'data post'],
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json',
|
||||
'Accept' => 'application/json',
|
||||
'test-header' => 'ABC',
|
||||
'info-request-type' => '_POST',
|
||||
],
|
||||
'query' => ['foo' => 'BAR post'],
|
||||
]
|
||||
);
|
||||
print "_POST RESPONSE: <pre>" . print_r($data, true) . "</pre>";
|
||||
print "<hr>";
|
||||
$data = $client->request(
|
||||
"post",
|
||||
'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlRequests.target.php'
|
||||
. '?other=post_a',
|
||||
[
|
||||
"body" => ['payload' => 'data post', 'request' => 'I am the request body'],
|
||||
"headers" => [
|
||||
'Content-Type' => 'application/json',
|
||||
'Accept' => 'application/json',
|
||||
'test-header' => 'ABC',
|
||||
'info-request-type' => '_POST',
|
||||
],
|
||||
"query" => ['foo' => 'BAR post'],
|
||||
]
|
||||
);
|
||||
print "[request] _POST RESPONSE: <pre>" . print_r($data, true) . "</pre>";
|
||||
print "<hr>";
|
||||
$data = $client->request(
|
||||
"post",
|
||||
'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlRequests.target.php'
|
||||
. '?other=post_a',
|
||||
[
|
||||
"body" => 'string body here',
|
||||
"headers" => [
|
||||
'Content-Type' => 'application/json',
|
||||
'Accept' => 'application/json',
|
||||
'test-header' => 'ABC',
|
||||
'info-request-type' => '_POST',
|
||||
],
|
||||
"query" => ['foo' => 'BAR post'],
|
||||
]
|
||||
);
|
||||
print "[request|string body] _POST RESPONSE: <pre>" . print_r($data, true) . "</pre>";
|
||||
|
||||
print "<hr>";
|
||||
$data = $client->put(
|
||||
'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlRequests.target.php'
|
||||
. '?other=put_a',
|
||||
[
|
||||
"body" => ['payload' => 'data put'],
|
||||
"headers" => [
|
||||
'Content-Type' => 'application/json',
|
||||
'Accept' => 'application/json',
|
||||
'test-header' => 'ABC',
|
||||
'info-request-type' => '_PUT',
|
||||
],
|
||||
'query' => ['foo' => 'BAR put'],
|
||||
]
|
||||
);
|
||||
print "_PUT RESPONSE: <pre>" . print_r($data, true) . "</pre>";
|
||||
|
||||
print "<hr>";
|
||||
$data = $client->patch(
|
||||
'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlRequests.target.php'
|
||||
. '?other=patch_a',
|
||||
[
|
||||
"body" => ['payload' => 'data patch'],
|
||||
"headers" => [
|
||||
'Content-Type' => 'application/json',
|
||||
'Accept' => 'application/json',
|
||||
'test-header' => 'ABC',
|
||||
'info-request-type' => '_PATCH',
|
||||
],
|
||||
'query' => ['foo' => 'BAR patch'],
|
||||
]
|
||||
);
|
||||
print "_PATCH RESPONSE: <pre>" . print_r($data, true) . "</pre>";
|
||||
|
||||
print "<hr>";
|
||||
$data = $client->delete(
|
||||
'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlRequests.target.php'
|
||||
. '?other=delete_no_body_a',
|
||||
[
|
||||
"body" => null,
|
||||
"headers" => [
|
||||
'Content-Type' => 'application/json',
|
||||
'Accept' => 'application/json',
|
||||
'test-header' => 'ABC',
|
||||
'info-request-type' => '_DELETE',
|
||||
],
|
||||
"query" => ['foo' => 'BAR delete'],
|
||||
]
|
||||
);
|
||||
print "_DELETE RESPONSE: <pre>" . print_r($data, true) . "</pre>";
|
||||
|
||||
print "<hr>";
|
||||
$data = $client->delete(
|
||||
'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlRequests.target.php'
|
||||
. '?other=delete_body_a',
|
||||
[
|
||||
"body" => ['payload' => 'data delete'],
|
||||
"headers" => [
|
||||
'Content-Type' => 'application/json',
|
||||
'Accept' => 'application/json',
|
||||
'test-header' => 'ABC',
|
||||
'info-request-type' => '_DELETE',
|
||||
],
|
||||
"query" => ['foo' => 'BAR delete'],
|
||||
]
|
||||
);
|
||||
print "_DELETE RESPONSE BODY: <pre>" . print_r($data, true) . "</pre>";
|
||||
|
||||
print "<hr>";
|
||||
|
||||
try {
|
||||
$uc = new Curl([
|
||||
"base_uri" => 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/foo',
|
||||
"headers" => [
|
||||
'DEFAULT-master' => 'master-header',
|
||||
'default-header' => 'uc-get',
|
||||
'default-remove' => 'will be removed',
|
||||
'default-remove-array' => ['a', 'b'],
|
||||
'default-remove-array-part' => ['c', 'd'],
|
||||
'default-remove-array-part-alt' => ['c', 'd', 'e'],
|
||||
'default-overwrite' => 'will be overwritten',
|
||||
'default-add' => 'will be added',
|
||||
],
|
||||
'query' => [
|
||||
'global-p' => 'glob'
|
||||
]
|
||||
]);
|
||||
print "CONFIG: <pre>" . print_r($uc->getConfig(), true) . "</pre>";
|
||||
$uc->removeHeaders(['default-remove' => '']);
|
||||
$uc->removeHeaders(['default-remove-array' => ['a', 'b']]);
|
||||
$uc->removeHeaders(['default-remove-array-part' => 'c']);
|
||||
$uc->removeHeaders(['default-remove-array-part-alt' => ['c', 'd']]);
|
||||
$uc->setHeaders(['default-new' => 'Something new']);
|
||||
$uc->setHeaders(['default-overwrite' => 'Something Overwritten']);
|
||||
$uc->setHeaders(['default-add' => 'Something Added'], true);
|
||||
print "CONFIG: <pre>" . print_r($uc->getConfig(), true) . "</pre>";
|
||||
$data = $uc->request(
|
||||
'get',
|
||||
'UrlRequests.target.php',
|
||||
[
|
||||
'headers' => [
|
||||
'call-header' => 'call-get',
|
||||
'default-header' => 'overwrite-uc-get',
|
||||
'X-Foo' => ['bar', 'baz'],
|
||||
],
|
||||
'query' => [
|
||||
'other' => 'get_a',
|
||||
],
|
||||
]
|
||||
);
|
||||
print "[uc] _GET RESPONSE, nothing set: <pre>" . print_r($data, true) . "</pre>";
|
||||
print "[uc] SENT URL: " . $uc->getUrlSent() . "<br>";
|
||||
print "[uc] SENT URL PARSED: <pre>" . print_r($uc->getUrlParsedSent(), true) . "</pre>";
|
||||
print "[uc] SENT HEADERS: <pre>" . print_r($uc->getHeadersSent(), true) . "</pre>";
|
||||
} catch (Exception $e) {
|
||||
print "Exception: <pre>" . print_r(json_decode($e->getMessage(), true), true) . "</pre><br>";
|
||||
}
|
||||
|
||||
print "<hr>";
|
||||
try {
|
||||
$uc = new Curl([
|
||||
"base_uri" => 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/',
|
||||
"http_errors" => false,
|
||||
"headers" => [
|
||||
"Authorization" => "schmalztiegel",
|
||||
"RunAuthTest" => "yes",
|
||||
]
|
||||
]);
|
||||
$response = $uc->get('UrlRequests.target.php');
|
||||
print "AUTH REQUEST: <pre>" . print_r($response, true) . "</pre>";
|
||||
print "[uc] SENT URL: " . $uc->getUrlSent() . "<br>";
|
||||
print "[uc] SENT URL PARSED: <pre>" . print_r($uc->getUrlParsedSent(), true) . "</pre>";
|
||||
print "[uc] SENT HEADERS: <pre>" . print_r($uc->getHeadersSent(), true) . "</pre>";
|
||||
} catch (Exception $e) {
|
||||
print "Exception: <pre>" . print_r(json_decode($e->getMessage(), true), true) . "</pre><br>";
|
||||
}
|
||||
print "AUTH REQUEST WITH EXCEPTION:<br>";
|
||||
try {
|
||||
$uc = new Curl([
|
||||
"base_uri" => 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/',
|
||||
"http_errors" => true,
|
||||
"headers" => [
|
||||
"Authorization" => "schmalztiegel",
|
||||
"RunAuthTest" => "yes",
|
||||
]
|
||||
]);
|
||||
$response = $uc->get('UrlRequests.target.php');
|
||||
print "AUTH REQUEST: <pre>" . print_r($response, true) . "</pre>";
|
||||
print "[uc] SENT URL: " . $uc->getUrlSent() . "<br>";
|
||||
print "[uc] SENT URL PARSED: <pre>" . print_r($uc->getUrlParsedSent(), true) . "</pre>";
|
||||
print "[uc] SENT HEADERS: <pre>" . print_r($uc->getHeadersSent(), true) . "</pre>";
|
||||
} catch (Exception $e) {
|
||||
print "Exception: <pre>" . print_r(json_decode($e->getMessage(), true), true) . "</pre><br>";
|
||||
}
|
||||
print "AUTH REQUEST WITH EXCEPTION (UNSET):<br>";
|
||||
try {
|
||||
$uc = new Curl([
|
||||
"base_uri" => 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/',
|
||||
"http_errors" => true,
|
||||
"headers" => [
|
||||
"Authorization" => "schmalztiegel",
|
||||
"RunAuthTest" => "yes",
|
||||
]
|
||||
]);
|
||||
$response = $uc->get('UrlRequests.target.php', ['http_errors' => false]);
|
||||
print "AUTH REQUEST (UNSET): <pre>" . print_r($response, true) . "</pre>";
|
||||
print "[uc] SENT URL: " . $uc->getUrlSent() . "<br>";
|
||||
print "[uc] SENT URL PARSED: <pre>" . print_r($uc->getUrlParsedSent(), true) . "</pre>";
|
||||
print "[uc] SENT HEADERS: <pre>" . print_r($uc->getHeadersSent(), true) . "</pre>";
|
||||
} catch (Exception $e) {
|
||||
print "Exception: <pre>" . print_r(json_decode($e->getMessage(), true), true) . "</pre><br>";
|
||||
}
|
||||
print "AUTH REQUEST HEADER SET:<br>";
|
||||
try {
|
||||
$uc = new Curl([
|
||||
"base_uri" => 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/',
|
||||
"auth" => ["user", "pass", "basic"],
|
||||
"headers" => [
|
||||
"Authorization" => "schmalztiegel",
|
||||
"RunAuthTest" => "yes",
|
||||
]
|
||||
]);
|
||||
$response = $uc->get('UrlRequests.target.php');
|
||||
print "AUTH REQUEST (HEADER): <pre>" . print_r($response, true) . "</pre>";
|
||||
print "[uc] SENT URL: " . $uc->getUrlSent() . "<br>";
|
||||
print "[uc] SENT URL PARSED: <pre>" . print_r($uc->getUrlParsedSent(), true) . "</pre>";
|
||||
print "[uc] SENT HEADERS: <pre>" . print_r($uc->getHeadersSent(), true) . "</pre>";
|
||||
} catch (Exception $e) {
|
||||
print "Exception: <pre>" . print_r(json_decode($e->getMessage(), true), true) . "</pre><br>";
|
||||
}
|
||||
|
||||
print "<hr>";
|
||||
$uc = new Curl([
|
||||
"base_uri" => 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/',
|
||||
"headers" => [
|
||||
"header-one" => "one"
|
||||
]
|
||||
]);
|
||||
$response = $uc->get('UrlRequests.target.php', ["headers" => null, "query" => ["test" => "one-test"]]);
|
||||
print "HEADER RESET REQUEST: <pre>" . print_r($response, true) . "</pre>";
|
||||
print "[uc] SENT URL: " . $uc->getUrlSent() . "<br>";
|
||||
print "[uc] SENT URL PARSED: <pre>" . print_r($uc->getUrlParsedSent(), true) . "</pre>";
|
||||
print "[uc] SENT HEADERS: <pre>" . print_r($uc->getHeadersSent(), true) . "</pre>";
|
||||
|
||||
print "<hr>";
|
||||
$uc = new Curl([
|
||||
"base_uri" => 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/',
|
||||
"headers" => [
|
||||
'bar' => 'foo:bar'
|
||||
]
|
||||
]);
|
||||
$response = $uc->get('UrlRequests.target.php');
|
||||
print "HEADER SET TEST REQUEST: <pre>" . print_r($response, true) . "</pre>";
|
||||
print "[uc] SENT URL: " . $uc->getUrlSent() . "<br>";
|
||||
print "[uc] SENT URL PARSED: <pre>" . print_r($uc->getUrlParsedSent(), true) . "</pre>";
|
||||
print "[uc] SENT HEADERS: <pre>" . print_r($uc->getHeadersSent(), true) . "</pre>";
|
||||
|
||||
print "</body></html>";
|
||||
|
||||
// __END__
|
||||
155
www/composer.lock
generated
155
www/composer.lock
generated
@@ -1,155 +0,0 @@
|
||||
{
|
||||
"_readme": [
|
||||
"This file locks the dependencies of your project to a known state",
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "2c73ea6fc1eba5ffc313409ccaa3b732",
|
||||
"packages": [
|
||||
{
|
||||
"name": "egrajp/smarty-extended",
|
||||
"version": "4.5.2",
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://git.egplusww.jp/api/packages/Composer/composer/files/egrajp%2Fsmarty-extended/4.5.2/egrajp-smarty-extended.4.5.2.zip",
|
||||
"shasum": "a2c67a5047aad349a2cfa54240a44da449df9c4c"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"classmap": [
|
||||
"src/"
|
||||
]
|
||||
},
|
||||
"license": [
|
||||
"LGPL-3.0"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Clemens Schwaighofer",
|
||||
"email": "clemens.schwaighofer@egplusww.com"
|
||||
}
|
||||
],
|
||||
"description": "Smarty, extended with gettext, checkbox/radio labels and index numbers",
|
||||
"homepage": "https://github.com/smarty-php/smarty/",
|
||||
"keywords": [
|
||||
"templating"
|
||||
],
|
||||
"time": "2024-04-16T18:25:27+09:00"
|
||||
},
|
||||
{
|
||||
"name": "gullevek/dotenv",
|
||||
"version": "v2.1.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/gullevek/dotEnv.git",
|
||||
"reference": "b9feacaded4e48effff9da7d1173752aef3dc27f"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/gullevek/dotEnv/zipball/b9feacaded4e48effff9da7d1173752aef3dc27f",
|
||||
"reference": "b9feacaded4e48effff9da7d1173752aef3dc27f",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=7.4.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phan/phan": "^5.4",
|
||||
"phpstan/phpstan": "^1.10",
|
||||
"phpunit/phpunit": "^9"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"gullevek\\dotEnv\\": "src/",
|
||||
"gullevek\\dotenv\\": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Clemens Schwaighofer",
|
||||
"email": "gullevek@gullevek.org",
|
||||
"homepage": "http://gullevek.org"
|
||||
}
|
||||
],
|
||||
"description": "Simple .env file processing and storing in _ENV array",
|
||||
"homepage": "https://github.com/gullevek/dotEnv",
|
||||
"keywords": [
|
||||
".env",
|
||||
"_ENV",
|
||||
"dotenv",
|
||||
"environment variables"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/gullevek/dotEnv/issues",
|
||||
"source": "https://github.com/gullevek/dotEnv/tree/v2.1.0"
|
||||
},
|
||||
"time": "2024-08-21T02:41:15+00:00"
|
||||
},
|
||||
{
|
||||
"name": "psr/log",
|
||||
"version": "3.0.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/php-fig/log.git",
|
||||
"reference": "fe5ea303b0887d5caefd3d431c3e61ad47037001"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/php-fig/log/zipball/fe5ea303b0887d5caefd3d431c3e61ad47037001",
|
||||
"reference": "fe5ea303b0887d5caefd3d431c3e61ad47037001",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=8.0.0"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "3.x-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Psr\\Log\\": "src"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "PHP-FIG",
|
||||
"homepage": "https://www.php-fig.org/"
|
||||
}
|
||||
],
|
||||
"description": "Common interface for logging libraries",
|
||||
"homepage": "https://github.com/php-fig/log",
|
||||
"keywords": [
|
||||
"log",
|
||||
"psr",
|
||||
"psr-3"
|
||||
],
|
||||
"support": {
|
||||
"source": "https://github.com/php-fig/log/tree/3.0.0"
|
||||
},
|
||||
"time": "2021-07-14T16:46:02+00:00"
|
||||
}
|
||||
],
|
||||
"packages-dev": [],
|
||||
"aliases": [],
|
||||
"minimum-stability": "stable",
|
||||
"stability-flags": [],
|
||||
"prefer-stable": false,
|
||||
"prefer-lowest": false,
|
||||
"platform": {
|
||||
"php": ">=8.1"
|
||||
},
|
||||
"platform-dev": [],
|
||||
"plugin-api-version": "2.6.0"
|
||||
}
|
||||
@@ -51,6 +51,7 @@ $SITE_CONFIG = [
|
||||
'soba.tequila.jp' => $__LOCAL_CONFIG,
|
||||
'soba.teq.jp' => $__LOCAL_CONFIG,
|
||||
'soba-local.tokyo.tequila.jp' => $__LOCAL_CONFIG,
|
||||
'localhost' => $__LOCAL_CONFIG,
|
||||
];
|
||||
|
||||
// __END__
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
declare(strict_types=1);
|
||||
|
||||
ob_start();
|
||||
require 'config.php';
|
||||
require getcwd() . DIRECTORY_SEPARATOR . 'config.php';
|
||||
|
||||
// should be utf8
|
||||
header("Content-type: text/html; charset=" . DEFAULT_ENCODING);
|
||||
|
||||
@@ -960,10 +960,7 @@ class Login
|
||||
. "AND ear.edit_access_right_id = epa.edit_access_right_id "
|
||||
. "AND epa.enabled = 1 AND epa.edit_group_id = " . $res["edit_group_id"] . " "
|
||||
. "ORDER BY ep.order_number";
|
||||
while ($res = $this->db->dbReturn($q)) {
|
||||
if (!is_array($res)) {
|
||||
break;
|
||||
}
|
||||
while (is_array($res = $this->db->dbReturn($q))) {
|
||||
// page id array for sub data readout
|
||||
$edit_page_ids[$res['edit_page_id']] = $res['cuid'];
|
||||
// create the array for pages
|
||||
@@ -1303,11 +1300,9 @@ class Login
|
||||
{
|
||||
$is_valid_password = true;
|
||||
// check for valid in regex arrays in list
|
||||
if (is_array($this->password_valid_chars)) {
|
||||
foreach ($this->password_valid_chars as $password_valid_chars) {
|
||||
if (!preg_match("/$password_valid_chars/", $password)) {
|
||||
$is_valid_password = false;
|
||||
}
|
||||
foreach ($this->password_valid_chars as $password_valid_chars) {
|
||||
if (!preg_match("/$password_valid_chars/", $password)) {
|
||||
$is_valid_password = false;
|
||||
}
|
||||
}
|
||||
// check for min length
|
||||
|
||||
@@ -31,6 +31,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Admin;
|
||||
|
||||
use CoreLibs\Convert\Json;
|
||||
|
||||
class Backend
|
||||
{
|
||||
// page name
|
||||
@@ -42,7 +44,7 @@ class Backend
|
||||
/** @var array<string> */
|
||||
public array $action_list = [
|
||||
'action', 'action_id', 'action_sub_id', 'action_yes', 'action_flag',
|
||||
'action_menu', 'action_value', 'action_error', 'action_loaded'
|
||||
'action_menu', 'action_value', 'action_type', 'action_error', 'action_loaded'
|
||||
];
|
||||
/** @var string */
|
||||
public string $action;
|
||||
@@ -61,20 +63,31 @@ class Backend
|
||||
/** @var string */
|
||||
public string $action_value;
|
||||
/** @var string */
|
||||
public string $action_type;
|
||||
/** @var string */
|
||||
public string $action_error;
|
||||
|
||||
// ACL array variable if we want to set acl data from outisde
|
||||
/** @var array<mixed> */
|
||||
public array $acl = [];
|
||||
/** @var int */
|
||||
public int $default_acl;
|
||||
|
||||
// queue key
|
||||
/** @var string */
|
||||
public string $queue_key;
|
||||
|
||||
/** @var array<string> list of allowed types for edit log write */
|
||||
private const WRITE_TYPES = ['BINARY', 'BZIP2', 'LZIP', 'STRING', 'SERIAL', 'JSON'];
|
||||
/** @var array<string> list of available write types for log */
|
||||
private array $write_types_available = [];
|
||||
|
||||
// the current active edit access id
|
||||
/** @var int|null */
|
||||
public int|null $edit_access_id;
|
||||
/** @var string */
|
||||
public string $page_name;
|
||||
|
||||
// error/warning/info messages
|
||||
/** @var array<mixed> */
|
||||
public array $messages = [];
|
||||
@@ -84,6 +97,7 @@ class Backend
|
||||
public bool $warning = false;
|
||||
/** @var bool */
|
||||
public bool $info = false;
|
||||
|
||||
// internal lang & encoding vars
|
||||
/** @var string */
|
||||
public string $lang_dir = '';
|
||||
@@ -95,6 +109,7 @@ class Backend
|
||||
public string $domain;
|
||||
/** @var string */
|
||||
public string $encoding;
|
||||
|
||||
/** @var \CoreLibs\Logging\Logging logger */
|
||||
public \CoreLibs\Logging\Logging $log;
|
||||
/** @var \CoreLibs\DB\IO database */
|
||||
@@ -103,6 +118,7 @@ class Backend
|
||||
public \CoreLibs\Language\L10n $l;
|
||||
/** @var \CoreLibs\Create\Session session class */
|
||||
public \CoreLibs\Create\Session $session;
|
||||
|
||||
// smarty publics [end processing in smarty class]
|
||||
/** @var array<mixed> */
|
||||
public array $DATA = [];
|
||||
@@ -117,18 +133,20 @@ class Backend
|
||||
/**
|
||||
* main class constructor
|
||||
*
|
||||
* @param \CoreLibs\DB\IO $db Database connection class
|
||||
* @param \CoreLibs\Logging\Logging $log Logging class
|
||||
* @param \CoreLibs\Create\Session $session Session interface class
|
||||
* @param \CoreLibs\Language\L10n $l10n l10n language class
|
||||
* @param int|null $set_default_acl_level Default ACL level
|
||||
* @param \CoreLibs\DB\IO $db Database connection class
|
||||
* @param \CoreLibs\Logging\Logging $log Logging class
|
||||
* @param \CoreLibs\Create\Session $session Session interface class
|
||||
* @param \CoreLibs\Language\L10n $l10n l10n language class
|
||||
* @param int|null $set_default_acl_level [default=null] Default ACL level
|
||||
* @param bool $init_action_vars [default=true] If the action vars should be set
|
||||
*/
|
||||
public function __construct(
|
||||
\CoreLibs\DB\IO $db,
|
||||
\CoreLibs\Logging\Logging $log,
|
||||
\CoreLibs\Create\Session $session,
|
||||
\CoreLibs\Language\L10n $l10n,
|
||||
?int $set_default_acl_level = null
|
||||
?int $set_default_acl_level = null,
|
||||
bool $init_action_vars = true
|
||||
) {
|
||||
// attach db class
|
||||
$this->db = $db;
|
||||
@@ -151,9 +169,9 @@ class Backend
|
||||
// set the page name
|
||||
$this->page_name = \CoreLibs\Get\System::getPageName();
|
||||
|
||||
// set the action ids
|
||||
foreach ($this->action_list as $_action) {
|
||||
$this->$_action = $_POST[$_action] ?? '';
|
||||
// NOTE: if any of the "action" vars are used somewhere, it is recommended to NOT set them here
|
||||
if ($init_action_vars) {
|
||||
$this->adbSetActionVars();
|
||||
}
|
||||
|
||||
if ($set_default_acl_level === null) {
|
||||
@@ -170,9 +188,12 @@ class Backend
|
||||
}
|
||||
|
||||
// queue key
|
||||
if (preg_match("/^(add|save|delete|remove|move|up|down|push_live)$/", $this->action)) {
|
||||
if (preg_match("/^(add|save|delete|remove|move|up|down|push_live)$/", $this->action ?? '')) {
|
||||
$this->queue_key = \CoreLibs\Create\RandomKey::randomKeyGen(3);
|
||||
}
|
||||
|
||||
// check what edit log data write types are allowed
|
||||
$this->adbSetEditLogWriteTypeAvailable();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -183,7 +204,26 @@ class Backend
|
||||
// NO OP
|
||||
}
|
||||
|
||||
// PUBLIC METHODS |=================================================>
|
||||
// MARK: PRIVATE METHODS
|
||||
|
||||
/**
|
||||
* set the write types that are allowed
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function adbSetEditLogWriteTypeAvailable()
|
||||
{
|
||||
// check what edit log data write types are allowed
|
||||
$this->write_types_available = self::WRITE_TYPES;
|
||||
if (!function_exists('bzcompress')) {
|
||||
$this->write_types_available = array_diff($this->write_types_available, ['BINARY', 'BZIP']);
|
||||
}
|
||||
if (!function_exists('gzcompress')) {
|
||||
$this->write_types_available = array_diff($this->write_types_available, ['LZIP']);
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: PUBLIC METHODS |=================================================>
|
||||
|
||||
/**
|
||||
* set internal ACL from login ACL
|
||||
@@ -195,30 +235,95 @@ class Backend
|
||||
$this->acl = $acl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return current set ACL
|
||||
*
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function adbGetAcl(): array
|
||||
{
|
||||
return $this->acl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set _POST action vars if needed
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function adbSetActionVars()
|
||||
{
|
||||
// set the action ids
|
||||
foreach ($this->action_list as $_action) {
|
||||
$this->$_action = $_POST[$_action] ?? '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* writes all action vars plus other info into edit_log table
|
||||
*
|
||||
* @param string $event any kind of event description,
|
||||
* @param string|array<mixed> $data any kind of data related to that event
|
||||
* @param string $write_type write type can bei STRING or BINARY
|
||||
* @param string|null $db_schema override target schema
|
||||
* @param string $event [default=''] any kind of event description,
|
||||
* @param string|array<mixed> $data [default=''] any kind of data related to that event
|
||||
* @param string $write_type [default=JSON] write type can be
|
||||
* JSON, STRING/SERIEAL, BINARY/BZIP or ZLIB
|
||||
* @param string|null $db_schema [default=null] override target schema
|
||||
* @return void
|
||||
*/
|
||||
public function adbEditLog(
|
||||
string $event = '',
|
||||
string|array $data = '',
|
||||
string $write_type = 'STRING',
|
||||
string $write_type = 'JSON',
|
||||
?string $db_schema = null
|
||||
): void {
|
||||
$data_binary = '';
|
||||
$data_write = '';
|
||||
if ($write_type == 'BINARY') {
|
||||
$data_binary = $this->db->dbEscapeBytea((string)bzcompress(serialize($data)));
|
||||
$data_write = 'see bzip compressed data_binary field';
|
||||
// check if write type is valid, if not fallback to JSON
|
||||
if (!in_array($write_type, $this->write_types_available)) {
|
||||
$this->log->warning('Write type not in allowed array, fallback to JSON', context:[
|
||||
"write_type" => $write_type,
|
||||
"write_list" => $this->write_types_available,
|
||||
]);
|
||||
$write_type = 'JSON';
|
||||
}
|
||||
if ($write_type == 'STRING') {
|
||||
$data_binary = '';
|
||||
$data_write = $this->db->dbEscapeString(serialize($data));
|
||||
switch ($write_type) {
|
||||
case 'BINARY':
|
||||
case 'BZIP':
|
||||
$data_binary = $this->db->dbEscapeBytea((string)bzcompress(serialize($data)));
|
||||
$data_write = Json::jsonConvertArrayTo([
|
||||
'type' => 'BZIP',
|
||||
'message' => 'see bzip compressed data_binary field'
|
||||
]);
|
||||
break;
|
||||
case 'ZLIB':
|
||||
$data_binary = $this->db->dbEscapeBytea((string)gzcompress(serialize($data)));
|
||||
$data_write = Json::jsonConvertArrayTo([
|
||||
'type' => 'ZLIB',
|
||||
'message' => 'see zlib compressed data_binary field'
|
||||
]);
|
||||
break;
|
||||
case 'STRING':
|
||||
case 'SERIAL':
|
||||
$data_binary = $this->db->dbEscapeBytea(Json::jsonConvertArrayTo([
|
||||
'type' => 'SERIAL',
|
||||
'message' => 'see serial string data field'
|
||||
]));
|
||||
$data_write = serialize($data);
|
||||
break;
|
||||
case 'JSON':
|
||||
$data_binary = $this->db->dbEscapeBytea(Json::jsonConvertArrayTo([
|
||||
'type' => 'JSON',
|
||||
'message' => 'see json string data field'
|
||||
]));
|
||||
// must be converted to array
|
||||
if (!is_array($data)) {
|
||||
$data = ["data" => $data];
|
||||
}
|
||||
$data_write = Json::jsonConvertArrayTo($data);
|
||||
break;
|
||||
default:
|
||||
$this->log->alert('Invalid type for data compression was set', context:[
|
||||
"write_type" => $write_type
|
||||
]);
|
||||
break;
|
||||
}
|
||||
|
||||
/** @var string $DB_SCHEMA check schema */
|
||||
@@ -228,44 +333,62 @@ class Backend
|
||||
} elseif (!empty($this->db->dbGetSchema())) {
|
||||
$DB_SCHEMA = $this->db->dbGetSchema();
|
||||
}
|
||||
$q = "INSERT INTO " . $DB_SCHEMA . ".edit_log "
|
||||
. "(euid, event_date, event, data, data_binary, page, "
|
||||
. "ip, user_agent, referer, script_name, query_string, server_name, http_host, "
|
||||
. "http_accept, http_accept_charset, http_accept_encoding, session_id, "
|
||||
. "action, action_id, action_yes, action_flag, action_menu, action_loaded, action_value, action_error) "
|
||||
. "VALUES "
|
||||
. "(" . $this->db->dbEscapeString(isset($_SESSION['EUID']) && is_numeric($_SESSION['EUID']) ?
|
||||
$_SESSION['EUID'] :
|
||||
'NULL')
|
||||
. ", "
|
||||
. "NOW(), "
|
||||
. "'" . $this->db->dbEscapeString((string)$event) . "', "
|
||||
. "'" . $data_write . "', "
|
||||
. "'" . $data_binary . "', "
|
||||
. "'" . $this->db->dbEscapeString((string)$this->page_name) . "', "
|
||||
. "'" . ($_SERVER["REMOTE_ADDR"] ?? '') . "', "
|
||||
. "'" . $this->db->dbEscapeString($_SERVER['HTTP_USER_AGENT'] ?? '') . "', "
|
||||
. "'" . $this->db->dbEscapeString($_SERVER['HTTP_REFERER'] ?? '') . "', "
|
||||
. "'" . $this->db->dbEscapeString($_SERVER['SCRIPT_FILENAME'] ?? '') . "', "
|
||||
. "'" . $this->db->dbEscapeString($_SERVER['QUERY_STRING'] ?? '') . "', "
|
||||
. "'" . $this->db->dbEscapeString($_SERVER['SERVER_NAME'] ?? '') . "', "
|
||||
. "'" . $this->db->dbEscapeString($_SERVER['HTTP_HOST'] ?? '') . "', "
|
||||
. "'" . $this->db->dbEscapeString($_SERVER['HTTP_ACCEPT'] ?? '') . "', "
|
||||
. "'" . $this->db->dbEscapeString($_SERVER['HTTP_ACCEPT_CHARSET'] ?? '') . "', "
|
||||
. "'" . $this->db->dbEscapeString($_SERVER['HTTP_ACCEPT_ENCODING'] ?? '') . "', "
|
||||
. ($this->session->getSessionId() === false ?
|
||||
"NULL" :
|
||||
"'" . $this->session->getSessionId() . "'")
|
||||
. ", "
|
||||
. "'" . $this->db->dbEscapeString($this->action) . "', "
|
||||
. "'" . $this->db->dbEscapeString($this->action_id) . "', "
|
||||
. "'" . $this->db->dbEscapeString($this->action_yes) . "', "
|
||||
. "'" . $this->db->dbEscapeString($this->action_flag) . "', "
|
||||
. "'" . $this->db->dbEscapeString($this->action_menu) . "', "
|
||||
. "'" . $this->db->dbEscapeString($this->action_loaded) . "', "
|
||||
. "'" . $this->db->dbEscapeString($this->action_value) . "', "
|
||||
. "'" . $this->db->dbEscapeString($this->action_error) . "')";
|
||||
$this->db->dbExec($q, 'NULL');
|
||||
$q = <<<SQL
|
||||
INSERT INTO {DB_SCHEMA}.edit_log (
|
||||
euid, event_date, event, data, data_binary, page,
|
||||
ip, user_agent, referer, script_name, query_string, server_name, http_host,
|
||||
http_accept, http_accept_charset, http_accept_encoding, session_id,
|
||||
action, action_id, action_yes, action_flag, action_menu, action_loaded,
|
||||
action_value, action_type, action_error
|
||||
) VALUES (
|
||||
$1, NOW(), $2, $3, $4, $5,
|
||||
$6, $7, $8, $9, $10, $11, $12,
|
||||
$13, $14, $15, $16,
|
||||
$17, $18, $19, $20, $21, $22,
|
||||
$23, $24, $25
|
||||
)
|
||||
SQL;
|
||||
$this->db->dbExecParams(
|
||||
str_replace(
|
||||
['{DB_SCHEMA}'],
|
||||
[$DB_SCHEMA],
|
||||
$q
|
||||
),
|
||||
[
|
||||
// row 1
|
||||
isset($_SESSION['EUID']) && is_numeric($_SESSION['EUID']) ?
|
||||
$_SESSION['EUID'] : null,
|
||||
(string)$event,
|
||||
$data_write,
|
||||
$data_binary,
|
||||
(string)$this->page_name,
|
||||
// row 2
|
||||
$_SERVER["REMOTE_ADDR"] ?? '',
|
||||
$_SERVER['HTTP_USER_AGENT'] ?? '',
|
||||
$_SERVER['HTTP_REFERER'] ?? '',
|
||||
$_SERVER['SCRIPT_FILENAME'] ?? '',
|
||||
$_SERVER['QUERY_STRING'] ?? '',
|
||||
$_SERVER['SERVER_NAME'] ?? '',
|
||||
$_SERVER['HTTP_HOST'] ?? '',
|
||||
// row 3
|
||||
$_SERVER['HTTP_ACCEPT'] ?? '',
|
||||
$_SERVER['HTTP_ACCEPT_CHARSET'] ?? '',
|
||||
$_SERVER['HTTP_ACCEPT_ENCODING'] ?? '',
|
||||
$this->session->getSessionId() !== false ?
|
||||
$this->session->getSessionId() : null,
|
||||
// row 4
|
||||
$this->action ?? '',
|
||||
$this->action_id ?? '',
|
||||
$this->action_yes ?? '',
|
||||
$this->action_flag ?? '',
|
||||
$this->action_menu ?? '',
|
||||
$this->action_loaded ?? '',
|
||||
$this->action_value ?? '',
|
||||
$this->action_type ?? '',
|
||||
$this->action_error ?? '',
|
||||
],
|
||||
'NULL'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -302,10 +425,7 @@ class Backend
|
||||
?string $set_content_path = null,
|
||||
int $flag = 0,
|
||||
): array {
|
||||
if (
|
||||
$set_content_path === null ||
|
||||
!is_string($set_content_path)
|
||||
) {
|
||||
if ($set_content_path === null) {
|
||||
/** @deprecated adbTopMenu missing set_content_path parameter */
|
||||
trigger_error(
|
||||
'Calling adbTopMenu without set_content_path parameter is deprecated',
|
||||
@@ -515,16 +635,30 @@ class Backend
|
||||
} elseif (!empty($this->db->dbGetSchema())) {
|
||||
$DB_SCHEMA = $this->db->dbGetSchema();
|
||||
}
|
||||
$q = "INSERT INTO " . $DB_SCHEMA . ".live_queue ("
|
||||
. "queue_key, key_value, key_name, type, target, data, group_key, action, associate, file"
|
||||
. ") VALUES ("
|
||||
. "'" . $this->db->dbEscapeString($queue_key) . "', '" . $this->db->dbEscapeString($key_value) . "', "
|
||||
. "'" . $this->db->dbEscapeString($key_name) . "', '" . $this->db->dbEscapeString($type) . "', "
|
||||
. "'" . $this->db->dbEscapeString($target) . "', '" . $this->db->dbEscapeString($data) . "', "
|
||||
. "'" . $this->queue_key . "', '" . $this->action . "', "
|
||||
. "'" . $this->db->dbEscapeString((string)$associate) . "', "
|
||||
. "'" . $this->db->dbEscapeString((string)$file) . "')";
|
||||
$this->db->dbExec($q);
|
||||
$q = <<<SQL
|
||||
INSERT INTO {DB_SCHEMA}.live_queue (
|
||||
queue_key, key_value, key_name, type,
|
||||
target, data, group_key, action, associate, file
|
||||
) VALUES (
|
||||
$1, $2, $3, $4,
|
||||
$5, $6, $7, $8, $9, $10
|
||||
)
|
||||
SQL;
|
||||
// $this->db->dbExec($q);
|
||||
$this->db->dbExecParams(
|
||||
str_replace(
|
||||
['{DB_SCHEMA}'],
|
||||
[$DB_SCHEMA],
|
||||
$q
|
||||
),
|
||||
[
|
||||
$queue_key, $key_value,
|
||||
$key_name, $type,
|
||||
$target, $data,
|
||||
$this->queue_key, $this->action,
|
||||
(string)$associate, (string)$file
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1139,118 +1139,6 @@ class Basic
|
||||
|
||||
// *** BETTER PASSWORD OPTIONS END ***
|
||||
|
||||
// *** COLORS ***
|
||||
// [!!! DEPRECATED !!!]
|
||||
// moved to \CoreLibs\Convert\Colors
|
||||
|
||||
/**
|
||||
* converts a hex RGB color to the int numbers
|
||||
* @param string $hexStr RGB hexstring
|
||||
* @param bool $returnAsString flag to return as string
|
||||
* @param string $seperator string seperator: default: ","
|
||||
* @return string|array<mixed>|bool false on error or array with RGB or
|
||||
* a string with the seperator
|
||||
* @deprecated use \CoreLibs\Convert\Colors::hex2rgb() instead
|
||||
*/
|
||||
public static function hex2rgb(string $hexStr, bool $returnAsString = false, string $seperator = ',')
|
||||
{
|
||||
trigger_error('Method ' . __METHOD__ . ' is deprecated, use \CoreLibs\Convert\Colors::hex2rgb()', E_USER_DEPRECATED);
|
||||
return \CoreLibs\Convert\Colors::hex2rgb($hexStr, $returnAsString, $seperator);
|
||||
}
|
||||
|
||||
/**
|
||||
* converts the rgb values from int data to the valid rgb html hex string
|
||||
* optional can turn of leading #
|
||||
* @param int $red red 0-255
|
||||
* @param int $green green 0-255
|
||||
* @param int $blue blue 0-255
|
||||
* @param bool $hex_prefix default true, prefix with "#"
|
||||
* @return string|bool rgb in hex values with leading # if set
|
||||
* @deprecated use \CoreLibs\Convert\Colors::rgb2hex() instead
|
||||
*/
|
||||
public static function rgb2hex(int $red, int $green, int $blue, bool $hex_prefix = true)
|
||||
{
|
||||
trigger_error('Method ' . __METHOD__ . ' is deprecated, use \CoreLibs\Convert\Colors::rgb2hex()', E_USER_DEPRECATED);
|
||||
return \CoreLibs\Convert\Colors::rgb2hex($red, $green, $blue, $hex_prefix);
|
||||
}
|
||||
|
||||
/**
|
||||
* converts and int RGB to the HTML color string in hex format
|
||||
* @param int $red red 0-255
|
||||
* @param int $green green 0-255
|
||||
* @param int $blue blue 0-255
|
||||
* @return string|bool hex rgb string
|
||||
* @deprecated use rgb2hex instead
|
||||
*/
|
||||
public static function rgb2html(int $red, int $green, int $blue)
|
||||
{
|
||||
trigger_error('Method ' . __METHOD__ . ' is deprecated, use \CoreLibs\Convert\Colors::rgb2hex()', E_USER_DEPRECATED);
|
||||
// check that each color is between 0 and 255
|
||||
return \CoreLibs\Convert\Colors::rgb2hex($red, $green, $blue, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* converts RGB to HSB/V values
|
||||
* returns:
|
||||
* array with hue (0-360), sat (0-100%), brightness/value (0-100%)
|
||||
* @param int $red red 0-255
|
||||
* @param int $green green 0-255
|
||||
* @param int $blue blue 0-255
|
||||
* @return array<mixed>|bool Hue, Sat, Brightness/Value
|
||||
* @deprecated use \CoreLibs\Convert\Colors::rgb2hsb() instead
|
||||
*/
|
||||
public static function rgb2hsb(int $red, int $green, int $blue)
|
||||
{
|
||||
trigger_error('Method ' . __METHOD__ . ' is deprecated, use \CoreLibs\Convert\Colors::rgb2hsb()', E_USER_DEPRECATED);
|
||||
return \CoreLibs\Convert\Colors::rgb2hsb($red, $green, $blue);
|
||||
}
|
||||
|
||||
/**
|
||||
* converts HSB/V to RGB values RGB is full INT
|
||||
* @param int $H hue 0-360
|
||||
* @param float $S saturation 0-1 (float)
|
||||
* @param float $V brightness/value 0-1 (float)
|
||||
* @return array<mixed>|bool 0 red/1 green/2 blue array
|
||||
* @deprecated use \CoreLibs\Convert\Colors::hsb2rgb() instead
|
||||
*/
|
||||
public static function hsb2rgb(int $H, float $S, float $V)
|
||||
{
|
||||
trigger_error('Method ' . __METHOD__ . ' is deprecated, use \CoreLibs\Convert\Colors::hsb2rgb()', E_USER_DEPRECATED);
|
||||
return \CoreLibs\Convert\Colors::hsb2rgb($H, (int)round($S * 100), (int)round($V * 100));
|
||||
}
|
||||
|
||||
/**
|
||||
* converts a RGB (0-255) to HSL
|
||||
* return:
|
||||
* array with hue (0-360), saturation (0-100%) and luminance (0-100%)
|
||||
* @param int $r red 0-255
|
||||
* @param int $g green 0-255
|
||||
* @param int $b blue 0-255
|
||||
* @return array<mixed>|bool hue/sat/luminance
|
||||
* @deprecated use \CoreLibs\Convert\Colors::rgb2hsl() instead
|
||||
*/
|
||||
public static function rgb2hsl(int $r, int $g, int $b)
|
||||
{
|
||||
trigger_error('Method ' . __METHOD__ . ' is deprecated, use \CoreLibs\Convert\Colors::rgb2hsl()', E_USER_DEPRECATED);
|
||||
return \CoreLibs\Convert\Colors::rgb2hsb($r, $g, $b);
|
||||
}
|
||||
|
||||
/**
|
||||
* converts an HSL to RGB
|
||||
* @param int $h hue: 0-360 (degrees)
|
||||
* @param float $s saturation: 0-1
|
||||
* @param float $l luminance: 0-1
|
||||
* @return array<mixed>|bool red/blue/green 0-255 each
|
||||
* @deprecated use \CoreLibs\Convert\Colors::hsl2rgb() instead
|
||||
*/
|
||||
public static function hsl2rgb(int $h, float $s, float $l)
|
||||
{
|
||||
trigger_error('Method ' . __METHOD__ . ' is deprecated, use \CoreLibs\Convert\Colors::hsl2rgb()', E_USER_DEPRECATED);
|
||||
return \CoreLibs\Convert\Colors::hsl2rgb($h, $s * 100, $l * 100);
|
||||
}
|
||||
|
||||
// *** COLORS END ***
|
||||
|
||||
// *** EMAIL FUNCTIONS ***
|
||||
// [!!! DEPRECATED !!!]
|
||||
// Moved to \CoreLibs\Check\Email
|
||||
|
||||
@@ -119,6 +119,13 @@ class Colors
|
||||
|
||||
/**
|
||||
* check if html/css color string is valid
|
||||
*
|
||||
* TODO: update check for correct validate values
|
||||
* - space instead of ","
|
||||
* - / opcatiy checks
|
||||
* - loose numeric values
|
||||
* - lab/lch,oklab/oklch validation too
|
||||
*
|
||||
* @param string $color A color string of any format
|
||||
* @param int $flags defaults to ALL, else use | to combined from
|
||||
* HEX_RGB, HEX_RGBA, RGB, RGBA, HSL, HSLA
|
||||
@@ -168,9 +175,9 @@ class Colors
|
||||
if (preg_match("/$regex/", $color)) {
|
||||
// if valid regex, we now need to check if the content is actually valid
|
||||
// only for rgb/hsl type
|
||||
/** @var int|false */
|
||||
/** @var int<0, max>|false */
|
||||
$rgb_flag = strpos($color, 'rgb');
|
||||
/** @var int|false */
|
||||
/** @var int<0, max>|false */
|
||||
$hsl_flag = strpos($color, 'hsl');
|
||||
// if both not match, return true
|
||||
if (
|
||||
|
||||
@@ -509,6 +509,22 @@ class ArrayHandler
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove entries from a simple array, will not keep key order
|
||||
*
|
||||
* any array content is allowed
|
||||
*
|
||||
* https://stackoverflow.com/a/369608
|
||||
*
|
||||
* @param array<mixed> $array Array where elements are located
|
||||
* @param array<mixed> $remove Elements to remove
|
||||
* @return array<mixed> Array with $remove elements removed
|
||||
*/
|
||||
public static function arrayRemoveEntry(array $array, array $remove): array
|
||||
{
|
||||
return array_diff($array, $remove);
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
|
||||
359
www/lib/CoreLibs/Convert/Color/CieXyz.php
Normal file
359
www/lib/CoreLibs/Convert/Color/CieXyz.php
Normal file
@@ -0,0 +1,359 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* AUTHOR: Clemens Schwaighofer
|
||||
* CREATED: 2024/11/12
|
||||
* DESCRIPTION:
|
||||
* CIE XYZ color space conversion
|
||||
* This for various interims work
|
||||
* none public
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Convert\Color;
|
||||
|
||||
use CoreLibs\Convert\Math;
|
||||
use CoreLibs\Convert\Color\Coordinates\RGB;
|
||||
use CoreLibs\Convert\Color\Coordinates\Lab;
|
||||
use CoreLibs\Convert\Color\Coordinates\XYZ;
|
||||
|
||||
class CieXyz
|
||||
{
|
||||
// MARK: public wrapper functions
|
||||
|
||||
/**
|
||||
* Convert from RGB to OkLab
|
||||
* via xyz D65
|
||||
*
|
||||
* @param RGB $rgb
|
||||
* @return Lab
|
||||
*/
|
||||
public static function rgbViaXyzD65ToOkLab(RGB $rgb): Lab
|
||||
{
|
||||
return self::xyzD65ToOkLab(
|
||||
self::linRgbToXyzD65($rgb)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convet from OkLab to RGB
|
||||
* via xyz D65
|
||||
*
|
||||
* @param Lab $lab
|
||||
* @return RGB
|
||||
*/
|
||||
public static function okLabViaXyzD65ToRgb(Lab $lab): RGB
|
||||
{
|
||||
return self::xyzD65ToLinRgb(
|
||||
self::okLabToXyzD65($lab)
|
||||
)->fromLinear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert RGB to CIE Lab
|
||||
* via xyz D65 to xyz D50
|
||||
*
|
||||
* @param RGB $rgb
|
||||
* @return Lab
|
||||
*/
|
||||
public static function rgbViaXyzD65ViaXyzD50ToLab(RGB $rgb): Lab
|
||||
{
|
||||
return self::xyzD50ToLab(
|
||||
self::xyzD65ToXyzD50(
|
||||
self::linRgbToXyzD65($rgb)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert CIE Lab to RGB
|
||||
* via xyz D50 to xyz D65
|
||||
*
|
||||
* @param Lab $lab
|
||||
* @return RGB
|
||||
*/
|
||||
public static function labViaXyzD50ViaXyzD65ToRgb(Lab $lab): RGB
|
||||
{
|
||||
return self::xyzD65ToLinRgb(
|
||||
self::xyzD50ToXyxD65(
|
||||
self::labToXyzD50($lab)
|
||||
)
|
||||
)->fromLinear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert from oklab to cie lab
|
||||
*
|
||||
* @param Lab $lab
|
||||
* @return Lab
|
||||
*/
|
||||
public static function okLabViaXyzD65ViaXyzD50ToLab(Lab $lab): Lab
|
||||
{
|
||||
return self::xyzD50ToLab(
|
||||
self::xyzD65ToXyzD50(
|
||||
self::okLabToXyzD65($lab)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert from cie lab to oklab
|
||||
*
|
||||
* @param Lab $lab
|
||||
* @return Lab
|
||||
*/
|
||||
public static function labViaXyzD50ViaXyzD65ToOkLab(Lab $lab): Lab
|
||||
{
|
||||
return self::xyzD65ToOkLab(
|
||||
self::xyzD50ToXyxD65(
|
||||
self::labToXyzD50($lab)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// MARK: helper convert any array to array{float, float, float}
|
||||
|
||||
/**
|
||||
* This is a hack for phpstan until we write a proper matrix to class
|
||||
* conversion wrapper function
|
||||
*
|
||||
* @param array<array<float|int>|float|int> $_array
|
||||
* @return array{0:float,1:float,2:float}
|
||||
*/
|
||||
private static function convertArray(array $_array): array
|
||||
{
|
||||
/** @var array{0:float,1:float,2:float} */
|
||||
return [$_array[0], $_array[1], $_array[2]];
|
||||
}
|
||||
|
||||
// MARK: xyzD65 <-> xyzD50
|
||||
|
||||
/**
|
||||
* xyzD65 to xyzD50 whitepoint
|
||||
*
|
||||
* @param XYZ $xyz
|
||||
* @return XYZ
|
||||
*/
|
||||
private static function xyzD65ToXyzD50(XYZ $xyz): XYZ
|
||||
{
|
||||
return new XYZ(self::convertArray(Math::multiplyMatrices(
|
||||
a: [
|
||||
[1.0479298208405488, 0.022946793341019088, -0.05019222954313557],
|
||||
[0.029627815688159344, 0.990434484573249, -0.01707382502938514],
|
||||
[-0.009243058152591178, 0.015055144896577895, 0.7518742899580008],
|
||||
],
|
||||
b: $xyz->returnAsArray(),
|
||||
)), options: ["whitepoint" => 'D50']);
|
||||
}
|
||||
|
||||
/**
|
||||
* xyzD50 to xyzD65 whitepoint
|
||||
*
|
||||
* @param XYZ $xyz
|
||||
* @return XYZ
|
||||
*/
|
||||
private static function xyzD50ToXyxD65(XYZ $xyz): XYZ
|
||||
{
|
||||
return new XYZ(self::convertArray(Math::multiplyMatrices(
|
||||
a: [
|
||||
[0.9554734527042182, -0.023098536874261423, 0.0632593086610217],
|
||||
[-0.028369706963208136, 1.0099954580058226, 0.021041398966943008],
|
||||
[0.012314001688319899, -0.020507696433477912, 1.3303659366080753],
|
||||
],
|
||||
b: $xyz->returnAsArray()
|
||||
)), options: ["whitepoint" => 'D65']);
|
||||
}
|
||||
|
||||
// MARK: xyzD50 <-> Lab
|
||||
|
||||
/**
|
||||
* Convert xyzD50 to Lab (Cie)
|
||||
*
|
||||
* @param XYZ $xyz
|
||||
* @return Lab
|
||||
*/
|
||||
private static function xyzD50ToLab(XYZ $xyz): Lab
|
||||
{
|
||||
$_xyz = $xyz->returnAsArray();
|
||||
$d50 = [
|
||||
0.3457 / 0.3585,
|
||||
1.00000,
|
||||
(1.0 - 0.3457 - 0.3585) / 0.3585,
|
||||
];
|
||||
|
||||
$a = 216 / 24389;
|
||||
$b = 24389 / 27;
|
||||
|
||||
$_xyz = array_map(
|
||||
fn ($k, $v) => $v / $d50[$k],
|
||||
array_keys($_xyz),
|
||||
array_values($_xyz),
|
||||
);
|
||||
|
||||
$f = array_map(
|
||||
fn ($v) => (($v > $a) ?
|
||||
pow($v, 1 / 3) :
|
||||
(($b * $v + 16) / 116)
|
||||
),
|
||||
$_xyz,
|
||||
);
|
||||
|
||||
return new Lab([
|
||||
(116 * $f[1]) - 16,
|
||||
500 * ($f[0] - $f[1]),
|
||||
200 * ($f[1] - $f[2]),
|
||||
], colorspace: 'CIELab');
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert Lab (Cie) to xyz D50
|
||||
*
|
||||
* @param Lab $lab
|
||||
* @return XYZ
|
||||
*/
|
||||
private static function labToXyzD50(Lab $lab): XYZ
|
||||
{
|
||||
$_lab = $lab->returnAsArray();
|
||||
$a = 24389 / 27;
|
||||
$b = 216 / 24389;
|
||||
$f = [];
|
||||
$f[1] = ($_lab[0] + 16) / 116;
|
||||
$f[0] = $_lab[1] / 500 + $f[1];
|
||||
$f[2] = $f[1] - $_lab[2] / 200;
|
||||
$xyz = [
|
||||
// x
|
||||
pow($f[0], 3) > $b ?
|
||||
pow($f[0], 3) :
|
||||
(116 * $f[0] - 16) / $a,
|
||||
// y
|
||||
$_lab[0] > $a * $b ?
|
||||
pow(($_lab[0] + 16) / 116, 3) :
|
||||
$_lab[0] / $a,
|
||||
// z
|
||||
pow($f[2], 3) > $b ?
|
||||
pow($f[2], 3) :
|
||||
(116 * $f[2] - 16) / $a,
|
||||
];
|
||||
|
||||
$d50 = [
|
||||
0.3457 / 0.3585,
|
||||
1.00000,
|
||||
(1.0 - 0.3457 - 0.3585) / 0.3585,
|
||||
];
|
||||
|
||||
return new XYZ(
|
||||
self::convertArray(array_map(
|
||||
fn ($k, $v) => $v * $d50[$k],
|
||||
array_keys($xyz),
|
||||
$xyz,
|
||||
)),
|
||||
options: ["whitepoint" => 'D50']
|
||||
);
|
||||
}
|
||||
|
||||
// MARK: xyzD65 <-> (linear)RGB
|
||||
|
||||
/**
|
||||
* convert linear RGB to xyz D65
|
||||
* if rgb is not flagged linear, it will be auto converted
|
||||
*
|
||||
* @param RGB $rgb
|
||||
* @return XYZ
|
||||
*/
|
||||
private static function linRgbToXyzD65(RGB $rgb): XYZ
|
||||
{
|
||||
// if not linear, convert to linear
|
||||
if (!(bool)$rgb->get('linear')) {
|
||||
$rgb = (new RGB($rgb->returnAsArray()))->toLinear();
|
||||
}
|
||||
return new XYZ(self::convertArray(Math::multiplyMatrices(
|
||||
[
|
||||
[0.41239079926595934, 0.357584339383878, 0.1804807884018343],
|
||||
[0.21263900587151027, 0.715168678767756, 0.07219231536073371],
|
||||
[0.01933081871559182, 0.11919477979462598, 0.9505321522496607],
|
||||
],
|
||||
$rgb->returnAsArray()
|
||||
)), options: ["whitepoint" => 'D65']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert xyz D65 to linear RGB
|
||||
*
|
||||
* @param XYZ $xyz
|
||||
* @return RGB
|
||||
*/
|
||||
private static function xyzD65ToLinRgb(XYZ $xyz): RGB
|
||||
{
|
||||
// xyz D65 to linrgb
|
||||
return new RGB(self::convertArray(Math::multiplyMatrices(
|
||||
a : [
|
||||
[ 3.2409699419045226, -1.537383177570094, -0.4986107602930034 ],
|
||||
[ -0.9692436362808796, 1.8759675015077202, 0.04155505740717559 ],
|
||||
[ 0.05563007969699366, -0.20397695888897652, 1.0569715142428786 ],
|
||||
],
|
||||
b : $xyz->returnAsArray()
|
||||
)), options: ["linear" => true]);
|
||||
}
|
||||
|
||||
// MARK: xyzD65 <-> OkLab
|
||||
|
||||
/**
|
||||
* xyz D65 to OkLab
|
||||
*
|
||||
* @param XYZ $xyz
|
||||
* @return Lab
|
||||
*/
|
||||
private static function xyzD65ToOkLab(XYZ $xyz): Lab
|
||||
{
|
||||
return new Lab(self::convertArray(Math::multiplyMatrices(
|
||||
[
|
||||
[0.2104542553, 0.7936177850, -0.0040720468],
|
||||
[1.9779984951, -2.4285922050, 0.4505937099],
|
||||
[0.0259040371, 0.7827717662, -0.8086757660],
|
||||
],
|
||||
array_map(
|
||||
callback: fn ($v) => pow((float)$v, 1 / 3),
|
||||
array: Math::multiplyMatrices(
|
||||
a: [
|
||||
[0.8190224432164319, 0.3619062562801221, -0.12887378261216414],
|
||||
[0.0329836671980271, 0.9292868468965546, 0.03614466816999844],
|
||||
[0.048177199566046255, 0.26423952494422764, 0.6335478258136937],
|
||||
],
|
||||
b: $xyz->returnAsArray(),
|
||||
),
|
||||
)
|
||||
)), colorspace: 'OkLab');
|
||||
}
|
||||
|
||||
/**
|
||||
* xyz D65 to OkLab
|
||||
*
|
||||
* @param Lab $lab
|
||||
* @return XYZ
|
||||
*/
|
||||
private static function okLabToXyzD65(Lab $lab): XYZ
|
||||
{
|
||||
return new XYZ(self::convertArray(Math::multiplyMatrices(
|
||||
a: [
|
||||
[1.2268798733741557, -0.5578149965554813, 0.28139105017721583],
|
||||
[-0.04057576262431372, 1.1122868293970594, -0.07171106666151701],
|
||||
[-0.07637294974672142, -0.4214933239627914, 1.5869240244272418],
|
||||
],
|
||||
b: array_map(
|
||||
callback: fn ($v) => is_numeric($v) ? $v ** 3 : 0,
|
||||
array: Math::multiplyMatrices(
|
||||
a: [
|
||||
[0.99999999845051981432, 0.39633779217376785678, 0.21580375806075880339],
|
||||
[1.0000000088817607767, -0.1055613423236563494, -0.063854174771705903402],
|
||||
[1.0000000546724109177, -0.089484182094965759684, -1.2914855378640917399],
|
||||
],
|
||||
// Divide $lightness by 100 to convert from CSS OkLab
|
||||
b: $lab->returnAsArray(),
|
||||
),
|
||||
),
|
||||
)), options: ["whitepoint" => 'D65']);
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
1103
www/lib/CoreLibs/Convert/Color/Color.php
Normal file
1103
www/lib/CoreLibs/Convert/Color/Color.php
Normal file
File diff suppressed because it is too large
Load Diff
190
www/lib/CoreLibs/Convert/Color/Coordinates/HSB.php
Normal file
190
www/lib/CoreLibs/Convert/Color/Coordinates/HSB.php
Normal file
@@ -0,0 +1,190 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* AUTHOR: Clemens Schwaighofer
|
||||
* CREATED: 2024/11/11
|
||||
* DESCRIPTION:
|
||||
* Color Coordinate: HSB/HSV
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Convert\Color\Coordinates;
|
||||
|
||||
use CoreLibs\Convert\Color\Utils;
|
||||
|
||||
class HSB implements Interface\CoordinatesInterface
|
||||
{
|
||||
/** @var array<string> allowed colorspaces */
|
||||
private const COLORSPACES = ['sRGB'];
|
||||
|
||||
/** @var float hue */
|
||||
private float $H = 0.0;
|
||||
/** @var float saturation */
|
||||
private float $S = 0.0;
|
||||
/** @var float brightness / value */
|
||||
private float $B = 0.0;
|
||||
|
||||
/** @var string color space: either ok or cie */
|
||||
private string $colorspace = '';
|
||||
|
||||
/**
|
||||
* HSB (HSV) color coordinates
|
||||
* Hue/Saturation/Brightness or Value
|
||||
*
|
||||
* @param string|array{0:float,1:float,2:float} $colors
|
||||
* @param string $colorspace [default=sRGB]
|
||||
* @param array<string,string> $options [default=[]]
|
||||
* @throws \InvalidArgumentException only array colors allowed
|
||||
*/
|
||||
public function __construct(string|array $colors, string $colorspace = 'sRGB', array $options = [])
|
||||
{
|
||||
if (!is_array($colors)) {
|
||||
throw new \InvalidArgumentException('Only array colors allowed', 0);
|
||||
}
|
||||
$this->setColorspace($colorspace)->parseOptions($options)->setFromArray($colors);
|
||||
}
|
||||
|
||||
/**
|
||||
* set from array
|
||||
* where 0: Hue, 1: Saturation, 2: Brightness
|
||||
*
|
||||
* @param string|array{0:float,1:float,2:float} $colors
|
||||
* @param string $colorspace [default=sRGB]
|
||||
* @param array<string,string> $options [default=[]]
|
||||
* @return self
|
||||
*/
|
||||
public static function create(string|array $colors, string $colorspace = 'sRGB', array $options = []): self
|
||||
{
|
||||
return new HSB($colors, $colorspace, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* parse options
|
||||
*
|
||||
* @param array<string,string> $options
|
||||
* @return self
|
||||
*/
|
||||
private function parseOptions(array $options): self
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* set color
|
||||
*
|
||||
* @param string $name
|
||||
* @param float $value
|
||||
* @return void
|
||||
*/
|
||||
private function set(string $name, float $value): void
|
||||
{
|
||||
$name = strtoupper($name);
|
||||
if (!property_exists($this, $name)) {
|
||||
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||
}
|
||||
switch ($name) {
|
||||
case 'H':
|
||||
if ($value == 360.0) {
|
||||
$value = 0;
|
||||
}
|
||||
// if ($value < 0 || $value > 360) {
|
||||
if (Utils::compare(0.0, $value, 360.0, Utils::EPSILON_SMALL)) {
|
||||
throw new \LengthException(
|
||||
'Argument value ' . $value . ' for hue is not in the range of 0 to 360',
|
||||
1
|
||||
);
|
||||
}
|
||||
break;
|
||||
case 'S':
|
||||
// if ($value < 0 || $value > 100) {
|
||||
if (Utils::compare(0.0, $value, 100.0, Utils::EPSILON_SMALL)) {
|
||||
throw new \LengthException(
|
||||
'Argument value ' . $value . ' for saturation is not in the range of 0 to 100',
|
||||
2
|
||||
);
|
||||
}
|
||||
break;
|
||||
case 'B':
|
||||
// if ($value < 0 || $value > 100) {
|
||||
if (Utils::compare(0.0, $value, 100.0, Utils::EPSILON_SMALL)) {
|
||||
throw new \LengthException(
|
||||
'Argument value ' . $value . ' for brightness is not in the range of 0 to 100',
|
||||
3
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
$this->$name = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* get color
|
||||
*
|
||||
* @param string $name
|
||||
* @return float
|
||||
*/
|
||||
public function get(string $name): float|string|bool
|
||||
{
|
||||
$name = strtoupper($name);
|
||||
if (!property_exists($this, $name)) {
|
||||
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||
}
|
||||
return $this->$name;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the colorspace
|
||||
*
|
||||
* @param string $colorspace
|
||||
* @return self
|
||||
*/
|
||||
private function setColorspace(string $colorspace): self
|
||||
{
|
||||
if (!in_array($colorspace, $this::COLORSPACES)) {
|
||||
throw new \InvalidArgumentException('Not allowed colorspace', 0);
|
||||
}
|
||||
$this->colorspace = $colorspace;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the color as array
|
||||
* where 0: Hue, 1: Saturation, 2: Brightness
|
||||
*
|
||||
* @return array{0:float,1:float,2:float}
|
||||
*/
|
||||
public function returnAsArray(): array
|
||||
{
|
||||
return [$this->H, $this->S, $this->B];
|
||||
}
|
||||
|
||||
/**
|
||||
* set color as array
|
||||
* where 0: Hue, 1: Saturation, 2: Brightness
|
||||
*
|
||||
* @param array{0:float,1:float,2:float} $colors
|
||||
* @return self
|
||||
*/
|
||||
private function setFromArray(array $colors): self
|
||||
{
|
||||
$this->set('H', $colors[0]);
|
||||
$this->set('S', $colors[1]);
|
||||
$this->set('B', $colors[2]);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* no hsb in css
|
||||
*
|
||||
* @param float|string|null $opacity
|
||||
* @return string
|
||||
* @throws \ErrorException
|
||||
*/
|
||||
public function toCssString(null|float|string $opacity = null): string
|
||||
{
|
||||
throw new \ErrorException('HSB is not available as CSS color string', 0);
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
195
www/lib/CoreLibs/Convert/Color/Coordinates/HSL.php
Normal file
195
www/lib/CoreLibs/Convert/Color/Coordinates/HSL.php
Normal file
@@ -0,0 +1,195 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* AUTHOR: Clemens Schwaighofer
|
||||
* CREATED: 2024/11/11
|
||||
* DESCRIPTION:
|
||||
* Color Coordinate: HSL
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Convert\Color\Coordinates;
|
||||
|
||||
use CoreLibs\Convert\Color\Utils;
|
||||
|
||||
class HSL implements Interface\CoordinatesInterface
|
||||
{
|
||||
/** @var array<string> allowed colorspaces */
|
||||
private const COLORSPACES = ['sRGB'];
|
||||
|
||||
/** @var float hue */
|
||||
private float $H = 0.0;
|
||||
/** @var float saturation */
|
||||
private float $S = 0.0;
|
||||
/** @var float lightness (luminance) */
|
||||
private float $L = 0.0;
|
||||
|
||||
/** @var string color space: either ok or cie */
|
||||
private string $colorspace = '';
|
||||
|
||||
/**
|
||||
* Color Coordinate HSL
|
||||
* Hue/Saturation/Lightness
|
||||
*
|
||||
* @param string|array{0:float,1:float,2:float} $colors
|
||||
* @param string $colorspace [default=sRGB]
|
||||
* @param array<string,string> $options [default=[]]
|
||||
* @throws \InvalidArgumentException only array colors allowed
|
||||
*/
|
||||
public function __construct(string|array $colors, string $colorspace = 'sRGB', array $options = [])
|
||||
{
|
||||
if (!is_array($colors)) {
|
||||
throw new \InvalidArgumentException('Only array colors allowed', 0);
|
||||
}
|
||||
$this->setColorspace($colorspace)->parseOptions($options)->setFromArray($colors);
|
||||
}
|
||||
|
||||
/**
|
||||
* set from array
|
||||
* where 0: Hue, 1: Saturation, 2: Lightness
|
||||
*
|
||||
* @param string|array{0:float,1:float,2:float} $colors
|
||||
* @param string $colorspace [default=sRGB]
|
||||
* @param array<string,string> $options [default=[]]
|
||||
* @return self
|
||||
*/
|
||||
public static function create(string|array $colors, string $colorspace = 'sRGB', array $options = []): self
|
||||
{
|
||||
return new HSL($colors, $colorspace, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* parse options
|
||||
*
|
||||
* @param array<string,string> $options
|
||||
* @return self
|
||||
*/
|
||||
private function parseOptions(array $options): self
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* set color
|
||||
*
|
||||
* @param string $name
|
||||
* @param float $value
|
||||
* @return void
|
||||
*/
|
||||
private function set(string $name, float $value): void
|
||||
{
|
||||
if (!property_exists($this, $name)) {
|
||||
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||
}
|
||||
switch ($name) {
|
||||
case 'H':
|
||||
if ($value == 360.0) {
|
||||
$value = 0;
|
||||
}
|
||||
// if ($value < 0 || $value > 360) {
|
||||
if (Utils::compare(0.0, $value, 360.0, Utils::EPSILON_SMALL)) {
|
||||
throw new \LengthException(
|
||||
'Argument value ' . $value . ' for hue is not in the range of 0 to 360',
|
||||
1
|
||||
);
|
||||
}
|
||||
break;
|
||||
case 'S':
|
||||
// if ($value < 0 || $value > 100) {
|
||||
if (Utils::compare(0.0, $value, 100.0, Utils::EPSILON_SMALL)) {
|
||||
throw new \LengthException(
|
||||
'Argument value ' . $value . ' for saturation is not in the range of 0 to 100',
|
||||
2
|
||||
);
|
||||
}
|
||||
break;
|
||||
case 'L':
|
||||
// if ($value < 0 || $value > 100) {
|
||||
if (Utils::compare(0.0, $value, 100.0, Utils::EPSILON_SMALL)) {
|
||||
throw new \LengthException(
|
||||
'Argument value ' . $value . ' for lightness is not in the range of 0 to 100',
|
||||
3
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
$this->$name = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* get color
|
||||
*
|
||||
* @param string $name
|
||||
* @return float
|
||||
*/
|
||||
public function get(string $name): float|string|bool
|
||||
{
|
||||
if (!property_exists($this, $name)) {
|
||||
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||
}
|
||||
return $this->$name;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the colorspace
|
||||
*
|
||||
* @param string $colorspace
|
||||
* @return self
|
||||
*/
|
||||
private function setColorspace(string $colorspace): self
|
||||
{
|
||||
if (!in_array($colorspace, $this::COLORSPACES)) {
|
||||
throw new \InvalidArgumentException('Not allowed colorspace', 0);
|
||||
}
|
||||
$this->colorspace = $colorspace;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the color as array
|
||||
* where 0: Hue, 1: Saturation, 2: Lightness
|
||||
*
|
||||
* @return array{0:float,1:float,2:float}
|
||||
*/
|
||||
public function returnAsArray(): array
|
||||
{
|
||||
return [$this->H, $this->S, $this->L];
|
||||
}
|
||||
|
||||
/**
|
||||
* set color as array
|
||||
* where 0: Hue, 1: Saturation, 2: Lightness
|
||||
*
|
||||
* @param array{0:float,1:float,2:float} $colors
|
||||
* @return self
|
||||
*/
|
||||
private function setFromArray(array $colors): self
|
||||
{
|
||||
$this->set('H', $colors[0]);
|
||||
$this->set('S', $colors[1]);
|
||||
$this->set('L', $colors[2]);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* convert to css string with optional opacityt
|
||||
*
|
||||
* @param float|string|null $opacity
|
||||
* @return string
|
||||
*/
|
||||
public function toCssString(null|float|string $opacity = null): string
|
||||
{
|
||||
$string = 'hsl('
|
||||
. $this->H
|
||||
. ' '
|
||||
. $this->S
|
||||
. ' '
|
||||
. $this->L
|
||||
. Utils::setOpacity($opacity)
|
||||
. ')';
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
195
www/lib/CoreLibs/Convert/Color/Coordinates/HWB.php
Normal file
195
www/lib/CoreLibs/Convert/Color/Coordinates/HWB.php
Normal file
@@ -0,0 +1,195 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* AUTHOR: Clemens Schwaighofer
|
||||
* CREATED: 2024/11/11
|
||||
* DESCRIPTION:
|
||||
* Color Coordinate: HWB
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Convert\Color\Coordinates;
|
||||
|
||||
use CoreLibs\Convert\Color\Utils;
|
||||
|
||||
class HWB implements Interface\CoordinatesInterface
|
||||
{
|
||||
/** @var array<string> allowed colorspaces */
|
||||
private const COLORSPACES = ['sRGB'];
|
||||
|
||||
/** @var float Hue */
|
||||
private float $H = 0.0;
|
||||
/** @var float Whiteness */
|
||||
private float $W = 0.0;
|
||||
/** @var float Blackness */
|
||||
private float $B = 0.0;
|
||||
|
||||
/** @var string color space: either ok or cie */
|
||||
private string $colorspace = '';
|
||||
|
||||
/**
|
||||
* Color Coordinate: HWB
|
||||
* Hue/Whiteness/Blackness
|
||||
*
|
||||
* @param string|array{0:float,1:float,2:float} $colors
|
||||
* @param string $colorspace [default=sRGB]
|
||||
* @param array<string,string> $options [default=[]]
|
||||
* @throws \InvalidArgumentException only array colors allowed
|
||||
*/
|
||||
public function __construct(string|array $colors, string $colorspace = 'sRGB', array $options = [])
|
||||
{
|
||||
if (!is_array($colors)) {
|
||||
throw new \InvalidArgumentException('Only array colors allowed', 0);
|
||||
}
|
||||
$this->setColorspace($colorspace)->parseOptions($options)->setFromArray($colors);
|
||||
}
|
||||
|
||||
/**
|
||||
* set from array
|
||||
* where 0: Hue, 1: Whiteness, 2: Blackness
|
||||
*
|
||||
* @param string|array{0:float,1:float,2:float} $colors
|
||||
* @param string $colorspace [default=sRGB]
|
||||
* @param array<string,string> $options [default=[]]
|
||||
* @return self
|
||||
*/
|
||||
public static function create(string|array $colors, string $colorspace = 'sRGB', array $options = []): self
|
||||
{
|
||||
return new HWB($colors, $colorspace, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* parse options
|
||||
*
|
||||
* @param array<string,string> $options
|
||||
* @return self
|
||||
*/
|
||||
private function parseOptions(array $options): self
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* set color
|
||||
*
|
||||
* @param string $name
|
||||
* @param float $value
|
||||
* @return void
|
||||
*/
|
||||
private function set(string $name, float $value): void
|
||||
{
|
||||
if (!property_exists($this, $name)) {
|
||||
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||
}
|
||||
switch ($name) {
|
||||
case 'H':
|
||||
if ($value == 360.0) {
|
||||
$value = 0;
|
||||
}
|
||||
// if ($value < 0 || $value > 360) {
|
||||
if (Utils::compare(0.0, $value, 360.0, Utils::EPSILON_SMALL)) {
|
||||
throw new \LengthException(
|
||||
'Argument value ' . $value . ' for hue is not in the range of 0 to 360',
|
||||
1
|
||||
);
|
||||
}
|
||||
break;
|
||||
case 'W':
|
||||
// if ($value < 0 || $value > 100) {
|
||||
if (Utils::compare(0.0, $value, 100.0, Utils::EPSILON_SMALL)) {
|
||||
throw new \LengthException(
|
||||
'Argument value ' . $value . ' for whiteness is not in the range of 0 to 100',
|
||||
2
|
||||
);
|
||||
}
|
||||
break;
|
||||
case 'B':
|
||||
// if ($value < 0 || $value > 100) {
|
||||
if (Utils::compare(0.0, $value, 100.0, Utils::EPSILON_SMALL)) {
|
||||
throw new \LengthException(
|
||||
'Argument value ' . $value . ' for blackness is not in the range of 0 to 100',
|
||||
3
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
$this->$name = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* get color
|
||||
*
|
||||
* @param string $name
|
||||
* @return float
|
||||
*/
|
||||
public function get(string $name): float|string|bool
|
||||
{
|
||||
if (!property_exists($this, $name)) {
|
||||
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||
}
|
||||
return $this->$name;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the colorspace
|
||||
*
|
||||
* @param string $colorspace
|
||||
* @return self
|
||||
*/
|
||||
private function setColorspace(string $colorspace): self
|
||||
{
|
||||
if (!in_array($colorspace, $this::COLORSPACES)) {
|
||||
throw new \InvalidArgumentException('Not allowed colorspace', 0);
|
||||
}
|
||||
$this->colorspace = $colorspace;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the color as array
|
||||
* where 0: Hue, 1: Whiteness, 2: Blackness
|
||||
*
|
||||
* @return array{0:float,1:float,2:float}
|
||||
*/
|
||||
public function returnAsArray(): array
|
||||
{
|
||||
return [$this->H, $this->W, $this->B];
|
||||
}
|
||||
|
||||
/**
|
||||
* set color as array
|
||||
* where 0: Hue, 1: Whiteness, 2: Blackness
|
||||
*
|
||||
* @param array{0:float,1:float,2:float} $colors
|
||||
* @return self
|
||||
*/
|
||||
private function setFromArray(array $colors): self
|
||||
{
|
||||
$this->set('H', $colors[0]);
|
||||
$this->set('W', $colors[1]);
|
||||
$this->set('B', $colors[2]);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* convert to css string with optional opacityt
|
||||
*
|
||||
* @param float|string|null $opacity
|
||||
* @return string
|
||||
*/
|
||||
public function toCssString(null|float|string $opacity = null): string
|
||||
{
|
||||
$string = 'hwb('
|
||||
. $this->H
|
||||
. ' '
|
||||
. $this->W
|
||||
. ' '
|
||||
. $this->B
|
||||
. Utils::setOpacity($opacity)
|
||||
. ')';
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* AUTHOR: Clemens Schwaighofer
|
||||
* CREATED: Ymd
|
||||
* DESCRIPTION:
|
||||
* DescriptionHere
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Convert\Color\Coordinates\Interface;
|
||||
|
||||
interface CoordinatesInterface
|
||||
{
|
||||
/**
|
||||
* create class via "Class::create()" call
|
||||
* was used for multiple create interfaces
|
||||
* no longer needed, use "new Class()" instead
|
||||
*
|
||||
* @param string|array{0:float,1:float,2:float} $colors
|
||||
* @param string $colorspace [default='']
|
||||
* @param array<string,string|bool|int> $options [default=[]]
|
||||
* @return self
|
||||
*/
|
||||
public static function create(string|array $colors, string $colorspace = '', array $options = []): self;
|
||||
|
||||
/**
|
||||
* get color
|
||||
*
|
||||
* @param string $name
|
||||
* @return float
|
||||
*/
|
||||
public function get(string $name): float|string|bool;
|
||||
|
||||
/**
|
||||
* Returns the color as array
|
||||
* where 0: Lightness, 1: a, 2: b
|
||||
*
|
||||
* @return array{0:float,1:float,2:float}
|
||||
*/
|
||||
public function returnAsArray(): array;
|
||||
|
||||
/**
|
||||
* Convert into css string with optional opacity
|
||||
*
|
||||
* @param null|float|string|null $opacity
|
||||
* @return string
|
||||
*/
|
||||
public function toCssString(null|float|string $opacity = null): string;
|
||||
}
|
||||
|
||||
// __END__
|
||||
227
www/lib/CoreLibs/Convert/Color/Coordinates/LCH.php
Normal file
227
www/lib/CoreLibs/Convert/Color/Coordinates/LCH.php
Normal file
@@ -0,0 +1,227 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* AUTHOR: Clemens Schwaighofer
|
||||
* CREATED: 2024/11/11
|
||||
* DESCRIPTION:
|
||||
* Color Coordinate: Lch
|
||||
* for oklch or cie
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Convert\Color\Coordinates;
|
||||
|
||||
use CoreLibs\Convert\Color\Utils;
|
||||
|
||||
class LCH implements Interface\CoordinatesInterface
|
||||
{
|
||||
/** @var array<string> allowed colorspaces */
|
||||
private const COLORSPACES = ['OkLab', 'CIELab'];
|
||||
|
||||
/** @var float Lightness/Luminance
|
||||
* CIE: 0 to 100
|
||||
* OKlch: 0.0 to 1.0
|
||||
* BOTH: 0% to 100%
|
||||
*/
|
||||
private float $L = 0.0;
|
||||
/** @var float Chroma
|
||||
* CIE: 0 to 150, cannot be more than 230
|
||||
* OkLch: 0 to 0.4, does not exceed 0.5
|
||||
* BOTH: 0% to 100% (0 to 150, 0 to 0.4)
|
||||
*/
|
||||
private float $C = 0.0;
|
||||
/** @var float Hue
|
||||
* 0 to 360 deg
|
||||
*/
|
||||
private float $H = 0.0;
|
||||
|
||||
/** @var string color space: either ok or cie */
|
||||
private string $colorspace = '';
|
||||
|
||||
/**
|
||||
* Color Coordinate Lch
|
||||
* for oklch
|
||||
*
|
||||
* @param string|array{0:float,1:float,2:float} $colors
|
||||
* @param string $colorspace [default='']
|
||||
* @param array<string,string> $options [default=[]]
|
||||
* @throws \InvalidArgumentException only array colors allowed
|
||||
*/
|
||||
public function __construct(string|array $colors, string $colorspace = '', array $options = [])
|
||||
{
|
||||
if (!is_array($colors)) {
|
||||
throw new \InvalidArgumentException('Only array colors allowed', 0);
|
||||
}
|
||||
$this->setColorspace($colorspace)->parseOptions($options)->setFromArray($colors);
|
||||
}
|
||||
|
||||
/**
|
||||
* set from array
|
||||
* where 0: Lightness, 1: Chroma, 2: Hue
|
||||
*
|
||||
* @param string|array{0:float,1:float,2:float} $colors
|
||||
* @param string $colorspace [default='']
|
||||
* @param array<string,string> $options [default=[]]
|
||||
* @return self
|
||||
*/
|
||||
public static function create(string|array $colors, string $colorspace = '', array $options = []): self
|
||||
{
|
||||
return new LCH($colors, $colorspace, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* parse options
|
||||
*
|
||||
* @param array<string,string> $options
|
||||
* @return self
|
||||
*/
|
||||
private function parseOptions(array $options): self
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* set color
|
||||
*
|
||||
* @param string $name
|
||||
* @param float $value
|
||||
* @return void
|
||||
*/
|
||||
private function set(string $name, float $value): void
|
||||
{
|
||||
if (!property_exists($this, $name)) {
|
||||
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||
}
|
||||
switch ($name) {
|
||||
case 'L':
|
||||
// if ($this->colorspace == 'CIELab' && ($value < 0 || $value > 100)) {
|
||||
if ($this->colorspace == 'CIELab' && Utils::compare(0.0, $value, 100.0, Utils::ESPILON_BIG)) {
|
||||
throw new \LengthException(
|
||||
'Argument value ' . $value . ' for lightness is not in the range of 0 to 100 for CIE Lab',
|
||||
1
|
||||
);
|
||||
// } elseif ($this->colorspace == 'OkLab' && ($value < 0 || $value > 1)) {
|
||||
} elseif ($this->colorspace == 'OkLab' && Utils::compare(0.0, $value, 1.0, Utils::EPSILON_SMALL)) {
|
||||
throw new \LengthException(
|
||||
'Argument value ' . $value . ' for lightness is not in the range of 0.0 to 1.0 for OkLab',
|
||||
1
|
||||
);
|
||||
}
|
||||
break;
|
||||
case 'C':
|
||||
// if ($this->colorspace == 'CIELab' && ($value < 0 || $value > 230)) {
|
||||
if ($this->colorspace == 'CIELab' && Utils::compare(0.0, $value, 230.0, Utils::EPSILON_SMALL)) {
|
||||
throw new \LengthException(
|
||||
'Argument value ' . $value . ' for chroma is not in the range of '
|
||||
. '0 to 150 and a maximum of 230 for CIE Lab',
|
||||
1
|
||||
);
|
||||
// } elseif ($this->colorspace == 'OkLab' && ($value < 0 || $value > 0.55)) {
|
||||
} elseif ($this->colorspace == 'OkLab' && Utils::compare(0.0, $value, 0.55, Utils::EPSILON_SMALL)) {
|
||||
throw new \LengthException(
|
||||
'Argument value ' . $value . ' for lightness is not in the range of '
|
||||
. '0.0 to 0.4 and a maximum of 0.5 for OkLab',
|
||||
1
|
||||
);
|
||||
}
|
||||
break;
|
||||
case 'H':
|
||||
// if ($value < 0 || $value > 360) {
|
||||
if (Utils::compare(0.0, $value, 360.0, Utils::EPSILON_SMALL)) {
|
||||
throw new \LengthException(
|
||||
'Argument value ' . $value . ' for hue is not in the range of 0.0 to 360.0',
|
||||
1
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
$this->$name = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* get color
|
||||
*
|
||||
* @param string $name
|
||||
* @return float
|
||||
*/
|
||||
public function get(string $name): float|string|bool
|
||||
{
|
||||
if (!property_exists($this, $name)) {
|
||||
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||
}
|
||||
return $this->$name;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the colorspace
|
||||
*
|
||||
* @param string $colorspace
|
||||
* @return self
|
||||
*/
|
||||
private function setColorspace(string $colorspace): self
|
||||
{
|
||||
if (!in_array($colorspace, $this::COLORSPACES)) {
|
||||
throw new \InvalidArgumentException('Not allowed colorspace', 0);
|
||||
}
|
||||
$this->colorspace = $colorspace;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the color as array
|
||||
* where 0: Lightness, 1: Chroma, 2: Hue
|
||||
*
|
||||
* @return array{0:float,1:float,2:float}
|
||||
*/
|
||||
public function returnAsArray(): array
|
||||
{
|
||||
return [$this->L, $this->C, $this->H];
|
||||
}
|
||||
|
||||
/**
|
||||
* set color as array
|
||||
* where 0: Lightness, 1: Chroma, 2: Hue
|
||||
*
|
||||
* @param array{0:float,1:float,2:float} $colors
|
||||
* @return self
|
||||
*/
|
||||
private function setFromArray(array $colors): self
|
||||
{
|
||||
$this->set('L', $colors[0]);
|
||||
$this->set('C', $colors[1]);
|
||||
$this->set('H', $colors[2]);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert into css string with optional opacity
|
||||
*
|
||||
* @param null|float|string|null $opacity
|
||||
* @return string
|
||||
*/
|
||||
public function toCssString(null|float|string $opacity = null): string
|
||||
{
|
||||
$string = '';
|
||||
switch ($this->colorspace) {
|
||||
case 'CIELab':
|
||||
$string = 'lch';
|
||||
break;
|
||||
case 'OkLab':
|
||||
$string = 'oklch';
|
||||
break;
|
||||
}
|
||||
$string .= '('
|
||||
. $this->L
|
||||
. ' '
|
||||
. $this->C
|
||||
. ' '
|
||||
. $this->H
|
||||
. Utils::setOpacity($opacity)
|
||||
. ');';
|
||||
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
233
www/lib/CoreLibs/Convert/Color/Coordinates/Lab.php
Normal file
233
www/lib/CoreLibs/Convert/Color/Coordinates/Lab.php
Normal file
@@ -0,0 +1,233 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* AUTHOR: Clemens Schwaighofer
|
||||
* CREATED: 2024/11/11
|
||||
* DESCRIPTION:
|
||||
* Color Coordinate: Lab
|
||||
* for oklab or cie
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Convert\Color\Coordinates;
|
||||
|
||||
use CoreLibs\Convert\Color\Utils;
|
||||
|
||||
class Lab implements Interface\CoordinatesInterface
|
||||
{
|
||||
/** @var array<string> allowed colorspaces */
|
||||
private const COLORSPACES = ['OkLab', 'CIELab'];
|
||||
|
||||
/** @var float lightness/luminance
|
||||
* CIE: 0f to 100f
|
||||
* OKlab: 0.0 to 1.0
|
||||
* BOTH: 0% to 100%
|
||||
*/
|
||||
private float $L = 0.0;
|
||||
/** @var float a axis distance
|
||||
* CIE: -125 to 125, cannot be more than +/- 160
|
||||
* OKlab: -0.4 to 0.4, cannot exceed +/- 0.5
|
||||
* BOTH: -100% to 100% (+/-125 or 0.4)
|
||||
*/
|
||||
private float $a = 0.0;
|
||||
/** @var float b axis distance
|
||||
* CIE: -125 to 125, cannot be more than +/- 160
|
||||
* OKlab: -0.4 to 0.4, cannot exceed +/- 0.5
|
||||
* BOTH: -100% to 100% (+/-125 or 0.4)
|
||||
*/
|
||||
private float $b = 0.0;
|
||||
|
||||
/** @var string color space: either ok or cie */
|
||||
private string $colorspace = '';
|
||||
|
||||
/**
|
||||
* Color Coordinate: Lab
|
||||
* for oklab or cie
|
||||
*
|
||||
* @param string|array{0:float,1:float,2:float} $colors
|
||||
* @param string $colorspace [default='']
|
||||
* @param array<string,string> $options [default=[]]
|
||||
* @throws \InvalidArgumentException only array colors allowed
|
||||
*/
|
||||
public function __construct(string|array $colors, string $colorspace = '', array $options = [])
|
||||
{
|
||||
if (!is_array($colors)) {
|
||||
throw new \InvalidArgumentException('Only array colors allowed', 0);
|
||||
}
|
||||
$this->setColorspace($colorspace)->parseOptions($options)->setFromArray($colors);
|
||||
}
|
||||
|
||||
/**
|
||||
* set from array
|
||||
* where 0: Lightness, 1: a, 2: b
|
||||
*
|
||||
* @param array{0:float,1:float,2:float} $colors
|
||||
* @param string $colorspace [default='']
|
||||
* @param array<string,string> $options [default=[]]
|
||||
* @return self
|
||||
*/
|
||||
public static function create(string|array $colors, string $colorspace = '', array $options = []): self
|
||||
{
|
||||
return new Lab($colors, $colorspace, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* parse options
|
||||
*
|
||||
* @param array<string,string> $options
|
||||
* @return self
|
||||
*/
|
||||
private function parseOptions(array $options): self
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* set color
|
||||
*
|
||||
* @param string $name
|
||||
* @param float $value
|
||||
* @return void
|
||||
*/
|
||||
private function set(string $name, float $value): void
|
||||
{
|
||||
if (!property_exists($this, $name)) {
|
||||
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||
}
|
||||
switch ($name) {
|
||||
case 'L':
|
||||
// if ($this->colorspace == 'CIELab' && ($value < 0 || $value > 100)) {
|
||||
if ($this->colorspace == 'CIELab' && Utils::compare(0.0, $value, 100.0, Utils::ESPILON_BIG)) {
|
||||
throw new \LengthException(
|
||||
'Argument value ' . $value . ' for lightness is not in the range of 0 to 100 for CIE Lab',
|
||||
1
|
||||
);
|
||||
// } elseif ($this->colorspace == 'OkLab' && ($value < 0 || $value > 1)) {
|
||||
} elseif ($this->colorspace == 'OkLab' && Utils::compare(0.0, $value, 1.0, Utils::EPSILON_SMALL)) {
|
||||
throw new \LengthException(
|
||||
'Argument value ' . $value . ' for lightness is not in the range of 0.0 to 1.0 for OkLab',
|
||||
1
|
||||
);
|
||||
}
|
||||
break;
|
||||
case 'a':
|
||||
// if ($this->colorspace == 'CIELab' && ($value < -125 || $value > 125)) {
|
||||
if ($this->colorspace == 'CIELab' && Utils::compare(-125.0, $value, 125.0, Utils::EPSILON_SMALL)) {
|
||||
throw new \LengthException(
|
||||
'Argument value ' . $value . ' for a is not in the range of -125 to 125 for CIE Lab',
|
||||
2
|
||||
);
|
||||
// } elseif ($this->colorspace == 'OkLab' && ($value < -0.55 || $value > 0.55)) {
|
||||
} elseif ($this->colorspace == 'OkLab' && Utils::compare(-0.55, $value, 0.55, Utils::EPSILON_SMALL)) {
|
||||
throw new \LengthException(
|
||||
'Argument value ' . $value . ' for a is not in the range of -0.5 to 0.5 for OkLab',
|
||||
2
|
||||
);
|
||||
}
|
||||
break;
|
||||
case 'b':
|
||||
// if ($this->colorspace == 'CIELab' && ($value < -125 || $value > 125)) {
|
||||
if ($this->colorspace == 'CIELab' && Utils::compare(-125.0, $value, 125.0, Utils::EPSILON_SMALL)) {
|
||||
throw new \LengthException(
|
||||
'Argument value ' . $value . ' for b is not in the range of -125 to 125 for CIE Lab',
|
||||
3
|
||||
);
|
||||
// } elseif ($this->colorspace == 'OkLab' && ($value < -0.55 || $value > 0.55)) {
|
||||
} elseif ($this->colorspace == 'OkLab' && Utils::compare(-0.55, $value, 0.55, Utils::EPSILON_SMALL)) {
|
||||
throw new \LengthException(
|
||||
'Argument value ' . $value . ' for b is not in the range of -0.5 to 0.5 for OkLab',
|
||||
3
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
$this->$name = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* get color
|
||||
*
|
||||
* @param string $name
|
||||
* @return float
|
||||
*/
|
||||
public function get(string $name): float|string|bool
|
||||
{
|
||||
if (!property_exists($this, $name)) {
|
||||
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||
}
|
||||
return $this->$name;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the colorspace
|
||||
*
|
||||
* @param string $colorspace
|
||||
* @return self
|
||||
*/
|
||||
private function setColorspace(string $colorspace): self
|
||||
{
|
||||
if (!in_array($colorspace, $this::COLORSPACES)) {
|
||||
throw new \InvalidArgumentException('Not allowed colorspace', 0);
|
||||
}
|
||||
$this->colorspace = $colorspace;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the color as array
|
||||
* where 0: Lightness, 1: a, 2: b
|
||||
*
|
||||
* @return array{0:float,1:float,2:float}
|
||||
*/
|
||||
public function returnAsArray(): array
|
||||
{
|
||||
return [$this->L, $this->a, $this->b];
|
||||
}
|
||||
|
||||
/**
|
||||
* set color as array
|
||||
* where 0: Lightness, 1: a, 2: b
|
||||
*
|
||||
* @param array{0:float,1:float,2:float} $colors
|
||||
* @return self
|
||||
*/
|
||||
private function setFromArray(array $colors): self
|
||||
{
|
||||
$this->set('L', $colors[0]);
|
||||
$this->set('a', $colors[1]);
|
||||
$this->set('b', $colors[2]);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert into css string with optional opacity
|
||||
*
|
||||
* @param null|float|string|null $opacity
|
||||
* @return string
|
||||
*/
|
||||
public function toCssString(null|float|string $opacity = null): string
|
||||
{
|
||||
$string = '';
|
||||
switch ($this->colorspace) {
|
||||
case 'CIELab':
|
||||
$string = 'lab';
|
||||
break;
|
||||
case 'OkLab':
|
||||
$string = 'oklab';
|
||||
break;
|
||||
}
|
||||
$string .= '('
|
||||
. $this->L
|
||||
. ' '
|
||||
. $this->a
|
||||
. ' '
|
||||
. $this->b
|
||||
. Utils::setOpacity($opacity)
|
||||
. ');';
|
||||
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
329
www/lib/CoreLibs/Convert/Color/Coordinates/RGB.php
Normal file
329
www/lib/CoreLibs/Convert/Color/Coordinates/RGB.php
Normal file
@@ -0,0 +1,329 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* AUTHOR: Clemens Schwaighofer
|
||||
* CREATED: 2024/11/11
|
||||
* DESCRIPTION:
|
||||
* Color Coordinate: RGB
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Convert\Color\Coordinates;
|
||||
|
||||
use CoreLibs\Convert\Color\Utils;
|
||||
|
||||
class RGB implements Interface\CoordinatesInterface
|
||||
{
|
||||
/** @var array<string> allowed colorspaces */
|
||||
private const COLORSPACES = ['sRGB'];
|
||||
|
||||
/** @var float red 0 to 255 or 0.0f to 1.0f for linear RGB */
|
||||
private float $R = 0.0;
|
||||
/** @var float green 0 to 255 or 0.0f to 1.0f for linear RGB */
|
||||
private float $G = 0.0;
|
||||
/** @var float blue 0 to 255 or 0.0f to 1.0f for linear RGB */
|
||||
private float $B = 0.0;
|
||||
|
||||
/** @var string color space: either ok or cie */
|
||||
private string $colorspace = '';
|
||||
|
||||
/** @var bool set if this is linear */
|
||||
private bool $linear = false;
|
||||
|
||||
/**
|
||||
* Color Coordinate RGB
|
||||
* @param array{0:float,1:float,2:float}|string $colors RGB color array or hex string
|
||||
* @param string $colorspace [default=sRGB]
|
||||
* @param array<string,bool> $options [default=[]] only "linear" allowed at the moment
|
||||
*/
|
||||
public function __construct(string|array $colors, string $colorspace = 'sRGB', array $options = [])
|
||||
{
|
||||
$this->setColorspace($colorspace)->parseOptions($options);
|
||||
if (is_array($colors)) {
|
||||
$this->setFromArray($colors);
|
||||
} else {
|
||||
$this->setFromHex($colors);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* set from array or string
|
||||
* where 0: Red, 1: Green, 2: Blue
|
||||
* OR #ffffff or ffffff
|
||||
*
|
||||
* @param array{0:float,1:float,2:float}|string $colors RGB color array or hex string
|
||||
* @param string $colorspace [default=sRGB]
|
||||
* @param array<string,bool> $options [default=[]] only "linear" allowed at the moment
|
||||
* @return self
|
||||
*/
|
||||
public static function create(string|array $colors, string $colorspace = 'sRGB', array $options = []): self
|
||||
{
|
||||
return new RGB($colors, $colorspace, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* parse options
|
||||
*
|
||||
* @param array<string,bool> $options
|
||||
* @return self
|
||||
*/
|
||||
private function parseOptions(array $options): self
|
||||
{
|
||||
$this->flagLinear($options['linear'] ?? false);
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* set color
|
||||
*
|
||||
* @param string $name
|
||||
* @param float $value
|
||||
* @return void
|
||||
*/
|
||||
private function set(string $name, float $value): void
|
||||
{
|
||||
// do not allow setting linear from outside
|
||||
if ($name == 'linear') {
|
||||
return;
|
||||
}
|
||||
if (!property_exists($this, $name)) {
|
||||
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||
}
|
||||
// if not linear
|
||||
if (!$this->linear && ((int)$value < 0 || (int)$value > 255)) {
|
||||
throw new \LengthException('Argument value ' . $value . ' for color ' . $name
|
||||
. ' is not in the range of 0 to 255', 1);
|
||||
} elseif (
|
||||
// $this->linear && ($value < 0.0 || $value > 1.0)
|
||||
$this->linear && Utils::compare(0.0, $value, 1.0, 0.000001)
|
||||
) {
|
||||
throw new \LengthException('Argument value ' . $value . ' for color ' . $name
|
||||
. ' is not in the range of 0 to 1 for linear rgb', 2);
|
||||
}
|
||||
$this->$name = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* get color
|
||||
*
|
||||
* @param string $name
|
||||
* @return float|bool
|
||||
*/
|
||||
public function get(string $name): float|string|bool
|
||||
{
|
||||
if (!property_exists($this, $name)) {
|
||||
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||
}
|
||||
return $this->$name;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the colorspace
|
||||
*
|
||||
* @param string $colorspace
|
||||
* @return self
|
||||
*/
|
||||
private function setColorspace(string $colorspace): self
|
||||
{
|
||||
if (!in_array($colorspace, $this::COLORSPACES)) {
|
||||
throw new \InvalidArgumentException('Not allowed colorspace', 0);
|
||||
}
|
||||
$this->colorspace = $colorspace;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the color as array
|
||||
* where 0: Red, 1: Green, 2: Blue
|
||||
*
|
||||
* @return array{0:float,1:float,2:float}
|
||||
*/
|
||||
public function returnAsArray(): array
|
||||
{
|
||||
return [$this->R, $this->G, $this->B];
|
||||
}
|
||||
|
||||
/**
|
||||
* set color as array
|
||||
* where 0: Red, 1: Green, 2: Blue
|
||||
*
|
||||
* @param array{0:float,1:float,2:float} $colors
|
||||
* @return self
|
||||
*/
|
||||
private function setFromArray(array $colors): self
|
||||
{
|
||||
$this->set('R', $colors[0]);
|
||||
$this->set('G', $colors[1]);
|
||||
$this->set('B', $colors[2]);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return current set RGB as hex string. with or without # prefix
|
||||
*
|
||||
* @param bool $hex_prefix
|
||||
* @return string
|
||||
*/
|
||||
public function returnAsHex(bool $hex_prefix = true): string
|
||||
{
|
||||
// prefix
|
||||
$hex_color = '';
|
||||
if ($hex_prefix === true) {
|
||||
$hex_color = '#';
|
||||
}
|
||||
// convert if in linear
|
||||
if ($this->linear) {
|
||||
$this->fromLinear();
|
||||
}
|
||||
foreach ($this->returnAsArray() as $color) {
|
||||
$hex_color .= str_pad(dechex((int)$color), 2, '0', STR_PAD_LEFT);
|
||||
}
|
||||
return $hex_color;
|
||||
}
|
||||
|
||||
/**
|
||||
* set colors RGB from hex string
|
||||
*
|
||||
* @param string $hex_string
|
||||
* @return self
|
||||
*/
|
||||
private function setFromHex(string $hex_string): self
|
||||
{
|
||||
$hex_string = preg_replace("/[^0-9A-Fa-f]/", '', $hex_string); // Gets a proper hex string
|
||||
if (empty($hex_string) || !is_string($hex_string)) {
|
||||
throw new \InvalidArgumentException('hex_string argument cannot be empty', 3);
|
||||
}
|
||||
$rgbArray = [];
|
||||
if (strlen($hex_string) == 6) {
|
||||
// If a proper hex code, convert using bitwise operation.
|
||||
// No overhead... faster
|
||||
$colorVal = hexdec($hex_string);
|
||||
$rgbArray = [
|
||||
0xFF & ($colorVal >> 0x10),
|
||||
0xFF & ($colorVal >> 0x8),
|
||||
0xFF & $colorVal
|
||||
];
|
||||
} elseif (strlen($hex_string) == 3) {
|
||||
// If shorthand notation, need some string manipulations
|
||||
$rgbArray = [
|
||||
hexdec(str_repeat(substr($hex_string, 0, 1), 2)),
|
||||
hexdec(str_repeat(substr($hex_string, 1, 1), 2)),
|
||||
hexdec(str_repeat(substr($hex_string, 2, 1), 2))
|
||||
];
|
||||
} else {
|
||||
// Invalid hex color code
|
||||
throw new \UnexpectedValueException('Invalid hex_string: ' . $hex_string, 4);
|
||||
}
|
||||
return $this->setFromArray($rgbArray);
|
||||
}
|
||||
|
||||
/**
|
||||
* set as linear
|
||||
* can be used as chain call on create if input is linear RGB
|
||||
* RGB::__construct**(...)->flagLinear();
|
||||
* as it returns self
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
private function flagLinear(bool $linear): self
|
||||
{
|
||||
$this->linear = $linear;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Both function source:
|
||||
* https://bottosson.github.io/posts/colorwrong/#what-can-we-do%3F
|
||||
* but reverse f: fromLinear and f_inv for toLinear
|
||||
* Code copied from here:
|
||||
* https://stackoverflow.com/a/12894053
|
||||
*
|
||||
* converts RGB to linear
|
||||
* We come from 0-255 so we need to divide by 255
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function toLinear(): self
|
||||
{
|
||||
// if linear, as is
|
||||
if ($this->linear) {
|
||||
return $this;
|
||||
}
|
||||
$this->flagLinear(true)->setFromArray(array_map(
|
||||
callback: function (int|float $v) {
|
||||
$v = (float)($v / 255);
|
||||
$abs = abs($v);
|
||||
$sign = ($v < 0) ? -1 : 1;
|
||||
return (float)(
|
||||
$abs <= 0.04045 ?
|
||||
$v / 12.92 :
|
||||
$sign * pow(($abs + 0.055) / 1.055, 2.4)
|
||||
);
|
||||
},
|
||||
array: $this->returnAsArray(),
|
||||
));
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* convert back to normal sRGB from linear RGB
|
||||
* we go to 0-255 rgb so we multiply by 255
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function fromLinear(): self
|
||||
{
|
||||
// if not linear, as is
|
||||
if (!$this->linear) {
|
||||
return $this;
|
||||
}
|
||||
$this->flagLinear(false)->setFromArray(array_map(
|
||||
callback: function (int|float $v) {
|
||||
$abs = abs($v);
|
||||
$sign = ($v < 0) ? -1 : 1;
|
||||
// during reverse in some situations the values can become negative in very small ways
|
||||
// like -...E16 and ...E17
|
||||
return ($ret = (float)(255 * (
|
||||
$abs <= 0.0031308 ?
|
||||
$v * 12.92 :
|
||||
$sign * (1.055 * pow($abs, 1.0 / 2.4) - 0.055)
|
||||
))) < 0 ? 0 : $ret;
|
||||
},
|
||||
array: $this->returnAsArray(),
|
||||
));
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* convert to css string with optional opacity
|
||||
* Note: if this is a linear RGB, the data will converted during this operation and the converted back
|
||||
*
|
||||
* @param float|string|null $opacity
|
||||
* @return string
|
||||
*/
|
||||
public function toCssString(null|float|string $opacity = null): string
|
||||
{
|
||||
// if we are in linear mode, convert to normal mode temporary
|
||||
$was_linear = false;
|
||||
if ($this->linear) {
|
||||
$this->fromLinear();
|
||||
$was_linear = true;
|
||||
}
|
||||
$string = 'rgb('
|
||||
. (int)round($this->R, 0)
|
||||
. ' '
|
||||
. (int)round($this->G, 0)
|
||||
. ' '
|
||||
. (int)round($this->B, 0)
|
||||
. Utils::setOpacity($opacity)
|
||||
. ')';
|
||||
if ($was_linear) {
|
||||
$this->toLinear();
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
202
www/lib/CoreLibs/Convert/Color/Coordinates/XYZ.php
Normal file
202
www/lib/CoreLibs/Convert/Color/Coordinates/XYZ.php
Normal file
@@ -0,0 +1,202 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* AUTHOR: Clemens Schwaighofer
|
||||
* CREATED: 2024/11/11
|
||||
* DESCRIPTION:
|
||||
* Color Coordinate: XYZ (Cie) (colorspace CIEXYZ)
|
||||
* Note, this is only for the D50 & D65 whitepoint conversion
|
||||
* https://en.wikipedia.org/wiki/CIE_1931_color_space#Construction_of_the_CIE_XYZ_color_space_from_the_Wright%E2%80%93Guild_data
|
||||
* https://en.wikipedia.org/wiki/Standard_illuminant#Illuminant_series_D
|
||||
* https://en.wikipedia.org/wiki/Standard_illuminant#D65_values
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Convert\Color\Coordinates;
|
||||
|
||||
// use CoreLibs\Convert\Color\Utils;
|
||||
|
||||
class XYZ implements Interface\CoordinatesInterface
|
||||
{
|
||||
/** @var array<string> allowed colorspaces */
|
||||
private const COLORSPACES = ['CIEXYZ'];
|
||||
/** @var array<string> allowed whitepoints
|
||||
* D50: ICC profile PCS (horizon light) <-> CieLab
|
||||
* D65: RGB color space (noon) <-> linear RGB
|
||||
*/
|
||||
private const ILLUMINANT = ['D50', 'D65'];
|
||||
|
||||
/** @var float X coordinate */
|
||||
private float $X = 0.0;
|
||||
/** @var float Y coordinate (Luminance) */
|
||||
private float $Y = 0.0;
|
||||
/** @var float Z coordinate (blue) */
|
||||
private float $Z = 0.0;
|
||||
|
||||
/** @var string color space: either ok or cie */
|
||||
private string $colorspace = '';
|
||||
|
||||
/** @var string illuminat white point: only D50 and D65 are allowed */
|
||||
private string $whitepoint = '';
|
||||
|
||||
/**
|
||||
* Color Coordinate Lch
|
||||
* for oklch conversion
|
||||
*
|
||||
* @param string|array{0:float,1:float,2:float} $colors
|
||||
* @param string $colorspace [default=CIEXYZ]
|
||||
* @param array<string,string> $options [default=[]] Only "whitepoint" option allowed
|
||||
* @throws \InvalidArgumentException only array colors allowed
|
||||
*/
|
||||
public function __construct(
|
||||
string|array $colors,
|
||||
string $colorspace = 'CIEXYZ',
|
||||
array $options = [],
|
||||
) {
|
||||
if (!is_array($colors)) {
|
||||
throw new \InvalidArgumentException('Only array colors allowed', 0);
|
||||
}
|
||||
$this->setColorspace($colorspace)
|
||||
->parseOptions($options)
|
||||
->setFromArray($colors);
|
||||
}
|
||||
|
||||
/**
|
||||
* set from array
|
||||
* where 0: X, 1: Y, 2: Z
|
||||
*
|
||||
* @param array{0:float,1:float,2:float} $colors
|
||||
* @param string $colorspace [default=CIEXYZ]
|
||||
* @param array<string,string> $options [default=[]] Only "whitepoint" option allowed
|
||||
* @return self
|
||||
*/
|
||||
public static function create(
|
||||
string|array $colors,
|
||||
string $colorspace = 'CIEXYZ',
|
||||
array $options = [],
|
||||
): self {
|
||||
return new XYZ($colors, $colorspace, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* parse options
|
||||
*
|
||||
* @param array<string,string> $options
|
||||
* @return self
|
||||
*/
|
||||
private function parseOptions(array $options): self
|
||||
{
|
||||
$this->setWhitepoint($options['whitepoint'] ?? '');
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* set color
|
||||
*
|
||||
* @param string $name
|
||||
* @param float $value
|
||||
* @return void
|
||||
*/
|
||||
private function set(string $name, float $value): void
|
||||
{
|
||||
if (!property_exists($this, $name)) {
|
||||
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||
}
|
||||
// TODO: setup XYZ value limits
|
||||
// X: 0 to 95.047, Y: 0 to 100, Z: 0 to 108.88
|
||||
// if (Utils::compare(0.0, $value, 100.0, Utils::EPSILON_SMALL))) {
|
||||
// throw new \LengthException('Argument value ' . $value . ' for color ' . $name
|
||||
// . ' is not in the range of 0 to 100.0', 1);
|
||||
// }
|
||||
$this->$name = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* get color
|
||||
*
|
||||
* @param string $name
|
||||
* @return float
|
||||
*/
|
||||
public function get(string $name): float|string|bool
|
||||
{
|
||||
if (!property_exists($this, $name)) {
|
||||
throw new \ErrorException('Creation of dynamic property is not allowed', 0);
|
||||
}
|
||||
return $this->$name;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the colorspace
|
||||
*
|
||||
* @param string $colorspace
|
||||
* @return self
|
||||
*/
|
||||
private function setColorspace(string $colorspace): self
|
||||
{
|
||||
if (!in_array($colorspace, $this::COLORSPACES)) {
|
||||
throw new \InvalidArgumentException('Not allowed colorspace', 0);
|
||||
}
|
||||
$this->colorspace = $colorspace;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the whitepoint flag
|
||||
*
|
||||
* @param string $whitepoint
|
||||
* @return self
|
||||
*/
|
||||
private function setWhitepoint(string $whitepoint): self
|
||||
{
|
||||
if (empty($whitepoint)) {
|
||||
$this->whitepoint = '';
|
||||
return $this;
|
||||
}
|
||||
if (!in_array($whitepoint, $this::ILLUMINANT)) {
|
||||
throw new \InvalidArgumentException('Not allowed whitepoint', 0);
|
||||
}
|
||||
$this->whitepoint = $whitepoint;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the color as array
|
||||
* where 0: X, 1: Y, 2: Z
|
||||
*
|
||||
* @return array{0:float,1:float,2:float}
|
||||
*/
|
||||
public function returnAsArray(): array
|
||||
{
|
||||
return [$this->X, $this->Y, $this->Z];
|
||||
}
|
||||
|
||||
/**
|
||||
* set color as array
|
||||
* where 0: X, 1: Y, 2: Z
|
||||
*
|
||||
* @param array{0:float,1:float,2:float} $colors
|
||||
* @return self
|
||||
*/
|
||||
private function setFromArray(array $colors): self
|
||||
{
|
||||
$this->set('X', $colors[0]);
|
||||
$this->set('Y', $colors[1]);
|
||||
$this->set('Z', $colors[2]);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* no hsb in css
|
||||
*
|
||||
* @param float|string|null $opacity
|
||||
* @return string
|
||||
* @throws \ErrorException
|
||||
*/
|
||||
public function toCssString(null|float|string $opacity = null): string
|
||||
{
|
||||
throw new \ErrorException('XYZ is not available as CSS color string', 0);
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
35
www/lib/CoreLibs/Convert/Color/Stringify.php
Normal file
35
www/lib/CoreLibs/Convert/Color/Stringify.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* AUTHOR: Clemens Schwaighofer
|
||||
* CREATED: 2024/11/11
|
||||
* DESCRIPTION:
|
||||
* Convert color coordinate to CSS string
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Convert\Color;
|
||||
|
||||
use CoreLibs\Convert\Color\Coordinates\RGB;
|
||||
use CoreLibs\Convert\Color\Coordinates\HSL;
|
||||
use CoreLibs\Convert\Color\Coordinates\HWB;
|
||||
use CoreLibs\Convert\Color\Coordinates\Lab;
|
||||
use CoreLibs\Convert\Color\Coordinates\LCH;
|
||||
|
||||
class Stringify
|
||||
{
|
||||
/**
|
||||
* return the CSS string including optional opacity
|
||||
*
|
||||
* @param RGB|Lab|LCH|HSL|HWB $data
|
||||
* @param null|float|string $opacity
|
||||
* @return string
|
||||
*/
|
||||
public static function toCssString(RGB|Lab|LCH|HSL|HWB $data, null|float|string $opacity): string
|
||||
{
|
||||
return $data->toCssString($opacity);
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
56
www/lib/CoreLibs/Convert/Color/Utils.php
Normal file
56
www/lib/CoreLibs/Convert/Color/Utils.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* AUTHOR: Clemens Schwaighofer
|
||||
* CREATED: 2024/11/14
|
||||
* DESCRIPTION:
|
||||
* Utils for color
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Convert\Color;
|
||||
|
||||
use CoreLibs\Convert\Math;
|
||||
|
||||
class Utils
|
||||
{
|
||||
/** @var float deviation allowed for valid data checks, small */
|
||||
public const EPSILON_SMALL = 0.000000000001;
|
||||
/** @var float deviation allowed for valid data checks, medium */
|
||||
public const EPSILON_MEDIUM = 0.0000001;
|
||||
/** @var float deviation allowed for valid data checks, big */
|
||||
public const ESPILON_BIG = 0.0001;
|
||||
|
||||
public static function compare(float $lower, float $value, float $upper, float $epslion): bool
|
||||
{
|
||||
if (
|
||||
Math::compareWithEpsilon($value, '<', $lower, $epslion) ||
|
||||
Math::compareWithEpsilon($value, '>', $upper, $epslion)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the opactiy sub string part and return it
|
||||
*
|
||||
* @param null|float|string|null $opacity
|
||||
* @return string
|
||||
*/
|
||||
public static function setOpacity(null|float|string $opacity = null): string
|
||||
{
|
||||
// set opacity, either a string or float
|
||||
if (is_string($opacity)) {
|
||||
$opacity = ' / ' . $opacity;
|
||||
} elseif ($opacity !== null) {
|
||||
$opacity = ' / ' . $opacity;
|
||||
} else {
|
||||
$opacity = '';
|
||||
}
|
||||
return $opacity;
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
@@ -17,6 +17,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\Convert;
|
||||
|
||||
use CoreLibs\Convert\Color\Color;
|
||||
use CoreLibs\Convert\Color\Coordinates;
|
||||
|
||||
class Colors
|
||||
{
|
||||
/**
|
||||
@@ -30,6 +33,7 @@ class Colors
|
||||
* @param bool $hex_prefix default true, prefix with "#"
|
||||
* @return string rgb in hex values with leading # if set,
|
||||
* @throws \LengthException If any argument is not in the range of 0~255
|
||||
* @deprecated v9.20.0 use: new Coordinates\RGB([$red, $green, $blue]))->returnAsHex(true/false for #)
|
||||
*/
|
||||
public static function rgb2hex(
|
||||
int $red,
|
||||
@@ -37,20 +41,7 @@ class Colors
|
||||
int $blue,
|
||||
bool $hex_prefix = true
|
||||
): string {
|
||||
$hex_color = '';
|
||||
if ($hex_prefix === true) {
|
||||
$hex_color = '#';
|
||||
}
|
||||
foreach (['red', 'green', 'blue'] as $color) {
|
||||
// if not valid, abort
|
||||
if ($$color < 0 || $$color > 255) {
|
||||
throw new \LengthException('Argument value ' . $$color . ' for color ' . $color
|
||||
. ' is not in the range of 0 to 255', 1);
|
||||
}
|
||||
// pad left with 0
|
||||
$hex_color .= str_pad(dechex($$color), 2, '0', STR_PAD_LEFT);
|
||||
}
|
||||
return $hex_color;
|
||||
return (new Coordinates\RGB([$red, $green, $blue]))->returnAsHex($hex_prefix);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,32 +54,29 @@ class Colors
|
||||
* or a string with the seperator
|
||||
* @throws \InvalidArgumentException if hex string is empty
|
||||
* @throws \UnexpectedValueException if the hex string value is not valid
|
||||
* @deprecated v9.20.0 use: new Coordinates\RGB($hex_string) (build string/array from return data)
|
||||
*/
|
||||
public static function hex2rgb(
|
||||
string $hex_string,
|
||||
bool $return_as_string = false,
|
||||
string $seperator = ','
|
||||
): string|array {
|
||||
$hex_string = preg_replace("/[^0-9A-Fa-f]/", '', $hex_string); // Gets a proper hex string
|
||||
if (!is_string($hex_string)) {
|
||||
throw new \InvalidArgumentException('hex_string argument cannot be empty', 1);
|
||||
}
|
||||
$rgbArray = [];
|
||||
if (strlen($hex_string) == 6) {
|
||||
// If a proper hex code, convert using bitwise operation.
|
||||
// No overhead... faster
|
||||
$colorVal = hexdec($hex_string);
|
||||
$rgbArray['r'] = 0xFF & ($colorVal >> 0x10);
|
||||
$rgbArray['g'] = 0xFF & ($colorVal >> 0x8);
|
||||
$rgbArray['b'] = 0xFF & $colorVal;
|
||||
} elseif (strlen($hex_string) == 3) {
|
||||
// If shorthand notation, need some string manipulations
|
||||
$rgbArray['r'] = hexdec(str_repeat(substr($hex_string, 0, 1), 2));
|
||||
$rgbArray['g'] = hexdec(str_repeat(substr($hex_string, 1, 1), 2));
|
||||
$rgbArray['b'] = hexdec(str_repeat(substr($hex_string, 2, 1), 2));
|
||||
} else {
|
||||
// Invalid hex color code
|
||||
throw new \UnexpectedValueException('Invalid hex_string: ' . $hex_string, 2);
|
||||
// rewrite to previous r/g/b key output
|
||||
foreach ((new Coordinates\RGB($hex_string))->returnAsArray() as $p => $el) {
|
||||
$k = '';
|
||||
switch ($p) {
|
||||
case 0:
|
||||
$k = 'r';
|
||||
break;
|
||||
case 1:
|
||||
$k = 'g';
|
||||
break;
|
||||
case 2:
|
||||
$k = 'b';
|
||||
break;
|
||||
}
|
||||
$rgbArray[$k] = (int)round($el);
|
||||
}
|
||||
// returns the rgb string or the associative array
|
||||
return $return_as_string ? implode($seperator, $rgbArray) : $rgbArray;
|
||||
@@ -105,42 +93,16 @@ class Colors
|
||||
* @param int $blue blue 0-255
|
||||
* @return array<int|float> Hue, Sat, Brightness/Value
|
||||
* @throws \LengthException If any argument is not in the range of 0~255
|
||||
* @deprecated v9.20.0 use: Color::rgbToHsb(...)->returnAsArray() will return float unrounded
|
||||
*/
|
||||
public static function rgb2hsb(int $red, int $green, int $blue): array
|
||||
{
|
||||
// check that rgb is from 0 to 255
|
||||
foreach (['red', 'green', 'blue'] as $color) {
|
||||
if ($$color < 0 || $$color > 255) {
|
||||
throw new \LengthException('Argument value ' . $$color . ' for color ' . $color
|
||||
. ' is not in the range of 0 to 255', 1);
|
||||
}
|
||||
$$color = $$color / 255;
|
||||
}
|
||||
|
||||
$MAX = max($red, $green, $blue);
|
||||
$MIN = min($red, $green, $blue);
|
||||
$HUE = 0;
|
||||
|
||||
if ($MAX == $MIN) {
|
||||
return [0, 0, round($MAX * 100)];
|
||||
}
|
||||
if ($red == $MAX) {
|
||||
$HUE = ($green - $blue) / ($MAX - $MIN);
|
||||
} elseif ($green == $MAX) {
|
||||
$HUE = 2 + (($blue - $red) / ($MAX - $MIN));
|
||||
} elseif ($blue == $MAX) {
|
||||
$HUE = 4 + (($red - $green) / ($MAX - $MIN));
|
||||
}
|
||||
$HUE *= 60;
|
||||
if ($HUE < 0) {
|
||||
$HUE += 360;
|
||||
}
|
||||
|
||||
return [
|
||||
(int)round($HUE),
|
||||
(int)round((($MAX - $MIN) / $MAX) * 100),
|
||||
(int)round($MAX * 100)
|
||||
];
|
||||
return array_map(
|
||||
fn ($v) => (int)round($v),
|
||||
Color::rgbToHsb(
|
||||
new Coordinates\RGB([$red, $green, $blue])
|
||||
)->returnAsArray()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -153,80 +115,16 @@ class Colors
|
||||
* @param float $V brightness/value 0-100 (int)
|
||||
* @return array<int> 0 red/1 green/2 blue array as 0-255
|
||||
* @throws \LengthException If any argument is not in the valid range
|
||||
* @deprecated v9.20.0 use: Color::hsbToRgb(...)->returnAsArray() will return float unrounded
|
||||
*/
|
||||
public static function hsb2rgb(float $H, float $S, float $V): array
|
||||
{
|
||||
// check that H is 0 to 359, 360 = 0
|
||||
// and S and V are 0 to 1
|
||||
if ($H == 360) {
|
||||
$H = 0;
|
||||
}
|
||||
if ($H < 0 || $H > 359) {
|
||||
throw new \LengthException('Argument value ' . $H . ' for hue is not in the range of 0 to 359', 1);
|
||||
}
|
||||
if ($S < 0 || $S > 100) {
|
||||
throw new \LengthException('Argument value ' . $S . ' for saturation is not in the range of 0 to 100', 2);
|
||||
}
|
||||
if ($V < 0 || $V > 100) {
|
||||
throw new \LengthException('Argument value ' . $V . ' for brightness is not in the range of 0 to 100', 3);
|
||||
}
|
||||
// convert to internal 0-1 format
|
||||
$S /= 100;
|
||||
$V /= 100;
|
||||
|
||||
if ($S == 0) {
|
||||
$V = (int)round($V * 255);
|
||||
return [$V, $V, $V];
|
||||
}
|
||||
|
||||
$Hi = floor($H / 60);
|
||||
$f = ($H / 60) - $Hi;
|
||||
$p = $V * (1 - $S);
|
||||
$q = $V * (1 - ($S * $f));
|
||||
$t = $V * (1 - ($S * (1 - $f)));
|
||||
|
||||
switch ($Hi) {
|
||||
case 0:
|
||||
$red = $V;
|
||||
$green = $t;
|
||||
$blue = $p;
|
||||
break;
|
||||
case 1:
|
||||
$red = $q;
|
||||
$green = $V;
|
||||
$blue = $p;
|
||||
break;
|
||||
case 2:
|
||||
$red = $p;
|
||||
$green = $V;
|
||||
$blue = $t;
|
||||
break;
|
||||
case 3:
|
||||
$red = $p;
|
||||
$green = $q;
|
||||
$blue = $V;
|
||||
break;
|
||||
case 4:
|
||||
$red = $t;
|
||||
$green = $p;
|
||||
$blue = $V;
|
||||
break;
|
||||
case 5:
|
||||
$red = $V;
|
||||
$green = $p;
|
||||
$blue = $q;
|
||||
break;
|
||||
default:
|
||||
$red = 0;
|
||||
$green = 0;
|
||||
$blue = 0;
|
||||
}
|
||||
|
||||
return [
|
||||
(int)round($red * 255),
|
||||
(int)round($green * 255),
|
||||
(int)round($blue * 255)
|
||||
];
|
||||
return array_map(
|
||||
fn ($v) => (int)round($v),
|
||||
Color::hsbToRgb(
|
||||
new Coordinates\HSB([$H, $S, $V])
|
||||
)->returnAsArray()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -239,50 +137,16 @@ class Colors
|
||||
* @param int $blue blue 0-255
|
||||
* @return array<float> hue/sat/luminance
|
||||
* @throws \LengthException If any argument is not in the range of 0~255
|
||||
* @deprecated v9.20.0 use: Color::rgbToHsl(...)->returnAsArray() will return float unrounded
|
||||
*/
|
||||
public static function rgb2hsl(int $red, int $green, int $blue): array
|
||||
{
|
||||
// check that rgb is from 0 to 255
|
||||
foreach (['red', 'green', 'blue'] as $color) {
|
||||
if ($$color < 0 || $$color > 255) {
|
||||
throw new \LengthException('Argument value ' . $$color . ' for color ' . $color
|
||||
. ' is not in the range of 0 to 255', 1);
|
||||
}
|
||||
$$color = $$color / 255;
|
||||
}
|
||||
|
||||
$min = min($red, $green, $blue);
|
||||
$max = max($red, $green, $blue);
|
||||
$chroma = $max - $min;
|
||||
$sat = 0;
|
||||
$hue = 0;
|
||||
// luminance
|
||||
$lum = ($max + $min) / 2;
|
||||
|
||||
// achromatic
|
||||
if ($chroma == 0) {
|
||||
// H, S, L
|
||||
return [0.0, 0.0, round($lum * 100, 1)];
|
||||
} else {
|
||||
$sat = $chroma / (1 - abs(2 * $lum - 1));
|
||||
if ($max == $red) {
|
||||
$hue = fmod((($green - $blue) / $chroma), 6);
|
||||
if ($hue < 0) {
|
||||
$hue = (6 - fmod(abs($hue), 6));
|
||||
}
|
||||
} elseif ($max == $green) {
|
||||
$hue = ($blue - $red) / $chroma + 2;
|
||||
} elseif ($max == $blue) {
|
||||
$hue = ($red - $green) / $chroma + 4;
|
||||
}
|
||||
$hue = $hue * 60;
|
||||
// $sat = 1 - abs(2 * $lum - 1);
|
||||
return [
|
||||
round($hue, 1),
|
||||
round($sat * 100, 1),
|
||||
round($lum * 100, 1)
|
||||
];
|
||||
}
|
||||
return array_map(
|
||||
fn ($v) => round($v, 1),
|
||||
Color::rgbToHsl(
|
||||
new Coordinates\RGB([$red, $green, $blue])
|
||||
)->returnAsArray()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -294,57 +158,16 @@ class Colors
|
||||
* @param float $lum luminance: 0-100
|
||||
* @return array<int,float|int> red/blue/green 0-255 each
|
||||
* @throws \LengthException If any argument is not in the valid range
|
||||
* @deprecated v9.20.0 use: Color::hslToRgb(...)->returnAsArray() will return float unrounded
|
||||
*/
|
||||
public static function hsl2rgb(float $hue, float $sat, float $lum): array
|
||||
{
|
||||
if ($hue == 360) {
|
||||
$hue = 0;
|
||||
}
|
||||
if ($hue < 0 || $hue > 359) {
|
||||
throw new \LengthException('Argument value ' . $hue . ' for hue is not in the range of 0 to 359', 1);
|
||||
}
|
||||
if ($sat < 0 || $sat > 100) {
|
||||
throw new \LengthException('Argument value ' . $sat . ' for saturation is not in the range of 0 to 100', 2);
|
||||
}
|
||||
if ($lum < 0 || $lum > 100) {
|
||||
throw new \LengthException('Argument value ' . $lum . ' for luminance is not in the range of 0 to 100', 3);
|
||||
}
|
||||
// calc to internal convert value for hue
|
||||
$hue = (1 / 360) * $hue;
|
||||
// convert to internal 0-1 format
|
||||
$sat /= 100;
|
||||
$lum /= 100;
|
||||
// if saturation is 0
|
||||
if ($sat == 0) {
|
||||
$lum = (int)round($lum * 255);
|
||||
return [$lum, $lum, $lum];
|
||||
} else {
|
||||
$m2 = $lum < 0.5 ? $lum * ($sat + 1) : ($lum + $sat) - ($lum * $sat);
|
||||
$m1 = $lum * 2 - $m2;
|
||||
$hueue = function ($base) use ($m1, $m2) {
|
||||
// base = hue, hue > 360 (1) - 360 (1), else < 0 + 360 (1)
|
||||
$base = $base < 0 ? $base + 1 : ($base > 1 ? $base - 1 : $base);
|
||||
// 6: 60, 2: 180, 3: 240
|
||||
// 2/3 = 240
|
||||
// 1/3 = 120 (all from 360)
|
||||
if ($base * 6 < 1) {
|
||||
return $m1 + ($m2 - $m1) * $base * 6;
|
||||
}
|
||||
if ($base * 2 < 1) {
|
||||
return $m2;
|
||||
}
|
||||
if ($base * 3 < 2) {
|
||||
return $m1 + ($m2 - $m1) * ((2 / 3) - $base) * 6;
|
||||
}
|
||||
return $m1;
|
||||
};
|
||||
|
||||
return [
|
||||
(int)round(255 * $hueue($hue + (1 / 3))),
|
||||
(int)round(255 * $hueue($hue)),
|
||||
(int)round(255 * $hueue($hue - (1 / 3)))
|
||||
];
|
||||
}
|
||||
return array_map(
|
||||
fn ($v) => round($v),
|
||||
Color::hslToRgb(
|
||||
new Coordinates\HSL([$hue, $sat, $lum])
|
||||
)->returnAsArray()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -56,6 +56,180 @@ class Math
|
||||
return (float)$number;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* calc cube root
|
||||
*
|
||||
* @param float $number Number to cubic root
|
||||
* @return float Calculated value
|
||||
*/
|
||||
public static function cbrt(float|int $number): float
|
||||
{
|
||||
return pow((float)$number, 1.0 / 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* use PHP_FLOAT_EPSILON to compare if two float numbers are matching
|
||||
*
|
||||
* @param float $x
|
||||
* @param float $y
|
||||
* @param float $epsilon [default=PHP_FLOAT_EPSILON]
|
||||
* @return bool True equal
|
||||
*/
|
||||
public static function equalWithEpsilon(float $x, float $y, float $epsilon = PHP_FLOAT_EPSILON): bool
|
||||
{
|
||||
if (abs($x - $y) < $epsilon) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare two value base on direction given
|
||||
* The default delta is PHP_FLOAT_EPSILON
|
||||
*
|
||||
* @param float $value
|
||||
* @param string $compare
|
||||
* @param float $limit
|
||||
* @param float $epsilon [default=PHP_FLOAT_EPSILON]
|
||||
* @return bool True on smaller/large or equal
|
||||
*/
|
||||
public static function compareWithEpsilon(
|
||||
float $value,
|
||||
string $compare,
|
||||
float $limit,
|
||||
float $epsilon = PHP_FLOAT_EPSILON
|
||||
): bool {
|
||||
switch ($compare) {
|
||||
case '<':
|
||||
if ($value < ($limit - $epsilon)) {
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case '<=':
|
||||
if ($value <= ($limit - $epsilon)) {
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case '==':
|
||||
return self::equalWithEpsilon($value, $limit, $epsilon);
|
||||
case '>':
|
||||
if ($value > ($limit + $epsilon)) {
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case '>=':
|
||||
if ($value >= ($limit + $epsilon)) {
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is directly inspired by the multiplyMatrices() function in color.js
|
||||
* form Lea Verou and Chris Lilley.
|
||||
* (see https://github.com/LeaVerou/color.js/blob/main/src/multiply-matrices.js)
|
||||
* From:
|
||||
* https://github.com/matthieumastadenis/couleur/blob/3842cf51c9517e77afaa0a36ec78643a0c258e0b/src/utils/utils.php#L507
|
||||
*
|
||||
* It returns an array which is the product of the two number matrices passed as parameters.
|
||||
*
|
||||
* NOTE:
|
||||
* if the right side (B matrix) has a missing row, this row will be fillwed with 0 instead of
|
||||
* throwing an error:
|
||||
* A:
|
||||
* [
|
||||
* [1, 2, 3],
|
||||
* [4, 5, 6],
|
||||
* ]
|
||||
* B:
|
||||
* [
|
||||
* [7, 8, 9],
|
||||
* [10, 11, 12],
|
||||
* ]
|
||||
* The B will get a third row with [0, 0, 0] added to make the multiplication work as it will be
|
||||
* rewritten as
|
||||
* B-rewrite:
|
||||
* [
|
||||
* [7, 10, 0],
|
||||
* [8, 11, 12],
|
||||
* [0, 0, 0] <- automatically added
|
||||
* ]
|
||||
*
|
||||
* The same is done for unbalanced entries, they are filled with 0
|
||||
*
|
||||
* @param array<float|int|array<int|float>> $a m x n matrice
|
||||
* @param array<float|int|array<int|float>> $b n x p matrice
|
||||
*
|
||||
* @return array<float|int|array<int|float>> m x p product
|
||||
*/
|
||||
public static function multiplyMatrices(array $a, array $b): array
|
||||
{
|
||||
$m = count($a);
|
||||
|
||||
if (!is_array($a[0] ?? null)) {
|
||||
// $a is vector, convert to [[a, b, c, ...]]
|
||||
$a = [$a];
|
||||
}
|
||||
|
||||
if (!is_array($b[0])) {
|
||||
// $b is vector, convert to [[a], [b], [c], ...]]
|
||||
$b = array_map(
|
||||
callback: fn ($v) => [ $v ],
|
||||
array: $b,
|
||||
);
|
||||
}
|
||||
|
||||
$p = count($b[0]);
|
||||
|
||||
// transpose $b:
|
||||
// so that we can multiply row by row
|
||||
$bCols = array_map(
|
||||
callback: fn ($k) => array_map(
|
||||
(fn ($i) => is_array($i) ? $i[$k] ?? 0 : 0),
|
||||
$b,
|
||||
),
|
||||
array: array_keys($b[0]),
|
||||
);
|
||||
|
||||
$product = array_map(
|
||||
callback: fn ($row) => array_map(
|
||||
callback: fn ($col) => is_array($row) ?
|
||||
array_reduce(
|
||||
array: $row,
|
||||
callback: fn ($a, $v, $i = null) => $a + $v * (
|
||||
// if last entry missing for full copy add a 0 to it
|
||||
$col[$i ?? array_search($v, $row, true)] ?? 0 /** @phpstan-ignore-line */
|
||||
),
|
||||
initial: 0,
|
||||
) :
|
||||
array_reduce(
|
||||
array: $col,
|
||||
callback: fn ($a, $v) => $a + $v * $row,
|
||||
initial: 0,
|
||||
),
|
||||
array: $bCols,
|
||||
),
|
||||
array: $a,
|
||||
);
|
||||
|
||||
if ($m === 1) {
|
||||
// Avoid [[a, b, c, ...]]:
|
||||
return $product[0];
|
||||
}
|
||||
|
||||
if ($p === 1) {
|
||||
// Avoid [[a], [b], [c], ...]]:
|
||||
return array_map(
|
||||
callback: fn ($v) => $v[0] ?? 0,
|
||||
array: $product,
|
||||
);
|
||||
}
|
||||
|
||||
return $product;
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
|
||||
@@ -38,7 +38,7 @@ class Uids
|
||||
$uniqid_length++;
|
||||
}
|
||||
/** @var int<1,max> make sure that internal this is correct */
|
||||
$random_bytes_length = ($uniqid_length - ($uniqid_length % 2)) / 2;
|
||||
$random_bytes_length = (int)(($uniqid_length - ($uniqid_length % 2)) / 2);
|
||||
$uniqid = bin2hex(random_bytes($random_bytes_length));
|
||||
// if not forced shorten return next lower length
|
||||
if (!$force_length) {
|
||||
|
||||
@@ -374,7 +374,7 @@ class ArrayIO extends \CoreLibs\DB\IO
|
||||
public function dbDelete(array $table_array = [], bool $acl_limit = false): array
|
||||
{
|
||||
// is array and has values, override set and set new
|
||||
if (is_array($table_array) && count($table_array)) {
|
||||
if (count($table_array)) {
|
||||
$this->table_array = $table_array;
|
||||
}
|
||||
if (!$this->dbCheckPkSet()) {
|
||||
@@ -440,7 +440,7 @@ class ArrayIO extends \CoreLibs\DB\IO
|
||||
public function dbRead(bool $edit = false, array $table_array = []): array
|
||||
{
|
||||
// if array give, overrules internal array
|
||||
if (is_array($table_array) && count($table_array)) {
|
||||
if (count($table_array)) {
|
||||
$this->table_array = $table_array;
|
||||
}
|
||||
if (!$this->dbCheckPkSet()) {
|
||||
|
||||
@@ -284,7 +284,8 @@ class IO
|
||||
public const ERROR_HASH_TYPE = 'adler32';
|
||||
/** @var string regex to get returning with matches at position 1 */
|
||||
public const REGEX_RETURNING = '/\s+returning\s+(.+\s*(?:.+\s*)+);?$/i';
|
||||
/** @var array<string> allowed convert target for placeholder: pg or pdo (currently not available) */
|
||||
/** @var array<string> allowed convert target for placeholder:
|
||||
* pg or pdo (currently not available) */
|
||||
public const DB_CONVERT_PLACEHOLDER_TARGET = ['pg'];
|
||||
// REGEX_SELECT
|
||||
// REGEX_UPDATE
|
||||
@@ -823,6 +824,10 @@ class IO
|
||||
);
|
||||
break;
|
||||
default:
|
||||
// no context on DB_INFO
|
||||
if ($id == 'DB_INFO') {
|
||||
$context = [];
|
||||
}
|
||||
// used named arguments so we can easy change the order of debug
|
||||
$this->log->debug(
|
||||
group_id: $debug_id,
|
||||
@@ -910,7 +915,7 @@ class IO
|
||||
if ($cursor !== false) {
|
||||
[$db_prefix, $db_error_string] = $this->db_functions->__dbPrintError($cursor);
|
||||
}
|
||||
if ($cursor === false && method_exists($this->db_functions, '__dbPrintError')) {
|
||||
if ($cursor === false && method_exists($this->db_functions, '__dbPrintError')) { /** @phpstan-ignore-line */
|
||||
[$db_prefix, $db_error_string] = $this->db_functions->__dbPrintError();
|
||||
}
|
||||
// prefix the master if not the same
|
||||
@@ -1307,33 +1312,14 @@ class IO
|
||||
}
|
||||
|
||||
/**
|
||||
* count $ leading parameters only
|
||||
* count placeholder entries in the query
|
||||
*
|
||||
* @param string $query Query to check
|
||||
* @return int Number of parameters found
|
||||
*/
|
||||
private function __dbCountQueryParams(string $query): int
|
||||
{
|
||||
$match = [];
|
||||
// regex for params: only stand alone $number allowed
|
||||
// exclude all '' enclosed strings, ignore all numbers [note must start with digit]
|
||||
// can have space/tab/new line
|
||||
// must have <> = , ( [not equal, equal, comma, opening round bracket]
|
||||
// can have space/tab/new line
|
||||
// $ number with 1-9 for first and 0-9 for further digits
|
||||
// /s for matching new line in . list
|
||||
// [disabled, we don't used ^ or $] /m for multi line match
|
||||
// Matches in 1:, must be array_filtered to remove empty, count with array_unique
|
||||
$query_split = '[(=,?-]|->|->>|#>|#>>|@>|<@|\?\|\?\&|\|\||#-';
|
||||
preg_match_all(
|
||||
'/'
|
||||
. '(?:\'.*?\')?\s*(?:\?\?|<>|' . $query_split . ')\s*'
|
||||
. '(?:\d+|(?:\'.*?\')|(\$[1-9]{1}(?:[0-9]{1,})?))'
|
||||
. '/s',
|
||||
$query,
|
||||
$match
|
||||
);
|
||||
return count(array_unique(array_filter($match[1])));
|
||||
return $this->db_functions->__dbCountQueryParams($query);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1733,7 +1719,7 @@ class IO
|
||||
{
|
||||
if (
|
||||
!empty($this->dbh) &&
|
||||
$this->dbh instanceof \PgSql\Connection
|
||||
$this->dbh instanceof \PgSql\Connection /** @phpstan-ignore-line future could be other */
|
||||
) {
|
||||
// reset any client encodings set
|
||||
$this->dbResetEncoding();
|
||||
@@ -1814,14 +1800,13 @@ class IO
|
||||
$html_tags = ['{b}', '{/b}', '{br}'];
|
||||
$replace_html = ['<b>', '</b>', '<br>'];
|
||||
$replace_text = ['', '', ' **** '];
|
||||
$string = '';
|
||||
$string .= '{b}-DB-info->{/b} Connected to db {b}\'' . $this->db_name . '\'{/b} ';
|
||||
$string .= 'with schema {b}\'' . $this->db_schema . '\'{/b} ';
|
||||
$string .= 'as user {b}\'' . $this->db_user . '\'{/b} ';
|
||||
$string .= 'at host {b}\'' . $this->db_host . '\'{/b} ';
|
||||
$string .= 'on port {b}\'' . $this->db_port . '\'{/b} ';
|
||||
$string .= 'with ssl mode {b}\'' . $this->db_ssl . '\'{/b}{br}';
|
||||
$string .= '{b}-DB-info->{/b} DB IO Class debug output: {b}'
|
||||
$string = '{b}-DB-info->{/b} Connected to db {b}\'' . $this->db_name . '\'{/b} '
|
||||
. 'with schema {b}\'' . $this->db_schema . '\'{/b} '
|
||||
. 'as user {b}\'' . $this->db_user . '\'{/b} '
|
||||
. 'at host {b}\'' . $this->db_host . '\'{/b} '
|
||||
. 'on port {b}\'' . $this->db_port . '\'{/b} '
|
||||
. 'with ssl mode {b}\'' . $this->db_ssl . '\'{/b}{br}'
|
||||
. '{b}-DB-info->{/b} DB IO Class debug output: {b}'
|
||||
. ($this->dbGetDebug() ? 'Yes' : 'No') . '{/b}';
|
||||
if ($log === true) {
|
||||
// if debug, remove / change b
|
||||
@@ -1829,7 +1814,7 @@ class IO
|
||||
$html_tags,
|
||||
$replace_text,
|
||||
$string
|
||||
), 'dbInfo');
|
||||
), 'DB_INFO');
|
||||
} else {
|
||||
$string = $string . '{br}';
|
||||
}
|
||||
@@ -1985,7 +1970,7 @@ class IO
|
||||
if (is_array($array)) {
|
||||
$this->nbsp = '';
|
||||
$string .= $this->__printArray($array);
|
||||
$this->__dbDebugMessage('db', $string, 'dbDumpData');
|
||||
$this->__dbDebugMessage('db', $string, 'DB_INFO');
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
@@ -3157,7 +3142,8 @@ class IO
|
||||
'count' => 0,
|
||||
'query' => '',
|
||||
'result' => null,
|
||||
'returning_id' => false
|
||||
'returning_id' => false,
|
||||
'placeholder_converted' => [],
|
||||
];
|
||||
// if this is an insert query, check if we can add a return
|
||||
if ($this->dbCheckQueryForInsert($query, true)) {
|
||||
@@ -3197,6 +3183,39 @@ class IO
|
||||
$this->prepare_cursor[$stm_name]['pk_name'] = $pk_name;
|
||||
}
|
||||
}
|
||||
// QUERY PARAMS: run query params check and rewrite
|
||||
if ($this->dbGetConvertPlaceholder() === true) {
|
||||
try {
|
||||
$this->placeholder_converted = ConvertPlaceholder::convertPlaceholderInQuery(
|
||||
$query,
|
||||
null,
|
||||
$this->dbGetConvertPlaceholderTarget()
|
||||
);
|
||||
// write the new queries over the old
|
||||
if (!empty($this->placeholder_converted['query'])) {
|
||||
$query = $this->placeholder_converted['query'];
|
||||
}
|
||||
$this->prepare_cursor[$stm_name]['placeholder_converted'] = $this->placeholder_converted;
|
||||
} catch (\OutOfRangeException $e) {
|
||||
$this->__dbError($e->getCode(), context:[
|
||||
'statement_name' => $stm_name,
|
||||
'query' => $query,
|
||||
'location' => 'dbPrepare',
|
||||
'error' => 'OutOfRangeException',
|
||||
'exception' => $e
|
||||
]);
|
||||
return false;
|
||||
} catch (\RuntimeException $e) {
|
||||
$this->__dbError($e->getCode(), context:[
|
||||
'statement_name' => $stm_name,
|
||||
'query' => $query,
|
||||
'location' => 'dbPrepare',
|
||||
'error' => 'RuntimeException',
|
||||
'exception' => $e
|
||||
]);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// check prepared curser parameter count
|
||||
$this->prepare_cursor[$stm_name]['count'] = $this->__dbCountQueryParams($query);
|
||||
$this->prepare_cursor[$stm_name]['query'] = $query;
|
||||
@@ -3732,7 +3751,7 @@ class IO
|
||||
}
|
||||
|
||||
/**
|
||||
* convert db values (set)
|
||||
* convert db values (set) to php matching types
|
||||
*
|
||||
* @param Convert $convert
|
||||
* @return void
|
||||
@@ -3743,7 +3762,7 @@ class IO
|
||||
}
|
||||
|
||||
/**
|
||||
* unsert convert db values flag
|
||||
* unsert convert db values flag for converting db to php matching types
|
||||
*
|
||||
* @param Convert $convert
|
||||
* @return void
|
||||
@@ -3754,7 +3773,7 @@ class IO
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset to origincal config file set
|
||||
* Reset to original config file set for converting db to php matching type
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
@@ -3766,7 +3785,7 @@ class IO
|
||||
}
|
||||
|
||||
/**
|
||||
* check if a conert flag is set
|
||||
* check if a convert flag is set for converting db to php matching type
|
||||
*
|
||||
* @param Convert $convert
|
||||
* @return bool
|
||||
@@ -3780,7 +3799,7 @@ class IO
|
||||
}
|
||||
|
||||
/**
|
||||
* Set if we want to auto convert PDO/\Pg placeholders
|
||||
* Set if we want to auto convert to PDO/\Pg placeholders
|
||||
*
|
||||
* @param bool $flag
|
||||
* @return void
|
||||
@@ -4291,7 +4310,7 @@ class IO
|
||||
* @param string $stm_name The name of the stored statement
|
||||
* @param string $key Key field name in prepared cursor array
|
||||
* Allowed are: pk_name, count, query, returning_id
|
||||
* @return null|string|int|bool Entry from each of the valid keys
|
||||
* @return null|string|int|bool|array<string,mixed> Entry from each of the valid keys
|
||||
* Will return false on error
|
||||
* Not ethat returnin_id also can return false
|
||||
* but will not set an error entry
|
||||
@@ -4299,7 +4318,7 @@ class IO
|
||||
public function dbGetPrepareCursorValue(
|
||||
string $stm_name,
|
||||
string $key
|
||||
): null|string|int|bool {
|
||||
): null|string|int|bool|array {
|
||||
// if no statement name
|
||||
if (empty($stm_name)) {
|
||||
$this->__dbError(
|
||||
@@ -4310,7 +4329,7 @@ class IO
|
||||
return false;
|
||||
}
|
||||
// if not a valid key
|
||||
if (!in_array($key, ['pk_name', 'count', 'query', 'returning_id'])) {
|
||||
if (!in_array($key, ['pk_name', 'count', 'query', 'returning_id', 'placeholder_converted'])) {
|
||||
$this->__dbError(
|
||||
102,
|
||||
false,
|
||||
|
||||
@@ -285,6 +285,22 @@ interface SqlFunctions
|
||||
*/
|
||||
public function __dbConnectionBusySocketWait(int $timeout_seconds = 3): bool;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string $parameter
|
||||
* @param bool $strip
|
||||
* @return string
|
||||
*/
|
||||
public function __dbVersionInfo(string $parameter, bool $strip = true): string;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function __dbVersionInfoParameterList(): array;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
@@ -292,6 +308,13 @@ interface SqlFunctions
|
||||
*/
|
||||
public function __dbVersion(): string;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function __dbVersionNumeric(): int;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
@@ -306,6 +329,14 @@ interface SqlFunctions
|
||||
?int &$end = null
|
||||
): ?array;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string $parameter
|
||||
* @return string|bool
|
||||
*/
|
||||
public function __dbParameter(string $parameter): string|bool;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
@@ -343,6 +374,14 @@ interface SqlFunctions
|
||||
* @return string
|
||||
*/
|
||||
public function __dbGetEncoding(): string;
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param string $query
|
||||
* @return int
|
||||
*/
|
||||
public function __dbCountQueryParams(string $query): int;
|
||||
}
|
||||
|
||||
// __END__
|
||||
|
||||
@@ -51,6 +51,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\DB\SQL;
|
||||
|
||||
use CoreLibs\DB\Support\ConvertPlaceholder;
|
||||
|
||||
// below no ignore is needed if we want to use PgSql interface checks with PHP 8.0
|
||||
// as main system. Currently all @var sets are written as object
|
||||
/** @#phan-file-suppress PhanUndeclaredTypeProperty,PhanUndeclaredTypeParameter,PhanUndeclaredTypeReturnType */
|
||||
@@ -102,7 +104,7 @@ class PgSQL implements Interface\SqlFunctions
|
||||
* SELECT foo FROM bar WHERE foobar = $1
|
||||
*
|
||||
* @param string $query Query string with placeholders $1, ..
|
||||
* @param array<mixed> $params Matching parameters for each placerhold
|
||||
* @param array<mixed> $params Matching parameters for each placeholder
|
||||
* @return \PgSql\Result|false Query result
|
||||
*/
|
||||
public function __dbQueryParams(string $query, array $params): \PgSql\Result|false
|
||||
@@ -140,7 +142,7 @@ class PgSQL implements Interface\SqlFunctions
|
||||
* sends an async query to the server with params
|
||||
*
|
||||
* @param string $query Query string with placeholders $1, ..
|
||||
* @param array<mixed> $params Matching parameters for each placerhold
|
||||
* @param array<mixed> $params Matching parameters for each placeholder
|
||||
* @return bool true/false Query sent successful status
|
||||
*/
|
||||
public function __dbSendQueryParams(string $query, array $params): bool
|
||||
@@ -405,17 +407,13 @@ class PgSQL implements Interface\SqlFunctions
|
||||
}
|
||||
// no PK name given at all
|
||||
if (empty($pk_name)) {
|
||||
// if name is plurar, make it singular
|
||||
// if (preg_match("/.*s$/i", $table))
|
||||
// $table = substr($table, 0, -1);
|
||||
// set pk_name to "id"
|
||||
$pk_name = $table . "_id";
|
||||
}
|
||||
$seq = ($schema ? $schema . '.' : '') . $table . "_" . $pk_name . "_seq";
|
||||
$q = "SELECT CURRVAL('$seq') AS insert_id";
|
||||
$q = "SELECT CURRVAL(pg_get_serial_sequence($1, $2)) AS insert_id";
|
||||
// I have to do manually or I overwrite the original insert internal vars ...
|
||||
if ($q = $this->__dbQuery($q)) {
|
||||
if (is_array($res = $this->__dbFetchArray($q))) {
|
||||
if ($cursor = $this->__dbQueryParams($q, [$table, $pk_name])) {
|
||||
if (is_array($res = $this->__dbFetchArray($cursor))) {
|
||||
list($id) = $res;
|
||||
} else {
|
||||
return false;
|
||||
@@ -449,26 +447,36 @@ class PgSQL implements Interface\SqlFunctions
|
||||
$table_prefix = $schema . '.';
|
||||
}
|
||||
}
|
||||
$params = [$table_prefix . $table];
|
||||
$replace = ['', ''];
|
||||
// read from table the PK name
|
||||
// faster primary key get
|
||||
$q = "SELECT pg_attribute.attname AS column_name, "
|
||||
. "format_type(pg_attribute.atttypid, pg_attribute.atttypmod) AS type "
|
||||
. "FROM pg_index, pg_class, pg_attribute ";
|
||||
$q = <<<SQL
|
||||
SELECT
|
||||
pg_attribute.attname AS column_name,
|
||||
format_type(pg_attribute.atttypid, pg_attribute.atttypmod) AS type
|
||||
FROM pg_index, pg_class, pg_attribute{PG_NAMESPACE}
|
||||
WHERE
|
||||
-- regclass translates the OID to the name
|
||||
pg_class.oid = $1::regclass AND
|
||||
indrelid = pg_class.oid AND
|
||||
pg_attribute.attrelid = pg_class.oid AND
|
||||
pg_attribute.attnum = any(pg_index.indkey) AND
|
||||
indisprimary
|
||||
{NSPNAME}
|
||||
SQL;
|
||||
if ($schema) {
|
||||
$q .= ", pg_namespace ";
|
||||
$params[] = $schema;
|
||||
$replace = [
|
||||
", pg_namespace",
|
||||
"AND pg_class.relnamespace = pg_namespace.oid AND nspname = $2"
|
||||
];
|
||||
}
|
||||
$q .= "WHERE "
|
||||
// regclass translates the OID to the name
|
||||
. "pg_class.oid = '" . $table_prefix . $table . "'::regclass AND "
|
||||
. "indrelid = pg_class.oid AND ";
|
||||
if ($schema) {
|
||||
$q .= "nspname = '" . $schema . "' AND "
|
||||
. "pg_class.relnamespace = pg_namespace.oid AND ";
|
||||
}
|
||||
$q .= "pg_attribute.attrelid = pg_class.oid AND "
|
||||
. "pg_attribute.attnum = any(pg_index.indkey) "
|
||||
. "AND indisprimary";
|
||||
$cursor = $this->__dbQuery($q);
|
||||
$cursor = $this->__dbQueryParams(str_replace(
|
||||
['{PG_NAMESPACE}', '{NSPNAME}'],
|
||||
$replace,
|
||||
$q
|
||||
), $params);
|
||||
if ($cursor !== false) {
|
||||
$__db_fetch_array = $this->__dbFetchArray($cursor);
|
||||
if (!is_array($__db_fetch_array)) {
|
||||
@@ -893,11 +901,13 @@ class PgSQL implements Interface\SqlFunctions
|
||||
public function __dbSetSchema(string $db_schema): int
|
||||
{
|
||||
// check if schema actually exists
|
||||
$query = "SELECT EXISTS("
|
||||
. "SELECT 1 FROM information_schema.schemata "
|
||||
. "WHERE schema_name = " . $this->__dbEscapeLiteral($db_schema)
|
||||
. ")";
|
||||
$cursor = $this->__dbQuery($query);
|
||||
$query = <<<SQL
|
||||
SELECT EXISTS (
|
||||
SELECT 1 FROM information_schema.schemata
|
||||
WHERE schema_name = $1
|
||||
)
|
||||
SQL;
|
||||
$cursor = $this->__dbQueryParams($query, [$db_schema]);
|
||||
// abort if execution fails
|
||||
if ($cursor === false) {
|
||||
return 1;
|
||||
@@ -966,6 +976,34 @@ class PgSQL implements Interface\SqlFunctions
|
||||
{
|
||||
return $this->__dbShow('client_encoding');
|
||||
}
|
||||
|
||||
/**
|
||||
* Count placeholder queries. $ only
|
||||
*
|
||||
* @param string $query
|
||||
* @return int
|
||||
*/
|
||||
public function __dbCountQueryParams(string $query): int
|
||||
{
|
||||
$matches = [];
|
||||
// regex for params: only stand alone $number allowed
|
||||
// exclude all '' enclosed strings, ignore all numbers [note must start with digit]
|
||||
// can have space/tab/new line
|
||||
// must have <> = , ( [not equal, equal, comma, opening round bracket]
|
||||
// can have space/tab/new line
|
||||
// $ number with 1-9 for first and 0-9 for further digits
|
||||
// Collects also PDO ? and :named, but they are ignored
|
||||
// /s for matching new line in . list
|
||||
// [disabled, we don't used ^ or $] /m for multi line match
|
||||
// Matches in 1:, must be array_filtered to remove empty, count with array_unique
|
||||
// Regex located in the ConvertPlaceholder class
|
||||
preg_match_all(
|
||||
ConvertPlaceholder::REGEX_LOOKUP_PLACEHOLDERS,
|
||||
$query,
|
||||
$matches
|
||||
);
|
||||
return count(array_unique(array_filter($matches[3])));
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
|
||||
@@ -14,6 +14,67 @@ namespace CoreLibs\DB\Support;
|
||||
|
||||
class ConvertPlaceholder
|
||||
{
|
||||
/** @var string split regex */
|
||||
private const PATTERN_QUERY_SPLIT = '[(<>=,?-]|->|->>|#>|#>>|@>|<@|\?\|\?\&|\|\||#-';
|
||||
/** @var string the main regex including the pattern query split */
|
||||
private const PATTERN_ELEMENT = '(?:\'.*?\')?\s*(?:\?\?|' . self::PATTERN_QUERY_SPLIT . ')\s*';
|
||||
/** @var string parts to ignore in the SQL */
|
||||
private const PATTERN_IGNORE =
|
||||
// digit -> ignore
|
||||
'\d+|'
|
||||
// other string -> ignore
|
||||
. '(?:\'.*?\')|';
|
||||
/** @var string named parameters */
|
||||
private const PATTERN_NAMED = '(:\w+)';
|
||||
/** @var string question mark parameters */
|
||||
private const PATTERN_QUESTION_MARK = '(?:(?:\?\?)?\s*(\?{1}))';
|
||||
/** @var string numbered parameters */
|
||||
private const PATTERN_NUMBERED = '(\$[1-9]{1}(?:[0-9]{1,})?)';
|
||||
// below here are full regex that will be used
|
||||
/** @var string replace regex for named (:...) entries */
|
||||
public const REGEX_REPLACE_NAMED = '/'
|
||||
. '(' . self::PATTERN_ELEMENT . ')'
|
||||
. '('
|
||||
. self::PATTERN_IGNORE
|
||||
. self::PATTERN_NAMED
|
||||
. ')'
|
||||
. '/s';
|
||||
/** @var string replace regex for question mark (?) entries */
|
||||
public const REGEX_REPLACE_QUESTION_MARK = '/'
|
||||
. '(' . self::PATTERN_ELEMENT . ')'
|
||||
. '('
|
||||
. self::PATTERN_IGNORE
|
||||
. self::PATTERN_QUESTION_MARK
|
||||
. ')'
|
||||
. '/s';
|
||||
/** @var string replace regex for numbered ($n) entries */
|
||||
public const REGEX_REPLACE_NUMBERED = '/'
|
||||
. '(' . self::PATTERN_ELEMENT . ')'
|
||||
. '('
|
||||
. self::PATTERN_IGNORE
|
||||
. self::PATTERN_NUMBERED
|
||||
. ')'
|
||||
. '/s';
|
||||
/** @var string the main lookup query for all placeholders */
|
||||
public const REGEX_LOOKUP_PLACEHOLDERS = '/'
|
||||
// prefix string part, must match towards
|
||||
// seperator for ( = , ? - [and json/jsonb in pg doc section 9.15]
|
||||
. self::PATTERN_ELEMENT
|
||||
// match for replace part
|
||||
. '(?:'
|
||||
// ignore parts
|
||||
. self::PATTERN_IGNORE
|
||||
// :name named part (PDO) [1]
|
||||
. self::PATTERN_NAMED . '|'
|
||||
// ? question mark part (PDO) [2]
|
||||
. self::PATTERN_QUESTION_MARK . '|'
|
||||
// $n numbered part (\PG php) [3]
|
||||
. self::PATTERN_NUMBERED
|
||||
// end match
|
||||
. ')'
|
||||
// single line -> add line break to matches in "."
|
||||
. '/s';
|
||||
|
||||
/**
|
||||
* Convert PDO type query with placeholders to \PG style and vica versa
|
||||
* For PDO to: ? and :named
|
||||
@@ -27,44 +88,24 @@ class ConvertPlaceholder
|
||||
* found has -1 if an error occoured in the preg_match_all call
|
||||
*
|
||||
* @param string $query Query with placeholders to convert
|
||||
* @param array<mixed> $params The parameters that are used for the query, and will be updated
|
||||
* @param ?array<mixed> $params The parameters that are used for the query, and will be updated
|
||||
* @param string $convert_to Either pdo or pg, will be converted to lower case for check
|
||||
* @return array{original:array{query:string,params:array<mixed>},type:''|'named'|'numbered'|'question_mark',found:int,matches:array<string>,params_lookup:array<mixed>,query:string,params:array<mixed>}
|
||||
* @throws \OutOfRangeException 200
|
||||
* @return array{original:array{query:string,params:array<mixed>,empty_params:bool},type:''|'named'|'numbered'|'question_mark',found:int,matches:array<string>,params_lookup:array<mixed>,query:string,params:array<mixed>}
|
||||
* @throws \OutOfRangeException 200 If mixed placeholder types
|
||||
* @throws \InvalidArgumentException 300 or 301 if wrong convert to with found placeholders
|
||||
*/
|
||||
public static function convertPlaceholderInQuery(
|
||||
string $query,
|
||||
array $params,
|
||||
?array $params,
|
||||
string $convert_to = 'pg'
|
||||
): array {
|
||||
$convert_to = strtolower($convert_to);
|
||||
$matches = [];
|
||||
$query_split = '[(=,?-]|->|->>|#>|#>>|@>|<@|\?\|\?\&|\|\||#-';
|
||||
$pattern = '/'
|
||||
// prefix string part, must match towards
|
||||
// seperator for ( = , ? - [and json/jsonb in pg doc section 9.15]
|
||||
. '(?:\'.*?\')?\s*(?:\?\?|' . $query_split . ')\s*'
|
||||
// match for replace part
|
||||
. '(?:'
|
||||
// digit -> ignore
|
||||
. '\d+|'
|
||||
// other string -> ignore
|
||||
. '(?:\'.*?\')|'
|
||||
// :name named part (PDO)
|
||||
. '(:\w+)|'
|
||||
// ? question mark part (PDO)
|
||||
. '(?:(?:\?\?)?\s*(\?{1}))|'
|
||||
// $n numbered part (\PG php)
|
||||
. '(\$[1-9]{1}(?:[0-9]{1,})?)'
|
||||
// end match
|
||||
. ')'
|
||||
// single line -> add line break to matches in "."
|
||||
. '/s';
|
||||
// matches:
|
||||
// 1: :named
|
||||
// 2: ? question mark
|
||||
// 3: $n numbered
|
||||
$found = preg_match_all($pattern, $query, $matches, PREG_UNMATCHED_AS_NULL);
|
||||
$found = preg_match_all(self::REGEX_LOOKUP_PLACEHOLDERS, $query, $matches, PREG_UNMATCHED_AS_NULL);
|
||||
// if false or null set to -1
|
||||
// || $found === null
|
||||
if ($found === false) {
|
||||
@@ -77,10 +118,10 @@ class ConvertPlaceholder
|
||||
/** @var array<string> 3: $n matches */
|
||||
$numbered_matches = array_filter($matches[3]);
|
||||
// count matches
|
||||
$count_named = count($named_matches);
|
||||
$count_named = count(array_unique($named_matches));
|
||||
$count_qmark = count($qmark_matches);
|
||||
$count_numbered = count($numbered_matches);
|
||||
// throw if mixed
|
||||
$count_numbered = count(array_unique($numbered_matches));
|
||||
// throw exception if mixed found
|
||||
if (
|
||||
($count_named && $count_qmark) ||
|
||||
($count_named && $count_numbered) ||
|
||||
@@ -88,140 +129,195 @@ class ConvertPlaceholder
|
||||
) {
|
||||
throw new \OutOfRangeException('Cannot have named, question mark and numbered in the same query', 200);
|
||||
}
|
||||
// rebuild
|
||||
$matches_return = [];
|
||||
$type = '';
|
||||
// // throw if invalid conversion
|
||||
// if (($count_named || $count_qmark) && $convert_to != 'pg') {
|
||||
// throw new \InvalidArgumentException('Cannot convert from named or question mark placeholders to PDO', 300);
|
||||
// }
|
||||
// if ($count_numbered && $convert_to != 'pdo') {
|
||||
// throw new \InvalidArgumentException('Cannot convert from numbered placeholders to Pg', 301);
|
||||
// }
|
||||
// return array
|
||||
$return_placeholders = [
|
||||
// original
|
||||
'original' => [
|
||||
'query' => $query,
|
||||
'params' => $params ?? [],
|
||||
'empty_params' => $params === null ? true : false,
|
||||
],
|
||||
// type found, empty if nothing was done
|
||||
'type' => '',
|
||||
// int: found, not found; -1: problem (set from false)
|
||||
'found' => (int)$found,
|
||||
'matches' => [],
|
||||
// old to new lookup check
|
||||
'params_lookup' => [],
|
||||
// this must match the count in params in new
|
||||
'needed' => 0,
|
||||
// new
|
||||
'query' => '',
|
||||
'params' => [],
|
||||
];
|
||||
// replace basic regex and name settings
|
||||
if ($count_named) {
|
||||
$return_placeholders['type'] = 'named';
|
||||
$return_placeholders['matches'] = $named_matches;
|
||||
$return_placeholders['needed'] = $count_named;
|
||||
} elseif ($count_qmark) {
|
||||
$return_placeholders['type'] = 'question_mark';
|
||||
$return_placeholders['matches'] = $qmark_matches;
|
||||
$return_placeholders['needed'] = $count_qmark;
|
||||
// for each ?:DTN: -> replace with $1 ... $n, any remaining :DTN: remove
|
||||
} elseif ($count_numbered) {
|
||||
$return_placeholders['type'] = 'numbered';
|
||||
$return_placeholders['matches'] = $numbered_matches;
|
||||
$return_placeholders['needed'] = $count_numbered;
|
||||
}
|
||||
// run convert only if matching type and direction
|
||||
if (
|
||||
(($count_named || $count_qmark) && $convert_to == 'pg') ||
|
||||
($count_numbered && $convert_to == 'pdo')
|
||||
) {
|
||||
$param_list = self::updateParamList($return_placeholders);
|
||||
$return_placeholders['params_lookup'] = $param_list['params_lookup'];
|
||||
$return_placeholders['query'] = $param_list['query'];
|
||||
$return_placeholders['params'] = $param_list['params'];
|
||||
}
|
||||
// return data
|
||||
return $return_placeholders;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the params list from one style to the other to match the query output
|
||||
* if original.empty_params is set to true, no params replacement is done
|
||||
* if param replacement has been done in a dbPrepare then this has to be run
|
||||
* with the return palceholders array with params in original filled and empty_params turned off
|
||||
*
|
||||
* phpcs:disable Generic.Files.LineLength
|
||||
* @param array{original:array{query:string,params:array<mixed>,empty_params:bool},type:''|'named'|'numbered'|'question_mark',found:int,matches?:array<string>,params_lookup?:array<mixed>,query?:string,params?:array<mixed>} $converted_placeholders
|
||||
* phpcs:enable Generic.Files.LineLength
|
||||
* @return array{params_lookup:array<mixed>,query:string,params:array<mixed>}
|
||||
*/
|
||||
public static function updateParamList(array $converted_placeholders): array
|
||||
{
|
||||
// skip if nothing set
|
||||
if (!$converted_placeholders['found']) {
|
||||
return [
|
||||
'params_lookup' => [],
|
||||
'query' => '',
|
||||
'params' => []
|
||||
];
|
||||
}
|
||||
$query_new = '';
|
||||
$params_new = [];
|
||||
$params_lookup = [];
|
||||
if ($count_named && $convert_to == 'pg') {
|
||||
$type = 'named';
|
||||
$matches_return = $named_matches;
|
||||
// only check for :named
|
||||
$pattern_replace = '/'
|
||||
. '((?:\'.*?\')?\s*(?:\?\?|' . $query_split . ')\s*)'
|
||||
. '(\d+|(?:\'.*?\')|(:\w+))'
|
||||
. '/s';
|
||||
// 0: full
|
||||
// 1: pre part
|
||||
// 2: keep part UNLESS '3' is set
|
||||
// 3: replace part :named
|
||||
$pos = 0;
|
||||
$query_new = preg_replace_callback(
|
||||
$pattern_replace,
|
||||
function ($matches) use (&$pos, &$params_new, &$params_lookup, $params) {
|
||||
// only count up if $match[3] is not yet in lookup table
|
||||
if (!empty($matches[3]) && empty($params_lookup[$matches[3]])) {
|
||||
$pos++;
|
||||
$params_lookup[$matches[3]] = '$' . $pos;
|
||||
$params_new[] = $params[$matches[3]] ??
|
||||
throw new \RuntimeException(
|
||||
'Cannot lookup ' . $matches[3] . ' in params list',
|
||||
210
|
||||
);
|
||||
}
|
||||
// add the connectors back (1), and the data sets only if no replacement will be done
|
||||
return $matches[1] . (
|
||||
empty($matches[3]) ?
|
||||
$matches[2] :
|
||||
$params_lookup[$matches[3]] ??
|
||||
// set to null if params is empty
|
||||
$params = $converted_placeholders['original']['params'];
|
||||
$empty_params = $converted_placeholders['original']['empty_params'];
|
||||
switch ($converted_placeholders['type']) {
|
||||
case 'named':
|
||||
// 0: full
|
||||
// 0: full
|
||||
// 1: pre part
|
||||
// 2: keep part UNLESS '3' is set
|
||||
// 3: replace part :named
|
||||
$pos = 0;
|
||||
$query_new = preg_replace_callback(
|
||||
self::REGEX_REPLACE_NAMED,
|
||||
function ($matches) use (&$pos, &$params_new, &$params_lookup, $params, $empty_params) {
|
||||
// only count up if $match[3] is not yet in lookup table
|
||||
if (!empty($matches[3]) && empty($params_lookup[$matches[3]])) {
|
||||
$pos++;
|
||||
$params_lookup[$matches[3]] = '$' . $pos;
|
||||
// skip params setup if param list is empty
|
||||
if (!$empty_params) {
|
||||
$params_new[] = $params[$matches[3]] ??
|
||||
throw new \RuntimeException(
|
||||
'Cannot lookup ' . $matches[3] . ' in params list',
|
||||
210
|
||||
);
|
||||
}
|
||||
}
|
||||
// add the connectors back (1), and the data sets only if no replacement will be done
|
||||
return $matches[1] . (
|
||||
empty($matches[3]) ?
|
||||
$matches[2] :
|
||||
$params_lookup[$matches[3]] ??
|
||||
throw new \RuntimeException(
|
||||
'Cannot lookup ' . $matches[3] . ' in params lookup list',
|
||||
211
|
||||
)
|
||||
);
|
||||
},
|
||||
$converted_placeholders['original']['query']
|
||||
);
|
||||
break;
|
||||
case 'question_mark':
|
||||
if (!$empty_params) {
|
||||
// order and data stays the same
|
||||
$params_new = $params ?? [];
|
||||
}
|
||||
// 0: full
|
||||
// 1: pre part
|
||||
// 2: keep part UNLESS '3' is set
|
||||
// 3: replace part ?
|
||||
$pos = 0;
|
||||
$query_new = preg_replace_callback(
|
||||
self::REGEX_REPLACE_QUESTION_MARK,
|
||||
function ($matches) use (&$pos, &$params_lookup) {
|
||||
// only count pos up for actual replacements we will do
|
||||
if (!empty($matches[3])) {
|
||||
$pos++;
|
||||
$params_lookup[] = '$' . $pos;
|
||||
}
|
||||
// add the connectors back (1), and the data sets only if no replacement will be done
|
||||
return $matches[1] . (
|
||||
empty($matches[3]) ?
|
||||
$matches[2] :
|
||||
'$' . $pos
|
||||
);
|
||||
},
|
||||
$converted_placeholders['original']['query']
|
||||
);
|
||||
break;
|
||||
case 'numbered':
|
||||
// 0: full
|
||||
// 1: pre part
|
||||
// 2: keep part UNLESS '3' is set
|
||||
// 3: replace part $numbered
|
||||
$pos = 0;
|
||||
$query_new = preg_replace_callback(
|
||||
self::REGEX_REPLACE_NUMBERED,
|
||||
function ($matches) use (&$pos, &$params_new, &$params_lookup, $params, $empty_params) {
|
||||
// only count up if $match[3] is not yet in lookup table
|
||||
if (!empty($matches[3]) && empty($params_lookup[$matches[3]])) {
|
||||
$pos++;
|
||||
$params_lookup[$matches[3]] = ':' . $pos . '_named';
|
||||
// skip params setup if param list is empty
|
||||
if (!$empty_params) {
|
||||
$params_new[] = $params[($pos - 1)] ??
|
||||
throw new \RuntimeException(
|
||||
'Cannot lookup ' . ($pos - 1) . ' in params list',
|
||||
220
|
||||
);
|
||||
}
|
||||
}
|
||||
// add the connectors back (1), and the data sets only if no replacement will be done
|
||||
return $matches[1] . (
|
||||
empty($matches[3]) ?
|
||||
$matches[2] :
|
||||
$params_lookup[$matches[3]] ??
|
||||
throw new \RuntimeException(
|
||||
'Cannot lookup ' . $matches[3] . ' in params lookup list',
|
||||
211
|
||||
221
|
||||
)
|
||||
);
|
||||
},
|
||||
$query
|
||||
);
|
||||
} elseif ($count_qmark && $convert_to == 'pg') {
|
||||
$type = 'question_mark';
|
||||
$matches_return = $qmark_matches;
|
||||
// order and data stays the same
|
||||
$params_new = $params;
|
||||
// only check for ?
|
||||
$pattern_replace = '/'
|
||||
. '((?:\'.*?\')?\s*(?:\?\?|' . $query_split . ')\s*)'
|
||||
. '(\d+|(?:\'.*?\')|(?:(?:\?\?)?\s*(\?{1})))'
|
||||
. '/s';
|
||||
// 0: full
|
||||
// 1: pre part
|
||||
// 2: keep part UNLESS '3' is set
|
||||
// 3: replace part ?
|
||||
$pos = 0;
|
||||
$query_new = preg_replace_callback(
|
||||
$pattern_replace,
|
||||
function ($matches) use (&$pos, &$params_lookup) {
|
||||
// only count pos up for actual replacements we will do
|
||||
if (!empty($matches[3])) {
|
||||
$pos++;
|
||||
$params_lookup[] = '$' . $pos;
|
||||
}
|
||||
// add the connectors back (1), and the data sets only if no replacement will be done
|
||||
return $matches[1] . (
|
||||
empty($matches[3]) ?
|
||||
$matches[2] :
|
||||
'$' . $pos
|
||||
);
|
||||
},
|
||||
$query
|
||||
);
|
||||
// for each ?:DTN: -> replace with $1 ... $n, any remaining :DTN: remove
|
||||
} elseif ($count_numbered && $convert_to == 'pdo') {
|
||||
// convert numbered to named
|
||||
$type = 'numbered';
|
||||
$matches_return = $numbered_matches;
|
||||
// only check for $n
|
||||
$pattern_replace = '/'
|
||||
. '((?:\'.*?\')?\s*(?:\?\?|' . $query_split . ')\s*)'
|
||||
. '(\d+|(?:\'.*?\')|(\$[1-9]{1}(?:[0-9]{1,})?))'
|
||||
. '/s';
|
||||
// 0: full
|
||||
// 1: pre part
|
||||
// 2: keep part UNLESS '3' is set
|
||||
// 3: replace part $numbered
|
||||
$pos = 0;
|
||||
$query_new = preg_replace_callback(
|
||||
$pattern_replace,
|
||||
function ($matches) use (&$pos, &$params_new, &$params_lookup, $params) {
|
||||
// only count up if $match[3] is not yet in lookup table
|
||||
if (!empty($matches[3]) && empty($params_lookup[$matches[3]])) {
|
||||
$pos++;
|
||||
$params_lookup[$matches[3]] = ':' . $pos . '_named';
|
||||
$params_new[] = $params[($pos - 1)] ??
|
||||
throw new \RuntimeException(
|
||||
'Cannot lookup ' . ($pos - 1) . ' in params list',
|
||||
220
|
||||
);
|
||||
}
|
||||
// add the connectors back (1), and the data sets only if no replacement will be done
|
||||
return $matches[1] . (
|
||||
empty($matches[3]) ?
|
||||
$matches[2] :
|
||||
$params_lookup[$matches[3]] ??
|
||||
throw new \RuntimeException(
|
||||
'Cannot lookup ' . $matches[3] . ' in params lookup list',
|
||||
221
|
||||
)
|
||||
);
|
||||
},
|
||||
$query
|
||||
);
|
||||
);
|
||||
},
|
||||
$converted_placeholders['original']['query']
|
||||
);
|
||||
break;
|
||||
}
|
||||
// return, old query is always set
|
||||
return [
|
||||
// original
|
||||
'original' => [
|
||||
'query' => $query,
|
||||
'params' => $params,
|
||||
],
|
||||
// type found, empty if nothing was done
|
||||
'type' => $type,
|
||||
// int: found, not found; -1: problem (set from false)
|
||||
'found' => (int)$found,
|
||||
'matches' => $matches_return,
|
||||
// old to new lookup check
|
||||
'params_lookup' => $params_lookup,
|
||||
// new
|
||||
'query' => $query_new ?? '',
|
||||
'params' => $params_new,
|
||||
];
|
||||
|
||||
@@ -190,7 +190,6 @@ class GetTextReader
|
||||
private function loadTables(): void
|
||||
{
|
||||
if (
|
||||
is_array($this->cache_translations) &&
|
||||
is_array($this->table_originals) &&
|
||||
is_array($this->table_translations)
|
||||
) {
|
||||
@@ -318,10 +317,7 @@ class GetTextReader
|
||||
|
||||
if ($this->enable_cache) {
|
||||
// Caching enabled, get translated string from cache
|
||||
if (
|
||||
is_array($this->cache_translations) &&
|
||||
array_key_exists($string, $this->cache_translations)
|
||||
) {
|
||||
if (array_key_exists($string, $this->cache_translations)) {
|
||||
return $this->cache_translations[$string];
|
||||
} else {
|
||||
return $string;
|
||||
@@ -481,7 +477,7 @@ class GetTextReader
|
||||
$key = $single . chr(0) . $plural;
|
||||
|
||||
if ($this->enable_cache) {
|
||||
if (is_array($this->cache_translations) && !array_key_exists($key, $this->cache_translations)) {
|
||||
if (!array_key_exists($key, $this->cache_translations)) {
|
||||
return ($number != 1) ? $plural : $single;
|
||||
} else {
|
||||
$result = $this->cache_translations[$key];
|
||||
|
||||
@@ -24,16 +24,20 @@ class ErrorMessage
|
||||
|
||||
/** @var bool $log_error global flag to log error level message */
|
||||
private bool $log_error = false;
|
||||
/** @var bool $log_warning global flat to log warning level messages */
|
||||
private bool $log_warning = false;
|
||||
|
||||
/**
|
||||
* init ErrorMessage
|
||||
*
|
||||
* @param \CoreLibs\Logging\Logging $log
|
||||
* @param null|bool $log_error [=null], defaults to false if log is not level debug
|
||||
* @param null|bool $log_warning [=null], defaults to false if log is not level debug
|
||||
*/
|
||||
public function __construct(
|
||||
\CoreLibs\Logging\Logging $log,
|
||||
?bool $log_error = null
|
||||
?bool $log_error = null,
|
||||
?bool $log_warning = null
|
||||
) {
|
||||
$this->log = $log;
|
||||
// if log default logging is debug then log_error is default set to true
|
||||
@@ -43,6 +47,13 @@ class ErrorMessage
|
||||
$log_error = $log_error ?? false;
|
||||
}
|
||||
$this->log_error = $log_error;
|
||||
// if log default logging is debug then log_warning is default set to true
|
||||
if ($this->log->loggingLevelIsDebug() && $log_warning === null) {
|
||||
$log_warning = true;
|
||||
} else {
|
||||
$log_warning = $log_warning ?? false;
|
||||
}
|
||||
$this->log_warning = $log_warning;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,6 +92,8 @@ class ErrorMessage
|
||||
* @param array<mixed> $context Additionl info for abort/crash messages
|
||||
* @param bool|null $log_error [=null] log level 'error' to error, if null use global,
|
||||
* else set for this call only
|
||||
* @param bool|null $log_warning [=null] log level 'warning' to warning, if null use global,
|
||||
* else set for this call only
|
||||
*/
|
||||
public function setErrorMsg(
|
||||
string $error_id,
|
||||
@@ -93,10 +106,14 @@ class ErrorMessage
|
||||
?string $message = null,
|
||||
array $context = [],
|
||||
?bool $log_error = null,
|
||||
?bool $log_warning = null,
|
||||
): void {
|
||||
if ($log_error === null) {
|
||||
$log_error = $this->log_error;
|
||||
}
|
||||
if ($log_warning === null) {
|
||||
$log_warning = $this->log_warning;
|
||||
}
|
||||
$original_level = $level;
|
||||
$level = MessageLevel::fromName($level)->name;
|
||||
// if not string set, write message string if set, else level/error id
|
||||
@@ -121,6 +138,14 @@ class ErrorMessage
|
||||
'level' => $original_level,
|
||||
], $context));
|
||||
break;
|
||||
case 'warn':
|
||||
if ($log_warning) {
|
||||
$this->log->warning($message ?? $str, array_merge([
|
||||
'id' => $error_id,
|
||||
'level' => $original_level,
|
||||
], $context));
|
||||
}
|
||||
break;
|
||||
case 'error':
|
||||
if ($log_error) {
|
||||
$this->log->error($message ?? $str, array_merge([
|
||||
@@ -169,6 +194,8 @@ class ErrorMessage
|
||||
* @param array<mixed> $context Additionl info for abort/crash messages
|
||||
* @param bool|null $log_error [=null] log level 'error' to error, if null use global,
|
||||
* else set for this call only
|
||||
* @param bool|null $log_warning [=null] log level 'warning' to warning, if null use global,
|
||||
* else set for this call only
|
||||
*/
|
||||
public function setMessage(
|
||||
string $level,
|
||||
@@ -181,6 +208,7 @@ class ErrorMessage
|
||||
?string $message = null,
|
||||
array $context = [],
|
||||
?bool $log_error = null,
|
||||
?bool $log_warning = null,
|
||||
): void {
|
||||
$this->setErrorMsg(
|
||||
$error_id ?? '',
|
||||
@@ -192,7 +220,8 @@ class ErrorMessage
|
||||
$jump_target,
|
||||
$message,
|
||||
$context,
|
||||
$log_error
|
||||
$log_error,
|
||||
$log_warning
|
||||
);
|
||||
}
|
||||
|
||||
@@ -314,6 +343,27 @@ class ErrorMessage
|
||||
{
|
||||
return $this->log_error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the log warning flag
|
||||
*
|
||||
* @param bool $flag True to log level warning too, False for do not (Default)
|
||||
* @return void
|
||||
*/
|
||||
public function setFlagLogWarning(bool $flag): void
|
||||
{
|
||||
$this->log_warning = $flag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current log error flag
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getFlagLogWarning(): bool
|
||||
{
|
||||
return $this->log_warning;
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace CoreLibs\Logging\Logger;
|
||||
enum MessageLevel: int
|
||||
{
|
||||
case ok = 100;
|
||||
case success = 110; // special for file uploads
|
||||
case success = 150; // special for file uploads
|
||||
case info = 200;
|
||||
case notice = 250;
|
||||
case warn = 300;
|
||||
|
||||
@@ -474,7 +474,7 @@ class Generate
|
||||
$page_name_camel_case
|
||||
);
|
||||
try {
|
||||
/** @var TableArrays\Interface\TableArraysInterface|false $class */
|
||||
/** @var TableArrays\Interface\TableArraysInterface $class */
|
||||
$class = new $class_string($this);
|
||||
} catch (\Throwable $t) {
|
||||
$this->log->critical('CLASS LOADING: Failed loading: ' . $class_string . ' => ' . $t->getMessage());
|
||||
@@ -1757,14 +1757,9 @@ class Generate
|
||||
$this->dba->setTableArrayEntry($this->dba->getTableArray()[$key]['preset'], $key, 'value');
|
||||
}
|
||||
}
|
||||
if (is_array($this->reference_array)) {
|
||||
if (!is_array($this->reference_array)) {
|
||||
$this->reference_array = [];
|
||||
}
|
||||
reset($this->reference_array);
|
||||
foreach ($this->reference_array as $key => $value) {
|
||||
unset($this->reference_array[$key]['selected']);
|
||||
}
|
||||
reset($this->reference_array);
|
||||
foreach ($this->reference_array as $key => $value) {
|
||||
unset($this->reference_array[$key]['selected']);
|
||||
}
|
||||
$this->warning = 1;
|
||||
$this->msg = $this->l->__('Cleared for new Dataset!');
|
||||
@@ -1787,20 +1782,15 @@ class Generate
|
||||
$this->dba->unsetTableArrayEntry($key, 'input_value');
|
||||
}
|
||||
|
||||
if (is_array($this->reference_array)) {
|
||||
// load each reference_table
|
||||
if (!is_array($this->reference_array)) {
|
||||
$this->reference_array = [];
|
||||
}
|
||||
reset($this->reference_array);
|
||||
foreach ($this->reference_array as $key => $value) {
|
||||
unset($this->reference_array[$key]['selected']);
|
||||
$q = 'SELECT ' . $this->reference_array[$key]['other_table_pk']
|
||||
. ' FROM ' . $this->reference_array[$key]['table_name']
|
||||
. ' WHERE ' . $this->int_pk_name . ' = ' . $this->dba->getTableArray()[$this->int_pk_name]['value'];
|
||||
while (is_array($res = $this->dba->dbReturn($q))) {
|
||||
$this->reference_array[$key]['selected'][] = $res[$this->reference_array[$key]['other_table_pk']];
|
||||
}
|
||||
// load each reference_table
|
||||
reset($this->reference_array);
|
||||
foreach ($this->reference_array as $key => $value) {
|
||||
unset($this->reference_array[$key]['selected']);
|
||||
$q = 'SELECT ' . $this->reference_array[$key]['other_table_pk']
|
||||
. ' FROM ' . $this->reference_array[$key]['table_name']
|
||||
. ' WHERE ' . $this->int_pk_name . ' = ' . $this->dba->getTableArray()[$this->int_pk_name]['value'];
|
||||
while (is_array($res = $this->dba->dbReturn($q))) {
|
||||
$this->reference_array[$key]['selected'][] = $res[$this->reference_array[$key]['other_table_pk']];
|
||||
}
|
||||
}
|
||||
$this->warning = 1;
|
||||
@@ -1979,24 +1969,19 @@ class Generate
|
||||
// write the object
|
||||
$this->dba->dbWrite($addslashes, [], true);
|
||||
// write reference array (s) if necessary
|
||||
if (is_array($this->reference_array)) {
|
||||
if (!is_array($this->reference_array)) {
|
||||
$this->reference_array = [];
|
||||
reset($this->reference_array);
|
||||
foreach ($this->reference_array as $reference_array) {
|
||||
$q = 'DELETE FROM ' . $reference_array['table_name']
|
||||
. ' WHERE ' . $this->int_pk_name . ' = ' . $this->dba->getTableArray()[$this->int_pk_name]['value'];
|
||||
$this->dba->dbExec($q);
|
||||
$q = 'INSERT INTO ' . $reference_array['table_name']
|
||||
. ' (' . $reference_array['other_table_pk'] . ', ' . $this->int_pk_name . ') VALUES ';
|
||||
for ($i = 0, $i_max = count($reference_array['selected']); $i < $i_max; $i++) {
|
||||
$t_q = '(' . $reference_array['selected'][$i] . ', '
|
||||
. $this->dba->getTableArray()[$this->int_pk_name]['value'] . ')';
|
||||
$this->dba->dbExec($q . $t_q);
|
||||
}
|
||||
reset($this->reference_array);
|
||||
foreach ($this->reference_array as $reference_array) {
|
||||
$q = 'DELETE FROM ' . $reference_array['table_name']
|
||||
. ' WHERE ' . $this->int_pk_name . ' = ' . $this->dba->getTableArray()[$this->int_pk_name]['value'];
|
||||
$this->dba->dbExec($q);
|
||||
$q = 'INSERT INTO ' . $reference_array['table_name']
|
||||
. ' (' . $reference_array['other_table_pk'] . ', ' . $this->int_pk_name . ') VALUES ';
|
||||
for ($i = 0, $i_max = count($reference_array['selected']); $i < $i_max; $i++) {
|
||||
$t_q = '(' . $reference_array['selected'][$i] . ', '
|
||||
. $this->dba->getTableArray()[$this->int_pk_name]['value'] . ')';
|
||||
$this->dba->dbExec($q . $t_q);
|
||||
}
|
||||
} // foreach reference arrays
|
||||
} // if reference arrays
|
||||
} // foreach reference arrays
|
||||
// write element list
|
||||
if (!empty($this->element_list)) {
|
||||
$type = [];
|
||||
@@ -2230,16 +2215,11 @@ class Generate
|
||||
public function formDeleteTableArray()
|
||||
{
|
||||
// remove any reference arrays
|
||||
if (is_array($this->reference_array)) {
|
||||
if (!is_array($this->reference_array)) {
|
||||
$this->reference_array = [];
|
||||
}
|
||||
reset($this->reference_array);
|
||||
foreach ($this->reference_array as $reference_array) {
|
||||
$q = 'DELETE FROM ' . $reference_array['table_name']
|
||||
. ' WHERE ' . $this->int_pk_name . ' = ' . $this->dba->getTableArray()[$this->int_pk_name]['value'];
|
||||
$this->dba->dbExec($q);
|
||||
}
|
||||
reset($this->reference_array);
|
||||
foreach ($this->reference_array as $reference_array) {
|
||||
$q = 'DELETE FROM ' . $reference_array['table_name']
|
||||
. ' WHERE ' . $this->int_pk_name . ' = ' . $this->dba->getTableArray()[$this->int_pk_name]['value'];
|
||||
$this->dba->dbExec($q);
|
||||
}
|
||||
// remove any element list references
|
||||
if (!empty($this->element_list)) {
|
||||
|
||||
@@ -256,8 +256,8 @@ class Image
|
||||
}
|
||||
// check resize parameters
|
||||
if ($inc_width > $thumb_width || $inc_height > $thumb_height) {
|
||||
$thumb_width_r = 0;
|
||||
$thumb_height_r = 0;
|
||||
$thumb_width_r = 1;
|
||||
$thumb_height_r = 1;
|
||||
// we need to keep the aspect ration on longest side
|
||||
if (
|
||||
($inc_height > $inc_width &&
|
||||
@@ -288,6 +288,12 @@ class Image
|
||||
!file_exists($thumbnail_write_path . $thumbnail)
|
||||
) {
|
||||
// image, copy source image, offset in image, source x/y, new size, source image size
|
||||
if ($thumb_width_r < 1) {
|
||||
$thumb_width_r = 1;
|
||||
}
|
||||
if ($thumb_height_r < 1) {
|
||||
$thumb_height_r = 1;
|
||||
}
|
||||
$thumb = imagecreatetruecolor($thumb_width_r, $thumb_height_r);
|
||||
if ($thumb === false) {
|
||||
throw new \RuntimeException(
|
||||
@@ -380,9 +386,7 @@ class Image
|
||||
}
|
||||
}
|
||||
// add output path
|
||||
if ($thumbnail !== false) {
|
||||
$thumbnail = $thumbnail_web_path . $thumbnail;
|
||||
}
|
||||
$thumbnail = $thumbnail_web_path . $thumbnail;
|
||||
} elseif ($create_dummy === true) {
|
||||
// create dummy image in the thumbnail size
|
||||
// if one side is missing, use the other side to create a square
|
||||
@@ -399,10 +403,10 @@ class Image
|
||||
!file_exists($thumbnail_write_path . $thumbnail)
|
||||
) {
|
||||
// if both are unset, set to 250
|
||||
if ($thumb_height == 0) {
|
||||
if ($thumb_height < 1) {
|
||||
$thumb_height = 250;
|
||||
}
|
||||
if ($thumb_width == 0) {
|
||||
if ($thumb_width < 1) {
|
||||
$thumb_width = 250;
|
||||
}
|
||||
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
|
||||
|
||||
1041
www/lib/CoreLibs/UrlRequests/Curl.php
Normal file
1041
www/lib/CoreLibs/UrlRequests/Curl.php
Normal file
File diff suppressed because it is too large
Load Diff
128
www/lib/CoreLibs/UrlRequests/CurlTrait.php
Normal file
128
www/lib/CoreLibs/UrlRequests/CurlTrait.php
Normal file
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* AUTHOR: Clemens Schwaighofer
|
||||
* CREATED: 2024/10/29
|
||||
* DESCRIPTION:
|
||||
* Curl Client Trait for get/post/put/delete requests through the php curl inteface
|
||||
*
|
||||
* For anything more complex use guzzlehttp/http
|
||||
* https://docs.guzzlephp.org/en/stable/index.html
|
||||
*/
|
||||
|
||||
// phpcs:disable Generic.Files.LineLength
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CoreLibs\UrlRequests;
|
||||
|
||||
trait CurlTrait
|
||||
{
|
||||
/**
|
||||
* combined set call for any type of request with options type parameters
|
||||
* The following options can be set:
|
||||
* header: as array string:string
|
||||
* query as string or array string:string
|
||||
* body as string or array of any type
|
||||
*
|
||||
* @param string $type What type of request we send, will throw exception if not a valid one
|
||||
* @param string $url The url to send
|
||||
* @param array{auth?:null|array{0:string,1:string,2:string},headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>,http_errors?:null|bool} $options Request options
|
||||
* @return array{code:string,headers:array<string,array<string>>,content:string} [default=[]] Result code, headers and content as array, content is json
|
||||
* @throws \UnexpectedValueException on missing body data when body data is needed
|
||||
*/
|
||||
abstract public function request(string $type, string $url, array $options = []): array;
|
||||
|
||||
/**
|
||||
* Makes an request to the target url via curl: GET
|
||||
* Returns result as string (json)
|
||||
*
|
||||
* @param string $url The URL being requested,
|
||||
* including domain and protocol
|
||||
* @param array{auth?:null|array{0:string,1:string,2:string},headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>,http_errors?:null|bool} $options Options to set
|
||||
* @return array{code:string,headers:array<string,array<string>>,content:string} [default=[]] Result code, headers and content as array, content is json
|
||||
*/
|
||||
public function get(string $url, array $options = []): array
|
||||
{
|
||||
return $this->request(
|
||||
"get",
|
||||
$url,
|
||||
$options,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes an request to the target url via curl: POST
|
||||
* Returns result as string (json)
|
||||
*
|
||||
* @param string $url The URL being requested,
|
||||
* including domain and protocol
|
||||
* @param array{auth?:null|array{0:string,1:string,2:string},headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>,http_errors?:null|bool} $options Options to set
|
||||
* @return array{code:string,headers:array<string,array<string>>,content:string} Result code, headers and content as array, content is json
|
||||
*/
|
||||
public function post(string $url, array $options): array
|
||||
{
|
||||
return $this->request(
|
||||
"post",
|
||||
$url,
|
||||
$options,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes an request to the target url via curl: PUT
|
||||
* Returns result as string (json)
|
||||
*
|
||||
* @param string $url The URL being requested,
|
||||
* including domain and protocol
|
||||
* @param array{auth?:null|array{0:string,1:string,2:string},headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>,http_errors?:null|bool} $options Options to set
|
||||
* @return array{code:string,headers:array<string,array<string>>,content:string} Result code, headers and content as array, content is json
|
||||
*/
|
||||
public function put(string $url, array $options): array
|
||||
{
|
||||
return $this->request(
|
||||
"put",
|
||||
$url,
|
||||
$options,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes an request to the target url via curl: PATCH
|
||||
* Returns result as string (json)
|
||||
*
|
||||
* @param string $url The URL being requested,
|
||||
* including domain and protocol
|
||||
* @param array{auth?:null|array{0:string,1:string,2:string},headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>,http_errors?:null|bool} $options Options to set
|
||||
* @return array{code:string,headers:array<string,array<string>>,content:string} Result code, headers and content as array, content is json
|
||||
*/
|
||||
public function patch(string $url, array $options): array
|
||||
{
|
||||
return $this->request(
|
||||
"patch",
|
||||
$url,
|
||||
$options,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes an request to the target url via curl: DELETE
|
||||
* Returns result as string (json)
|
||||
* Note that DELETE body is optional
|
||||
*
|
||||
* @param string $url The URL being requested,
|
||||
* including domain and protocol
|
||||
* @param array{auth?:null|array{0:string,1:string,2:string},headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>,http_errors?:null|bool} $options Options to set
|
||||
* @return array{code:string,headers:array<string,array<string>>,content:string} [default=[]] Result code, headers and content as array, content is json
|
||||
*/
|
||||
public function delete(string $url, array $options = []): array
|
||||
{
|
||||
return $this->request(
|
||||
"delete",
|
||||
$url,
|
||||
$options,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// __END__
|
||||
83
www/lib/CoreLibs/UrlRequests/Interface/RequestsInterface.php
Normal file
83
www/lib/CoreLibs/UrlRequests/Interface/RequestsInterface.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* AUTHOR: Clemens Schwaighofer
|
||||
* CREATED: 2024/9/20
|
||||
* DESCRIPTION:
|
||||
* URL Requests client interface
|
||||
*/
|
||||
|
||||
namespace CoreLibs\UrlRequests\Interface;
|
||||
|
||||
interface RequestsInterface
|
||||
{
|
||||
/**
|
||||
* get the config array with all settings
|
||||
*
|
||||
* @return array<string,mixed> all current config settings
|
||||
*/
|
||||
public function getConfig(): array;
|
||||
|
||||
/**
|
||||
* Return the full url as it was sent
|
||||
*
|
||||
* @return string url sent
|
||||
*/
|
||||
public function getUrlSent(): string;
|
||||
|
||||
/**
|
||||
* get the parsed url
|
||||
*
|
||||
* @return array{scheme?:string,user?:string,host?:string,port?:string,path?:string,query?:string,fragment?:string,pass?:string}
|
||||
*/
|
||||
public function getUrlParsedSent(): array;
|
||||
|
||||
/**
|
||||
* Return the full headers as they where sent
|
||||
*
|
||||
* @return array<string,string>
|
||||
*/
|
||||
public function getHeadersSent(): array;
|
||||
|
||||
/**
|
||||
* set, add or overwrite header
|
||||
* On default this will overwrite header, and not set
|
||||
*
|
||||
* @param array<string,string|array<string>> $header
|
||||
* @param bool $add [default=false] if set will add header to existing value
|
||||
* @return void
|
||||
*/
|
||||
public function setHeaders(array $header, bool $add = false): void;
|
||||
|
||||
/**
|
||||
* remove header entry
|
||||
* if key is only set then match only key, if both are set both sides must match
|
||||
*
|
||||
* @param array<string,string> $remove_headers
|
||||
* @return void
|
||||
*/
|
||||
public function removeHeaders(array $remove_headers): void;
|
||||
|
||||
/**
|
||||
* Update the base url set, if empty will unset the base url
|
||||
*
|
||||
* @param string $base_uri
|
||||
* @return void
|
||||
*/
|
||||
public function setBaseUri(string $base_uri): void;
|
||||
|
||||
/**
|
||||
* combined set call for any type of request with options type parameters
|
||||
*
|
||||
* phpcs:disable Generic.Files.LineLength
|
||||
* @param string $type
|
||||
* @param string $url
|
||||
* @param array{auth?:null|array{0:string,1:string,2:string},headers?:null|array<string,string|array<string>>,query?:null|array<string,string>,body?:null|string|array<mixed>,http_errors?:null|bool} $options
|
||||
* @return array{code:string,headers:array<string,array<string>>,content:string} Result code, headers and content as array, content is json
|
||||
* @throws \UnexpectedValueException on missing body data when body data is needed
|
||||
* phpcs:enable Generic.Files.LineLength
|
||||
*/
|
||||
public function request(string $type, string $url, array $options = []): array;
|
||||
}
|
||||
|
||||
// __END__
|
||||
2
www/media/sqlite/.gitignore
vendored
Normal file
2
www/media/sqlite/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user