<commit_before><?php
/**
 * Elasticraft plugin for Craft CMS 3.x
 *
 * Desc.
 *
 * @link      https://dfo.no
 * @copyright Copyright (c) 2017 <NAME>
 */

namespace dfo\elasticraft\models;

use dfo\elasticraft\Elasticraft;

use Craft;
use craft\base\Model;
use craft\base\Element;

/**
 * ElasticDocument Model
 *
 * Models are containers for data. Just about every time information is passed
 * between services, controllers, and templates in Craft, it’s passed via a model.
 *
 * https://craftcms.com/docs/plugins/models
 *
 * @author    <NAME>
 * @package   Elasticraft
 * @since     1.0.0
 */
class ElasticDocument extends Model
{

    const ELEMENT_DOCUMENT_TYPE = 'element';

    // Public Properties
    // =========================================================================

    /**
     * Some model attribute
     *
     * @var string
     */
    public $type;
    public $id;
    public $body = [];

    protected $transformers = [];

    // Public Methods
    // =========================================================================

    public function init()
    {
        parent::init();

        $this->transformers = Elasticraft::$plugin
            ->getInstance()
            ->getSettings()
            ->transformers;
    }

    public static function withElement( craft\base\Element $element )
    {
        $instance = new self();
        $instance->_loadByElement( $element );
        return $instance;
    }

    // Helper methods
    // =========================================================================

    public static function elementHasTransformer( craft\base\Element $element): bool
    {
        $instance = new self();
        $transformer = $instance->_getTransformerForElement( $element );
        if( isset( $instance->transformers[$transformer] ) ) 
            return true;
        return false;
    }

    /**
     * Returns the validation rules for attributes.
     *
     * Validation rules are used by [[validate()]] to check if attribute values are valid.
     * Child classes may override this method to declare different validation rules.
     *
     * More info: http://www.yiiframework.com/doc-2.0/guide-input-validation.html
     *
     * @return array
     */
    public function rules()
    {
        return [
            [['id', 'type'], 'string'],
            [['body'], 'array'],
            [['id', 'type'], 'required'],
        ];
    }

    // Private Methods
    // =========================================================================

    private function _loadByElement( craft\base\Element $element )
    {
        $this->type = self::ELEMENT_DOCUMENT_TYPE;
        $this->id = $element->id;

        $transformer = $this->_getTransformerForElement( $element );

        // set document body if one is defined in the config.
        if( isset( $this->transformers[$transformer] ) ) 
            $this->body = $this->transformers[$transformer]->transform($element);

        // set body['type'] if it is not alreade defined in transformer
        if (!isset($this->body['type']))
            $this->body['type'] = $transformer;
    }

    private function _getTransformerForElement( craft\base\Element $element ): string
    {
        switch (get_class($element)) {
            case 'craft\elements\Entry':
                return $element->section->handle;
            case 'craft\elements\GlobalSet':
                return $element->handle;
            default:
                return 'default';
        }<commit_msg>cleaning.<commit_after><?php
/**
 * Elasticraft plugin for Craft CMS 3.x
 *
 * Desc.
 *
 * @link      https://dfo.no
 * @copyright Copyright (c) 2017 <NAME>
 */

namespace dfo\elasticraft\models;

use dfo\elasticraft\Elasticraft;

use Craft;
use craft\base\Model;
use craft\base\Element;

/**
 * ElasticDocument Model
 *
 * Models are containers for data. Just about every time information is passed
 * between services, controllers, and templates in Craft, it’s passed via a model.
 *
 * https://craftcms.com/docs/plugins/models
 *
 * @author    <NAME>
 * @package   Elasticraft
 * @since     1.0.0
 */
class ElasticDocument extends Model
{

    const ELEMENT_DOCUMENT_TYPE = 'element';

    // Public Properties
    // =========================================================================

    /**
     * Some model attribute
     *
     * @var string
     */
    public $type;
    public $id;
    public $body = [];

    protected $transformers = [];

    // Public Methods
    // =========================================================================

    public function init()
    {
        parent::init();

        $this->transformers = Elasticraft::$plugin
            ->getInstance()
            ->getSettings()
            ->transformers;
    }

    public static function withElement( Element $element )
    {
        $instance = new self();
        $instance->_loadByElement( $element );
        return $instance;
    }

    // Helper methods
    // =========================================================================

    public static function elementHasTransformer( Element $element): bool
    {
        $instance = new self();
        $transformer = $instance->_getTransformerForElement( $element );
        if( isset( $instance->transformers[$transformer] ) ) 
            return true;
        return false;
    }

    /**
     * Returns the validation rules for attributes.
     *
     * Validation rules are used by [[validate()]] to check if attribute values are valid.
     * Child classes may override this method to declare different validation rules.
     *
     * More info: http://www.yiiframework.com/doc-2.0/guide-input-validation.html
     *
     * @return array
     */
    public function rules()
    {
        return [
            [['id', 'type'], 'string'],
            [['id', 'type'], 'required'],
        ];
    }

    // Private Methods
    // =========================================================================

    private function _loadByElement( Element $element )
    {
        $this->type = self::ELEMENT_DOCUMENT_TYPE;
        $this->id = $element->id;

        $transformer = $this->_getTransformerForElement( $element );

        // set document body if one is defined in the config.
        if( isset( $this->transformers[$transformer] ) ) 
            $this->body = $this->transformers[$transformer]->transform($element);

        // set body['type'] if it is not alreade defined in transformer
        if (!isset($this->body['type']))
            $this->body['type'] = $transformer;
    }

    private function _getTransformerForElement( Element $element ): string
    {
        switch (get_class($element)) {
            case 'craft\elements\Entry':
                return $element->section->handle;
            case 'craft\elements\GlobalSet':
                return $element->handle;
            default:
                return 'default';
        }
{'fraction_non_alphanumeric': 0.19959531724237606, 'fraction_numerical': 0.0028905911258852437, 'mean_word_length': 2.4223541048466863, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 47, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '338444', 'n_tokens_mistral': 1847, 'n_tokens_neox': 1741, 'n_words': 468}