<commit_before><?php
namespace CrudView\View\Helper;

use Cake\ORM\Entity;
use Cake\Utility\Hash;
use Cake\Utility\Inflector;
use Cake\Utility\Text;
use Cake\View\Helper;
use Cake\View\Helper\FormHelper;

class CrudViewHelper extends Helper
{

    /**
     * List of helpers used by this helper
     *
     * @var array
     */
    public $helpers = ['Form', 'Html', 'Time', 'Paginator'];

    /**
     * Context
     *
     * @var \Cake\ORM\Entity
     */
    protected $_context;

    /**
     * Set context
     *
     * @param \Cake\ORM\Entity $record Entity.
     * @return void
     */
    public function setContext(Entity $record)
    {
        $this->_context = $record;
    }

    /**
     * Get context
     *
     * @return \Cake\ORM\Entity
     */
    public function getContext()
    {
        return $this->_context;
    }

    /**
     * Process a single field into an output
     *
     * @param string $field The field to process.
     * @param array $data The raw entity data.
     * @param array $options Processing options.
     * @return string
     */
    public function process($field, Entity $data, array $options = [])
    {
        $this->setContext($data);

        $value = $this->fieldValue($data, $field);
        $options += ['formatter' => null];

        if ($options['formatter'] === 'element') {
            $context = $this->getContext();
            return $this->_View->element($options['element'], compact('context', 'field', 'value', 'options'));
        }

        if ($options['formatter'] === 'relation') {
            $relation = $this->relation($field, $value, $options);
            if ($relation) {
                return $relation['output'];
            }
        }

        if (is_callable($options['formatter'])) {
            return $options['formatter']($field, $value, $this->getContext(), $options);
        }

        $value = $this->introspect($field, $value, $options);

        return $value;
    }

    /**
     * Get the current field value
     *
     * @param array $data The raw entity data array.
     * @param string $field The field to extract, if null, the field from the entity context is used.
     * @return mixed
     */
    public function fieldValue(Entity $data = null, $field = null)
    {
        if (empty($field)) {
            $field = $this->field();
        }

        if (empty($data)) {
            $data = $this->getContext();
        }

        return $data->get($field);
    }

    /**
     * Returns a formatted output for a given field
     *
     * @param string $field Name of field.
     * @param array $value The value that the field should have within related data.
     * @param array $options Options array.
     * @return string formatted value
     */
    public function introspect($field, $value, array $options = [])
    {
        $output = $this->relation($field, $value, $options);

        if ($output) {
            return $output['output'];
        }

        $type = $this->schema()->columnType($field);

        if ($type === 'boolean') {
            return $this->formatBoolean($field, $value, $options);
        }

        if (in_array($type, ['datetime', 'date', 'timestamp'])) {
            return $this->formatDate($field, $value, $options);
        }

        if ($type === 'time') {
            return $this->formatTime($field, $value, $options);
        }

        $value = $this->formatString($field, $value, $options);

        if ($field === $this->getViewVar('displayField')) {
            $value = $this->createViewLink($value);
        }

        return $value;
    }

    /**
     * Format a boolean value for display
     *
     * @param string $field Name of field.
     * @param array $value Value of field.
     * @param array $options Options array.
     * @return string
     */
    public function formatBoolean($field, $value, array $options)
    {
        return (bool)$value ?
            $this->Html->label(__d('crud', 'Yes'), 'success') :
            $this->Html->label(__d('crud', 'No'), 'danger');
    }

    /**
     * Format a date for display
     *
     * @param string $field Name of field.
     * @param array $value Value of field.
     * @param array $options Options array.
     * @return string
     */
    public function formatDate($field, $value, array $options)
    {
        return $this->Time->timeAgoInWords($value, $options);
    }

    /**
     * Format a time for display
     *
     * @param string $field Name of field.
     * @param array $value Value of field.
     * @param array $options Options array.
     * @return string
     */
    public function formatTime($field, $value, array $options)
    {
        return $this->Time->nice($value, $options);
    }

    /**
     * Format a string for display
     *
     * @param string $field Name of field.
     * @param array $value Value of field.
     * @param array $options Options array.
     * @return string
     */
    public function formatString($field, $value, array $options)
    {
        return h(Text::truncate($value, 200));<commit_msg>Fixing bug in CrudView date formatting, a null value does not mean "now"<commit_after><?php
namespace CrudView\View\Helper;

use Cake\ORM\Entity;
use Cake\Utility\Hash;
use Cake\Utility\Inflector;
use Cake\Utility\Text;
use Cake\View\Helper;
use Cake\View\Helper\FormHelper;

class CrudViewHelper extends Helper
{

    /**
     * List of helpers used by this helper
     *
     * @var array
     */
    public $helpers = ['Form', 'Html', 'Time', 'Paginator'];

    /**
     * Context
     *
     * @var \Cake\ORM\Entity
     */
    protected $_context;

    /**
     * Set context
     *
     * @param \Cake\ORM\Entity $record Entity.
     * @return void
     */
    public function setContext(Entity $record)
    {
        $this->_context = $record;
    }

    /**
     * Get context
     *
     * @return \Cake\ORM\Entity
     */
    public function getContext()
    {
        return $this->_context;
    }

    /**
     * Process a single field into an output
     *
     * @param string $field The field to process.
     * @param array $data The raw entity data.
     * @param array $options Processing options.
     * @return string
     */
    public function process($field, Entity $data, array $options = [])
    {
        $this->setContext($data);

        $value = $this->fieldValue($data, $field);
        $options += ['formatter' => null];

        if ($options['formatter'] === 'element') {
            $context = $this->getContext();
            return $this->_View->element($options['element'], compact('context', 'field', 'value', 'options'));
        }

        if ($options['formatter'] === 'relation') {
            $relation = $this->relation($field, $value, $options);
            if ($relation) {
                return $relation['output'];
            }
        }

        if (is_callable($options['formatter'])) {
            return $options['formatter']($field, $value, $this->getContext(), $options);
        }

        $value = $this->introspect($field, $value, $options);

        return $value;
    }

    /**
     * Get the current field value
     *
     * @param array $data The raw entity data array.
     * @param string $field The field to extract, if null, the field from the entity context is used.
     * @return mixed
     */
    public function fieldValue(Entity $data = null, $field = null)
    {
        if (empty($field)) {
            $field = $this->field();
        }

        if (empty($data)) {
            $data = $this->getContext();
        }

        return $data->get($field);
    }

    /**
     * Returns a formatted output for a given field
     *
     * @param string $field Name of field.
     * @param array $value The value that the field should have within related data.
     * @param array $options Options array.
     * @return string formatted value
     */
    public function introspect($field, $value, array $options = [])
    {
        $output = $this->relation($field, $value, $options);

        if ($output) {
            return $output['output'];
        }

        $type = $this->schema()->columnType($field);

        if ($type === 'boolean') {
            return $this->formatBoolean($field, $value, $options);
        }

        if (in_array($type, ['datetime', 'date', 'timestamp'])) {
            return $this->formatDate($field, $value, $options);
        }

        if ($type === 'time') {
            return $this->formatTime($field, $value, $options);
        }

        $value = $this->formatString($field, $value, $options);

        if ($field === $this->getViewVar('displayField')) {
            $value = $this->createViewLink($value);
        }

        return $value;
    }

    /**
     * Format a boolean value for display
     *
     * @param string $field Name of field.
     * @param array $value Value of field.
     * @param array $options Options array.
     * @return string
     */
    public function formatBoolean($field, $value, array $options)
    {
        return (bool)$value ?
            $this->Html->label(__d('crud', 'Yes'), 'success') :
            $this->Html->label(__d('crud', 'No'), 'danger');
    }

    /**
     * Format a date for display
     *
     * @param string $field Name of field.
     * @param array $value Value of field.
     * @param array $options Options array.
     * @return string
     */
    public function formatDate($field, $value, array $options)
    {
        if ($value === null) {
            return $this->Html->label(__d('crud', 'N/A'), 'info');
        }
        return $this->Time->timeAgoInWords($value, $options);
    }

    /**
     * Format a time for display
     *
     * @param string $field Name of field.
     * @param array $value Value of field.
     * @param array $options Options array.
     * @return string
     */
    public function formatTime($field, $value, array $options)
    {
        if ($value === null) {
            return $this->Html->label(__d('crud', 'N/A'), 'info');
        }
        return $this->Time->nice($value, $options);
    }

    /**
     * Format a string for display
     *
     * @param string $field Name of field.
     * @param array $value Value of field.
     * @param array $options Options array.
     * @return string
     */
    public function formatString($field, $value, array $options)
    {
        return h(Text::truncate($value, 200));
{'fraction_non_alphanumeric': 0.14813034701528405, 'fraction_numerical': 0.0005767567047966932, 'mean_word_length': 2.073559822747415, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 69, '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': '2472421', 'n_tokens_mistral': 3032, 'n_tokens_neox': 2956, 'n_words': 849}