ICanBoogie/Facets 0.4.x
  • Namespace
  • Class

Namespaces

  • ICanBoogie
    • Facets
      • RecordCollection

Classes

  • BooleanCriterion
  • Criterion
  • CriterionList
  • CriterionNotDefined
  • CriterionValue
  • Fetcher
  • IntervalCriterionValue
  • QueryString
  • QueryStringWord
  • RecordCollection
  • SetCriterionValue

Interfaces

  • CriterionInterface
  • FetcherInterface

Traits

  • CriterionTrait
  • FetcherTrait
  • HumanizePairsTrait
  • ModelBindings
  • ParseQueryStringTrait
  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

/*
 * This file is part of the ICanBoogie package.
 *
 * (c) Olivier Laviale <olivier.laviale@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ICanBoogie\Facets;

use ICanBoogie\ToArray;

/**
 * Representation of an interval, suitable for the SQL `BETWEEN` operator.
 *
 * An interval is created by separating two values with two dots ("..") e.g. "2000..2014".
 */
class IntervalCriterionValue implements ToArray
{
    const SEPARATOR = '..';

    /**
     * Instantiate a {@link IntervalCriterionValue} instance from a value.
     *
     * @param mixed $value
     *
     * @return IntervalCriterionValue|null
     */
    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;
    }

    /**
     * Formats the interval as a string.
     *
     * @return string
     */
    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;
    }

    /**
     * @return array An array made of the {@link $min} and {@link max} values.
     */
    public function to_array()
    {
        return [ $this->min, $this->max ];
    }
}
ICanBoogie/Facets 0.4.x API documentation generated by ApiGen