<commit_before><?php
namespace Event;

use Talk\TalkApi;
use Talk\TalkEntity;

/**
 * Class EventScheduler
 *
 * Takes an event and constructs a data
 * structure to facilitate schedule layout view
 *
 */
class EventScheduler
{
    protected $talkApi;
    protected $distinctDates;

    /**
     * Constructor
     *
     * @param TalkApi $talkApi
     */
    public function __construct(TalkApi $talkApi)
    {
        $this->talkApi = $talkApi;
    }

    /**
     * Builds schedule data into an array structure
     * for schedule view
     *
     * @param EventEntity $event
     * @return array
     */
    public function getScheduleData(EventEntity $event)
    {
        $talks     = $this->getTalks($event->getTalksUri().'?start=0&resultsperpage=1000');
        $eventDays = $this->getEventDays($talks);

        return $eventDays;
    }

    /**
     * Retrieves talk collection from API
     *
     * @param string $talks_uri
     * @return array
     */
    public function getTalks($talks_uri)
    {
        $talks = $this->talkApi->getCollection($talks_uri);

        return $talks;
    }

    /**
     * Get an array of populated EventSchedulerDay objects
     *
     * @param array $talks
     * @return array Array of EventSchedulerDay objects
     */
    public function getEventDays($talks)
    {
        if (empty($talks) || empty($talks['talks'])) {
            return [];
        }

        $talks      = $talks['talks'];
        $talksByDay = $this->organiseTalksByDayAndTime($talks);

        $tracksByDay = $this->getTracksByDay($talks);

        $eventDays = [];
        foreach ($talksByDay as $date => $talks) {
            $eventDays[] = new EventSchedulerDay($date, $talks, $tracksByDay[$date]);
        }

        return $eventDays;
    }

    /**
     * Organise event talks into an multi-dimensional
     * associative array by day, then by time for each
     * day
     *
     * @param TalkEntity[] $talks
     * @return array
     */
    protected function organiseTalksByDayAndTime($talks)
    {
        $talksByDay = [];
        usort($talks, function (TalkEntity $a, TalkEntity $b) {
            return $a->getStartDateTime() <=> $b->getStartDateTime() ?:
                ($a->getTracks() && $b->getTracks()
                    ? strcasecmp($a->getTracks()[0]->track_uri, $b->getTracks()[0]->track_uri)
                    : $a['id'] <=> $b['id']);
        });

        foreach ($talks as $talk) {
            $dateTime = $talk->getStartDateTime();
            $date     = $dateTime->format('d-m-Y');
            $time     = $dateTime->format('H:i');

            if (!isset($talksByDay[$date]) || !array_key_exists($date, $talksByDay)) {
                $talksByDay[$date] = [];
            }
            if (!isset($talksByDay[$date][$time]) || !array_key_exists($time, $talksByDay[$date])) {
                $talksByDay[$date][$time] = [];
            }

            $talksByDay[$date][$time][] = $talk;<commit_msg>Ensure talks and tracks are ordered the same way<commit_after><?php
namespace Event;

use Talk\TalkApi;
use Talk\TalkEntity;

/**
 * Class EventScheduler
 *
 * Takes an event and constructs a data
 * structure to facilitate schedule layout view
 *
 */
class EventScheduler
{
    protected $talkApi;
    protected $distinctDates;

    /**
     * Constructor
     *
     * @param TalkApi $talkApi
     */
    public function __construct(TalkApi $talkApi)
    {
        $this->talkApi = $talkApi;
    }

    /**
     * Builds schedule data into an array structure
     * for schedule view
     *
     * @param EventEntity $event
     * @return array
     */
    public function getScheduleData(EventEntity $event)
    {
        $talks     = $this->getTalks($event->getTalksUri().'?start=0&resultsperpage=1000');
        $eventDays = $this->getEventDays($talks);

        return $eventDays;
    }

    /**
     * Retrieves talk collection from API
     *
     * @param string $talks_uri
     * @return array
     */
    public function getTalks($talks_uri)
    {
        $talks = $this->talkApi->getCollection($talks_uri);

        return $talks;
    }

    /**
     * Get an array of populated EventSchedulerDay objects
     *
     * @param array $talks
     * @return array Array of EventSchedulerDay objects
     */
    public function getEventDays($talks)
    {
        if (empty($talks) || empty($talks['talks'])) {
            return [];
        }

        $talks      = $talks['talks'];
        usort($talks, function (TalkEntity $a, TalkEntity $b) {
            return $a->getStartDateTime() <=> $b->getStartDateTime() ?:
                ($a->getTracks() && $b->getTracks()
                    ? strcasecmp($a->getTracks()[0]->track_uri, $b->getTracks()[0]->track_uri)
                    : $a['id'] <=> $b['id']);
        });

        $talksByDay = $this->organiseTalksByDayAndTime($talks);

        $tracksByDay = $this->getTracksByDay($talks);

        $eventDays = [];
        foreach ($talksByDay as $date => $talks) {
            $eventDays[] = new EventSchedulerDay($date, $talks, $tracksByDay[$date]);
        }

        return $eventDays;
    }

    /**
     * Organise event talks into an multi-dimensional
     * associative array by day, then by time for each
     * day
     *
     * @param TalkEntity[] $talks
     * @return array
     */
    protected function organiseTalksByDayAndTime($talks)
    {
        $talksByDay = [];

        foreach ($talks as $talk) {
            $dateTime = $talk->getStartDateTime();
            $date     = $dateTime->format('d-m-Y');
            $time     = $dateTime->format('H:i');

            if (!isset($talksByDay[$date]) || !array_key_exists($date, $talksByDay)) {
                $talksByDay[$date] = [];
            }
            if (!isset($talksByDay[$date][$time]) || !array_key_exists($time, $talksByDay[$date])) {
                $talksByDay[$date][$time] = [];
            }

            $talksByDay[$date][$time][] = $talk;
{'fraction_non_alphanumeric': 0.14598049108644467, 'fraction_numerical': 0.0023545240497813654, 'mean_word_length': 2.1432346723044398, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 47, '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': '949497', 'n_tokens_mistral': 1909, 'n_tokens_neox': 1821, 'n_words': 375}