<commit_before><?php

/*
 * This file is part of CalendR, a Fréquence web project.
 *
 * (c) 2012 Fréquence web
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CalendR\Event\Collection;

use CalendR\Period\PeriodInterface,
    CalendR\Event\EventInterface;

class Indexed implements CollectionInterface
{
    /**
     * @var array|array|EventInterface
     */
    protected $events;

    /**
     * Event count
     *
     * @var int
     */
    protected $count = 0;

    /**
     * The function used to index events.
     * Takes a \DateTime in parameter and must return an array index for this value.
     *
     * By default :
     * ```php
     *  function(\DateTime $dateTime) {
     *      return $dateTime->format('Y-m-d');
     *  }
     * ```
     *
     * @var callable
     */
    protected $indexFunction;

    /**
     * @param array $events
     * @param null $callable
     */
    public function __construct(array $events = array(), $callable = null)
    {
        if (is_callable($callable)) {
            $this->indexFunction = $callable;
        } else {
            $this->indexFunction = function(\DateTime $dateTime) {
                return $dateTime->format('Y-m-d');
            };
        }

        foreach ($events as $event) {
            $this->add($event);
        }
    }

    /**
     * Adds an event to the collection
     *
     * @param CalendR\Event\EventInterface $event
     */
    public function add(EventInterface $event)
    {
        $index = $this->computeIndex($event);
        if (isset($this->events[$index])) {
            $this->events[$index][] = $event;
        } else {
            $this->events[$index] = array($event);
        }

        $this->count++;
    }

    /**
     * Removes an event from the collection
     *
     * @param CalendR\Event\EventInterface $event
     */
    public function remove(EventInterface $event)
    {
        $index = $this->computeIndex($event);
        if (isset($this->events[$index])) {
            foreach ($this->events[$index] as $key => $event) {
                if ($event->getUid() == $event->getUid()) {
                    unset($this->events[$index][$key]);
                    $this->count--;
                }
            }
        }
    }

    /**
     * returns events
     *
     * @param $index
     * @return array
     */
    public function find($index)
    {
        if ($index instanceof PeriodInterface) {
            $index = $index->getBegin();
        }
        if ($index instanceof \DateTime) {
            $index = $this->computeIndex($index);
        }<commit_msg>Added has method to Indexed collection<commit_after><?php

/*
 * This file is part of CalendR, a Fréquence web project.
 *
 * (c) 2012 Fréquence web
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CalendR\Event\Collection;

use CalendR\Period\PeriodInterface,
    CalendR\Event\EventInterface;

class Indexed implements CollectionInterface
{
    /**
     * @var array|array|EventInterface
     */
    protected $events;

    /**
     * Event count
     *
     * @var int
     */
    protected $count = 0;

    /**
     * The function used to index events.
     * Takes a \DateTime in parameter and must return an array index for this value.
     *
     * By default :
     * ```php
     *  function(\DateTime $dateTime) {
     *      return $dateTime->format('Y-m-d');
     *  }
     * ```
     *
     * @var callable
     */
    protected $indexFunction;

    /**
     * @param array $events
     * @param null $callable
     */
    public function __construct(array $events = array(), $callable = null)
    {
        if (is_callable($callable)) {
            $this->indexFunction = $callable;
        } else {
            $this->indexFunction = function(\DateTime $dateTime) {
                return $dateTime->format('Y-m-d');
            };
        }

        foreach ($events as $event) {
            $this->add($event);
        }
    }

    /**
     * Adds an event to the collection
     *
     * @param CalendR\Event\EventInterface $event
     */
    public function add(EventInterface $event)
    {
        $index = $this->computeIndex($event);
        if (isset($this->events[$index])) {
            $this->events[$index][] = $event;
        } else {
            $this->events[$index] = array($event);
        }

        $this->count++;
    }

    /**
     * Removes an event from the collection
     *
     * @param CalendR\Event\EventInterface $event
     */
    public function remove(EventInterface $event)
    {
        $index = $this->computeIndex($event);
        if (isset($this->events[$index])) {
            foreach ($this->events[$index] as $key => $event) {
                if ($event->getUid() == $event->getUid()) {
                    unset($this->events[$index][$key]);
                    $this->count--;
                }
            }
        }
    }

    /**
     * Returns if we have events for the given index
     *
     * @return bool
     */
    public function has($index)
    {
      return 0 < count($this->find($index));
    }

    /**
     * returns events
     *
     * @param $index
     * @return array
     */
    public function find($index)
    {
        if ($index instanceof PeriodInterface) {
            $index = $index->getBegin();
        }
        if ($index instanceof \DateTime) {
            $index = $this->computeIndex($index);
        }
{'fraction_non_alphanumeric': 0.13632286995515694, 'fraction_numerical': 0.0019730941704035874, 'mean_word_length': 1.7755102040816326, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '399273', 'n_tokens_mistral': 1617, 'n_tokens_neox': 1585, 'n_words': 390}