<commit_before><?php

namespace VergilLai\NodeCategories;

use DB;
use Illuminate\Database\Eloquent\Model;

/**
 * Class NodeCategoryTrait
 *
 * @package VergilLai\NodeCategories
 * @author Vergil <<EMAIL>>
 */
trait NodeCategoryTrait
{
    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function parent()
    {
        return $this->belongsTo(self::class, 'parent_id', 'id');
    }

    public function getParent()
    {
        return $this->findOrFail($this->parent_id);
    }

    /**
     * Get all children node
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function childrens()
    {
        return $this->where('node', 'like', $this->node.'%')->get();
    }

    /**
     * Get all parent node
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function parents()
    {
        return $this->where(DB::raw("LOCATE(`node`, '{$this->node}')"), '>', 0)
            ->where('id', '<>', $this->id)->get();
    }
}<commit_msg>add trait method<commit_after><?php

namespace VergilLai\NodeCategories;

use DB;
use Illuminate\Database\Eloquent\Model;

/**
 * Class NodeCategoryTrait
 *
 * @package VergilLai\NodeCategories
 * @author Vergil <<EMAIL>>
 */
trait NodeCategoryTrait
{
    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function parent()
    {
        return $this->belongsTo(self::class, 'parent_id', 'id');
    }

    public function getParent()
    {
        return $this->findOrFail($this->parent_id);
    }

    /**
     * Get all children node
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function childrens()
    {
        return $this->where('node', 'like', $this->node.'%')->get();
    }

    /**
     * Get all parent node
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function parents()
    {
        return $this->where(DB::raw("LOCATE(`node`, '{$this->node}')"), '>', 0)
            ->where('id', '<>', $this->id)->get();
    }

    /**
     * Get tree structures
     * @param callable $map
     * @return array
     */
    public static function getTree(\Closure $map = null)
    {
        return (new static)->tree($map);
    }

    /**
     * Get tree structures
     * @param callable $map
     * @return array
     */
    public function tree(\Closure $map = null)
    {
        /**
         * @var \Illuminate\Database\Eloquent\Collection $collection
         */
        $collection = $this->get();

        if (is_callable($map)) {
            $collection->map($map);
        }

        $categories = $collection->keyBy('id')->toArray();

        $nodesKey = $this->getNodesKey();

        foreach($categories as $cate) {
            $categories[$cate['parent_id']][$nodesKey][] = & $categories[$cate['id']];
        }

        return isset($categories[0][$nodesKey]) ? $categories[0][$nodesKey] : [];
    }

    /**
     * Get the tree structures nodes key name.
     * @return string
     */
    protected function getNodesKey()
    {
        return property_exists($this, 'nodesKey') ? $this->nodesKey : 'nodes';
    }
}
{'fraction_non_alphanumeric': 0.1676722769133058, 'fraction_numerical': 0.0012702445220704986, 'mean_word_length': 2.3870967741935485, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 40, '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': '6855533', 'n_tokens_mistral': 997, 'n_tokens_neox': 998, 'n_words': 185}