mirror of
https://codeberg.org/vlw/scaffold.git
synced 2026-01-13 15:06:00 +01:00
Compare commits
3 commits
4fa45083d0
...
2f6e24b0a1
| Author | SHA1 | Date | |
|---|---|---|---|
| 2f6e24b0a1 | |||
| 9517203418 | |||
| d9fca6d4e1 |
8 changed files with 177 additions and 27 deletions
|
|
@ -10,7 +10,7 @@
|
|||
class API {
|
||||
/**
|
||||
* Create a new API request with automatic request validation
|
||||
*
|
||||
*
|
||||
* @param ?Ruleset $ruleset An optional Reflect\Rules\Ruleset to eval
|
||||
*/
|
||||
public function __construct(?Ruleset $ruleset = null) {
|
||||
|
|
|
|||
115
src/Database/Controller.php
Normal file
115
src/Database/Controller.php
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
<?php
|
||||
|
||||
namespace vlw\Scaffold\Database;
|
||||
|
||||
use ReflectionClass;
|
||||
use ReflectionProperty;
|
||||
use ReflectionNamedType;
|
||||
use ReflectionUnionType;
|
||||
|
||||
/**
|
||||
* Modify database entity values using an instanced database Model
|
||||
*/
|
||||
class Controller {
|
||||
/**
|
||||
* Returns an array of allowed property types
|
||||
*
|
||||
* @param ReflectionNamedType|ReflectionUnionType A \ReflectionType instance to retrieve types from
|
||||
* @return array<string> An array containing the allowed property types (literals and classes)
|
||||
*/
|
||||
private static function get_types(ReflectionNamedType|ReflectionUnionType $type): array {
|
||||
$types = [];
|
||||
|
||||
// Property value is nullable
|
||||
if ($type->allowsNull()) {
|
||||
$types[] = "null";
|
||||
}
|
||||
|
||||
// Property only accepts one type
|
||||
if ($type instanceof ReflectionNamedType) {
|
||||
// Add type name to output and trim shorthand nullable from name if present
|
||||
$types[] = ltrim($type->getName(), "?");
|
||||
return $types;
|
||||
}
|
||||
|
||||
return array_merge($types, $type->getTypes());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance of a class-like object
|
||||
*
|
||||
* @param string Namespaced class name to instance
|
||||
* @param array<mixed> Arguments to pass to the class constructor
|
||||
* @return object Instanced class
|
||||
*/
|
||||
private static function instance_class(string $class_name, array $args): object {
|
||||
$class = new ReflectionClass($class_name);
|
||||
|
||||
// Return new instance of Enum from case name
|
||||
if ($class->isEnum()) {
|
||||
return $class_name::fromName(...$args);
|
||||
}
|
||||
|
||||
return $class->newInstance(...$args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instance a new Controller for a given database Model
|
||||
*
|
||||
* @param Model An instance of a database Model entity
|
||||
*/
|
||||
public function __construct(private readonly Model $model) {}
|
||||
|
||||
/**
|
||||
* Update a database entity by passing an assoc array of column => value
|
||||
*
|
||||
* @param array<array> Associative array of column keys and column values to update
|
||||
* @return void
|
||||
*/
|
||||
public function update(array $values): void {
|
||||
foreach($values as $key => $value) {
|
||||
// Bail out if we're trying to update a column that has not been implemented
|
||||
// by the passed database Model
|
||||
if (!in_array($key, $this->model->columns)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$property = new ReflectionProperty($this->model, $key);
|
||||
$types = self::get_types($property->getSettableType());
|
||||
|
||||
// Value is definitely a primitive
|
||||
if (!is_string($value)) {
|
||||
// Property does not accept this primitive as value, bail out
|
||||
if (!in_array(gettype($value), $types)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Set primitve value and move on
|
||||
$this->model->{$key} = $value;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Returns the name of the first class object
|
||||
$class_name = array_find($types, fn(string $type): bool => class_exists($type));
|
||||
|
||||
// No class object found, string value must be literal
|
||||
if (!$class_name) {
|
||||
// Property does not accept string literals as value, bail out
|
||||
if (!in_array("string", $types)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Set string literal value and move on
|
||||
$this->model->{$key} = $value;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Wrap single value in an array (as one constructor argument)
|
||||
if (!is_array($value)) {
|
||||
$value = [$value];
|
||||
}
|
||||
|
||||
$this->model->{$key} = self::instance_class($class_name, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -29,7 +29,7 @@
|
|||
|
||||
/**
|
||||
* Paginate request by chaining this after vlw/MySQL->from() and before vlw/MySQL->select()
|
||||
*
|
||||
*
|
||||
* @param ?Paginate $paginate a vlw\Scaffold\Helper\Paginate instance
|
||||
* @return self Instance chainable with vlw\MySQL
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -10,7 +10,8 @@
|
|||
* Abstract reading, creating, and updating of database entities.
|
||||
*/
|
||||
abstract class Model {
|
||||
const DATE_FORMAT = Database::DATE_FORMAT;
|
||||
public const DATE_FORMAT = Database::DATE_FORMAT;
|
||||
public const DATETIME_FORMAT = Database::DATETIME_FORMAT;
|
||||
|
||||
abstract public string $id { get; }
|
||||
|
||||
|
|
@ -21,7 +22,7 @@
|
|||
|
||||
/**
|
||||
* Insert values into a database table
|
||||
*
|
||||
*
|
||||
* @param string $table The target database table
|
||||
*/
|
||||
protected static function create(string $table, array $values): bool {
|
||||
|
|
@ -30,22 +31,22 @@
|
|||
|
||||
/**
|
||||
* Create a new Model to retrieve a specific entity
|
||||
*
|
||||
*
|
||||
* @param string $table Target table where the entity is stored
|
||||
* @param array<string> $columns An array of strings for each target database column
|
||||
* @param array<array> $where vlw/MySQL->where() to locate the target entity
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly string $table,
|
||||
private readonly array $columns,
|
||||
private readonly array $where
|
||||
public readonly string $table,
|
||||
public readonly array $columns,
|
||||
public readonly array $where
|
||||
) {
|
||||
$this->db = new Database();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch and store column values for instanced row
|
||||
*
|
||||
*
|
||||
* @return ?array<array> The column and value of each target database column
|
||||
*/
|
||||
private ?array $row {
|
||||
|
|
@ -69,7 +70,7 @@
|
|||
|
||||
/**
|
||||
* Returns true if instanced target ebity exists
|
||||
*
|
||||
*
|
||||
* @return bool Entity exists
|
||||
*/
|
||||
public function isset(): bool {
|
||||
|
|
@ -83,7 +84,7 @@
|
|||
|
||||
/**
|
||||
* Get current row column value
|
||||
*
|
||||
*
|
||||
* @param string $key Target column
|
||||
* @return mixed Value of target column
|
||||
*/
|
||||
|
|
@ -93,7 +94,7 @@
|
|||
|
||||
/**
|
||||
* Update current row column with value
|
||||
*
|
||||
*
|
||||
* @param string $key Target column to update
|
||||
* @param mixed $value New value of target column
|
||||
* @return bool Update was successful
|
||||
|
|
|
|||
34
src/Helpers/License.php
Normal file
34
src/Helpers/License.php
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace vlw\Scaffold\Helpers;
|
||||
|
||||
class License {
|
||||
public const MAGNET_END = PHP_EOL . "// @license-end";
|
||||
|
||||
public const GPL_V3_MAGNET = "// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later";
|
||||
public const GPL_V3_BLOCK = "<script><!--//--><![CDATA[//><!--
|
||||
/**
|
||||
* @licstart The following is the entire license notice for the JavaScript
|
||||
* code in this page.
|
||||
*
|
||||
* Copyright (C) 2020 Free Software Foundation.
|
||||
*
|
||||
* The JavaScript code in this page is free software: you can redistribute
|
||||
* it and/or modify it under the terms of the GNU General Public License
|
||||
* (GNU GPL) as published by the Free Software Foundation, either version 3
|
||||
* of the License, or (at your option) any later version. The code is
|
||||
* distributed WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU GPL
|
||||
* for more details.
|
||||
*
|
||||
* As additional permission under GNU GPL version 3 section 7, you may
|
||||
* distribute non-source (e.g., minimized or compacted) forms of that code
|
||||
* without the copy of the GNU GPL normally required by section 4, provided
|
||||
* you include this license notice and a URL through which recipients can
|
||||
* access the Corresponding Source.
|
||||
*
|
||||
* @licend The above is the entire license notice for the JavaScript code
|
||||
* in this page.
|
||||
*/
|
||||
//--><!]]></script>";
|
||||
}
|
||||
|
|
@ -13,7 +13,7 @@
|
|||
|
||||
/**
|
||||
* Start a new Paginate instance
|
||||
*
|
||||
*
|
||||
* @param int $total Total amount of items to paginate
|
||||
* @param ?int $size Amount of items to display on each page
|
||||
* @param ?int $current Index of current page
|
||||
|
|
@ -40,7 +40,7 @@
|
|||
|
||||
/**
|
||||
* Get current database offset
|
||||
*
|
||||
*
|
||||
* @return int Database offset of first item on current page
|
||||
*/
|
||||
public int $offset {
|
||||
|
|
@ -49,7 +49,7 @@
|
|||
|
||||
/**
|
||||
* Get total amount of pages
|
||||
*
|
||||
*
|
||||
* @return int Total number of pages
|
||||
*/
|
||||
public int $pages {
|
||||
|
|
@ -58,7 +58,7 @@
|
|||
|
||||
/**
|
||||
* Returns true if a previous page exists
|
||||
*
|
||||
*
|
||||
* @return bool Previous page exists
|
||||
*/
|
||||
public bool $previous {
|
||||
|
|
@ -67,7 +67,7 @@
|
|||
|
||||
/**
|
||||
* True if a next page exists
|
||||
*
|
||||
*
|
||||
* @return bool Next page exists
|
||||
*/
|
||||
public bool $next {
|
||||
|
|
@ -76,7 +76,7 @@
|
|||
|
||||
/**
|
||||
* Get pages witin ±range from current page
|
||||
*
|
||||
*
|
||||
* @param ?int $range Desired range from current page to return
|
||||
* @return array<int> Indecies of pages within the desired range
|
||||
*/
|
||||
|
|
@ -95,7 +95,7 @@
|
|||
|
||||
/**
|
||||
* Returns a url to a page by index
|
||||
*
|
||||
*
|
||||
* @param int $page Page index
|
||||
* @return string Search parameters to the target page
|
||||
*/
|
||||
|
|
@ -105,7 +105,7 @@
|
|||
|
||||
/**
|
||||
* Returns a url to the previous page
|
||||
*
|
||||
*
|
||||
* @return string Search parameters to the previous page
|
||||
*/
|
||||
public function previous(): string {
|
||||
|
|
@ -114,7 +114,7 @@
|
|||
|
||||
/**
|
||||
* Returns a url to the next page
|
||||
*
|
||||
*
|
||||
* @return string Search parameters to the next page
|
||||
*/
|
||||
public function next(): string {
|
||||
|
|
@ -123,7 +123,7 @@
|
|||
|
||||
/**
|
||||
* Clamp page within range of all pages
|
||||
*
|
||||
*
|
||||
* @param int $page Page index
|
||||
* @return int Clamped page index
|
||||
*/
|
||||
|
|
@ -133,7 +133,7 @@
|
|||
|
||||
/**
|
||||
* Construct search parameters for a page while preserving existing search parameters
|
||||
*
|
||||
*
|
||||
* @param int $page Page index
|
||||
* @return string A search parameter to the target page
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
|
||||
/**
|
||||
* Generate an all binary 0:s UUID
|
||||
*
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function nil(): string {
|
||||
|
|
@ -19,7 +19,7 @@
|
|||
|
||||
/**
|
||||
* Generate an all binary 1:s UUID
|
||||
*
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function max(): string {
|
||||
|
|
@ -28,7 +28,7 @@
|
|||
|
||||
/**
|
||||
* Generate a version 4 UUID
|
||||
*
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function v4(): string {
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
class Scaffold {
|
||||
/**
|
||||
* Include a source file
|
||||
*
|
||||
*
|
||||
* @param string $pathname Relative path from project root to a source file
|
||||
*/
|
||||
public static function load(string $pathname): void {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue