<commit_before><?php

namespace Bkwld\Decoy\Collections;

// Deps
use App;
use Bkwld\Library\Utils;
use Bkwld\Decoy\Exceptions\Exception;
use Bkwld\Decoy\Models\Element;
use Cache;
use Illuminate\Database\Eloquent\Collection as ModelCollection;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Log;
use Symfony\Component\Yaml\Yaml;

/**
 * Produces a store of all the site Elements
 */
class Elements extends Collection
{

    /**
     * The cache key used for the Elements collection
     *
     * @var string
     */
    protected $cache_key = 'decoy.elements.data';

    /**
     * The parse YAML contents
     *
     * @var array
     */
    protected $config;

    /**
     * Store whether this collection includes extra config from the YAML
     *
     * @var boolean
     */
    protected $has_extra = false;

    /**
     * Store the locale that was used to hydrate the collection
     *
     * @var string
     */
    protected $locale;

    /**
     * The model class that should be used for each Element instance
     *
     * @var string
     */
    protected $model = null;


    /**
     * An array of Element keys that have been persisted in the DB
     *
     * @var null | array
     */
    protected $updated_items = null;

    /**
     * Map the items into a collection of Element instances
     *
     * @return Bkwld\Decoy\Collections\Elements
     */
    public function asModels()
    {
        $this->hydrate();

        return new ModelCollection(array_map(function ($element, $key) {

            // Add the key as an attribute
            return new $this->model(array_merge($element, ['key' => $key]));
        }, $this->all(), array_keys($this->items)));
    }

    /**
     * Get an element given it's key
     *
     * @param  string                     $key
     * @return Bkwld\Decoy\Models\Element
     */
    public function get($key, $default = null)
    {
        $this->hydrate();

        // Build an element from the item in the collection or throw an
        // exception if the key isn't valid
        if (!$this->has($key)) {
            if ($default) {
                return $default;
            } else {
                if (App::isLocal()) {
                    throw new Exception("Element key '{$key}' is not declared in elements.yaml.");

                // If running on a server, return an empty Element, whose ->toString()
                // will return an empty string.
                } else {
                    \Log::error("Element key '{$key}' is not declared in elements.yaml.");

                    return new $this->model();
                }
            }

            // Add the key as an attribute
        } else {
            return new $this->model(array_merge($this->items[$key], ['key' => $key]));
        }
    }

    /**
     * Set the locale that should be used for this collection
     *
     * @param  string $locale
     * @return $this
     */
    public function localize($locale)
    {
        $this->locale = $locale;

        return $this;<commit_msg>Refactor Elements::get() to reduce indenting<commit_after><?php

namespace Bkwld\Decoy\Collections;

// Deps
use App;
use Bkwld\Library\Utils;
use Bkwld\Decoy\Exceptions\Exception;
use Bkwld\Decoy\Models\Element;
use Cache;
use Illuminate\Database\Eloquent\Collection as ModelCollection;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Log;
use Symfony\Component\Yaml\Yaml;

/**
 * Produces a store of all the site Elements
 */
class Elements extends Collection
{

    /**
     * The cache key used for the Elements collection
     *
     * @var string
     */
    protected $cache_key = 'decoy.elements.data';

    /**
     * The parse YAML contents
     *
     * @var array
     */
    protected $config;

    /**
     * Store whether this collection includes extra config from the YAML
     *
     * @var boolean
     */
    protected $has_extra = false;

    /**
     * Store the locale that was used to hydrate the collection
     *
     * @var string
     */
    protected $locale;

    /**
     * The model class that should be used for each Element instance
     *
     * @var string
     */
    protected $model = null;


    /**
     * An array of Element keys that have been persisted in the DB
     *
     * @var null | array
     */
    protected $updated_items = null;

    /**
     * Map the items into a collection of Element instances
     *
     * @return Bkwld\Decoy\Collections\Elements
     */
    public function asModels()
    {
        $this->hydrate();

        return new ModelCollection(array_map(function ($element, $key) {

            // Add the key as an attribute
            return new $this->model(array_merge($element, ['key' => $key]));
        }, $this->all(), array_keys($this->items)));
    }

    /**
     * Get an element given it's key
     *
     * @param  string                     $key
     * @return Bkwld\Decoy\Models\Element
     */
    public function get($key, $default = null)
    {
        $this->hydrate();

        // Create an Element instance using the data for the key
        if ($this->has($key)) {
            return new $this->model(array_merge($this->items[$key], ['key' => $key]));
        }

        // If the key doesn't exist but a default was passed in, return it
        if ($default) {
            return $default;
        }

        // if the key doesn't exist, but running locally, throw an exception
        if (App::isLocal()) {
            throw new Exception("Element key '{$key}' is not declared in elements.yaml.");
        }

        // Otherwise, like if key doesn't exist and running on production,
        // return an empty Element, whose ->toString() will return an empty string.
        Log::error("Element key '{$key}' is not declared in elements.yaml.");
        return new $this->model();
    }

    /**
     * Set the locale that should be used for this collection
     *
     * @param  string $locale
     * @return $this
     */
    public function localize($locale)
    {
        $this->locale = $locale;

        return $this;
{'fraction_non_alphanumeric': 0.11366622864651774, 'fraction_numerical': 0.0, 'mean_word_length': 1.9387065637065637, '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': '5373427', 'n_tokens_mistral': 1729, 'n_tokens_neox': 1672, 'n_words': 557}