From 3aec24ddb375b7cf18c10a7f866e5b554ba838e5 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Fri, 23 May 2025 16:53:08 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- php/src/cv.php | 24 ++++++++++++++++++++---- php/src/file/Stream.php | 21 ++++++++++++++------- php/src/php/time/DateInterval.php | 2 +- php/src/php/time/Delay.php | 9 +++++++-- 4 files changed, 42 insertions(+), 14 deletions(-) diff --git a/php/src/cv.php b/php/src/cv.php index 3bd8773..fd61a8e 100644 --- a/php/src/cv.php +++ b/php/src/cv.php @@ -86,6 +86,12 @@ class cv { $b = $tmp; } + /** cloner une valeur */ + static final function clone($value) { + if (is_object($value)) $value = clone $value; + return $value; + } + ############################################################################# /** mettre à jour $dest avec $value si $cond($value) est vrai */ @@ -197,19 +203,29 @@ class cv { } /** - * retourner [$bool, $string, $array] initialisés chacun en fonction du type + * retourner [$bool, $scalar, $array] initialisés chacun en fonction du type * de $value. * * @throws ValueException si $value n'est d'aucun de ces types */ static final function check_bsa($value, ?string $prefix=null, bool $throw_exception=true): array { $bool = is_bool($value)? $value : null; - $string = is_string($value)? $value : null; + $scalar = !is_bool($value) && is_scalar($value)? $value : null; $array = is_array($value)? $value : null; - if ($bool === null && $string === null && $array === null && $throw_exception) { + if ($bool === null && $scalar === null && $array === null && $throw_exception) { throw ValueException::invalid_kind($value, "value", $prefix); } else { - return [$bool, $string, $array]; + return [$bool, $scalar, $array]; + } + } + + static final function subclass_of($value, $class): bool { + if (is_string($value)) { + return $value === $class || is_subclass_of($value, $class); + } elseif (is_object($value)) { + return $value instanceof $class; + } else { + return false; } } diff --git a/php/src/file/Stream.php b/php/src/file/Stream.php index 65216ee..c1d7496 100644 --- a/php/src/file/Stream.php +++ b/php/src/file/Stream.php @@ -299,8 +299,8 @@ class Stream extends AbstractIterator implements IReader, IWriter { /** retourner le contenu du fichier sous forme de chaine */ function getContents(bool $close=true, bool $alreadyLocked=false): string { - $useLocking = $this->useLocking; - if ($useLocking && !$alreadyLocked) $this->lock(LOCK_SH); + $useLocking = $this->useLocking && !$alreadyLocked; + if ($useLocking) $this->lock(LOCK_SH); try { return IOException::ensure_valid(stream_get_contents($this->fd), $this->throwOnError); } finally { @@ -380,7 +380,9 @@ class Stream extends AbstractIterator implements IReader, IWriter { /** @throws IOException */ function ftruncate(int $size=0, bool $rewind=true): self { $fd = $this->getResource(); - IOException::ensure_valid(ftruncate($fd, $size), $this->throwOnError); + $r = ftruncate($fd, $size); + $this->stat = null; + IOException::ensure_valid($r, $this->throwOnError); if ($rewind) rewind($fd); return $this; } @@ -390,6 +392,7 @@ class Stream extends AbstractIterator implements IReader, IWriter { $fd = $this->getResource(); if ($length === null) $r = fwrite($fd, $data); else $r = fwrite($fd, $data, $length); + $this->stat = null; return IOException::ensure_valid($r, $this->throwOnError); } @@ -403,9 +406,13 @@ class Stream extends AbstractIterator implements IReader, IWriter { $line[] = strval($col); } $line = implode($sep, $line); - IOException::ensure_valid(fwrite($fd, "$line\n"), $this->throwOnError); + $r = fwrite($fd, "$line\n"); + $this->stat = null; + IOException::ensure_valid($r, $this->throwOnError); } else { - IOException::ensure_valid(fputcsv($fd, $row, $params[0], $params[1], $params[2]), $this->throwOnError); + $r = fputcsv($fd, $row, $params[0], $params[1], $params[2]); + $this->stat = null; + IOException::ensure_valid($r, $this->throwOnError); } } @@ -471,8 +478,8 @@ class Stream extends AbstractIterator implements IReader, IWriter { } function putContents(string $contents, bool $close=true, bool $alreadyLocked=false): void { - $useLocking = $this->useLocking; - if ($useLocking && !$alreadyLocked) $this->lock(LOCK_EX); + $useLocking = $this->useLocking && !$alreadyLocked; + if ($useLocking) $this->lock(LOCK_EX); try { $this->fwrite($contents); } finally { diff --git a/php/src/php/time/DateInterval.php b/php/src/php/time/DateInterval.php index c9ca935..210e6ba 100644 --- a/php/src/php/time/DateInterval.php +++ b/php/src/php/time/DateInterval.php @@ -29,7 +29,7 @@ class DateInterval extends \DateInterval { } function __construct($duration) { - if (is_int($duration)) $duration = "PT${duration}S"; + if (is_numeric($duration)) $duration = "PT${duration}S"; if ($duration instanceof \DateInterval) { $this->y = $duration->y; $this->m = $duration->m; diff --git a/php/src/php/time/Delay.php b/php/src/php/time/Delay.php index 14a5307..4afcf37 100644 --- a/php/src/php/time/Delay.php +++ b/php/src/php/time/Delay.php @@ -3,6 +3,7 @@ namespace nulib\php\time; use DateTimeInterface; use InvalidArgumentException; +use nulib\ValueException; /** * Class Delay: une durée jusqu'à un moment destination. le moment destination @@ -115,6 +116,10 @@ class Delay { $this->repr = $repr; } + function __clone() { + $this->dest = clone $this->dest; + } + function __serialize(): array { return [$this->dest, $this->repr]; } @@ -130,7 +135,7 @@ class Delay { } function addDuration($duration) { - if (is_int($duration) && $duration < 0) { + if (is_numeric($duration) && $duration < 0) { $this->dest->sub(DateInterval::with(-$duration)); } else { $this->dest->add(DateInterval::with($duration)); @@ -138,7 +143,7 @@ class Delay { } function subDuration($duration) { - if (is_int($duration) && $duration < 0) { + if (is_numeric($duration) && $duration < 0) { $this->dest->add(DateInterval::with(-$duration)); } else { $this->dest->sub(DateInterval::with($duration));