3v4l.org

run code in 300+ PHP versions simultaneously
<?php error_reporting(-1); class Room { private $number; private $countOfGuests; private $price; private $arrivalDate; private $dateOfDeparture; private $guests = array("firstName" => array(), "lastName" => array()); private $isFree = TRUE; public function __construct(int $number, int $countOfGuests, int $price, DateTime $arrivalDate, DateTime $dateOfDeparture) { $this->number = $number; $this->price = $price; $this->countOfGuests = $countOfGuests; $this->arrivalDate = $arrivalDate; $this->dateOfDeparture = $dateOfDeparture; } public function getIsFree(): bool { return $this->isFree; } public function changeIsFtee():void { if ($this->isFree == TRUE){ $this->isFree = FALSE; } else { $this->isFree = TRUE; } } public function getArrivalDate(): DateTime { return $this->arrivalDate; } public function getDateOfDeparture(): DateTime { return $this->dateOfDeparture; } public function addGuestToRoom($firstName, $lastName): void { $this->guests["firstName"][] = $firstName; $this->guests["lastName"][] = $lastName; } public function getPrice(): int { return $this->price; } public function getCountOfGuests(): int { return $this->countOfGuests; } public function getGuests(): array { return $this->guests; } public function getNumber(): int { return $this->number; } public function setDate(Hotel $hotel, DateTime $arrivalDate, DateTime $dateOfDeparture): void { $this->arrivalDate = $arrivalDate; $this->dateOfDeparture = $dateOfDeparture; } } class Hotel { private $rooms = array(); private $guests = array(); private $receipts = array("date" => array(), "money" => array()); public function addRoomForHotel(int $countOfGuests, int $price): void { $this->rooms[] = new Room(count($this->rooms) + 1, $countOfGuests, $price, new DateTime(), new DateTime()); } public function addGuest(string $firstName, string $lastName): void { $this->guests[] = new Guest($firstName, $lastName); } public function getRooms(): array { return $this->rooms; } public function getRoom(int $numberOfRoom): Room { return $this->rooms[$numberOfRoom - 1]; } public function getGuest(string $firstName, string $lastName): Guest { foreach ($this->guests as $guest) { if ($guest->getFirstName() == $firstName && $guest->getLastName() == $lastName){ return $guest; break; } } if (1){ throw new Exception("Неправильно введены данные клиента"); } } public function getReceipts(): array { return $this->receipts; } public function addDateOfReceipts(DateTime $date): void { $this->receipts["date"][] = $date; } public function addMoneyOfReceipts(int $money): void { $this->receipts["money"][] = $money; } public function replenishMoney(int $money, int $numberInArray): void { $this->receipts["money"][$numberInArray] += $money; } } class Guest { private $firstName; private $lastName; private $visitedRooms = array(); public function __construct(string $firstName, string $lastName) { $this->firstName = $firstName; $this->lastName = $lastName; } public function addVisitedRoom(int $numberOfRoom): void { $this->visitedRooms[] = $numberOfRoom; } public function getFirstName(): string { return $this->firstName; } public function getLastName(): string { return $this->lastName; } public function getVisitedRooms(): array { return $this->visitedRooms; } } class Registration { public function getStatisticOfReceipts(Hotel $hotel, DateTime $startDate, DateTime $endDate): void { echo "Дата Сумма\n"; foreach ($hotel->getReceipts()["date"] as $key => $date) { if($date == $startDate || $date > $startDate){ for($i = $key; $hotel->getReceipts()["date"][$i] != $endDate; $i++){ echo $hotel->getReceipts()["date"][$i]->format('Y-m-d'), " ", $hotel->getReceipts()["money"][$i], "\n"; if($hotel->getReceipts()["date"][$i] == end($hotel->getReceipts()["date"])){ break; } } } break; } } public function registerGuests(Hotel $hotel, int $numberOfRoom, string $firstName, string $lastName, DateTime $arrivalDate, DateTime $dateOfDeparture): void { $hotel->getRoom($numberOfRoom)->setDate($hotel, $arrivalDate, $dateOfDeparture); $hotel->getRoom($numberOfRoom)->addGuestToRoom($firstName, $lastName); $hotel->getRoom($numberOfRoom)->changeIsFtee(); $hotel->getGuest($firstName, $lastName)->addVisitedRoom($numberOfRoom); $this->addReceiptsForHotel($hotel, $numberOfRoom, $arrivalDate); } public function selectRoom(Hotel $hotel, DateTime $arrivalDate, DateTime $dateOfDeparture, int $countOfGuests): void { $availableRoom = $this->searchRooms($hotel, $dateOfDeparture, $arrivalDate); $roomsForList = array(); while ($countOfGuests > 0) { $maxCountOfGuestInRoom = $availableRoom[0]->getCountOfGuests(); foreach ($availableRoom as $room) { if ($room->getCountOfGuests() > $maxCountOfGuestInRoom && $room->getCountOfGuests() <= $countOfGuests){ $maxCountOfGuestInRoom = $room->getCountOfGuests(); } } $roomsForList[] = $this->SearchSuitableRoom($hotel, $arrivalDate, $dateOfDeparture, $maxCountOfGuestInRoom); end($roomsForList)->changeIsFtee(); $countOfGuests -= $maxCountOfGuestInRoom; } $this->printInformationOfRooms($roomsForList); foreach ($roomsForList as $room) { $room->changeIsFtee(); } } public function getFreeRoomsInRange(Hotel $hotel, DateTime $arrivalDate, DateTime $dateOfDeparture):void { $this->printInformationOfRooms($this->searchRooms($hotel, $dateOfDeparture, $arrivalDate)); } public function getFreeRooms(Hotel $hotel, DateTime $date): void { $this->printInformationOfRooms($this->searchRooms($hotel, $date, $date)); } private function searchRooms(Hotel $hotel, DateTime $date1, DateTime $date2) { $freeRooms = array(); foreach ($hotel->getRooms() as $room) { if ($room->getIsFree() && ($room->getArrivalDate() > $date1 || $room->getDateOfDeparture() < $date2)){ $freeRooms[] = $room; } } return $freeRooms; } private function SearchSuitableRoom(Hotel $hotel, DateTime $arrivalDate, DateTime $dateOfDeparture, int $countOfGuests): Room { $availableRoom = $this->searchRooms($hotel, $dateOfDeparture, $arrivalDate); foreach ($availableRoom as $number => $room) { if ($room->getCountOfGuests() < $countOfGuests){ unset($availableRoom[$number]); } } sort($availableRoom); if (count($availableRoom) == 0){ throw new Exception("Нет подходящих номеров"); } $minPrice = $availableRoom[0]->getPrice(); foreach ($availableRoom as $number => $room) { if ($room->getPrice() < $minPrice){ $minPrice = $room->getPrice(); for ($i = 0; $i < $number; $i++){ unset($availableRoom[$i]); } } elseif ($room->getPrice() > $minPrice){ unset($availableRoom[$number]); } } sort($availableRoom); $minGuests = $availableRoom[0]->getCountOfGuests(); foreach ($availableRoom as $number => $room) { if ($room->getCountOfGuests() < $minGuests){ $minGuests = $room->getCountOfGuests(); for ($i = 0; $i < $number; $i++){ unset($availableRoom[$i]); } } elseif ($room->getCountOfGuests() > $minGuests){ unset($availableRoom[$number]); } } sort($availableRoom); return $availableRoom[0]; } public function printHistoryOfGuest(Hotel $hotel, string $firstName, string $lastName): void { echo "\n$firstName $lastName История\n"; $rooms = $hotel->getGuest($firstName, $lastName)->getVisitedRooms(); foreach ($rooms as &$room) { $room = $hotel->getRoom($room); } $this->printInformationOfRooms($rooms); } public function printHistoryOfRoom(Hotel $hotel, int $numberOfRoom): void { echo "\nИстория комнаты № $numberOfRoom\n\n"; echo "Имя Фамилия\n"; $guests = $hotel->getRoom($numberOfRoom)->getGuests(); for($i = 0; $i < count($guests["firstName"]); $i++){ echo $this->padLeft($guests["firstName"][$i], 10) . $this->padLeft($guests["lastName"][$i], 10) . "\n"; } } private function printInformationOfRooms(array $rooms): void { echo "\nНомер вместимость стоимость\n"; foreach($rooms as $room){ $this->printRoomsRow($room->getNumber(), $room->getCountOfGuests(), $room->getPrice()); } } private function padLeft($value, int $columnLength): void { echo $value; echo str_repeat(" ", $columnLength - mb_strlen($value)); } private function printRoomsRow(int $numberOfRoom, int $countOfGuests, int $price): void { $col1 = 6; $col2 = 12; $col3 = 20; echo $this->padLeft($numberOfRoom, $col1) . $this->padLeft($countOfGuests, $col2) . $this->padLeft($price, $col3) . "\n"; } private function addReceiptsForHotel(Hotel $hotel, int $numberOfRoom, DateTime $arrivalDate): void { foreach ($hotel->getReceipts()["date"] as $key => $date) { if ($arrivalDate == $date){ $hotel->replenishMoney($hotel->getRoom($numberOfRoom)->getPrice(), $key); return; } } $hotel->addDateOfReceipts($arrivalDate); $hotel->addMoneyOfReceipts($hotel->getRoom($numberOfRoom)->getPrice()); } } $hotel = new Hotel; $hotel->addRoomForHotel(1, 500); $hotel->addRoomForHotel(3, 700); $hotel->addRoomForHotel(2, 1500); $hotel->addRoomForHotel(2, 1500); $hotel->addRoomForHotel(3, 1500); $hotel->addGuest("Ivan", "Ivanov"); $hotel->addGuest("Petr", "Petrov"); $registration = new Registration; $registration->registerGuests($hotel, 3, "Ivan", "Ivanov", new DateTime('2015-05-10'), new DateTime('2015-05-22')); $registration->registerGuests($hotel, 1, "Petr", "Petrov", new DateTime('2015-05-10'), new DateTime('2015-05-12')); $registration->registerGuests($hotel, 4, "Ivan", "Ivanov", new DateTime('2015-05-27'), new DateTime('2015-06-03')); $registration->registerGuests($hotel, 3, "Petr", "Petrov", new DateTime('2015-07-13'), new DateTime('2015-07-15')); // $registration->getFreeRooms($hotel, new DateTime('2015-05-12')); // $registration->getFreeRoomsInRange($hotel, new DateTime('2015-05-13'), new DateTime('2015-05-25')); // $registration->selectRoom($hotel, new DateTime('2015-05-12'), new DateTime('2015-05-25'), 4); // $registration->printHistoryOfGuest($hotel, "Ivan", "Ivanov"); // $registration->printHistoryOfRoom($hotel, 3); // $registration->getStatisticOfReceipts($hotel, new DateTime('2015-05-05'), new DateTime('2015-07-25'));
Output for 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0

preferences:
161.16 ms | 402 KiB | 170 Q