60 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			60 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
namespace nur\sery\wip\php\access;
 | 
						|
 | 
						|
use nur\sery\cl;
 | 
						|
 | 
						|
/**
 | 
						|
 * Class PostAccess: accès à une valeur de $_POST
 | 
						|
 */
 | 
						|
class PostAccess extends AbstractAccess {
 | 
						|
  function __construct($key, bool $allowEmpty=false) {
 | 
						|
    $this->key = $key;
 | 
						|
    $this->allowEmpty = $allowEmpty;
 | 
						|
  }
 | 
						|
 | 
						|
  /** @var int|string */
 | 
						|
  protected $key;
 | 
						|
 | 
						|
  protected bool $allowEmpty;
 | 
						|
 | 
						|
  function exists($src=null): bool {
 | 
						|
    $key = $this->key;
 | 
						|
    if ($key === null) return false;
 | 
						|
    return array_key_exists($key, $_POST);
 | 
						|
  }
 | 
						|
 | 
						|
  public function available($src=null): bool {
 | 
						|
    $key = $this->key;
 | 
						|
    if ($key === null) return false;
 | 
						|
    if (array_key_exists($key, $_POST)) {
 | 
						|
      return $this->allowEmpty || $_POST[$key] !== "";
 | 
						|
    } else {
 | 
						|
      return false;
 | 
						|
    }
 | 
						|
  }
 | 
						|
 | 
						|
  function get($src=null, $default=null) {
 | 
						|
    $key = $this->key;
 | 
						|
    if ($key === null) return $default;
 | 
						|
    if (array_key_exists($key, $_POST)) {
 | 
						|
      $value = $_POST[$key];
 | 
						|
      if ($value === "" && !$this->allowEmpty) return $default;
 | 
						|
      return $value;
 | 
						|
    } else {
 | 
						|
      return $default;
 | 
						|
    }
 | 
						|
  }
 | 
						|
 | 
						|
  function set($value, &$dest=null): void {
 | 
						|
    $key = $this->key;
 | 
						|
    if ($key === null) return;
 | 
						|
    cl::set($_POST, $key, $value);
 | 
						|
  }
 | 
						|
 | 
						|
  function del(&$dest=null): void {
 | 
						|
    $key = $this->key;
 | 
						|
    if ($key === null) return;
 | 
						|
    cl::del($_POST, $key);
 | 
						|
  }
 | 
						|
}
 |