ICanBoogie/Facets v0.3.1
  • 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
  • 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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 
<?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\ActiveRecord\Query;
use ICanBoogie\ToArray;

/**
 * A list of criteria.
 */
class CriterionList implements \IteratorAggregate, \ArrayAccess, ToArray
{
    /**
     * Criterion list.
     *
     * @var Criterion[]
     */
    protected $criterion_list = [];

    /**
     * Initializes the {@link $criterion_list} property.
     *
     * @param array $criterion_list A list of criteria.
     */
    public function __construct(array $criterion_list = [])
    {
        foreach ($criterion_list as $criterion_id => &$criterion)
        {
            if (is_string($criterion))
            {
                $criterion = new $criterion($criterion_id);
            }

            $this[$criterion_id] = $criterion;
        }
    }

    /**
     * Clones the criteria of the criterion list.
     */
    public function __clone()
    {
        foreach ($this->criterion_list as $criterion_id => &$criterion)
        {
            $criterion = clone $criterion;
        }
    }

    public function getIterator()
    {
        return new \ArrayIterator($this->criterion_list);
    }

    public function offsetExists($criterion_id)
    {
        return isset($this->criterion_list[$criterion_id]);
    }

    public function offsetGet($criterion_id)
    {
        if (!$this->offsetExists($criterion_id))
        {
            throw new CriterionNotDefined([ $criterion_id, $this ]);
        }

        return $this->criterion_list[$criterion_id];
    }

    public function offsetSet($criterion_id, $criterion)
    {
        $this->criterion_list[$criterion_id] = $criterion;
    }

    public function offsetUnset($criterion_id)
    {
        unset($this->criterion_list[$criterion_id]);
    }

    public function to_array()
    {
        return $this->criterion_list;
    }

    /**
     * Parses the query string and marks words matched by criteria.
     *
     * @param QueryString|string $q
     *
     * @return QueryString
     */
    public function parse_query_string($q)
    {
        if (!($q instanceof QueryString))
        {
            $q = new QueryString($q);
        }

        foreach ($this->criterion_list as $criterion)
        {
            $criterion->parse_query_string($q);
        }

        return $q;
    }

    /**
     * Alters the conditions according to the specified modifiers.
     *
     * The {@link Criterion::alter_conditions()} method is invoked for each criterion.
     *
     * @param array $conditions The conditions to alter.
     * @param array $modifiers The modifiers.
     *
     * @return array The altered conditions.
     */
    public function alter_conditions(array &$conditions, array $modifiers)
    {
        foreach ($this->criterion_list as $criterion)
        {
            $criterion->alter_conditions($conditions, $modifiers);
        }

        return $this;
    }

    /**
     * Alters the query with initial requirements.
     *
     * The {@link Criterion::alter_query()} method is invoked for each criterion.
     *
     * @param Query $query
     *
     * @return CriterionList
     */
    public function alter_query(Query &$query)
    {
        foreach ($this->criterion_list as $criterion)
        {
            $query = $criterion->alter_query($query);
        }

        return $this;
    }

    /**
     * Alters the query with the criteria matching the values.
     *
     * The {@link Criterion::alter_query_with_value()} method is invoked for each criterion
     * matching the a value.
     *
     * @param Query $query The query to alter.
     * @param array $values The criteria values, as returned by the {@link alter_conditions()} method.
     *
     * @return CriterionList
     */
    public function alter_query_with_conditions(Query &$query, array $values)
    {
        foreach ($this->criterion_list as $criterion)
        {
            if (!array_key_exists($criterion->id, $values))
            {
                continue;
            }

            $value = $values[$criterion->id];
            $value = $criterion->parse_value($value);

            $query = $criterion->alter_query_with_value($query, $value);
        }

        return $this;
    }

    /**
     * Alters the query with a criterion and an order direction.
     *
     * The {@link Criterion::alter_query_with_order()} method is invoked on the criterion
     * matching the `$criterion_id` parameter.
     *
     * @param Query $query
     * @param string $criterion_id Criterion identifier. If prefixed with the minus sign "-"
     * `$order_direction` is overrode with `-1`.
     * @param int $order_direction The direction of the order: 1 ascending, -1 descending.
     * Default: 1.
     *
     * @return CriterionList
     */
    public function alter_query_with_order(Query &$query, $criterion_id, $order_direction = 1)
    {
        if ($criterion_id{0} == '-')
        {
            $order_direction = -1;
            $criterion_id = substr($criterion_id, 1);
        }

        if (empty($this->criterion_list[$criterion_id]))
        {
            return $this;
        }

        $query = $this->criterion_list[$criterion_id]
        ->alter_query_with_order($query, $order_direction);

        return $this;
    }

    /**
     * Alters the records with the criteria.
     *
     * The {@link Criterion::alter_records()} method is invoked for each criterion.
     *
     * @param array $records
     *
     * @return CriterionList
     */
    public function alter_records(array &$records)
    {
        foreach ($this->criterion_list as $criterion)
        {
            $criterion->alter_records($records);
        }

        return $this;
    }

    /**
     * Returns human readable values.
     *
     * @param array $conditions
     *
     * @return string[]
     */
    public function humanize(array $conditions)
    {
        $humanized = [];

        foreach ($this->criterion_list as $criterion_id => $criterion)
        {
            if (!isset($conditions[$criterion_id]) || $conditions[$criterion_id] === '')
            {
                continue;
            }

            $value = $criterion->parse_value($conditions[$criterion_id]);
            $humanized[$criterion_id] = $criterion->format_humanized_value($criterion->humanize($value));
        }

        return array_filter($humanized);
    }
}
ICanBoogie/Facets v0.3.1 API documentation generated by ApiGen