modifs.mineures sans commentaires
This commit is contained in:
parent
b4de22cc74
commit
3f357c9d45
|
@ -1,8 +1,8 @@
|
|||
# -*- coding: utf-8 mode: yaml -*- vim:sw=2:sts=2:et:ai:si:sta:fenc=utf-8
|
||||
composer_php_min: '7.3'
|
||||
composer_php_min: '7.4'
|
||||
composer_php_max: '8.0'
|
||||
composer_registry: pubdocker.univ-reunion.fr
|
||||
composer_image: image/phpbuilder:d10
|
||||
composer_image: image/phpbuilder:d11
|
||||
require:
|
||||
branch:
|
||||
master:
|
||||
|
|
|
@ -44,7 +44,7 @@
|
|||
<path value="$PROJECT_DIR$/vendor/theseer/tokenizer" />
|
||||
</include_path>
|
||||
</component>
|
||||
<component name="PhpProjectSharedConfiguration" php_language_level="7.3" />
|
||||
<component name="PhpProjectSharedConfiguration" php_language_level="7.4" />
|
||||
<component name="PhpStanOptionsConfiguration">
|
||||
<option name="transferred" value="true" />
|
||||
</component>
|
||||
|
|
|
@ -9,10 +9,10 @@
|
|||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=7.3"
|
||||
"php": ">=7.4"
|
||||
},
|
||||
"require-dev": {
|
||||
"nulib/tests": "7.3",
|
||||
"nulib/tests": "7.4",
|
||||
"ext-curl": "*"
|
||||
},
|
||||
"autoload": {
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "f05e766a09e965a7dd63ce61d5274eab",
|
||||
"content-hash": "d0cd1ea89a82b87aa4a078dcea524293",
|
||||
"packages": [],
|
||||
"packages-dev": [
|
||||
{
|
||||
|
@ -194,7 +194,7 @@
|
|||
},
|
||||
{
|
||||
"name": "nulib/tests",
|
||||
"version": "7.3",
|
||||
"version": "7.4",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://git.univ-reunion.fr/sda-php/nulib-tests.git",
|
||||
|
@ -1778,7 +1778,7 @@
|
|||
"prefer-stable": false,
|
||||
"prefer-lowest": false,
|
||||
"platform": {
|
||||
"php": ">=7.3"
|
||||
"php": ">=7.4"
|
||||
},
|
||||
"platform-dev": {
|
||||
"ext-curl": "*"
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
namespace nulib\sys;
|
||||
|
||||
/**
|
||||
* Interface IContent: un objet capable de produire du contenu à afficher. le
|
||||
* contenu retourné doit être pris tel quel,sans plus d'analyse
|
||||
*/
|
||||
interface IContent {
|
||||
/** retourner le contenu à afficher */
|
||||
function getContent(): iterable;
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
namespace nulib\sys;
|
||||
|
||||
/**
|
||||
* Interface IPrintable: un objet qui écrit du contenu sur la sortie standard
|
||||
*/
|
||||
interface IPrintable {
|
||||
/** afficher le contenu */
|
||||
function print(): void;
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
namespace nulib\sys;
|
||||
|
||||
/**
|
||||
* Interface IStaticContent: comme {@link IContent} mais la liste retournée est
|
||||
* une liste de contenu statique à analyser récursivement
|
||||
*/
|
||||
interface IStaticContent extends IContent {
|
||||
}
|
|
@ -0,0 +1,146 @@
|
|||
<?php
|
||||
namespace nulib\sys;
|
||||
|
||||
use nulib\cl;
|
||||
|
||||
/**
|
||||
* Class content: gestionnaire de contenu (statique ou dynamique)
|
||||
*/
|
||||
class content {
|
||||
/** quoter $vs si c'est un scalaire. sinon retourner le tableau tel quel */
|
||||
static final function quote($contents): iterable {
|
||||
if ($contents === null || $contents === false) return [];
|
||||
elseif ($contents instanceof IContent) return [$contents];
|
||||
elseif ($contents instanceof IPrintable) return [$contents];
|
||||
elseif (is_iterable($contents)) return $contents;
|
||||
else return [htmlspecialchars(strval($contents))];
|
||||
}
|
||||
|
||||
/**
|
||||
* applatir $contents, et retourner une liste de valeurs scalaires:
|
||||
* - les instances de IContent et IPrintable sont remplacées par leur contenu
|
||||
* - les chaines sont ajoutées telles quelles
|
||||
* - les autres objets sont transformés en chaine de caractère avec strval()
|
||||
*/
|
||||
static function flatten($contents, ?array &$dest=null): array {
|
||||
if ($dest === null) $dest = [];
|
||||
if ($contents === null) return $dest;
|
||||
if ($contents instanceof IContent) {
|
||||
$contents = $contents->getContent();
|
||||
} elseif ($contents instanceof IPrintable) {
|
||||
ob_start(null, 0, PHP_OUTPUT_HANDLER_STDFLAGS ^ PHP_OUTPUT_HANDLER_FLUSHABLE);
|
||||
$contents->print();
|
||||
$dest[] = ob_get_clean();
|
||||
return $dest;
|
||||
} elseif (is_scalar($contents)) {
|
||||
$dest[] = $contents;
|
||||
return $dest;
|
||||
} elseif (!is_iterable($contents)) {
|
||||
$dest[] = strval($contents);
|
||||
return $dest;
|
||||
}
|
||||
# sinon parcourir $contents
|
||||
foreach ($contents as $value) {
|
||||
if ($value === null) continue;
|
||||
elseif (is_scalar($value)) $dest[] = $value;
|
||||
else self::flatten($value, $dest);
|
||||
}
|
||||
return $dest;
|
||||
}
|
||||
|
||||
/**
|
||||
* résoudre tout le contenu statique: il ne restera plus qu'une liste de
|
||||
* chaines ou d'instances de IContent, IPrintable ou des objets quelconques
|
||||
*/
|
||||
static function resolve_static($contents, $dest_class=null, bool $quote=true, ?array &$dest=null): array {
|
||||
if ($dest === null) $dest = [];
|
||||
if ($contents === null) return $dest;
|
||||
if (is_string($contents)) {
|
||||
if ($quote) $contents = htmlspecialchars($contents);
|
||||
$dest[] = $contents;
|
||||
return $dest;
|
||||
} elseif ($contents instanceof IStaticContent) {
|
||||
$contents = $contents->getContent();
|
||||
$dest = cl::merge($dest, self::resolve_static($contents, $dest_class, false));
|
||||
return $dest;
|
||||
} elseif ($contents instanceof IContent) {
|
||||
$dest[] = $contents;
|
||||
return $dest;
|
||||
} elseif ($contents instanceof IPrintable) {
|
||||
$dest[] = $contents;
|
||||
return $dest;
|
||||
} elseif (!is_iterable($contents)) {
|
||||
$dest[] = $contents;
|
||||
return $dest;
|
||||
}
|
||||
foreach ($contents as $content) {
|
||||
if ($content === null) continue;
|
||||
elseif ($contents instanceof IStaticContent) {
|
||||
$contents = $content->getContent();
|
||||
$dest = cl::merge($dest, self::resolve_static($contents, $dest_class, false));
|
||||
} elseif ($contents instanceof IContent) $dest[] = $content;
|
||||
elseif ($contents instanceof IPrintable) $dest[] = $content;
|
||||
elseif (!is_array($content)) $dest[] = $content;
|
||||
else {
|
||||
# ici, $content est soit:
|
||||
# - un appel de fonction ou méthode statique d'une des formes suivantes:
|
||||
# - [class, method, ...args]
|
||||
# - [null, method, ...args]
|
||||
# - [[class, method], ...args]
|
||||
# - [[null, method], ...args]
|
||||
# - ou une définition d'objet d'une des formes suivantes
|
||||
# - [class, ...args]
|
||||
# - [[class], ...args]
|
||||
$func_isa_class = false;
|
||||
if (array_key_exists(0, $content) && is_array($content[0])) {
|
||||
# $content est de la forme [func_or_class, ...args]
|
||||
$func = $content[0];
|
||||
$args = array_slice($content, 1);
|
||||
if (func::is_static($func)) {
|
||||
func::ensure_func($func, $dest_class, $args);
|
||||
} elseif (func::is_class($func)) {
|
||||
$func_isa_class = true;
|
||||
func::fix_class_args($func, $args);
|
||||
} else {
|
||||
func::ensure_func($func, $dest_class, $args);
|
||||
}
|
||||
} else {
|
||||
# $content est de la forme $func
|
||||
$func = $content;
|
||||
if (func::is_static($func)) {
|
||||
func::ensure_func($func, $dest_class, $args);
|
||||
} elseif (func::is_class($func)) {
|
||||
$func_isa_class = true;
|
||||
func::fix_class_args($func, $args);
|
||||
} else {
|
||||
func::ensure_func($func, $dest_class, $args);
|
||||
}
|
||||
}
|
||||
# chaque argument est un contenu statique
|
||||
foreach ($args as &$arg) {
|
||||
$array = is_array($arg);
|
||||
$arg = self::resolve_static($arg, $dest_class, false);
|
||||
$arg = self::flatten($arg);
|
||||
if ($array) {
|
||||
$arg = [implode("", $arg)];
|
||||
} else {
|
||||
switch (count($arg)) {
|
||||
case 0: $arg = null; break;
|
||||
case 1: $arg = $arg[0]; break;
|
||||
default: $arg = implode("", $arg);
|
||||
}
|
||||
}
|
||||
}; unset($arg);
|
||||
# puis appeler la fonction
|
||||
if ($func_isa_class) $content = func::cons($func, ...$args);
|
||||
else $content = func::call($func, ...$args);
|
||||
$dest[] = $content;
|
||||
}
|
||||
}
|
||||
return $dest;
|
||||
}
|
||||
|
||||
static function to_string($contents, $dest_class=null, bool $quote=true): string {
|
||||
return implode("", self::flatten(self::resolve_static($contents, $dest_class, $quote)));
|
||||
}
|
||||
}
|
|
@ -4,8 +4,8 @@ namespace nulib\sys;
|
|||
use Closure;
|
||||
use nulib\cl;
|
||||
use nulib\ValueException;
|
||||
use nur\sery\ref\sys\ref_func;
|
||||
use nur\sery\schema\Schema;
|
||||
use nulib\ref\sys\ref_func;
|
||||
use nulib\schema\Schema;
|
||||
use ReflectionClass;
|
||||
use ReflectionFunction;
|
||||
use ReflectionMethod;
|
||||
|
@ -18,8 +18,11 @@ class func {
|
|||
* tester si $func est une chaine de la forme "XXX::method" où XXX est une
|
||||
* chaine quelconque éventuellement vide, ou un tableau de la forme ["method"]
|
||||
* ou [anything, "method", ...]
|
||||
*
|
||||
* Avec la forme tableau, "method" ne doit pas contenir le caractère '\', pour
|
||||
* pouvoir utiliser conjointement {@link is_class()}
|
||||
*/
|
||||
static final function is_static($func): bool {
|
||||
static final function is_static($func, bool $allowClass=false): bool {
|
||||
if (is_string($func)) {
|
||||
$pos = strpos($func, "::");
|
||||
if ($pos === false) return false;
|
||||
|
@ -27,10 +30,14 @@ class func {
|
|||
} elseif (is_array($func) && array_key_exists(0, $func)) {
|
||||
$count = count($func);
|
||||
if ($count == 1) {
|
||||
return is_string($func[0]) && strlen($func[0]) > 0;
|
||||
if (!is_string($func[0]) || strlen($func[0]) == 0) return false;
|
||||
if (strpos($func[0], "\\") !== false) return false;
|
||||
return true;
|
||||
} elseif ($count > 1) {
|
||||
if (!array_key_exists(1, $func)) return false;
|
||||
return is_string($func[1]) && strlen($func[1]) > 0;
|
||||
if (!is_string($func[1]) || strlen($func[1]) == 0) return false;
|
||||
if (strpos($func[1], "\\") !== false) return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
@ -351,7 +358,8 @@ class func {
|
|||
*
|
||||
* NB: il est possible d'avoir {@link is_static()} et {@link is_class()}
|
||||
* vraies pour la même valeur. s'il faut supporter les deux cas, appeler
|
||||
* {@link is_class()} d'abord
|
||||
* {@link is_static()} d'abord, mais dans ce cas, on ne supporte que les
|
||||
* classes qui sont dans un package
|
||||
*/
|
||||
static final function is_class($class): bool {
|
||||
if (is_string($class)) {
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
namespace nulib\sys;
|
||||
|
||||
use nulib\sys\impl\html;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class contentTest extends TestCase {
|
||||
function testFlattern() {
|
||||
self::assertSame([], content::flatten(null));
|
||||
self::assertSame([], content::flatten([null]));
|
||||
self::assertSame([""], content::flatten(""));
|
||||
self::assertSame([""], content::flatten([""]));
|
||||
|
||||
self::assertSame(["a", "b", "c", "d"], content::flatten(["a", ["b", ["c"], "d"]]));
|
||||
}
|
||||
|
||||
function testResolve_static() {
|
||||
self::assertSame([], content::resolve_static(null, null));
|
||||
self::assertSame([], content::resolve_static([null], [null]));
|
||||
self::assertSame([""], content::resolve_static("", ""));
|
||||
self::assertSame([""], content::resolve_static([""], [""]));
|
||||
|
||||
self::assertSame(["<quoted>"], content::resolve_static("<quoted>", "<quoted>"));
|
||||
self::assertSame(["<non-quoted>"], content::resolve_static(["<non-quoted>"], ["<non-quoted>"]));
|
||||
|
||||
self::assertSame(
|
||||
"<h1>title<q/></h1><p>hello<nq/><span>brave<q/></span><span>world<nq/></span></p>",
|
||||
content::to_string([
|
||||
[html::H1, "title<q/>"],
|
||||
[html::P, [
|
||||
"hello<nq/>",
|
||||
[html::SPAN, "brave<q/>"],
|
||||
[html::SPAN, ["world<nq/>"]],
|
||||
]],
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace {
|
||||
function func36() {
|
||||
return 36;
|
||||
}
|
||||
function func36(): int { return 36; }
|
||||
|
||||
function func_m1($a): array { return [$a]; }
|
||||
function func_o1($b=9): array { return [$b]; }
|
||||
function func_v(...$c): array { return [...$c]; }
|
||||
function func_m1o1($a, $b=9): array { return [$a, $b]; }
|
||||
function func_m1v($a, ...$c): array { return [$a, ...$c]; }
|
||||
function func_m1o1v($a, $b=9, ...$c): array { return [$a, $b, ...$c]; }
|
||||
function func_o1v($b=9, ...$c): array { return [$b, ...$c]; }
|
||||
}
|
||||
|
||||
namespace nulib\sys {
|
||||
|
@ -143,6 +149,88 @@ namespace nulib\sys {
|
|||
self::assertSame(21, func::call($closure));
|
||||
}
|
||||
|
||||
function test_prepare_fill() {
|
||||
# vérifier que les arguments sont bien remplis, en fonction du fait qu'ils
|
||||
# soient obligatoires, facultatifs ou variadiques
|
||||
|
||||
# m1
|
||||
self::assertSame([null], func::call("func_m1"));
|
||||
self::assertSame([null], func::call("func_m1", null));
|
||||
self::assertSame([null], func::call("func_m1", null, null));
|
||||
self::assertSame([null], func::call("func_m1", null, null, null));
|
||||
self::assertSame([null], func::call("func_m1", null, null, null, null));
|
||||
self::assertSame([1], func::call("func_m1", 1));
|
||||
self::assertSame([1], func::call("func_m1", 1, 2));
|
||||
self::assertSame([1], func::call("func_m1", 1, 2, 3));
|
||||
self::assertSame([1], func::call("func_m1", 1, 2, 3, 4));
|
||||
|
||||
# o1
|
||||
self::assertSame([9], func::call("func_o1"));
|
||||
self::assertSame([null], func::call("func_o1", null));
|
||||
self::assertSame([null], func::call("func_o1", null, null));
|
||||
self::assertSame([null], func::call("func_o1", null, null, null));
|
||||
self::assertSame([null], func::call("func_o1", null, null, null, null));
|
||||
self::assertSame([1], func::call("func_o1", 1));
|
||||
self::assertSame([1], func::call("func_o1", 1, 2));
|
||||
self::assertSame([1], func::call("func_o1", 1, 2, 3));
|
||||
self::assertSame([1], func::call("func_o1", 1, 2, 3, 4));
|
||||
|
||||
# v
|
||||
self::assertSame([], func::call("func_v"));
|
||||
self::assertSame([null], func::call("func_v", null));
|
||||
self::assertSame([null, null], func::call("func_v", null, null));
|
||||
self::assertSame([null, null, null], func::call("func_v", null, null, null));
|
||||
self::assertSame([null, null, null, null], func::call("func_v", null, null, null, null));
|
||||
self::assertSame([1], func::call("func_v", 1));
|
||||
self::assertSame([1, 2], func::call("func_v", 1, 2));
|
||||
self::assertSame([1, 2, 3], func::call("func_v", 1, 2, 3));
|
||||
self::assertSame([1, 2, 3, 4], func::call("func_v", 1, 2, 3, 4));
|
||||
|
||||
# m1o1
|
||||
self::assertSame([null, 9], func::call("func_m1o1"));
|
||||
self::assertSame([null, 9], func::call("func_m1o1", null));
|
||||
self::assertSame([null, null], func::call("func_m1o1", null, null));
|
||||
self::assertSame([null, null], func::call("func_m1o1", null, null, null));
|
||||
self::assertSame([null, null], func::call("func_m1o1", null, null, null, null));
|
||||
self::assertSame([1, 9], func::call("func_m1o1", 1));
|
||||
self::assertSame([1, 2], func::call("func_m1o1", 1, 2));
|
||||
self::assertSame([1, 2], func::call("func_m1o1", 1, 2, 3));
|
||||
self::assertSame([1, 2], func::call("func_m1o1", 1, 2, 3, 4));
|
||||
|
||||
# m1v
|
||||
self::assertSame([null], func::call("func_m1v"));
|
||||
self::assertSame([null], func::call("func_m1v", null));
|
||||
self::assertSame([null, null], func::call("func_m1v", null, null));
|
||||
self::assertSame([null, null, null], func::call("func_m1v", null, null, null));
|
||||
self::assertSame([null, null, null, null], func::call("func_m1v", null, null, null, null));
|
||||
self::assertSame([1], func::call("func_m1v", 1));
|
||||
self::assertSame([1, 2], func::call("func_m1v", 1, 2));
|
||||
self::assertSame([1, 2, 3], func::call("func_m1v", 1, 2, 3));
|
||||
self::assertSame([1, 2, 3, 4], func::call("func_m1v", 1, 2, 3, 4));
|
||||
|
||||
# m1o1v
|
||||
self::assertSame([null, 9], func::call("func_m1o1v"));
|
||||
self::assertSame([null, 9], func::call("func_m1o1v", null));
|
||||
self::assertSame([null, null], func::call("func_m1o1v", null, null));
|
||||
self::assertSame([null, null, null], func::call("func_m1o1v", null, null, null));
|
||||
self::assertSame([null, null, null, null], func::call("func_m1o1v", null, null, null, null));
|
||||
self::assertSame([1, 9], func::call("func_m1o1v", 1));
|
||||
self::assertSame([1, 2], func::call("func_m1o1v", 1, 2));
|
||||
self::assertSame([1, 2, 3], func::call("func_m1o1v", 1, 2, 3));
|
||||
self::assertSame([1, 2, 3, 4], func::call("func_m1o1v", 1, 2, 3, 4));
|
||||
|
||||
# o1v
|
||||
self::assertSame([9], func::call("func_o1v"));
|
||||
self::assertSame([null], func::call("func_o1v", null));
|
||||
self::assertSame([null, null], func::call("func_o1v", null, null));
|
||||
self::assertSame([null, null, null], func::call("func_o1v", null, null, null));
|
||||
self::assertSame([null, null, null, null], func::call("func_o1v", null, null, null, null));
|
||||
self::assertSame([1], func::call("func_o1v", 1));
|
||||
self::assertSame([1, 2], func::call("func_o1v", 1, 2));
|
||||
self::assertSame([1, 2, 3], func::call("func_o1v", 1, 2, 3));
|
||||
self::assertSame([1, 2, 3, 4], func::call("func_o1v", 1, 2, 3, 4));
|
||||
}
|
||||
|
||||
function testCall_all() {
|
||||
$c1 = new C1();
|
||||
$c2 = new C2();
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
namespace nulib\sys\impl;
|
||||
|
||||
use nulib\sys\IContent;
|
||||
|
||||
class AContent implements IContent {
|
||||
function getContent(): iterable {
|
||||
return ["<span>content</span>"];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
namespace nulib\sys\impl;
|
||||
|
||||
use nulib\sys\IPrintable;
|
||||
|
||||
class APrintable implements IPrintable {
|
||||
function print(): void {
|
||||
echo "<p>printable</p>";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
namespace nulib\sys\impl;
|
||||
|
||||
use nulib\sys\IStaticContent;
|
||||
|
||||
class AStaticContent implements IStaticContent {
|
||||
function getContent(): iterable {
|
||||
return [
|
||||
[html::P, "static content"],
|
||||
];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
namespace nulib\sys\impl;
|
||||
|
||||
use nulib\sys\content;
|
||||
use nulib\sys\IStaticContent;
|
||||
|
||||
class ATag implements IStaticContent {
|
||||
function __construct(string $tag, $contents=null) {
|
||||
$this->tag = $tag;
|
||||
$this->contents = $contents;
|
||||
}
|
||||
|
||||
protected $tag;
|
||||
protected $contents;
|
||||
|
||||
function getContent(): iterable {
|
||||
return [
|
||||
"<$this->tag>",
|
||||
...content::quote($this->contents),
|
||||
"</$this->tag>",
|
||||
];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
namespace nulib\sys\impl;
|
||||
|
||||
class html {
|
||||
const H1 = [self::class, "h1"];
|
||||
const DIV = [self::class, "div"];
|
||||
const P = [self::class, "p"];
|
||||
const SPAN = [self::class, "span"];
|
||||
|
||||
static function h1($contents) { return new ATag("h1", $contents); }
|
||||
static function div($contents) { return new ATag("div", $contents); }
|
||||
static function p($contents) { return new ATag("p", $contents); }
|
||||
static function span($contents) { return new ATag("span", $contents); }
|
||||
}
|
Loading…
Reference in New Issue