<commit_before><?php

namespace CatLab\Charon\Models\Properties\Base;

use CatLab\Charon\Enums\Action;
use CatLab\Charon\Exceptions\InvalidScalarException;
use CatLab\Charon\Interfaces\Context;
use CatLab\Charon\Interfaces\ResourceDefinitionManipulator;
use CatLab\Charon\Interfaces\Transformer;
use CatLab\Charon\Library\TransformerLibrary;
use CatLab\Charon\Models\CurrentPath;
use CatLab\Charon\Models\Properties\IdentifierField;
use CatLab\Charon\Models\Properties\RelationshipField;
use CatLab\Charon\Models\Properties\ResourceField;
use CatLab\Charon\Models\ResourceDefinition;
use CatLab\Charon\Transformers\DateTransformer;
use CatLab\Charon\Transformers\ScalarTransformer;
use CatLab\Requirements\Enums\PropertyType;
use CatLab\Requirements\Exceptions\PropertyValidationException;
use CatLab\Requirements\Interfaces\Property;
use CatLab\Requirements\Interfaces\Validator;

/**
 * Class Field
 * @package CatLab\Charon\Models\Properties\Base
 */
class Field implements Property, ResourceDefinitionManipulator
{
    use \CatLab\Requirements\Traits\RequirementSetter {
        setType as traitSetType;
    }

    /**
     * Define in which contexts this attribute should be visible
     * @var string[]
     */
    protected $actions;

    /**
     * @var string
     */
    protected $name;

    /**
     * Specify to give the field a different name
     * @var string
     */
    protected $displayName;

    /**
     * @var ResourceDefinition
     */
    protected $resourceDefinition;

    /**
     * @var bool
     */
    protected $visible;

    /**
     * @var string
     */
    protected $path;

    /**
     * @var string
     */
    protected $transformer;

    /**
     * @var bool
     */
    protected $requiredForSorting;

    /**
     * @var bool
     */
    protected $alwaysValidate;

    /**
     * ResourceField constructor.
     * @param ResourceDefinition $resourceDefinition
     * @param string $fieldName
     */
    public function __construct(ResourceDefinition $resourceDefinition, $fieldName)
    {
        $this->actions = [];
        $this->visible = false;
        $this->path = '';
        $this->type = PropertyType::STRING;
        $this->alwaysValidate = false;

        $this->name = $fieldName;
        $this->displayName = $fieldName;
        $this->resourceDefinition = $resourceDefinition;

        $this->requiredForSorting = false;
    }

    /**
     * @param string $type
     * @param bool $useTransformer
     * @return $this
     */
    public function setType($type, $useTransformer = true)
    {
        $this->traitSetType($type);

        // Set scalar transformer if type is not string.
        if ($useTransformer) {

            switch ($type) {
                case PropertyType::BOOL:
                case PropertyType::INTEGER:
                case PropertyType::NUMBER:

                    try {
                        $this->transformer(new ScalarTransformer($type));
                    } catch (InvalidScalarException $e) {
                        // silently ignore
                    }

                    break;
            }
        }

        return $this;
    }

    /**
     * @return ResourceDefinition
     */
    public function getResourceDefinition()
    {
        return $this->resourceDefinition;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @return string
     */
    public function getDisplayName()
    {
        return $this->displayName;
    }

    /**
     * @param string $displayName
     * @return $this
     */
    public function setDisplayName($displayName)
    {
        $this->displayName = $displayName;
        return $this;
    }

    /**
     * @param string $name
     * @return $this
     */
    public function display($name)
    {
        return $this->setDisplayName($name);
    }

    /**<commit_msg>Add a 'label' property to fields.<commit_after><?php

namespace CatLab\Charon\Models\Properties\Base;

use CatLab\Charon\Enums\Action;
use CatLab\Charon\Exceptions\InvalidScalarException;
use CatLab\Charon\Interfaces\Context;
use CatLab\Charon\Interfaces\ResourceDefinitionManipulator;
use CatLab\Charon\Interfaces\Transformer;
use CatLab\Charon\Library\TransformerLibrary;
use CatLab\Charon\Models\CurrentPath;
use CatLab\Charon\Models\Properties\IdentifierField;
use CatLab\Charon\Models\Properties\RelationshipField;
use CatLab\Charon\Models\Properties\ResourceField;
use CatLab\Charon\Models\ResourceDefinition;
use CatLab\Charon\Transformers\DateTransformer;
use CatLab\Charon\Transformers\ScalarTransformer;
use CatLab\Requirements\Enums\PropertyType;
use CatLab\Requirements\Exceptions\PropertyValidationException;
use CatLab\Requirements\Interfaces\Property;
use CatLab\Requirements\Interfaces\Validator;

/**
 * Class Field
 * @package CatLab\Charon\Models\Properties\Base
 */
class Field implements Property, ResourceDefinitionManipulator
{
    use \CatLab\Requirements\Traits\RequirementSetter {
        setType as traitSetType;
    }

    /**
     * Define in which contexts this attribute should be visible
     * @var string[]
     */
    protected $actions;

    /**
     * @var string
     */
    protected $name;

    /**
     * Specify to give the field a different name
     * @var string
     */
    protected $displayName;

    /**
     * Specify a human readable name to show in a potential editor.
     * @var string
     */
    protected $labelName;

    /**
     * @var ResourceDefinition
     */
    protected $resourceDefinition;

    /**
     * @var bool
     */
    protected $visible;

    /**
     * @var string
     */
    protected $path;

    /**
     * @var string
     */
    protected $transformer;

    /**
     * @var bool
     */
    protected $requiredForSorting;

    /**
     * @var bool
     */
    protected $alwaysValidate;

    /**
     * ResourceField constructor.
     * @param ResourceDefinition $resourceDefinition
     * @param string $fieldName
     */
    public function __construct(ResourceDefinition $resourceDefinition, $fieldName)
    {
        $this->actions = [];
        $this->visible = false;
        $this->path = '';
        $this->type = PropertyType::STRING;
        $this->alwaysValidate = false;

        $this->name = $fieldName;
        $this->displayName = $fieldName;
        $this->resourceDefinition = $resourceDefinition;

        $this->requiredForSorting = false;
    }

    /**
     * @param string $type
     * @param bool $useTransformer
     * @return $this
     */
    public function setType($type, $useTransformer = true)
    {
        $this->traitSetType($type);

        // Set scalar transformer if type is not string.
        if ($useTransformer) {

            switch ($type) {
                case PropertyType::BOOL:
                case PropertyType::INTEGER:
                case PropertyType::NUMBER:

                    try {
                        $this->transformer(new ScalarTransformer($type));
                    } catch (InvalidScalarException $e) {
                        // silently ignore
                    }

                    break;
            }
        }

        return $this;
    }

    /**
     * @return ResourceDefinition
     */
    public function getResourceDefinition()
    {
        return $this->resourceDefinition;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @return string
     */
    public function getDisplayName()
    {
        return $this->displayName;
    }

    /**
     * @param string $displayName
     * @return $this
     */
    public function setDisplayName($displayName)
    {
        $this->displayName = $displayName;
        return $this;
    }

    /**
     * @param string $name
     * @return $this
     */
    public function display($name)
    {
        return $this->setDisplayName($name);
    }

    /**
     * @param $name
     * @return $this
     */
    public function setLabel($name)
    {
        $this->labelName = $name;
        return $this;
    }

    /**
     * @param $name
     * @return $this
     */
    public function label($name)
    {
        return $this->setLabel($name);
    }

    /**
     * @return string
     */
    public function getLabel()
    {
        if ($this->labelName) {
            return $this->labelName;
        }
        return $this->getDisplayName();
    }

    /**
{'fraction_non_alphanumeric': 0.11187403812004262, 'fraction_numerical': 0.0, 'mean_word_length': 2.085463842220599, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 40, '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': '3814721', 'n_tokens_mistral': 2438, 'n_tokens_neox': 2328, 'n_words': 489}