<commit_before><?php
/**
 * Category
 */
namespace Delatbabel\NestedCategories\Models;

use Baum\Node;
use Cviebrock\EloquentSluggable\SluggableInterface;
use Cviebrock\EloquentSluggable\SluggableTrait;
use Delatbabel\Fluents\Fluents;

/**
 * Category
 *
 * A Laravel 5 package for adding one or more types of category hierarchy to a website.
 *
 * e.g. a hierarchy for blog categories and another for product categories
 *
 * This is a Laravel 5 reimplementation of
 * [Laravel 4 Categories](https://github.com/FbF/Laravel-Categories)
 *
 * @link https://github.com/delatbabel/nestedcategories
 */
class Category extends Node implements SluggableInterface
{
    use SluggableTrait, Fluents;

    /**
     * Used for Cviebrock/EloquentSluggable
     * @var array
     */
    protected $sluggable = [
        'build_from'         => 'name',
        'save_to'            => 'slug',
        'max_length'         => 255,
        'unique'             => true,
        'include_trashed'    => true,
    ];

    /**
     * Stores the old parent id before editing
     * @var integer
     */
    protected $oldParentId = null;

    protected $casts = [
        'extended_data'     => 'array',
    ];

    /**
     * The "booting" method of the model.
     *
     * @return void
     */
    protected static function boot()
    {
        parent::boot();

        static::updating(function ($category) {

            // Baum triggers a parent move, which puts the item last in the list,
            // even if the old and new parents are the same
            if ($category->isParentIdSame()) {
                $category->stopBaumParentMove();
            }
        });
    }

    /**
     * Check for dirty parent ID.
     *
     * Returns true if the parent_id value in the database is different to the current
     * value in the model's dirty attributes
     *
     * @return bool
     */
    protected function isParentIdSame()
    {
        $dirty             = $this->getDirty();
        $oldNavItem        = self::where('id', '=', $this->id)->first();
        $oldParent         = $oldNavItem->parent;
        $oldParentId       = $oldParent->id;
        $isParentColumnSet = isset($dirty[$this->getParentColumnName()]);
        if ($isParentColumnSet) {
            $isNewParentSameAsOld = $dirty[$this->getParentColumnName()] == $oldParentId;
        } else {
            $isNewParentSameAsOld = false;
        }
        return $isParentColumnSet && $isNewParentSameAsOld;
    }<commit_msg>Fix bug on empty parent, code tidy<commit_after><?php
/**
 * Category
 */
namespace Delatbabel\NestedCategories\Models;

use Baum\Node;
use Cviebrock\EloquentSluggable\SluggableInterface;
use Cviebrock\EloquentSluggable\SluggableTrait;
use Delatbabel\Fluents\Fluents;

/**
 * Category
 *
 * A Laravel 5 package for adding one or more types of category hierarchy to a website.
 *
 * e.g. a hierarchy for blog categories and another for product categories
 *
 * This is a Laravel 5 reimplementation of
 * [Laravel 4 Categories](https://github.com/FbF/Laravel-Categories)
 *
 * @link https://github.com/delatbabel/nestedcategories
 */
class Category extends Node implements SluggableInterface
{
    use SluggableTrait, Fluents;

    /**
     * Used for Cviebrock/EloquentSluggable
     * @var array
     */
    protected $sluggable = [
        'build_from'         => 'name',
        'save_to'            => 'slug',
        'max_length'         => 255,
        'unique'             => true,
        'include_trashed'    => true,
    ];

    /**
     * Stores the old parent id before editing
     * @var integer
     */
    protected $oldParentId = null;

    protected $casts = [
        'extended_data'     => 'array',
    ];

    /**
     * The "booting" method of the model.
     *
     * @return void
     */
    protected static function boot()
    {
        parent::boot();

        static::updating(function ($category) {

            // Baum triggers a parent move, which puts the item last in the list,
            // even if the old and new parents are the same
            if ($category->isParentIdSame()) {
                $category->stopBaumParentMove();
            }
        });
    }

    /**
     * Check for dirty parent ID.
     *
     * Returns true if the parent_id value in the database is different to the current
     * value in the model's dirty attributes
     *
     * @return bool
     */
    protected function isParentIdSame()
    {
        $dirty             = $this->getDirty();
        $oldNavItem        = self::where('id', '=', $this->id)->first();
        $oldParent         = $oldNavItem->parent;
        $oldParentId       = empty($oldParent) ? null : $oldParent->id;
        $isParentColumnSet = isset($dirty[$this->getParentColumnName()]);
        if ($isParentColumnSet) {
            $isNewParentSameAsOld = ($dirty[$this->getParentColumnName()] == $oldParentId);
        } else {
            $isNewParentSameAsOld = false;
        }
        return $isParentColumnSet && $isNewParentSameAsOld;
    }
{'fraction_non_alphanumeric': 0.11416070007955449, 'fraction_numerical': 0.002386634844868735, 'mean_word_length': 2.1490294301815904, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, 'https://': 4, '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': '1323908', 'n_tokens_mistral': 1517, 'n_tokens_neox': 1454, 'n_words': 382}