<commit_before><?php

/**
 * Deep
 *
 * @package      rsanchez\Deep
 * @author       <NAME> <<EMAIL>>
 */

namespace rsanchez\Deep\Hydrator;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection;
use rsanchez\Deep\Collection\EntryCollection;
use rsanchez\Deep\Model\Entry;
use rsanchez\Deep\Hydrator\AbstractHydrator;
use rsanchez\Deep\Repository\SiteRepository;
use rsanchez\Deep\Repository\UploadPrefRepositoryInterface;

/**
 * Hydrator for the WYSIWYG fields
 */
class WysiwygHydrator extends AbstractHydrator
{
    /**
     * UploadPref model repository
     * @var \rsanchez\Deep\Repository\SiteRepository
     */
    protected $siteRepository;

    /**
     * UploadPref model repository
     * @var \rsanchez\Deep\Repository\UploadPrefRepositoryInterface
     */
    protected $uploadPrefRepository;

    /**
     * {@inheritdoc}
     *
     * @param \rsanchez\Deep\Collection\EntryCollection               $collection
     * @param string                                                  $fieldtype
     * @param \rsanchez\Deep\Repository\SiteRepository                $siteRepository
     * @param \rsanchez\Deep\Repository\UploadPrefRepositoryInterface $uploadPrefRepository
     */
    public function __construct(EntryCollection $collection, $fieldtype, SiteRepository $siteRepository, UploadPrefRepositoryInterface $uploadPrefRepository)
    {
        parent::__construct($collection, $fieldtype);

        $this->siteRepository = $siteRepository;

        $this->uploadPrefRepository = $uploadPrefRepository;
    }

    /**
     * {@inheritdoc}
     */
    public function hydrate(Entry $entry)
    {
        $fieldtype = $this->fieldtype;
        $collection = $this->collection;
        $uploadPrefRepository = $this->uploadPrefRepository;

        $parse = array($this, 'parse');

        // loop through all this fields
        $entry->channel->fieldsByType($this->fieldtype)->each(function ($field) use ($entry, $parse) {

            $value = $entry->getAttribute('field_id_'.$field->field_id);

            $value = call_user_func($parse, $value);

            $entry->setAttribute($field->field_name, $value);

        });

        // loop through all matrix fields
        $entry->channel->fieldsByType('matrix')->each(function ($field) use ($collection, $entry, $fieldtype, $parse) {

            $entry->getAttribute($field->field_name)->each(function ($row) use ($collection, $entry, $field, $fieldtype, $parse) {

                $cols = $collection->getMatrixCols()->filter(function ($col) use ($field, $fieldtype) {
                    return $col->field_id === $field->field_id && $col->col_type === $fieldtype;
                });

                $cols->each(function ($col) use ($row, $parse) {
                    $value = $row->getAttribute('col_id_'.$col->col_id);

                    $value = call_user_func($parse, $value);

                    $row->setAttribute($col->col_name, $value);
                });

            });

        });

        // loop through all grid fields
        $entry->channel->fieldsByType('grid')->each(function ($field) use ($collection, $entry, $fieldtype, $parse) {

            $entry->getAttribute($field->field_name)->each(function ($row) use ($collection, $entry, $field, $fieldtype, $parse) {

                $cols = $collection->getGridCols()->filter(function ($col) use ($field, $fieldtype) {
                    return $col->field_id === $field->field_id && $col->col_type === $fieldtype;
                });

                $cols->each(function ($col) use ($row, $parse) {
                    $value = $row->getAttribute('col_id_'.$col->col_id);

                    $value = call_user_func($parse, $value);

                    $row->setAttribute($col->col_name, $value);
                });

            });

        });
    }

    /**
     * Parse a string for these values:
     *
     * {filedir_X}, {assets_X:file_name}, {page_X}
     *
     * @param  string $value WYSIWYG content
     * @return string
     */
    protected function parse($value)
    {
        preg_match_all('#{page_(\d+)}#', $value, $pageMatches);

        foreach ($pageMatches[1] as $i => $entryId) {
            if ($pageUri = $this->siteRepository->getPageUri($entryId)) {
                $value = str_replace($pageMatches[0][$i], $pageUri, $value);
            }
        }

        preg_match_all('#{filedir_(\d+)}#', $value, $filedirMatches);

        foreach ($filedirMatches[1] as $i => $id) {
            if ($uploadPref = $this->uploadPrefRepository->find($id)) {
                $value = str_replace($filedirMatches[0][$i], $uploadPref->url, $value);
            }<commit_msg>Fix #11<commit_after><?php

/**
 * Deep
 *
 * @package      rsanchez\Deep
 * @author       <NAME> <<EMAIL>>
 */

namespace rsanchez\Deep\Hydrator;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection;
use rsanchez\Deep\Collection\EntryCollection;
use rsanchez\Deep\Model\Entry;
use rsanchez\Deep\Hydrator\AbstractHydrator;
use rsanchez\Deep\Repository\SiteRepository;
use rsanchez\Deep\Repository\UploadPrefRepositoryInterface;

/**
 * Hydrator for the WYSIWYG fields
 */
class WysiwygHydrator extends AbstractHydrator
{
    /**
     * UploadPref model repository
     * @var \rsanchez\Deep\Repository\SiteRepository
     */
    protected $siteRepository;

    /**
     * UploadPref model repository
     * @var \rsanchez\Deep\Repository\UploadPrefRepositoryInterface
     */
    protected $uploadPrefRepository;

    /**
     * {@inheritdoc}
     *
     * @param \rsanchez\Deep\Collection\EntryCollection               $collection
     * @param string                                                  $fieldtype
     * @param \rsanchez\Deep\Repository\SiteRepository                $siteRepository
     * @param \rsanchez\Deep\Repository\UploadPrefRepositoryInterface $uploadPrefRepository
     */
    public function __construct(EntryCollection $collection, $fieldtype, SiteRepository $siteRepository, UploadPrefRepositoryInterface $uploadPrefRepository)
    {
        parent::__construct($collection, $fieldtype);

        $this->siteRepository = $siteRepository;

        $this->uploadPrefRepository = $uploadPrefRepository;
    }

    /**
     * {@inheritdoc}
     */
    public function hydrate(Entry $entry)
    {
        $fieldtype = $this->fieldtype;
        $collection = $this->collection;
        $uploadPrefRepository = $this->uploadPrefRepository;

        $parse = array($this, 'parse');

        // loop through all this fields
        $entry->channel->fieldsByType($this->fieldtype)->each(function ($field) use ($entry, $parse) {

            $value = $entry->getAttribute('field_id_'.$field->field_id);

            $value = call_user_func($parse, $value);

            $entry->setAttribute($field->field_name, $value);

        });

        // loop through all matrix fields
        $entry->channel->fieldsByType('matrix')->each(function ($field) use ($collection, $entry, $fieldtype, $parse) {

            $entry->getAttribute($field->field_name)->each(function ($row) use ($collection, $entry, $field, $fieldtype, $parse) {

                $cols = $collection->getMatrixCols()->filter(function ($col) use ($field, $fieldtype) {
                    return $col->field_id === $field->field_id && $col->col_type === $fieldtype;
                });

                $cols->each(function ($col) use ($row, $parse) {
                    $value = $row->getAttribute('col_id_'.$col->col_id);

                    $value = call_user_func($parse, $value);

                    $row->setAttribute($col->col_name, $value);
                });

            });

        });

        // loop through all grid fields
        $entry->channel->fieldsByType('grid')->each(function ($field) use ($collection, $entry, $fieldtype, $parse) {

            $entry->getAttribute($field->field_name)->each(function ($row) use ($collection, $entry, $field, $fieldtype, $parse) {

                $cols = $collection->getGridCols()->filter(function ($col) use ($field, $fieldtype) {
                    return $col->field_id === $field->field_id && $col->col_type === $fieldtype;
                });

                $cols->each(function ($col) use ($row, $parse) {
                    $value = $row->getAttribute('col_id_'.$col->col_id);

                    $value = call_user_func($parse, $value);

                    $row->setAttribute($col->col_name, $value);
                });

            });

        });
    }

    /**
     * Parse a string for these values:
     *
     * {filedir_X}, {assets_X:file_name}, {page_X}
     *
     * @param  string $value WYSIWYG content
     * @return string
     */
    public function parse($value)
    {
        preg_match_all('#{page_(\d+)}#', $value, $pageMatches);

        foreach ($pageMatches[1] as $i => $entryId) {
            if ($pageUri = $this->siteRepository->getPageUri($entryId)) {
                $value = str_replace($pageMatches[0][$i], $pageUri, $value);
            }
        }

        preg_match_all('#{filedir_(\d+)}#', $value, $filedirMatches);

        foreach ($filedirMatches[1] as $i => $id) {
            if ($uploadPref = $this->uploadPrefRepository->find($id)) {
                $value = str_replace($filedirMatches[0][$i], $uploadPref->url, $value);
            }
{'fraction_non_alphanumeric': 0.14903177490103778, 'fraction_numerical': 0.0010698619878035734, 'mean_word_length': 2.394335511982571, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 113, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '500558', 'n_tokens_mistral': 2728, 'n_tokens_neox': 2625, 'n_words': 481}