1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 
	<?php
namespace ICanBoogie\Facets;
use ICanBoogie\ToArray;
class IntervalCriterionValue implements ToArray
{
    const SEPARATOR = '..';
    
    static public function from($value)
    {
        if (!$value)
        {
            return null;
        }
        if (is_array($value))
        {
            if (!array_key_exists('min', $value) || !array_key_exists('max', $value))
            {
                return null;
            }
            $min = $value['min'];
            $max = $value['max'];
        }
        else
        {
            $value = trim($value);
            if ($value === self::SEPARATOR || strpos($value, self::SEPARATOR) === false)
            {
                return null;
            }
            $interval = explode(self::SEPARATOR, $value);
            if (count($interval) != 2)
            {
                return null;
            }
            list($min, $max) = array_map('trim', $interval);
        }
        if ($min === '') $min = null;
        if ($max === '') $max = null;
        return new static($min, $max);
    }
    public $min;
    public $max;
    public function __construct($min, $max)
    {
        $this->min = $min;
        $this->max = $max;
    }
    
    public function __toString()
    {
        if (!$this->min && !$this->max)
        {
            return '';
        }
        if ($this->min == $this->max)
        {
            return (string) $this->min;
        }
        return $this->min . self::SEPARATOR . $this->max;
    }
    
    public function to_array()
    {
        return [ $this->min, $this->max ];
    }
}