modifs.mineures sans commentaires
This commit is contained in:
		
							parent
							
								
									547e72e7e8
								
							
						
					
					
						commit
						30593623fb
					
				
							
								
								
									
										67
									
								
								src/F.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								src/F.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,67 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace nur;
 | 
			
		||||
 | 
			
		||||
use nur\b\values\Mparams;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Class F: gestion unifiée des paramètres POST + GET
 | 
			
		||||
 */
 | 
			
		||||
class F extends Mparams {
 | 
			
		||||
  /** tester si le paramètre $name existe dans $_POST ou $_GET */
 | 
			
		||||
  static final function has($name): bool {
 | 
			
		||||
    if ($name === null || $name === false) return false;
 | 
			
		||||
    elseif (array_key_exists($name, $_POST)) return true;
 | 
			
		||||
    elseif (array_key_exists($name, $_GET)) return true;
 | 
			
		||||
    else return false;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** obtenir le paramètre $name en cherchant dans $_POST puis $_GET */
 | 
			
		||||
  static final function get($name, $default=null, bool $trim=false) {
 | 
			
		||||
    if ($name === null || $name === false) $value = $default;
 | 
			
		||||
    elseif (array_key_exists($name, $_POST)) $value = $_POST[$name];
 | 
			
		||||
    elseif (array_key_exists($name, $_GET)) $value = $_GET[$name];
 | 
			
		||||
    else $value = $default;
 | 
			
		||||
    if ($trim) $value = str::trim($value);
 | 
			
		||||
    return $value;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * modifier le paramètre.
 | 
			
		||||
   *
 | 
			
		||||
   * - s'il provenait de $_POST, modifier $_POST
 | 
			
		||||
   * - s'il provenait de $_GET, modifier $_GET
 | 
			
		||||
   * - sinon modifier $_POST
 | 
			
		||||
   */
 | 
			
		||||
  static final function set(string $name, ?string $value): void {
 | 
			
		||||
    if (array_key_exists($name, $_POST)) $_POST[$name] = $value;
 | 
			
		||||
    elseif (array_key_exists($name, $_GET)) $_GET[$name] = $value;
 | 
			
		||||
    else $_POST[$name] = $value;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** obtenir la liste des clés définies dans les paramètres */
 | 
			
		||||
  protected static final function get_names(): array {
 | 
			
		||||
    return array_keys(array_merge(
 | 
			
		||||
      array_fill_keys(array_keys($_POST), true),
 | 
			
		||||
      array_fill_keys(array_keys($_GET), true),
 | 
			
		||||
    ));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * retourner une liste des paramètres qui ont été passés, en les sélectionnant
 | 
			
		||||
   * selon le contenu de $includes et $excludes. ensuite, fusionner le tableau
 | 
			
		||||
   * $merge s'il est spécifié
 | 
			
		||||
   *
 | 
			
		||||
   * pour être sélectionné, un paramètre ne doit pas être dans $excludes, et si
 | 
			
		||||
   * $includes n'est pas null, doit être dans $includes
 | 
			
		||||
   */
 | 
			
		||||
  static final function select(?array $includes=null, ?array $excludes=null, ?array $merges=null): array {
 | 
			
		||||
    $params = [];
 | 
			
		||||
    foreach (self::get_names() as $name) {
 | 
			
		||||
      if ($excludes !== null && in_array($name, $excludes)) continue;
 | 
			
		||||
      if ($includes !== null && !in_array($name, $includes)) continue;
 | 
			
		||||
      $params[$name] = self::get($name);
 | 
			
		||||
    }
 | 
			
		||||
    if ($merges !== null) $params = array_merge($params, $merges);
 | 
			
		||||
    return $params;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										27
									
								
								src/G.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								src/G.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,27 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace nur;
 | 
			
		||||
 | 
			
		||||
use nur\b\values\Mparams;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Class G: gestion des paramètres $_GET
 | 
			
		||||
 */
 | 
			
		||||
class G extends Mparams {
 | 
			
		||||
  /** tester si le paramètre $name existe */
 | 
			
		||||
  static final function has($name): bool {
 | 
			
		||||
    if ($name === null || $name === false) return false;
 | 
			
		||||
    return array_key_exists($name, $_GET);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** obtenir le paramètre $name */
 | 
			
		||||
  static final function get($name, $default=null, bool $trim=false) {
 | 
			
		||||
    $value = A::get($_GET, $name, $default);
 | 
			
		||||
    if ($trim) $value = str::trim($value);
 | 
			
		||||
    return $value;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** modifier le paramètre. */
 | 
			
		||||
  static final function set(string $name, ?string $value): void {
 | 
			
		||||
    $_GET[$name] = $value;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										32
									
								
								src/P.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								src/P.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,32 @@
 | 
			
		||||
<?php # -*- coding: utf-8 mode: php -*- vim:sw=2:sts=2:et:ai:si:sta:fenc=utf-8
 | 
			
		||||
namespace nur;
 | 
			
		||||
 | 
			
		||||
use nur\b\values\Mparams;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Class P: gestion des paramètres $_POST
 | 
			
		||||
 */
 | 
			
		||||
class P extends Mparams {
 | 
			
		||||
  /** tester si le paramètre $name existe */
 | 
			
		||||
  static final function has($name): bool {
 | 
			
		||||
    if ($name === null || $name === false) return false;
 | 
			
		||||
    return array_key_exists($name, $_POST);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** obtenir le paramètre $name */
 | 
			
		||||
  static final function get($name, $default=null, bool $trim=false) {
 | 
			
		||||
    $value = A::get($_POST, $name, $default);
 | 
			
		||||
    if ($trim) $value = str::trim($value);
 | 
			
		||||
    return $value;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** modifier le paramètre. */
 | 
			
		||||
  static final function set(string $name, ?string $value): void {
 | 
			
		||||
    $_POST[$name] = $value;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** obtenir le corps de la requête POST */
 | 
			
		||||
  static final function raw(): string {
 | 
			
		||||
    return file_get_contents("php://input");
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										22
									
								
								src/R.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								src/R.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,22 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace nur;
 | 
			
		||||
 | 
			
		||||
use nur\b\values\Mparams;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Class R: gestion des paramètres $_REQUEST (GET + POST + COOKIES)
 | 
			
		||||
 */
 | 
			
		||||
class R extends Mparams {
 | 
			
		||||
  /** tester si le paramètre $name existe */
 | 
			
		||||
  static final function has($name): bool {
 | 
			
		||||
    if ($name === null || $name === false) return false;
 | 
			
		||||
    return array_key_exists($name, $_REQUEST);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** obtenir le paramètre $name */
 | 
			
		||||
  static final function get($name, $default=null, bool $trim=false) {
 | 
			
		||||
    $value = A::get($_REQUEST, $name, $default);
 | 
			
		||||
    if ($trim) $value = str::trim($value);
 | 
			
		||||
    return $value;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										97
									
								
								src/values/akey.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										97
									
								
								src/values/akey.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,97 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace nur;
 | 
			
		||||
 | 
			
		||||
use ArrayAccess;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Class akey: des outils pour accéder aux valeurs d'un tableau
 | 
			
		||||
 *
 | 
			
		||||
 * @see valx
 | 
			
		||||
 */
 | 
			
		||||
class akey {
 | 
			
		||||
  /** obtenir la valeur d'une clé */
 | 
			
		||||
  static final function get($array, $key, $default=null) {
 | 
			
		||||
    if ($array instanceof ArrayAccess) {
 | 
			
		||||
      if ($array->offsetExists($key)) return $array->offsetGet($key);
 | 
			
		||||
      else return $default;
 | 
			
		||||
    } else {
 | 
			
		||||
      if (!is_array($array)) $array = A::with($array);
 | 
			
		||||
      return A::get($array, $key, $default);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** spécifier la valeur d'une clé */
 | 
			
		||||
  static final function set(&$array, $key, $value) {
 | 
			
		||||
    if ($array instanceof ArrayAccess) {
 | 
			
		||||
      $array->offsetSet($key, $value);
 | 
			
		||||
    } else {
 | 
			
		||||
      A::set($array, $key, $value);
 | 
			
		||||
    }
 | 
			
		||||
    return $value;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** initialiser $dest avec les valeurs de $values */
 | 
			
		||||
  static final function set_values(&$array, ?array $values): void {
 | 
			
		||||
    if ($values === null) return;
 | 
			
		||||
    foreach ($values as $key => $value) {
 | 
			
		||||
      self::set($array, $key, $value);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** incrémenter la valeur de la clé */
 | 
			
		||||
  static final function inc(&$array, $key): int {
 | 
			
		||||
    if ($array instanceof ArrayAccess) {
 | 
			
		||||
      $value = (int)$array->offsetGet($key);
 | 
			
		||||
      $array->offsetSet($key, ++$value);
 | 
			
		||||
      return $value;
 | 
			
		||||
    } else {
 | 
			
		||||
      A::ensure_array($array);
 | 
			
		||||
      $value = (int)A::get($array, $key);
 | 
			
		||||
      return $array[$key] = ++$value;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** décrémenter la valeur de la clé */
 | 
			
		||||
  static final function dec(&$array, $key, bool $allow_negative=false): int {
 | 
			
		||||
    if ($array instanceof ArrayAccess) {
 | 
			
		||||
      $value = (int)$array->offsetGet($key);
 | 
			
		||||
      if ($allow_negative || $value > 0) $array->offsetSet($key, --$value);
 | 
			
		||||
      return $value;
 | 
			
		||||
    } else {
 | 
			
		||||
      A::ensure_array($array);
 | 
			
		||||
      $value = (int)A::get($array, $key);
 | 
			
		||||
      if ($allow_negative || $value > 0) $array[$key] = --$value;
 | 
			
		||||
      return $value;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * fusionner $merge dans la valeur de la clé, qui est d'abord transformé en
 | 
			
		||||
   * tableau si nécessaire
 | 
			
		||||
   */
 | 
			
		||||
  static final function merge(&$array, $key, $merge): void {
 | 
			
		||||
    if ($array instanceof ArrayAccess) {
 | 
			
		||||
      $value = $array->offsetGet($key);
 | 
			
		||||
      A::merge($value, $merge);
 | 
			
		||||
      $array->offsetSet($key, $value);
 | 
			
		||||
    } else {
 | 
			
		||||
      A::ensure_array($array);
 | 
			
		||||
      A::merge($array[$key], $merge);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * ajouter $value à la valeur de la clé, qui est d'abord transformé en
 | 
			
		||||
   * tableau si nécessaire
 | 
			
		||||
   */
 | 
			
		||||
  static final function append(&$array, $key, $value): void {
 | 
			
		||||
    if ($array instanceof ArrayAccess) {
 | 
			
		||||
      $value = $array->offsetGet($key);
 | 
			
		||||
      A::set($value, null, $value);
 | 
			
		||||
      $array->offsetSet($key, $value);
 | 
			
		||||
    } else {
 | 
			
		||||
      A::ensure_array($array);
 | 
			
		||||
      A::set($array[$key], null, $value);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										150
									
								
								src/values/oprop.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										150
									
								
								src/values/oprop.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,150 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace nur;
 | 
			
		||||
 | 
			
		||||
use ReflectionClass;
 | 
			
		||||
use ReflectionException;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Class oprop: des outils pour accéder aux propriétés d'un objet
 | 
			
		||||
 *
 | 
			
		||||
 * @see valx
 | 
			
		||||
 */
 | 
			
		||||
class oprop {
 | 
			
		||||
  /** obtenir la valeur d'une propriété */
 | 
			
		||||
  static final function get(object $object, string $property, $default=null) {
 | 
			
		||||
    $c = new ReflectionClass($object);
 | 
			
		||||
    try {
 | 
			
		||||
      $p = $c->getProperty($property);
 | 
			
		||||
      $p->setAccessible(true);
 | 
			
		||||
      return $p->getValue($object);
 | 
			
		||||
    } catch (ReflectionException $e) {
 | 
			
		||||
      if (property_exists($object, $property)) return $object->$property;
 | 
			
		||||
      else return $default;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static final function _set(ReflectionClass $c, object $object, string $property, $value) {
 | 
			
		||||
    try {
 | 
			
		||||
      $p = $c->getProperty($property);
 | 
			
		||||
      $p->setAccessible(true);
 | 
			
		||||
      $p->setValue($object, $value);
 | 
			
		||||
    } catch (ReflectionException $e) {
 | 
			
		||||
      $object->$property = $value;
 | 
			
		||||
    }
 | 
			
		||||
    return $value;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** spécifier la valeur d'une propriété */
 | 
			
		||||
  static final function set(object $object, string $property, $value) {
 | 
			
		||||
    $c = new ReflectionClass($object);
 | 
			
		||||
    return self::_set($c, $object, $property, $value);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * initialiser $dest avec les valeurs de $values
 | 
			
		||||
   *
 | 
			
		||||
   * les noms des clés de $values sont transformées en camelCase pour avoir les
 | 
			
		||||
   * noms des propriétés correspondantes
 | 
			
		||||
   */
 | 
			
		||||
  static final function set_values(object $object, ?array $values, ?array $keys=null): void {
 | 
			
		||||
    if ($values === null) return;
 | 
			
		||||
    if ($keys === null) $keys = array_keys($values);
 | 
			
		||||
    $c = new ReflectionClass($object);
 | 
			
		||||
    foreach ($keys as $key) {
 | 
			
		||||
      if (array_key_exists($key, $values)) {
 | 
			
		||||
        $property = str::us2camel($key);
 | 
			
		||||
        self::_set($c, $object, $property, $values[$key]);
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** incrémenter la valeur d'une propriété */
 | 
			
		||||
  static final function inc(object $object, string $property): int {
 | 
			
		||||
    $c = new ReflectionClass($object);
 | 
			
		||||
    try {
 | 
			
		||||
      $p = $c->getProperty($property);
 | 
			
		||||
      $p->setAccessible(true);
 | 
			
		||||
      $value = (int)$p->getValue($object);
 | 
			
		||||
      $value++;
 | 
			
		||||
      $p->setValue($object, $value);
 | 
			
		||||
    } catch (ReflectionException $e) {
 | 
			
		||||
      if (property_exists($object, $property)) {
 | 
			
		||||
        $value = (int)$object->$property;
 | 
			
		||||
        $value++;
 | 
			
		||||
      } else {
 | 
			
		||||
        $value = 1;
 | 
			
		||||
      }
 | 
			
		||||
      $object->$property = $value;
 | 
			
		||||
    }
 | 
			
		||||
    return $value;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** décrémenter la valeur d'une propriété */
 | 
			
		||||
  static final function dec(object $object, string $property, bool $allow_negative=false): int {
 | 
			
		||||
    $c = new ReflectionClass($object);
 | 
			
		||||
    try {
 | 
			
		||||
      $p = $c->getProperty($property);
 | 
			
		||||
      $p->setAccessible(true);
 | 
			
		||||
      $value = (int)$p->getValue($object);
 | 
			
		||||
      if ($allow_negative || $value > 0) {
 | 
			
		||||
        $value --;
 | 
			
		||||
        $p->setValue($object, $value);
 | 
			
		||||
      }
 | 
			
		||||
    } catch (ReflectionException $e) {
 | 
			
		||||
      if (property_exists($object, $property)) {
 | 
			
		||||
        $value = (int)$object->$property;
 | 
			
		||||
      } else {
 | 
			
		||||
        $value = 0;
 | 
			
		||||
      }
 | 
			
		||||
      if ($allow_negative || $value > 0) $value--;
 | 
			
		||||
      $object->$property = $value;
 | 
			
		||||
    }
 | 
			
		||||
    return $value;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Fusionner la valeur à la propriété qui est transformée en tableau si
 | 
			
		||||
   * nécessaire
 | 
			
		||||
   */
 | 
			
		||||
  static final function merge(object $object, string $property, $array): void {
 | 
			
		||||
    $c = new ReflectionClass($object);
 | 
			
		||||
    try {
 | 
			
		||||
      $p = $c->getProperty($property);
 | 
			
		||||
      $p->setAccessible(true);
 | 
			
		||||
      $values = A::with($p->getValue($object));
 | 
			
		||||
      A::merge($values, A::with($array));
 | 
			
		||||
      $p->setValue($object, $values);
 | 
			
		||||
    } catch (ReflectionException $e) {
 | 
			
		||||
      if (property_exists($object, $property)) {
 | 
			
		||||
        $values = A::with($object->$property);
 | 
			
		||||
      } else {
 | 
			
		||||
        $values = [];
 | 
			
		||||
      }
 | 
			
		||||
      A::merge($values, A::with($array));
 | 
			
		||||
      $object->$property = $values;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Ajouter la valeur à la propriété qui est transformée en tableau si
 | 
			
		||||
   * nécessaire
 | 
			
		||||
   */
 | 
			
		||||
  static final function append(object $object, string $property, $value): void {
 | 
			
		||||
    $c = new ReflectionClass($object);
 | 
			
		||||
    try {
 | 
			
		||||
      $p = $c->getProperty($property);
 | 
			
		||||
      $p->setAccessible(true);
 | 
			
		||||
      $values = A::with($p->getValue($object));
 | 
			
		||||
      $values[] = $value;
 | 
			
		||||
      $p->setValue($object, $values);
 | 
			
		||||
    } catch (ReflectionException $e) {
 | 
			
		||||
      if (property_exists($object, $property)) {
 | 
			
		||||
        $values = A::with($object->$property);
 | 
			
		||||
      } else {
 | 
			
		||||
        $values = [];
 | 
			
		||||
      }
 | 
			
		||||
      $values[] = $value;
 | 
			
		||||
      $object->$property = $values;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										120
									
								
								src/values/prop.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										120
									
								
								src/values/prop.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,120 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace nur;
 | 
			
		||||
 | 
			
		||||
use ReflectionClass;
 | 
			
		||||
use ReflectionException;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Class prop: des outils pour accéder aux propriétés d'un objet. la différence
 | 
			
		||||
 * avec {@link oprop} est qu'une tentative est effectuée pour accéder d'abord à
 | 
			
		||||
 * la propriété via une méthode normalisée
 | 
			
		||||
 *
 | 
			
		||||
 * @see valx
 | 
			
		||||
 */
 | 
			
		||||
class prop {
 | 
			
		||||
  static function split_prefix_name(string $name): array {
 | 
			
		||||
    preg_match('/^(_*)(.*)/', $name, $ms);
 | 
			
		||||
    return [$ms[1], $ms[2]];
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static function get_getter_name(string $property, bool $bool=false): string {
 | 
			
		||||
    [$prefix, $name] = self::split_prefix_name($property);
 | 
			
		||||
    $get = $bool? "is": "get";
 | 
			
		||||
    return $prefix.$get.str::upper1(str::us2camel($name));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static function get_setter_name(string $property): string {
 | 
			
		||||
    [$prefix, $name] = self::split_prefix_name($property);
 | 
			
		||||
    return $prefix."set".str::upper1(str::us2camel($name));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static function get_deletter_name(string $property): string {
 | 
			
		||||
    [$prefix, $name] = self::split_prefix_name($property);
 | 
			
		||||
    return $prefix."del".str::upper1(str::us2camel($name));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** obtenir la valeur d'une propriété */
 | 
			
		||||
  static final function get(object $object, string $property, $default=null, ?string $method=null) {
 | 
			
		||||
    if ($method === null) $method = self::get_getter_name($property);
 | 
			
		||||
    $c = new ReflectionClass($object);
 | 
			
		||||
    try {
 | 
			
		||||
      $m = $c->getMethod($method);
 | 
			
		||||
    } catch (ReflectionException $e) {
 | 
			
		||||
      return oprop::get($object, $property, $default);
 | 
			
		||||
    }
 | 
			
		||||
    return func::call([$object, $m], $default);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** spécifier la valeur d'une propriété */
 | 
			
		||||
  static final function set(object $object, string $property, $value, ?string $method=null) {
 | 
			
		||||
    $c = new ReflectionClass($object);
 | 
			
		||||
    return self::_set($c, $object, $property, $value, $method);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private static final function _set(ReflectionClass $c, object $object, string $property, $value, ?string $method) {
 | 
			
		||||
    if ($method === null) $method = self::get_setter_name($property);
 | 
			
		||||
    try {
 | 
			
		||||
      $m = $c->getMethod($method);
 | 
			
		||||
    } catch (ReflectionException $e) {
 | 
			
		||||
      return oprop::_set($c, $object, $property, $value);
 | 
			
		||||
    }
 | 
			
		||||
    func::call([$object, $m], $value);
 | 
			
		||||
    return $value;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * initialiser $dest avec les valeurs de $values
 | 
			
		||||
   *
 | 
			
		||||
   * les noms des clés de $values sont transformées en camelCase pour avoir les
 | 
			
		||||
   * noms des propriétés correspondantes
 | 
			
		||||
   */
 | 
			
		||||
  static final function set_values(object $object, ?array $values, ?array $keys=null): void {
 | 
			
		||||
    if ($values === null) return;
 | 
			
		||||
    if ($keys === null) $keys = array_keys($values);
 | 
			
		||||
    $c = new ReflectionClass($object);
 | 
			
		||||
    foreach ($keys as $key) {
 | 
			
		||||
      if (array_key_exists($key, $values)) {
 | 
			
		||||
        $property = str::us2camel($key);
 | 
			
		||||
        self::_set($c, $object, $property, $values[$key], null);
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** incrémenter la valeur d'une propriété */
 | 
			
		||||
  static final function inc(object $object, string $property): int {
 | 
			
		||||
    $value = intval(self::get($object, $property, 0));
 | 
			
		||||
    $value++;
 | 
			
		||||
    self::set($object, $property, $value);
 | 
			
		||||
    return $value;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /** décrémenter la valeur d'une propriété */
 | 
			
		||||
  static final function dec(object $object, string $property, bool $allow_negative=false): int {
 | 
			
		||||
    $value = intval(self::get($object, $property, 0));
 | 
			
		||||
    if ($allow_negative || $value > 0) {
 | 
			
		||||
      $value--;
 | 
			
		||||
      self::set($object, $property, $value);
 | 
			
		||||
    }
 | 
			
		||||
    return $value;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Fusionner la valeur à la propriété qui est transformée en tableau si
 | 
			
		||||
   * nécessaire
 | 
			
		||||
   */
 | 
			
		||||
  static final function merge(object $object, string $property, $array): void {
 | 
			
		||||
    $values = A::with(self::get($object, $property));
 | 
			
		||||
    A::merge($values, A::with($array));
 | 
			
		||||
    self::set($object, $property, $values);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Ajouter la valeur à la propriété qui est transformée en tableau si
 | 
			
		||||
   * nécessaire
 | 
			
		||||
   */
 | 
			
		||||
  static final function append(object $object, string $property, $value): void {
 | 
			
		||||
    $values = A::with(self::get($object, $property));
 | 
			
		||||
    $values[] = $value;
 | 
			
		||||
    self::set($object, $property, $values);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										83
									
								
								src/values/valm.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								src/values/valm.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,83 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace nur;
 | 
			
		||||
 | 
			
		||||
use ArrayAccess;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Class valm: frontend pour accéder à une propriété d'un objet (par une méthode
 | 
			
		||||
 * normalisée ou en direct) ou à la clé d'un tableau. cette classe utilise
 | 
			
		||||
 * {@link prop} ou {@link akey} en fonction de la nature de la cible
 | 
			
		||||
 *
 | 
			
		||||
 * si la cible est un objet, les noms des clés sont transformés en camelCase
 | 
			
		||||
 * pour avoir les propriétés correspondantes
 | 
			
		||||
 */
 | 
			
		||||
class valm {
 | 
			
		||||
  static final function get($src, $name, $default=null) {
 | 
			
		||||
    if (is_object($src) && !($src instanceof ArrayAccess)) {
 | 
			
		||||
      $name = str::us2camel($name);
 | 
			
		||||
      return prop::get($src, $name, $default);
 | 
			
		||||
    } else {
 | 
			
		||||
      return akey::get($src, $name, $default);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static final function set(&$dest, $name, $value) {
 | 
			
		||||
    if (is_object($dest) && !($dest instanceof ArrayAccess)) {
 | 
			
		||||
      $name = str::us2camel($name);
 | 
			
		||||
      return prop::set($dest, $name, $value);
 | 
			
		||||
    } else {
 | 
			
		||||
      return akey::set($dest, $name, $value);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * initialiser $dest avec les valeurs de $values
 | 
			
		||||
   *
 | 
			
		||||
   * si $dest est un objet, les noms des clés sont transformées en camelCase
 | 
			
		||||
   * pour avoir les propriétés correspondantes
 | 
			
		||||
   */
 | 
			
		||||
  static final function set_values(&$dest, ?array $values): void {
 | 
			
		||||
    if ($values === null) return;
 | 
			
		||||
    if (is_object($dest) && !($dest instanceof ArrayAccess)) {
 | 
			
		||||
      prop::set_values($dest, $values);
 | 
			
		||||
    } else {
 | 
			
		||||
      akey::set_values($dest, $values);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static final function inc(&$dest, $name): int {
 | 
			
		||||
    if (is_object($dest) && !($dest instanceof ArrayAccess)) {
 | 
			
		||||
      $name = str::us2camel($name);
 | 
			
		||||
      return prop::inc($dest, $name);
 | 
			
		||||
    } else {
 | 
			
		||||
      return akey::inc($dest, $name);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static final function dec(&$dest, $name): int {
 | 
			
		||||
    if (is_object($dest) && !($dest instanceof ArrayAccess)) {
 | 
			
		||||
      $name = str::us2camel($name);
 | 
			
		||||
      return prop::dec($dest, $name);
 | 
			
		||||
    } else {
 | 
			
		||||
      return akey::dec($dest, $name);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static final function merge(&$dest, $name, $merge): void {
 | 
			
		||||
    if (is_object($dest) && !($dest instanceof ArrayAccess)) {
 | 
			
		||||
      $name = str::us2camel($name);
 | 
			
		||||
      prop::merge($dest, $name, $merge);
 | 
			
		||||
    } else {
 | 
			
		||||
      akey::merge($dest, $name, $merge);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static final function append(&$dest, $name, $value): void {
 | 
			
		||||
    if (is_object($dest) && !($dest instanceof ArrayAccess)) {
 | 
			
		||||
      $name = str::us2camel($name);
 | 
			
		||||
      prop::append($dest, $name, $value);
 | 
			
		||||
    } else {
 | 
			
		||||
      akey::append($dest, $name, $value);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										83
									
								
								src/values/valx.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								src/values/valx.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,83 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace nur;
 | 
			
		||||
 | 
			
		||||
use ArrayAccess;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Class valx: frontend pour accéder à une propriété d'un objet ou à la clé
 | 
			
		||||
 * d'un tableau. cette classe utilise {@link oprop} ou {@link akey} en fonction
 | 
			
		||||
 * de la nature de la cible
 | 
			
		||||
 *
 | 
			
		||||
 * si la cible est un objet, les noms des clés sont transformés en camelCase
 | 
			
		||||
 * pour avoir les propriétés correspondantes
 | 
			
		||||
 */
 | 
			
		||||
class valx {
 | 
			
		||||
  static final function get($src, $name, $default=null) {
 | 
			
		||||
    if (is_object($src) && !($src instanceof ArrayAccess)) {
 | 
			
		||||
      $name = str::us2camel($name);
 | 
			
		||||
      return oprop::get($src, $name, $default);
 | 
			
		||||
    } else {
 | 
			
		||||
      return akey::get($src, $name, $default);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static final function set(&$dest, $name, $value) {
 | 
			
		||||
    if (is_object($dest) && !($dest instanceof ArrayAccess)) {
 | 
			
		||||
      $name = str::us2camel($name);
 | 
			
		||||
      return oprop::set($dest, $name, $value);
 | 
			
		||||
    } else {
 | 
			
		||||
      return akey::set($dest, $name, $value);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * initialiser $dest avec les valeurs de $values
 | 
			
		||||
   *
 | 
			
		||||
   * si $dest est un objet, les noms des clés sont transformées en camelCase
 | 
			
		||||
   * pour avoir les propriétés correspondantes
 | 
			
		||||
   */
 | 
			
		||||
  static final function set_values(&$dest, ?array $values): void {
 | 
			
		||||
    if ($values === null) return;
 | 
			
		||||
    if (is_object($dest) && !($dest instanceof ArrayAccess)) {
 | 
			
		||||
      oprop::set_values($dest, $values);
 | 
			
		||||
    } else {
 | 
			
		||||
      akey::set_values($dest, $values);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static final function inc(&$dest, $name): int {
 | 
			
		||||
    if (is_object($dest) && !($dest instanceof ArrayAccess)) {
 | 
			
		||||
      $name = str::us2camel($name);
 | 
			
		||||
      return oprop::inc($dest, $name);
 | 
			
		||||
    } else {
 | 
			
		||||
      return akey::inc($dest, $name);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static final function dec(&$dest, $name): int {
 | 
			
		||||
    if (is_object($dest) && !($dest instanceof ArrayAccess)) {
 | 
			
		||||
      $name = str::us2camel($name);
 | 
			
		||||
      return oprop::dec($dest, $name);
 | 
			
		||||
    } else {
 | 
			
		||||
      return akey::dec($dest, $name);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static final function merge(&$dest, $name, $merge): void {
 | 
			
		||||
    if (is_object($dest) && !($dest instanceof ArrayAccess)) {
 | 
			
		||||
      $name = str::us2camel($name);
 | 
			
		||||
      oprop::merge($dest, $name, $merge);
 | 
			
		||||
    } else {
 | 
			
		||||
      akey::merge($dest, $name, $merge);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static final function append(&$dest, $name, $value): void {
 | 
			
		||||
    if (is_object($dest) && !($dest instanceof ArrayAccess)) {
 | 
			
		||||
      $name = str::us2camel($name);
 | 
			
		||||
      oprop::append($dest, $name, $value);
 | 
			
		||||
    } else {
 | 
			
		||||
      akey::append($dest, $name, $value);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user