From 98710c7fbd484d4b51aedbfddfccd981ff46b085 Mon Sep 17 00:00:00 2001 From: aB0t Date: Thu, 17 Oct 2024 01:15:12 +0200 Subject: [PATCH] Update 2024-10-17 01:15:11 --- README.md | 1 + .../README.md | 8 + .../code.php | 19 + .../code.power | 18 + .../settings.json | 4 + .../README.md | 166 +++++++++ .../code.php | 345 ++++++++++++++++++ .../code.power | 319 ++++++++++++++++ .../settings.json | 24 ++ super-powers.json | 11 + 10 files changed, 915 insertions(+) create mode 100644 src/79fd4f39-824d-4ab6-936d-959705ff24ec/README.md create mode 100644 src/79fd4f39-824d-4ab6-936d-959705ff24ec/code.php create mode 100644 src/79fd4f39-824d-4ab6-936d-959705ff24ec/code.power create mode 100644 src/79fd4f39-824d-4ab6-936d-959705ff24ec/settings.json diff --git a/README.md b/README.md index 8bbf427..1a3e99f 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,7 @@ This repository contains an index (see below) of all the approved powers within - **final class Schema** | [Details](src/b3d2ec33-76d4-4c3b-bb2c-86ac14a221ce) | [Code](src/b3d2ec33-76d4-4c3b-bb2c-86ac14a221ce/code.php) | [Settings](src/b3d2ec33-76d4-4c3b-bb2c-86ac14a221ce/settings.json) | SPK: `Super---b3d2ec33_76d4_4c3b_bb2c_86ac14a221ce---Power` - **final class SchemaChecker** | [Details](src/709d7294-9a43-46e2-b64e-d16a16f0eab1) | [Code](src/709d7294-9a43-46e2-b64e-d16a16f0eab1/code.php) | [Settings](src/709d7294-9a43-46e2-b64e-d16a16f0eab1/settings.json) | SPK: `Super---709d7294_9a43_46e2_b64e_d16a16f0eab1---Power` + - **final class Validator** | [Details](src/79fd4f39-824d-4ab6-936d-959705ff24ec) | [Code](src/79fd4f39-824d-4ab6-936d-959705ff24ec/code.php) | [Settings](src/79fd4f39-824d-4ab6-936d-959705ff24ec/settings.json) | SPK: `Super---79fd4f39_824d_4ab6_936d_959705ff24ec---Power` - **Namespace**: [VDM\Joomla\Componentbuilder\Utilities](#vdm-joomla-componentbuilder-utilities) - **abstract class UserHelper** | [Details](src/7832a726-87b6-4e95-887e-7b725d3fab8f) | [Code](src/7832a726-87b6-4e95-887e-7b725d3fab8f/code.php) | [Settings](src/7832a726-87b6-4e95-887e-7b725d3fab8f/settings.json) | SPK: `Super---7832a726_87b6_4e95_887e_7b725d3fab8f---Power` diff --git a/src/19b2ba92-1655-4384-acfb-979c80de8b6d/README.md b/src/19b2ba92-1655-4384-acfb-979c80de8b6d/README.md index b03be8f..8cacacb 100644 --- a/src/19b2ba92-1655-4384-acfb-979c80de8b6d/README.md +++ b/src/19b2ba92-1655-4384-acfb-979c80de8b6d/README.md @@ -15,6 +15,7 @@ class Table #Gold { + register(Container $container) : void + getTable(Container $container) : DataTable + getSchema(Container $container) : Schema + + getValidator(Container $container) : Validator } note right of Table::register @@ -37,6 +38,13 @@ note right of Table::getSchema since: 3.2.2 return: Schema end note + +note right of Table::getValidator + Get The Validator Class. + + since: 3.2.2 + return: Validator +end note @enduml ``` diff --git a/src/19b2ba92-1655-4384-acfb-979c80de8b6d/code.php b/src/19b2ba92-1655-4384-acfb-979c80de8b6d/code.php index eaf4110..5974578 100644 --- a/src/19b2ba92-1655-4384-acfb-979c80de8b6d/code.php +++ b/src/19b2ba92-1655-4384-acfb-979c80de8b6d/code.php @@ -16,6 +16,7 @@ use Joomla\DI\Container; use Joomla\DI\ServiceProviderInterface; use VDM\Joomla\Componentbuilder\Table as DataTable; use VDM\Joomla\Componentbuilder\Table\Schema; +use VDM\Joomla\Componentbuilder\Table\Validator; /** @@ -40,6 +41,9 @@ class Table implements ServiceProviderInterface $container->alias(Schema::class, 'Table.Schema') ->share('Table.Schema', [$this, 'getSchema'], true); + + $container->alias(Validator::class, 'Table.Validator') + ->share('Table.Validator', [$this, 'getValidator'], true); } /** @@ -68,6 +72,21 @@ class Table implements ServiceProviderInterface return new Schema( $container->get('Table') ); + } + + /** + * Get The Validator Class. + * + * @param Container $container The DI container. + * + * @return Validator + * @since 3.2.2 + */ + public function getValidator(Container $container): Validator + { + return new Validator( + $container->get('Table') + ); } } diff --git a/src/19b2ba92-1655-4384-acfb-979c80de8b6d/code.power b/src/19b2ba92-1655-4384-acfb-979c80de8b6d/code.power index 6212758..6ceda7c 100644 --- a/src/19b2ba92-1655-4384-acfb-979c80de8b6d/code.power +++ b/src/19b2ba92-1655-4384-acfb-979c80de8b6d/code.power @@ -13,6 +13,9 @@ $container->alias(Schema::class, 'Table.Schema') ->share('Table.Schema', [$this, 'getSchema'], true); + + $container->alias(Validator::class, 'Table.Validator') + ->share('Table.Validator', [$this, 'getValidator'], true); } /** @@ -41,4 +44,19 @@ return new Schema( $container->get('Table') ); + } + + /** + * Get The Validator Class. + * + * @param Container $container The DI container. + * + * @return Validator + * @since 3.2.2 + */ + public function getValidator(Container $container): Validator + { + return new Validator( + $container->get('Table') + ); } \ No newline at end of file diff --git a/src/19b2ba92-1655-4384-acfb-979c80de8b6d/settings.json b/src/19b2ba92-1655-4384-acfb-979c80de8b6d/settings.json index cf0ef5f..bd7dcf1 100644 --- a/src/19b2ba92-1655-4384-acfb-979c80de8b6d/settings.json +++ b/src/19b2ba92-1655-4384-acfb-979c80de8b6d/settings.json @@ -19,6 +19,10 @@ "use_selection1": { "use": "b3d2ec33-76d4-4c3b-bb2c-86ac14a221ce", "as": "default" + }, + "use_selection2": { + "use": "79fd4f39-824d-4ab6-936d-959705ff24ec", + "as": "default" } }, "extendsinterfaces": null, diff --git a/src/79fd4f39-824d-4ab6-936d-959705ff24ec/README.md b/src/79fd4f39-824d-4ab6-936d-959705ff24ec/README.md new file mode 100644 index 0000000..9e678d3 --- /dev/null +++ b/src/79fd4f39-824d-4ab6-936d-959705ff24ec/README.md @@ -0,0 +1,166 @@ +``` +██████╗ ██████╗ ██╗ ██╗███████╗██████╗ +██╔══██╗██╔═══██╗██║ ██║██╔════╝██╔══██╗ +██████╔╝██║ ██║██║ █╗ ██║█████╗ ██████╔╝ +██╔═══╝ ██║ ██║██║███╗██║██╔══╝ ██╔══██╗ +██║ ╚██████╔╝╚███╔███╔╝███████╗██║ ██║ +╚═╝ ╚═════╝ ╚══╝╚══╝ ╚══════╝╚═╝ ╚═╝ +``` +# final class Validator (Details) +> namespace: **VDM\Joomla\Componentbuilder\Table** + +```uml +@startuml +class Validator << (F,LightGreen) >> #RoyalBlue { + # Table $table + # array $validators + + __construct(Table $table) + + getValid(mixed $value, string $field, ...) : mixed + - validate(mixed $value, array $dbField) : bool + - getDefault(array $dbField) : mixed + - parseDataType(string $datatype) : array + - getDatabaseField(string $field, string $table) : array + - validateInteger(mixed $value, array $typeInfo) : bool + - validateString(mixed $value, array $typeInfo) : bool + - validateText(mixed $value, array $typeInfo) : bool + - validateFloat(mixed $value, array $typeInfo) : bool + - validateDecimal(mixed $value, array $typeInfo) : bool + - validateDate(mixed $value, array $typeInfo) : bool + - validateJson(mixed $value, array $typeInfo) : bool + - validateBlob(mixed $value, array $typeInfo) : bool +} + +note right of Validator::__construct + Constructor. + + since: 5.3.0 +end note + +note left of Validator::getValid + Returns the valid value based on datatype definition. +If the value is valid, return it. If not, return the default value, +NULL (if allowed), or an empty string if 'EMPTY' is set. + + since: 5.3.0 + return: mixed + + arguments: + mixed $value + string $field + string $table +end note + +note right of Validator::validate + Validate if the given value is valid for the provided database field. +This is a private method as `getValid()` will handle the actual logic. + + since: 5.3.0 + return: bool +end note + +note left of Validator::getDefault + Handle returning the default value, null, or empty string if validation fails. + + since: 5.3.0 + return: mixed +end note + +note right of Validator::parseDataType + Parse the data type from the database field and extract details like type, size, and precision. + + since: 5.3.0 + return: array +end note + +note left of Validator::getDatabaseField + Retrieve the database field structure for the specified field and table. +In your case, you use `$db = $this->table->get($table, $field, 'db')`. + + since: 5.3.0 + return: array +end note + +note right of Validator::validateInteger + Validate integer types (including tinyint, smallint, mediumint, etc.). + + since: 5.3.0 + return: bool +end note + +note left of Validator::validateString + Validate string types like VARCHAR and CHAR. + + since: 5.3.0 + return: bool +end note + +note right of Validator::validateText + Validate text types like TEXT, TINYTEXT, MEDIUMTEXT, LONGTEXT. + + since: 5.3.0 + return: bool +end note + +note left of Validator::validateFloat + Validate float, double, and decimal types. + + since: 5.3.0 + return: bool +end note + +note right of Validator::validateDecimal + Validate decimal types (numeric precision and scale). + + since: 5.3.0 + return: bool +end note + +note left of Validator::validateDate + Validate date, datetime, timestamp, and time types. + + since: 5.3.0 + return: bool +end note + +note right of Validator::validateJson + Validate JSON types. + + since: 5.3.0 + return: bool +end note + +note left of Validator::validateBlob + Validate BLOB types (including TINYBLOB, MEDIUMBLOB, LONGBLOB). + + since: 5.3.0 + return: bool +end note + +@enduml +``` + +The Power feature in JCB allows you to write PHP classes and their implementations, making it easy to include them in your Joomla project. JCB handles linking, autoloading, namespacing, and folder structure creation for you. + +By using the SPK (Super Power Key) in your custom code (replacing the class name in your code with the SPK), JCB will automatically pull the power from the repository into your project. This makes it available in your JCB instance, allowing you to edit it and include the class in your generated Joomla component. + +JCB uses placeholders like [[[`NamespacePrefix`]]] and [[[`ComponentNamespace`]]] in namespacing to prevent collisions and improve reusability across different JCB systems. You can also set the **JCB powers path** globally or per component under the **Dynamic Integration** tab, providing flexibility and easy maintainability. + +To add this specific Power to your project in JCB: + +> simply use this SPK +``` +Super---79fd4f39_824d_4ab6_936d_959705ff24ec---Power +``` +> remember to replace the `---` with `___` to activate this Power in your code + +--- +``` + ██╗ ██████╗██████╗ + ██║██╔════╝██╔══██╗ + ██║██║ ██████╔╝ +██ ██║██║ ██╔══██╗ +╚█████╔╝╚██████╗██████╔╝ + ╚════╝ ╚═════╝╚═════╝ +``` +> Build with [Joomla Component Builder](https://git.vdm.dev/joomla/Component-Builder) + diff --git a/src/79fd4f39-824d-4ab6-936d-959705ff24ec/code.php b/src/79fd4f39-824d-4ab6-936d-959705ff24ec/code.php new file mode 100644 index 0000000..887a066 --- /dev/null +++ b/src/79fd4f39-824d-4ab6-936d-959705ff24ec/code.php @@ -0,0 +1,345 @@ + + * @git Joomla Component Builder + * @copyright Copyright (C) 2015 Vast Development Method. All rights reserved. + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +namespace VDM\Joomla\Componentbuilder\Table; + + +use VDM\Joomla\Componentbuilder\Table; + + +/** + * Table Value Validator + * + * @since 5.3.0 + */ +final class Validator +{ + /** + * The Table Class. + * + * @var Table + * @since 5.3.0 + */ + protected Table $table; + + /** + * A map of MySQL base types to their respective validation methods. + * + * @var array + * @since 5.3.0 + */ + protected array $validators = []; + + /** + * Constructor. + * + * @param Table $table The Table Class. + * + * @since 5.3.0 + */ + public function __construct(Table $table) + { + $this->table = $table; + + // Register datatype validators (mapping MySQL types to handlers) + $this->validators = [ + 'int' => [$this, 'validateInteger'], + 'tinyint' => [$this, 'validateInteger'], + 'smallint' => [$this, 'validateInteger'], + 'mediumint' => [$this, 'validateInteger'], + 'bigint' => [$this, 'validateInteger'], + 'varchar' => [$this, 'validateString'], + 'char' => [$this, 'validateString'], + 'text' => [$this, 'validateText'], + 'tinytext' => [$this, 'validateText'], + 'mediumtext' => [$this, 'validateText'], + 'longtext' => [$this, 'validateText'], + 'decimal' => [$this, 'validateDecimal'], + 'float' => [$this, 'validateFloat'], + 'double' => [$this, 'validateFloat'], + 'date' => [$this, 'validateDate'], + 'datetime' => [$this, 'validateDate'], + 'timestamp' => [$this, 'validateDate'], + 'time' => [$this, 'validateDate'], + 'json' => [$this, 'validateJson'], + 'blob' => [$this, 'validateBlob'], + ]; + } + + /** + * Returns the valid value based on datatype definition. + * If the value is valid, return it. If not, return the default value, + * NULL (if allowed), or an empty string if 'EMPTY' is set. + * + * @param mixed $value The value to validate. + * @param string $field The field name. + * @param string $table The table name. + * + * @return mixed Returns the valid value, or the default, NULL, or empty string based on validation. + * @since 5.3.0 + */ + public function getValid($value, string $field, string $table) + { + // Get the database field definition + if (($dbField = $this->getDatabaseField($field, $table)) === null) + { + return null; // not legal field or table + } + + // Check if the value is valid for the field + if ($this->validate($value, $dbField)) + { + return $value; + } + + // If invalid, return default, NULL (if allowed), or empty string + return $this->getDefault($dbField); + } + + /** + * Validate if the given value is valid for the provided database field. + * This is a private method as `getValid()` will handle the actual logic. + * + * @param mixed $value The value to validate. + * @param array $dbField The database field details (type, default, null_switch, etc.). + * + * @return bool Returns true if the value is valid, false otherwise. + * @since 5.3.0 + */ + private function validate($value, array $dbField): bool + { + // Extract datatype and handle the validation + $typeInfo = $this->parseDataType($dbField['type']); + $baseType = $typeInfo['type']; + + // Use the appropriate validator if it exists + if (isset($this->validators[$baseType])) + { + return call_user_func($this->validators[$baseType], $value, $typeInfo); + } + + // If no validator exists, assume invalid + return false; + } + + /** + * Handle returning the default value, null, or empty string if validation fails. + * + * @param array $dbField The database field details. + * + * @return mixed The default value, null, or empty string based on field settings. + * @since 5.3.0 + */ + private function getDefault(array $dbField) + { + // If a default value is provided, return it + if (!empty($dbField['default'])) + { + return strtoupper($dbField['default']) === 'EMPTY' ? '' : $dbField['default']; + } + + // Check if NULL is allowed + if (isset($dbField['null_switch']) && strtoupper($dbField['null_switch']) === 'NULL') + { + return null; + } + + // If no default value, return an empty string + return ''; + } + + /** + * Parse the data type from the database field and extract details like type, size, and precision. + * + * @param string $datatype The full MySQL datatype (e.g., VARCHAR(255)). + * + * @return array An array containing 'type', 'size', and other relevant info. + * @since 5.3.0 + */ + private function parseDataType(string $datatype): array + { + $pattern = '/(?\w+)(\((?\d+)(,\s*(?\d+))?\))?/i'; + preg_match($pattern, $datatype, $matches); + + return [ + 'type' => isset($matches['type']) ? strtolower($matches['type']) : strtolower($datatype), + 'size' => $matches['size'] ?? null, + 'precision' => $matches['precision'] ?? null, + ]; + } + + /** + * Retrieve the database field structure for the specified field and table. + * In your case, you use `$db = $this->table->get($table, $field, 'db')`. + * + * @param string $field The field name. + * @param string $table The table name. + * + * @return array The database field details, including type, default, null_switch, etc. + * @since 5.3.0 + */ + private function getDatabaseField(string $field, string $table): array + { + // Simulated retrieval of field details. Replace with actual logic. + return $this->table->get($table, $field, 'db'); + } + + // ----------------- Validation Methods ----------------- + + /** + * Validate integer types (including tinyint, smallint, mediumint, etc.). + * + * @param mixed $value The value to validate. + * @param array $typeInfo The parsed data type information. + * + * @return bool True if valid, false otherwise. + * @since 5.3.0 + */ + private function validateInteger($value, array $typeInfo): bool + { + if (!is_numeric($value)) + { + return false; + } + + $value = (int)$value; + if (isset($typeInfo['unsigned']) && $typeInfo['unsigned'] && $value < 0) + { + return false; + } + + return true; + } + + /** + * Validate string types like VARCHAR and CHAR. + * + * @param mixed $value The value to validate. + * @param array $typeInfo The parsed data type information. + * + * @return bool True if valid, false otherwise. + * @since 5.3.0 + */ + private function validateString($value, array $typeInfo): bool + { + if (!is_string($value)) + { + return false; + } + + // Check if the length exceeds the allowed size + if ($typeInfo['size'] !== null && strlen($value) > (int)$typeInfo['size']) + { + return false; + } + + return true; + } + + /** + * Validate text types like TEXT, TINYTEXT, MEDIUMTEXT, LONGTEXT. + * + * @param mixed $value The value to validate. + * @param array $typeInfo The parsed data type information. + * + * @return bool True if valid, false otherwise. + * @since 5.3.0 + */ + private function validateText($value, array $typeInfo): bool + { + return is_string($value); + } + + /** + * Validate float, double, and decimal types. + * + * @param mixed $value The value to validate. + * @param array $typeInfo The parsed data type information. + * + * @return bool True if valid, false otherwise. + * @since 5.3.0 + */ + private function validateFloat($value, array $typeInfo): bool + { + return is_numeric($value); + } + + /** + * Validate decimal types (numeric precision and scale). + * + * @param mixed $value The value to validate. + * @param array $typeInfo The parsed data type information. + * + * @return bool True if valid, false otherwise. + * @since 5.3.0 + */ + private function validateDecimal($value, array $typeInfo): bool + { + return is_numeric($value); + } + + /** + * Validate date, datetime, timestamp, and time types. + * + * @param mixed $value The value to validate. + * @param array $typeInfo The parsed data type information. + * + * @return bool True if valid, false otherwise. + * @since 5.3.0 + */ + private function validateDate($value, array $typeInfo): bool + { + $formats = [ + 'date' => 'Y-m-d', + 'datetime' => 'Y-m-d H:i:s', + 'timestamp' => 'Y-m-d H:i:s', + 'time' => 'H:i:s', + ]; + + if (!isset($formats[$typeInfo['type']])) + { + return false; + } + + $dateTime = \DateTime::createFromFormat($formats[$typeInfo['type']], $value); + return $dateTime && $dateTime->format($formats[$typeInfo['type']]) === $value; + } + + /** + * Validate JSON types. + * + * @param mixed $value The value to validate. + * @param array $typeInfo The parsed data type information. + * + * @return bool True if valid, false otherwise. + * @since 5.3.0 + */ + private function validateJson($value, array $typeInfo): bool + { + json_decode($value); + return json_last_error() === JSON_ERROR_NONE; + } + + /** + * Validate BLOB types (including TINYBLOB, MEDIUMBLOB, LONGBLOB). + * + * @param mixed $value The value to validate. + * @param array $typeInfo The parsed data type information. + * + * @return bool True if valid, false otherwise. + * @since 5.3.0 + */ + private function validateBlob($value, array $typeInfo): bool + { + return is_string($value) || is_resource($value); + } +} + diff --git a/src/79fd4f39-824d-4ab6-936d-959705ff24ec/code.power b/src/79fd4f39-824d-4ab6-936d-959705ff24ec/code.power new file mode 100644 index 0000000..6b787c2 --- /dev/null +++ b/src/79fd4f39-824d-4ab6-936d-959705ff24ec/code.power @@ -0,0 +1,319 @@ + /** + * The Table Class. + * + * @var Table + * @since 5.3.0 + */ + protected Table $table; + + /** + * A map of MySQL base types to their respective validation methods. + * + * @var array + * @since 5.3.0 + */ + protected array $validators = []; + + /** + * Constructor. + * + * @param Table $table The Table Class. + * + * @since 5.3.0 + */ + public function __construct(Table $table) + { + $this->table = $table; + + // Register datatype validators (mapping MySQL types to handlers) + $this->validators = [ + 'int' => [$this, 'validateInteger'], + 'tinyint' => [$this, 'validateInteger'], + 'smallint' => [$this, 'validateInteger'], + 'mediumint' => [$this, 'validateInteger'], + 'bigint' => [$this, 'validateInteger'], + 'varchar' => [$this, 'validateString'], + 'char' => [$this, 'validateString'], + 'text' => [$this, 'validateText'], + 'tinytext' => [$this, 'validateText'], + 'mediumtext' => [$this, 'validateText'], + 'longtext' => [$this, 'validateText'], + 'decimal' => [$this, 'validateDecimal'], + 'float' => [$this, 'validateFloat'], + 'double' => [$this, 'validateFloat'], + 'date' => [$this, 'validateDate'], + 'datetime' => [$this, 'validateDate'], + 'timestamp' => [$this, 'validateDate'], + 'time' => [$this, 'validateDate'], + 'json' => [$this, 'validateJson'], + 'blob' => [$this, 'validateBlob'], + ]; + } + + /** + * Returns the valid value based on datatype definition. + * If the value is valid, return it. If not, return the default value, + * NULL (if allowed), or an empty string if 'EMPTY' is set. + * + * @param mixed $value The value to validate. + * @param string $field The field name. + * @param string $table The table name. + * + * @return mixed Returns the valid value, or the default, NULL, or empty string based on validation. + * @since 5.3.0 + */ + public function getValid($value, string $field, string $table) + { + // Get the database field definition + if (($dbField = $this->getDatabaseField($field, $table)) === null) + { + return null; // not legal field or table + } + + // Check if the value is valid for the field + if ($this->validate($value, $dbField)) + { + return $value; + } + + // If invalid, return default, NULL (if allowed), or empty string + return $this->getDefault($dbField); + } + + /** + * Validate if the given value is valid for the provided database field. + * This is a private method as `getValid()` will handle the actual logic. + * + * @param mixed $value The value to validate. + * @param array $dbField The database field details (type, default, null_switch, etc.). + * + * @return bool Returns true if the value is valid, false otherwise. + * @since 5.3.0 + */ + private function validate($value, array $dbField): bool + { + // Extract datatype and handle the validation + $typeInfo = $this->parseDataType($dbField['type']); + $baseType = $typeInfo['type']; + + // Use the appropriate validator if it exists + if (isset($this->validators[$baseType])) + { + return call_user_func($this->validators[$baseType], $value, $typeInfo); + } + + // If no validator exists, assume invalid + return false; + } + + /** + * Handle returning the default value, null, or empty string if validation fails. + * + * @param array $dbField The database field details. + * + * @return mixed The default value, null, or empty string based on field settings. + * @since 5.3.0 + */ + private function getDefault(array $dbField) + { + // If a default value is provided, return it + if (!empty($dbField['default'])) + { + return strtoupper($dbField['default']) === 'EMPTY' ? '' : $dbField['default']; + } + + // Check if NULL is allowed + if (isset($dbField['null_switch']) && strtoupper($dbField['null_switch']) === 'NULL') + { + return null; + } + + // If no default value, return an empty string + return ''; + } + + /** + * Parse the data type from the database field and extract details like type, size, and precision. + * + * @param string $datatype The full MySQL datatype (e.g., VARCHAR(255)). + * + * @return array An array containing 'type', 'size', and other relevant info. + * @since 5.3.0 + */ + private function parseDataType(string $datatype): array + { + $pattern = '/(?\w+)(\((?\d+)(,\s*(?\d+))?\))?/i'; + preg_match($pattern, $datatype, $matches); + + return [ + 'type' => isset($matches['type']) ? strtolower($matches['type']) : strtolower($datatype), + 'size' => $matches['size'] ?? null, + 'precision' => $matches['precision'] ?? null, + ]; + } + + /** + * Retrieve the database field structure for the specified field and table. + * In your case, you use `$db = $this->table->get($table, $field, 'db')`. + * + * @param string $field The field name. + * @param string $table The table name. + * + * @return array The database field details, including type, default, null_switch, etc. + * @since 5.3.0 + */ + private function getDatabaseField(string $field, string $table): array + { + // Simulated retrieval of field details. Replace with actual logic. + return $this->table->get($table, $field, 'db'); + } + + // ----------------- Validation Methods ----------------- + + /** + * Validate integer types (including tinyint, smallint, mediumint, etc.). + * + * @param mixed $value The value to validate. + * @param array $typeInfo The parsed data type information. + * + * @return bool True if valid, false otherwise. + * @since 5.3.0 + */ + private function validateInteger($value, array $typeInfo): bool + { + if (!is_numeric($value)) + { + return false; + } + + $value = (int)$value; + if (isset($typeInfo['unsigned']) && $typeInfo['unsigned'] && $value < 0) + { + return false; + } + + return true; + } + + /** + * Validate string types like VARCHAR and CHAR. + * + * @param mixed $value The value to validate. + * @param array $typeInfo The parsed data type information. + * + * @return bool True if valid, false otherwise. + * @since 5.3.0 + */ + private function validateString($value, array $typeInfo): bool + { + if (!is_string($value)) + { + return false; + } + + // Check if the length exceeds the allowed size + if ($typeInfo['size'] !== null && strlen($value) > (int)$typeInfo['size']) + { + return false; + } + + return true; + } + + /** + * Validate text types like TEXT, TINYTEXT, MEDIUMTEXT, LONGTEXT. + * + * @param mixed $value The value to validate. + * @param array $typeInfo The parsed data type information. + * + * @return bool True if valid, false otherwise. + * @since 5.3.0 + */ + private function validateText($value, array $typeInfo): bool + { + return is_string($value); + } + + /** + * Validate float, double, and decimal types. + * + * @param mixed $value The value to validate. + * @param array $typeInfo The parsed data type information. + * + * @return bool True if valid, false otherwise. + * @since 5.3.0 + */ + private function validateFloat($value, array $typeInfo): bool + { + return is_numeric($value); + } + + /** + * Validate decimal types (numeric precision and scale). + * + * @param mixed $value The value to validate. + * @param array $typeInfo The parsed data type information. + * + * @return bool True if valid, false otherwise. + * @since 5.3.0 + */ + private function validateDecimal($value, array $typeInfo): bool + { + return is_numeric($value); + } + + /** + * Validate date, datetime, timestamp, and time types. + * + * @param mixed $value The value to validate. + * @param array $typeInfo The parsed data type information. + * + * @return bool True if valid, false otherwise. + * @since 5.3.0 + */ + private function validateDate($value, array $typeInfo): bool + { + $formats = [ + 'date' => 'Y-m-d', + 'datetime' => 'Y-m-d H:i:s', + 'timestamp' => 'Y-m-d H:i:s', + 'time' => 'H:i:s', + ]; + + if (!isset($formats[$typeInfo['type']])) + { + return false; + } + + $dateTime = \DateTime::createFromFormat($formats[$typeInfo['type']], $value); + return $dateTime && $dateTime->format($formats[$typeInfo['type']]) === $value; + } + + /** + * Validate JSON types. + * + * @param mixed $value The value to validate. + * @param array $typeInfo The parsed data type information. + * + * @return bool True if valid, false otherwise. + * @since 5.3.0 + */ + private function validateJson($value, array $typeInfo): bool + { + json_decode($value); + return json_last_error() === JSON_ERROR_NONE; + } + + /** + * Validate BLOB types (including TINYBLOB, MEDIUMBLOB, LONGBLOB). + * + * @param mixed $value The value to validate. + * @param array $typeInfo The parsed data type information. + * + * @return bool True if valid, false otherwise. + * @since 5.3.0 + */ + private function validateBlob($value, array $typeInfo): bool + { + return is_string($value) || is_resource($value); + } \ No newline at end of file diff --git a/src/79fd4f39-824d-4ab6-936d-959705ff24ec/settings.json b/src/79fd4f39-824d-4ab6-936d-959705ff24ec/settings.json new file mode 100644 index 0000000..eccd6e1 --- /dev/null +++ b/src/79fd4f39-824d-4ab6-936d-959705ff24ec/settings.json @@ -0,0 +1,24 @@ +{ + "add_head": "0", + "add_licensing_template": "2", + "extends": "", + "guid": "79fd4f39-824d-4ab6-936d-959705ff24ec", + "implements": null, + "load_selection": null, + "name": "Validator", + "power_version": "1.0.0", + "system_name": "VDM.Table.Validator", + "type": "final class", + "use_selection": { + "use_selection0": { + "use": "bfd1d6d5-56c1-4fe9-9fee-1c5910e1f5d8", + "as": "default" + } + }, + "extendsinterfaces": null, + "namespace": "[[[NamespacePrefix]]]\\Joomla\\[[[ComponentNamespace]]].Table.Validator", + "description": "Table Value Validator\r\n\r\n@since 5.3.0", + "licensing_template": "\/**\r\n * @package Joomla.Component.Builder\r\n *\r\n * @created 4th September, 2022\r\n * @author Llewellyn van der Merwe \r\n * @git Joomla Component Builder \r\n * @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.\r\n * @license GNU General Public License version 2 or later; see LICENSE.txt\r\n *\/\r\n", + "head": "", + "composer": "" +} \ No newline at end of file diff --git a/super-powers.json b/super-powers.json index 490b7b3..4a0ef71 100644 --- a/super-powers.json +++ b/super-powers.json @@ -637,6 +637,17 @@ "spk": "Super---7832a726_87b6_4e95_887e_7b725d3fab8f---Power", "guid": "7832a726-87b6-4e95-887e-7b725d3fab8f" }, + "79fd4f39-824d-4ab6-936d-959705ff24ec": { + "name": "Validator", + "type": "final class", + "namespace": "VDM\\Joomla\\Componentbuilder\\Table", + "code": "src\/79fd4f39-824d-4ab6-936d-959705ff24ec\/code.php", + "power": "src\/79fd4f39-824d-4ab6-936d-959705ff24ec\/code.power", + "settings": "src\/79fd4f39-824d-4ab6-936d-959705ff24ec\/settings.json", + "path": "src\/79fd4f39-824d-4ab6-936d-959705ff24ec", + "spk": "Super---79fd4f39_824d_4ab6_936d_959705ff24ec---Power", + "guid": "79fd4f39-824d-4ab6-936d-959705ff24ec" + }, "7c1fb50f-8fb1-4627-8705-6fedf7182ca5": { "name": "Upsert", "type": "final class",