<commit_before><?php
namespace TypiCMS\Modules\Pages\Observers;

use Config;
use TypiCMS\Modules\Pages\Models\PageTranslation;

class UriObserver
{

    /**
     * On create, update uri
     *
     * @param  PageTranslation $model
     * @return void
     */
    public function creating(PageTranslation $model)
    {

        $model->uri = $this->incrementWhileExists($model, $model->slug);

    }

    /**
     * On update, change uri
     *
     * @param  PageTranslation $model
     * @return void
     */
    public function updating(PageTranslation $model)
    {

        $parentUri = $this->getParentUri($model);

        if ($parentUri) {
            $uri = $parentUri;
            if ($model->slug) {
                $uri .= '/' . $model->slug;
            }
        } else {
            $uri = $model->slug;
        }

        $model->uri = $this->incrementWhileExists($model, $uri, $model->id);
    }

    /**
     * Get parent page’s URI
     *
     * @param  PageTranslation $model
     * @return string
     */
    private function getParentUri(PageTranslation $model)
    {
        if ($parentPage = $model->page->parent) {
            return $parentPage->translate($model->locale)->uri;
        }
        return null;
    }

    /**
     * Check if uri exists in all uris array
     *
     * @param  string  $uri
     * @param  integer $id
     * @return bool
     */
    private function uriExists($model, $uri, $id)
    {
        $query = $model->where('uri', $uri)
            ->where('locale', $model->locale);
        if ($id) {
            $query->where('id', '!=', $id);
        }

        if ($query->first()) {
            return true;
        }
        return false;
    }

    /**
     * Add '-x' on uri if it exists in page_translations table
     *
     * @param  string  $uri
     * @param  integer $id in case of update, except this id
     * @return string
     */
    private function incrementWhileExists($model, $uri, $id = null)
    {
        $originalUri = $uri;

        $i = 0;
        // Check if uri is unique
        while ($this->uriExists($model, $uri, $id)) {
            $i++;
            // increment uri if it exists
            $uri = $originalUri . '-' . $i;
        }

        return $uri;
    }
}<commit_msg>uri = null when empty<commit_after><?php
namespace TypiCMS\Modules\Pages\Observers;

use Config;
use TypiCMS\Modules\Pages\Models\PageTranslation;

class UriObserver
{

    /**
     * On create, update uri
     *
     * @param  PageTranslation $model
     * @return void
     */
    public function creating(PageTranslation $model)
    {

        $model->uri = $this->incrementWhileExists($model, $model->slug);

    }

    /**
     * On update, change uri
     *
     * @param  PageTranslation $model
     * @return void
     */
    public function updating(PageTranslation $model)
    {

        $parentUri = $this->getParentUri($model);

        if ($parentUri) {
            $uri = $parentUri;
            if ($model->slug) {
                $uri .= '/' . $model->slug;
            }
        } else {
            $uri = $model->slug;
        }

        $model->uri = $this->incrementWhileExists($model, $uri, $model->id);
    }

    /**
     * Get parent page’s URI
     *
     * @param  PageTranslation $model
     * @return string
     */
    private function getParentUri(PageTranslation $model)
    {
        if ($parentPage = $model->page->parent) {
            return $parentPage->translate($model->locale)->uri;
        }
        return null;
    }

    /**
     * Check if uri exists in all uris array
     *
     * @param  string  $uri
     * @param  integer $id
     * @return bool
     */
    private function uriExists($model, $uri, $id)
    {
        $query = $model->where('uri', $uri)
            ->where('locale', $model->locale);
        if ($id) {
            $query->where('id', '!=', $id);
        }

        if ($query->first()) {
            return true;
        }
        return false;
    }

    /**
     * Add '-x' on uri if it exists in page_translations table
     *
     * @param  string  $uri
     * @param  integer $id in case of update, except this id
     * @return string
     */
    private function incrementWhileExists($model, $uri, $id = null)
    {
        if (! $uri) {
            return null;
        }

        $originalUri = $uri;

        $i = 0;
        // Check if uri is unique
        while ($this->uriExists($model, $uri, $id)) {
            $i++;
            // increment uri if it exists
            $uri = $originalUri . '-' . $i;
        }

        return $uri;
    }
}
{'fraction_non_alphanumeric': 0.1399912778020061, 'fraction_numerical': 0.0004361098996947231, 'mean_word_length': 1.6438040345821325, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, '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': '5423789', 'n_tokens_mistral': 1394, 'n_tokens_neox': 1371, 'n_words': 339}