File: /home/marketing.cfbon.ru/public_html/app/Models/Shop.php
<?php
namespace App\Models;
use App\Services\MetroService;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\App;
class Shop extends Model
{
protected $table = 'shops';
protected $fillable = [
'address',
'metro_data',
'latitude',
'longitude',
'phone',
'working_days',
'weekend_days',
'worked_start',
'worked_end',
'weekend_start',
'weekend_end',
'is_open',
'number',
];
protected $casts = [
'metro_data' => 'object',
'working_days' => 'object',
'weekend_days' => 'object',
'is_open' => 'boolean',
];
public function setMetroDataAttribute($value) : void
{
$metroService = App::make(MetroService::class);
$stationColorLine = $metroService->getColor($value);
$this->attributes['metro_data'] = json_encode(['name' => $value, 'color' => $stationColorLine]);
}
public function getScheduleAttribute() : object
{
$working = $this->makeSchedule($this->working_days, $this->worked_start, $this->worked_end);
if (!empty($this->weekend_days)) {
$weekend = $this->makeSchedule($this->weekend_days, $this->weekend_start, $this->weekend_end);
}
return (object)[
'working' => $working,
'weekend' => $weekend ?? null,
];
}
private function makeSchedule(array $days, string $startTime, string $endTime)
{
$countDays = count($days);
$scheduleDays = $countDays > 1 ? "$days[0] - {$days[$countDays - 1]}" : $days[0];
$scheduleStartTime = Carbon::parse($startTime)->format('H:i');
$scheduleEndTime = Carbon::parse($endTime)->format('H:i');
return (object)[
'days' => $scheduleDays,
'time' => "$scheduleStartTime - $scheduleEndTime"
];
}
}