<commit_before><?php

namespace TypiCMS\Modules\Core\Presenters;

use Carbon\Carbon;
use Croppa;
use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
use Laracasts\Presenter\Presenter as BasePresenter;

abstract class Presenter extends BasePresenter
{
    /**
     * @var mixed
     */
    protected $entity;

    /**
     * @param $entity
     */
    public function __construct($entity)
    {
        $this->entity = $entity;
    }

    /**
     * Allow for property-style retrieval.
     *
     * @param $property
     *
     * @return mixed
     */
    public function __get($property)
    {
        if (method_exists($this, $property)) {
            return $this->{$property}();
        }

        return $this->entity->{$property};
    }

    /**
     * Return a localized date.
     *
     * @param string $column
     *
     * @return Carbon
     */
    public function dateLocalized($column = 'date')
    {
        return $this->entity->$column->formatLocalized('%d %B %Y');
    }

    /**
     * Return a localized date and time.
     *
     * @param string $column
     *
     * @return Carbon
     */
    public function dateTimeLocalized($column = 'datetime')
    {
        return $this->entity->$column->formatLocalized('%d %B %Y %H:%M');
    }

    /**
     * Return resource's datetime or curent date and time if empty.
     *
     * @param string $column
     *
     * @return Carbon
     */
    public function datetimeOrNow($column = 'date')
    {
        $date = $this->entity->$column ?: Carbon::now();

        return $date->format('Y-m-d G:i:s');
    }

    /**
     * Return resource's date or curent date if empty.
     *
     * @param string $column
     *
     * @return Carbon
     */
    public function dateOrNow($column = 'date')
    {
        $date = $this->entity->$column ?: Carbon::now();

        return $date->format('Y-m-d');
    }

    /**
     * Return resource's time or curent time if empty.
     *
     * @param string $column
     *
     * @return Carbon
     */
    public function timeOrNow($column = 'date')
    {
        $date = $this->entity->$column ?: Carbon::now();

        return $date->format('G:i');
    }

    /**
     * Get url without http://.
     *
     * @param string $column
     *
     * @return string
     */
    public function urlWithoutScheme($column = 'website')
    {
        return str_replace(['http://', 'https://'], '', $this->entity->$column);
    }

    /**
     * Generate an external link.
     *
     * @param string $column
     *
     * @return string
     */
    public function link($column = 'website')
    {
        return '<a href="'.$this->entity->$column.'" target="_blank">'.$this->urlWithoutScheme($column).'</a>';
    }

    /**
     * Get the path of files linked to this model.
     *
     * @param Model  $model
     * @param string $field
     *
     * @return string path
     */
    protected function getPath(Model $model, $field = null)
    {
        $path = '';
        try {
            $path = '/'.$model->$field;
        } catch (Exception $e) {
            Log::info($e->getMessage());
        }

        return $path;
    }

    /**
     * Return src string of a resized or cropped image.
     *
     * @param int    $width   width of image, null for auto
     * @param int    $height  height of image, null for auto
     * @param array  $options see Croppa doc for options (https://github.com/BKWLD/croppa)
     * @param string $field   column name
     *
     * @return string HTML markup of an image
     */
    public function thumbSrc($width = null, $height = null, array $options = [], $field = 'image')
    {
        $src = $this->getPath($this->entity, $field);
        if (!is_file(public_path().$src)) {
            $src = $this->imgNotFound();
        }

        return Croppa::url($src, $width, $height, $options);
    }

    /**
     * Return url of a thumb.
     *
     * @param int    $width   width of image, null for auto
     * @param int    $height  height of image, null for auto
     * @param array  $options see Croppa doc for options (https://github.com/BKWLD/croppa)
     * @param string $field   column name
     *
     * @return string HTML markup of an image
     */
    public function thumbUrl($width = null, $height = null, array $options = [], $field = 'image')
    {
        $src = $this->thumbSrc($width, $height, $options, $field);

        return url($src);
    }

    /**
     * Return a resized or cropped img tag.
     *
     * @param int    $width   width of image, null for auto
     * @param int    $height  height of image, null for auto<commit_msg>return the url if the path doesn't match the croppa path pattern<commit_after><?php

namespace TypiCMS\Modules\Core\Presenters;

use Carbon\Carbon;
use Croppa;
use Config;
use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
use Laracasts\Presenter\Presenter as BasePresenter;

abstract class Presenter extends BasePresenter
{
    /**
     * @var mixed
     */
    protected $entity;

    /**
     * @param $entity
     */
    public function __construct($entity)
    {
        $this->entity = $entity;
    }

    /**
     * Allow for property-style retrieval.
     *
     * @param $property
     *
     * @return mixed
     */
    public function __get($property)
    {
        if (method_exists($this, $property)) {
            return $this->{$property}();
        }

        return $this->entity->{$property};
    }

    /**
     * Return a localized date.
     *
     * @param string $column
     *
     * @return Carbon
     */
    public function dateLocalized($column = 'date')
    {
        return $this->entity->$column->formatLocalized('%d %B %Y');
    }

    /**
     * Return a localized date and time.
     *
     * @param string $column
     *
     * @return Carbon
     */
    public function dateTimeLocalized($column = 'datetime')
    {
        return $this->entity->$column->formatLocalized('%d %B %Y %H:%M');
    }

    /**
     * Return resource's datetime or curent date and time if empty.
     *
     * @param string $column
     *
     * @return Carbon
     */
    public function datetimeOrNow($column = 'date')
    {
        $date = $this->entity->$column ?: Carbon::now();

        return $date->format('Y-m-d G:i:s');
    }

    /**
     * Return resource's date or curent date if empty.
     *
     * @param string $column
     *
     * @return Carbon
     */
    public function dateOrNow($column = 'date')
    {
        $date = $this->entity->$column ?: Carbon::now();

        return $date->format('Y-m-d');
    }

    /**
     * Return resource's time or curent time if empty.
     *
     * @param string $column
     *
     * @return Carbon
     */
    public function timeOrNow($column = 'date')
    {
        $date = $this->entity->$column ?: Carbon::now();

        return $date->format('G:i');
    }

    /**
     * Get url without http://.
     *
     * @param string $column
     *
     * @return string
     */
    public function urlWithoutScheme($column = 'website')
    {
        return str_replace(['http://', 'https://'], '', $this->entity->$column);
    }

    /**
     * Generate an external link.
     *
     * @param string $column
     *
     * @return string
     */
    public function link($column = 'website')
    {
        return '<a href="'.$this->entity->$column.'" target="_blank">'.$this->urlWithoutScheme($column).'</a>';
    }

    /**
     * Get the path of files linked to this model.
     *
     * @param Model  $model
     * @param string $field
     *
     * @return string path
     */
    protected function getPath(Model $model, $field = null)
    {
        $path = '';
        try {
            $path = '/'.$model->$field;
        } catch (Exception $e) {
            Log::info($e->getMessage());
        }

        return $path;
    }

    /**
     * Return src string of a resized or cropped image.
     *
     * @param int    $width   width of image, null for auto
     * @param int    $height  height of image, null for auto
     * @param array  $options see Croppa doc for options (https://github.com/BKWLD/croppa)
     * @param string $field   column name
     *
     * @return string HTML markup of an image
     */
    public function thumbSrc($width = null, $height = null, array $options = [], $field = 'image')
    {
        $src = $this->getPath($this->entity, $field);
        if (!is_file(public_path().$src)) {
            $src = $this->imgNotFound();
        } else if (!preg_match('#'.Config::get('croppa.path').'#', $src)) {
            return $src;
        }

        return Croppa::url($src, $width, $height, $options);
    }

    /**
     * Return url of a thumb.
     *
     * @param int    $width   width of image, null for auto
     * @param int    $height  height of image, null for auto
     * @param array  $options see Croppa doc for options (https://github.com/BKWLD/croppa)
     * @param string $field   column name
     *
     * @return string HTML markup of an image
     */
    public function thumbUrl($width = null, $height = null, array $options = [], $field = 'image')
    {
        $src = $this->thumbSrc($width, $height, $options, $field);

        return url($src);
    }

    /**
     * Return a resized or cropped img tag.
     *
     * @param int    $width   width of image, null for auto
     * @param int    $height  height of image, null for auto
{'fraction_non_alphanumeric': 0.14540059347181009, 'fraction_numerical': 0.0, 'mean_word_length': 1.974156949259376, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 67, 'https://': 6, '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': '4484971', 'n_tokens_mistral': 2923, 'n_tokens_neox': 2843, 'n_words': 780}