HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/ste.komma.pro/app/Trainings/Resources/Training.php
<?php

namespace App\Trainings\Resources;

use App\C4\C4;
use App\Locations\Models\Location;
use App\SteLanguages\Models\SteLanguage;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\App;

/**
 * Class Page
 *
 * @package App\Pages\Models
 * @property int site_id
 * @property int lft
 * @property int rgt
 * @property int tree
 * @property-read Carbon $date
 * @property int $id
 * @property int $active
 * @property \Carbon\Carbon|null $created_at
 * @property \Carbon\Carbon|null $updated_at
 */
final class Training
{
    protected $fillable = [
        'active',
        'ste_language_id',
        'level',
    ];

    public Location $location;
    public SteLanguage $language;
    public object $api_response;

    protected $apiFillable = [
        'c4_id' => 'id',
        'level' => 'niveau',
        'training_type' => 'soort',
        'price' => 'prijs_excl_btw',
        'duration' => 'duur',
        'amount_of_weeks' => 'aantal_weken',
        'lessons_each_week' => 'aantal_lessen_per_week',
        'start_date' => 'start_datum',
        'end_date' => 'eind_datum',
        'start_time' => 'eerste_dag_begin_tijd',
    ];

    public array $translatableKeys = [
        'training_type',
        'lessons_each_week'
    ];

    public static $filterLevels = [
        'A1',
        'A2',
        'B1',
        'B2',
        'C1',
        'C2',
    ];

    public function enrich(object $apiTraining)
    {
        $this->api_response = $apiTraining;
        $apiC4Keys = array_flip($this->apiFillable);

        foreach ($apiTraining as $apiKey => $value) {

            if(!in_array($apiKey, $this->apiFillable)) continue;
            $newKey = $apiC4Keys[$apiKey];

            switch ($newKey) {
                case 'start_date':
                case 'end_date':
                    $value = Carbon::createFromFormat('Y-m-d', $value);
                    break;

                case 'level':
                    if(in_array($value, array_keys(C4::LEVELS))) $this->filterable_level = C4::LEVELS[$value];
                    else $this->filterable_level = $value;
                    break;

                default:
                    break;
            }

            $this->{$newKey} = $value;
        }
    }

    public function setCurrentTranslation()
    {
        if(!isset($this->language->translations)) return null;

        if($translation = $this->language->translations->where('language_id', App::getLanguage()->id)->first()) {
            $this->language->setRelation('translation', $translation);
        }
    }

    /**
     * Fill the formatted start date on the resource
     */
    public function formatStartDate(): void
    {
        $this->formatted_start_date = __('calendar.day_names_min.' . $this->start_date->format('N') )  . ' ' . $this->start_date->format('d') . ' ' . __('calendar.month_names_short.' . ($this->start_date->format('n') - 1)) . ' ' . $this->start_date->format('Y');
        $this->in_consulation = $this->start_date->isSameDay(Carbon::create(2030,1,1));
    }

    /**
     * @return bool
     */
    public function inConsultation(): bool
    {
        return $this->start_date->isSameDay(Carbon::create(2030,1,1));
    }

}