90 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			90 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
namespace nur\b\date;
 | 
						|
 | 
						|
use nur\A;
 | 
						|
use nur\b\coll\BaseArray;
 | 
						|
use nur\b\IllegalAccessException;
 | 
						|
 | 
						|
/**
 | 
						|
 * Class Period: une période de temps définie par une date de début et une date
 | 
						|
 * de fin. les deux dates sont incluses dans la période
 | 
						|
 */
 | 
						|
class Period extends BaseArray {
 | 
						|
  function __construct(Datetime $start, ?Datetime $end=null, ?array $data=null) {
 | 
						|
    if ($end === null) $end = Datetime::undef();
 | 
						|
    if (!$end->after($start)) [$start, $end] = [$end, $start];
 | 
						|
    $period = [$start, $end];
 | 
						|
    A::update_n($period, $data);
 | 
						|
    parent::__construct($period);
 | 
						|
  }
 | 
						|
 | 
						|
  function start(): Datetime {
 | 
						|
    return $this->data[0];
 | 
						|
  }
 | 
						|
 | 
						|
  function end(): Datetime {
 | 
						|
    return $this->data[1];
 | 
						|
  }
 | 
						|
 | 
						|
  function __toString(): string {
 | 
						|
    return strval($this->data[0])." - ".strval($this->data[1]);
 | 
						|
  }
 | 
						|
 | 
						|
  /** vérifier si cette période est strictement située avant la date spécifiée */
 | 
						|
  function before(Datetime $datetime): bool {
 | 
						|
    if ($datetime->isNull() || $datetime->isUndef()) return false;
 | 
						|
    $end = $this->end();
 | 
						|
    if (!$end->isUndef()) return !$end->after($datetime);
 | 
						|
    $start = $this->start();
 | 
						|
    return !$start->isUndef() && !$start->after($datetime);
 | 
						|
  }
 | 
						|
 | 
						|
  /** vérifier si cette période contient la date spécifiée */
 | 
						|
  function contains(Datetime $datetime): bool {
 | 
						|
    if ($datetime->isNull() || $datetime->isUndef()) return false;
 | 
						|
    $start = $this->start();
 | 
						|
    if (!$start->isUndef() && !$start->before($datetime)) return false;
 | 
						|
    $end = $this->end();
 | 
						|
    if (!$end->isUndef() && !$end->after($datetime)) return false;
 | 
						|
    return true;
 | 
						|
  }
 | 
						|
 | 
						|
  /** vérifier si cette période est strictement située après la date spécifiée */
 | 
						|
  function after(Datetime $datetime): bool {
 | 
						|
    if ($datetime->isNull() || $datetime->isUndef()) return false;
 | 
						|
    $start = $this->start();
 | 
						|
    if (!$start->isUndef()) return !$start->before($datetime);
 | 
						|
    $end = $this->end();
 | 
						|
    return !$end->isUndef() && !$end->before($datetime);
 | 
						|
  }
 | 
						|
 | 
						|
  #############################################################################
 | 
						|
  const KEY_MAP = [
 | 
						|
    "start" => 0,
 | 
						|
    "end" => 1,
 | 
						|
  ];
 | 
						|
 | 
						|
  function _has($key): bool {
 | 
						|
    $key = A::get(self::KEY_MAP, $key, $key);
 | 
						|
    return parent::_has($key);
 | 
						|
  }
 | 
						|
 | 
						|
  function &_get($key, $default=null) {
 | 
						|
    $key = A::get(self::KEY_MAP, $key, $key);
 | 
						|
    return parent::_get($key, $default);
 | 
						|
  }
 | 
						|
 | 
						|
  function _set($key, $value) {
 | 
						|
    throw IllegalAccessException::immutable_object();
 | 
						|
  }
 | 
						|
  function _del($key) {
 | 
						|
    throw IllegalAccessException::immutable_object();
 | 
						|
  }
 | 
						|
 | 
						|
  function has($key): bool { return $this->_has($key); }
 | 
						|
  function get($key, ?Datetime $default=null): ?Datetime { return $this->_get($key, $default); }
 | 
						|
  function set($key, ?Datetime $value): self { return $this->_set($key, $value); }
 | 
						|
  function add(?Datetime $value): self { return $this->_set(null, $value); }
 | 
						|
  function del($key): self { return $this->_del($key); }
 | 
						|
}
 |