%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/camillo/camillo-api-master/app/Http/Resources/
Upload File :
Create Path :
Current File : //var/www/html/camillo/camillo-api-master/app/Http/Resources/CommunicationSentResource.php

<?php

namespace App\Http\Resources;

use Carbon\Carbon;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Facades\Log;

class CommunicationSentResource extends ResourceCollection
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $list = parent::toArray($request);
        if ($first = reset($list)) {
            $first = (object) $first;
            $data = [
                'is_read' => (bool) array_reduce($list, function($result, $elem) {
                    return $result*$elem['is_read'];
                }, true),
                'uuid'             => $first->uuid,
                'subject'        => $first->subject,
                'content'        => $first->content,
                'sender'         => [
                    'name'    => $first->sender['individual']['name'],
                    'surname' => $first->sender['individual']['surname']
                ],
                'minors'         => array_unique(array_map(
                    function ($el) use ($list) {
                        return [
                            'name'    => $el['minor']['name'],
                            'surname' => $el['minor']['surname'],
                            'is_read' => (bool) array_reduce($list, function($result, $elem) use ($el) {
                                return $el['minor']['id'] === $elem['minor']['id'] ? $result*$elem['is_read'] : $result;
                            }, true),
                            'receivers' => array_filter(array_map(function ($elem) use ($el) {
                                return $el['minor']['id'] === $elem['minor']['id'] ? [
                                    'name'    => $elem['receiver']['individual']['name'],
                                    'surname' => $elem['receiver']['individual']['surname'],
                                    'is_read' => (bool) $elem['is_read']
                                ] : false;
                            }, $list), function ($elem){return $elem;})
                        ];
                    }, $list
                ), SORT_REGULAR),
                'classroom_name' => $first->classroom['class_name'],
                'institute_name' => $first->classroom['institute']['name'],
                'notify'         => $first->notify,
                'date'           => Carbon::parse($first->created_at)->format('d-m-Y'),
                'time'           => Carbon::parse($first->created_at)->format('H:i')
            ];

            return $data;
        }
    }
}

Zerion Mini Shell 1.0