| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Models; |
| 6 |
|
| 7 |
/** |
| 8 |
* Availability Model |
| 9 |
* Represents a trip availability date with pricing and capacity |
| 10 |
*/ |
| 11 |
class Availability |
| 12 |
{ |
| 13 |
public int $id = 0; |
| 14 |
public int $trip_id = 0; |
| 15 |
public string $departure_date = ''; |
| 16 |
public ?string $arrival_date = null; |
| 17 |
public ?string $return_date = null; |
| 18 |
public ?string $departure_time = null; |
| 19 |
public ?string $arrival_time = null; |
| 20 |
public int $seats_total = 0; |
| 21 |
public int $seats_available = 0; |
| 22 |
public int $seats_reserved = 0; |
| 23 |
public int $seats_waitlist = 0; |
| 24 |
public string $pricing_type = 'regular'; |
| 25 |
public ?float $original_price = null; |
| 26 |
public ?float $discounted_price = null; |
| 27 |
public ?float $discount_percentage = null; |
| 28 |
public array $price_types = []; |
| 29 |
public string $status = 'available'; |
| 30 |
public ?string $from_location = null; |
| 31 |
public ?string $to_location = null; |
| 32 |
public ?string $from_latitude = null; |
| 33 |
public ?string $from_longitude = null; |
| 34 |
public ?string $to_latitude = null; |
| 35 |
public ?string $to_longitude = null; |
| 36 |
public ?string $special_notes = null; |
| 37 |
public ?string $cutoff_date = null; |
| 38 |
public int $cutoff_hours = 24; |
| 39 |
public bool $is_blocked = false; |
| 40 |
public ?string $block_reason = null; |
| 41 |
public ?int $alert_threshold = null; |
| 42 |
public ?string $last_synced_at = null; |
| 43 |
public string $created_at = ''; |
| 44 |
public string $updated_at = ''; |
| 45 |
|
| 46 |
/** |
| 47 |
* Create from array |
| 48 |
*/ |
| 49 |
public static function fromArray(array $data): self |
| 50 |
{ |
| 51 |
$availability = new self(); |
| 52 |
|
| 53 |
foreach ($data as $key => $value) { |
| 54 |
if (property_exists($availability, $key)) { |
| 55 |
// Handle boolean conversion |
| 56 |
if ($key === 'is_blocked' && is_string($value)) { |
| 57 |
$availability->$key = in_array(strtolower($value), ['1', 'true', 'yes'], true); |
| 58 |
} elseif ($key === 'id') { |
| 59 |
$availability->$key = (int) $value; |
| 60 |
} elseif ($key === 'trip_id') { |
| 61 |
$availability->$key = (int) $value; |
| 62 |
} elseif (in_array($key, ['seats_total', 'seats_available', 'seats_reserved', 'seats_waitlist', 'cutoff_hours', 'alert_threshold'], true)) { |
| 63 |
$availability->$key = (int) $value; |
| 64 |
} elseif (in_array($key, ['original_price', 'discounted_price', 'discount_percentage'], true)) { |
| 65 |
$availability->$key = $value !== null ? (float) $value : null; |
| 66 |
} elseif ($key === 'price_types') { |
| 67 |
// Handle price_types as JSON |
| 68 |
if (is_string($value)) { |
| 69 |
$availability->$key = json_decode($value, true) ?: []; |
| 70 |
} elseif (is_array($value)) { |
| 71 |
$availability->$key = $value; |
| 72 |
} else { |
| 73 |
$availability->$key = []; |
| 74 |
} |
| 75 |
} else { |
| 76 |
$availability->$key = $value; |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
return $availability; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Convert to array |
| 86 |
*/ |
| 87 |
public function toArray(): array |
| 88 |
{ |
| 89 |
return [ |
| 90 |
'id' => $this->id, |
| 91 |
'trip_id' => $this->trip_id, |
| 92 |
'departure_date' => $this->departure_date, |
| 93 |
'arrival_date' => $this->arrival_date, |
| 94 |
'return_date' => $this->return_date, |
| 95 |
'departure_time' => $this->departure_time, |
| 96 |
'arrival_time' => $this->arrival_time, |
| 97 |
'seats_total' => $this->seats_total, |
| 98 |
'seats_available' => $this->seats_available, |
| 99 |
'seats_reserved' => $this->seats_reserved, |
| 100 |
'seats_waitlist' => $this->seats_waitlist, |
| 101 |
'booked_seats' => $this->seats_total - $this->seats_available - $this->seats_reserved, |
| 102 |
'total_seats' => $this->seats_total, |
| 103 |
'available_seats' => $this->seats_available, |
| 104 |
'waitlist_count' => $this->seats_waitlist, |
| 105 |
'pricing_type' => $this->pricing_type, |
| 106 |
'original_price' => $this->original_price, |
| 107 |
'discounted_price' => $this->discounted_price, |
| 108 |
'discount_percentage' => $this->discount_percentage, |
| 109 |
'price_types' => $this->price_types, |
| 110 |
'status' => $this->status, |
| 111 |
'from_location' => $this->from_location, |
| 112 |
'to_location' => $this->to_location, |
| 113 |
'from_latitude' => $this->from_latitude, |
| 114 |
'from_longitude' => $this->from_longitude, |
| 115 |
'to_latitude' => $this->to_latitude, |
| 116 |
'to_longitude' => $this->to_longitude, |
| 117 |
'special_notes' => $this->special_notes, |
| 118 |
'cutoff_date' => $this->cutoff_date, |
| 119 |
'cutoff_hours' => $this->cutoff_hours, |
| 120 |
'is_blocked' => $this->is_blocked, |
| 121 |
'block_reason' => $this->block_reason, |
| 122 |
'alert_threshold' => $this->alert_threshold, |
| 123 |
'last_synced_at' => $this->last_synced_at, |
| 124 |
'created_at' => $this->created_at, |
| 125 |
'updated_at' => $this->updated_at, |
| 126 |
]; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|