<?php
namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class DeductionsReport implements FromArray, WithHeadings, WithMapping
{
    protected $deductions;
    protected $headings;

    public function __construct(array $deductions)
    {
        $this->deductions = $deductions;
        $this->headings = $this->extractHeadings($deductions);
    }

    /**
    * @return array
    */
    public function array(): array
    {
        return $this->deductions;
    }

    public function headings(): array
    {
        return $this->headings;
    }

    public function map($row): array
    {
        return array_values($row);
    }

    protected function extractHeadings(array $deductions): array
    {
        $headings = [];

        foreach ($deductions as $item) {
            $headings = array_unique(array_merge($headings, array_keys($item)));
        }

        return $headings;
    }
}

