<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class Roles extends JsonResource
{
    /**
     * Roles constructor.
     * @param $resource
     */
    public function __construct($resource)
    {
        $this->resource = $resource;
    }

    public function toArray($request)
    {
        $out = [
            'id'          => (int) $this->id,
            'name'        => $this->title,
            'language_id' => $this->language_id
        ];

        $out['children'] = [];

        // Get child info
        // if there is any
        if ($this->children) {
            foreach ($this->children as $child) {
                $out['children'][] = new Roles($child);
            }
        }

        return $out;
    }
}
