<commit_before><?php

namespace Elchristo\Calendar\Service\Builder;

use Elchristo\Calendar\Service\Config\Config;
use Elchristo\Calendar\Model\Event\CalendarEventInterface;
use Elchristo\Calendar\Model\Event\DefaultCalendarEvent;
use Elchristo\Calendar\Service\Color\DefaultColorStrategy;
use Elchristo\Calendar\Service\Color\ColorStrategyAwareInterface;

/**
 * Class to build instances of calendar events
 */
class EventBuilder
{
    private static $instance = null;
    private $configService;
    private $registeredEvents;
    private $registeredColors;
    private $registeredColorStrategies;

    /**
     * @param Config $config Calendar config
     */
    private function __construct(Config $config)
    {
        $this->configService = $config;
        $this->registeredEvents = $config->getRegisteredEvents();
        $this->registeredColors = $config->getRegisteredColors();
        $this->registeredColorStrategies = $config->getRegisteredColorStrategies();
    }

    /**
     * Build or return singleton instance of the builder
     *
     * @param Config $config
     * @return EventBuilder
     */
    public static function getInstance(Config $config)
    {
        if (self::$instance === null) {
            self::$instance = new self($config);
        }

        return self::$instance;
    }

    /**
     * Build a new calendar event instance
     *
     * @param string $name    Event name (in configuration)
     * @param array  $values  Event attribute values (name => value pairs)
     * @param array  $options Additional options passed to the event
     *
     * @return CalendarEventInterface
     */
    public function build($name, array $values = [], array $options = [])
    {
        if (isset($values['id'])) {
            $id = $values['id'];
            unset($values['id']);
        } else {
            $id = $this->generateEventId($name);
        }

        if (!\array_key_exists($name, $this->registeredEvents)
            || true !== \class_exists($this->registeredEvents[$name], true)
        ) {
            return new DefaultCalendarEvent($id, $values, $options);
        }

        // Apply color strategy
        $event = new $this->registeredEvents[$name]($id, $values, $options);
        if ($event instanceof ColorStrategyAwareInterface) {
            if (isset($options['color_strategy'])) {
                $this->injectColorStrategy($event, $options['color_strategy']);
            } else {
                \trigger_error(\sprintf('Missing option "colorStrategy" in event builder options for "%s". Default color strategy will be applied.', \get_class($event)), \E_USER_NOTICE);
            }
        }

        return $event;
    }

    /**
     * Generates an unique random event identifier prefixed by event name
     *
     * @param string $name The event name
     * @return string
     */
    private function generateEventId($name)
    {<commit_msg>Fix option name in error message<commit_after><?php

namespace Elchristo\Calendar\Service\Builder;

use Elchristo\Calendar\Service\Config\Config;
use Elchristo\Calendar\Model\Event\CalendarEventInterface;
use Elchristo\Calendar\Model\Event\DefaultCalendarEvent;
use Elchristo\Calendar\Service\Color\DefaultColorStrategy;
use Elchristo\Calendar\Service\Color\ColorStrategyAwareInterface;

/**
 * Class to build instances of calendar events
 */
class EventBuilder
{
    private static $instance = null;
    private $configService;
    private $registeredEvents;
    private $registeredColors;
    private $registeredColorStrategies;

    /**
     * @param Config $config Calendar config
     */
    private function __construct(Config $config)
    {
        $this->configService = $config;
        $this->registeredEvents = $config->getRegisteredEvents();
        $this->registeredColors = $config->getRegisteredColors();
        $this->registeredColorStrategies = $config->getRegisteredColorStrategies();
    }

    /**
     * Build or return singleton instance of the builder
     *
     * @param Config $config
     * @return EventBuilder
     */
    public static function getInstance(Config $config)
    {
        if (self::$instance === null) {
            self::$instance = new self($config);
        }

        return self::$instance;
    }

    /**
     * Build a new calendar event instance
     *
     * @param string $name    Event name (in configuration)
     * @param array  $values  Event attribute values (name => value pairs)
     * @param array  $options Additional options passed to the event
     *
     * @return CalendarEventInterface
     */
    public function build($name, array $values = [], array $options = [])
    {
        if (isset($values['id'])) {
            $id = $values['id'];
            unset($values['id']);
        } else {
            $id = $this->generateEventId($name);
        }

        if (!\array_key_exists($name, $this->registeredEvents)
            || true !== \class_exists($this->registeredEvents[$name], true)
        ) {
            return new DefaultCalendarEvent($id, $values, $options);
        }

        // Apply color strategy
        $event = new $this->registeredEvents[$name]($id, $values, $options);
        if ($event instanceof ColorStrategyAwareInterface) {
            if (isset($options['color_strategy'])) {
                $this->injectColorStrategy($event, $options['color_strategy']);
            } else {
                \trigger_error(\sprintf('Missing option "color_strategy" in event builder options for "%s". Default color strategy will be applied.', \get_class($event)), \E_USER_NOTICE);
            }
        }

        return $event;
    }

    /**
     * Generates an unique random event identifier prefixed by event name
     *
     * @param string $name The event name
     * @return string
     */
    private function generateEventId($name)
    {
{'fraction_non_alphanumeric': 0.11776788770968846, 'fraction_numerical': 0.0, 'mean_word_length': 2.6473158551810236, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '6610576', 'n_tokens_mistral': 1568, 'n_tokens_neox': 1529, 'n_words': 391}