modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2025-05-23 16:53:08 +04:00
parent c5b9812c82
commit 3aec24ddb3
4 changed files with 42 additions and 14 deletions

View File

@ -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;
}
}

View File

@ -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 {

View File

@ -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;

View File

@ -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));