Jacek Kowalski
2022-06-11 0a4874ce13c82f1d12e05bd05bbe49d363d96410
commit | author | age
bcd661 1 <?php
JK 2 class Output {
413654 3     private $db;
JK 4     private $mapper;
5     private $vehicleTypes;
6     
7     function __construct(Database $db, Mapper $mapper, VehicleTypes $vehicleTypes) {
8         $this->db = $db;
9         $this->mapper = $mapper;
10         $this->vehicleTypes = $vehicleTypes;
11     }
12     
13     function createMapping($saveConfig = FALSE) {
bcd661 14         $mapping = [];
413654 15         foreach($this->db->getAllById() as $vehicle) {
JK 16             $mapping[$vehicle['id']] = $this->vehicleTypes->getByNumber($vehicle['num']);
bcd661 17         }
JK 18         
19         if($saveConfig) {
20             $json = json_encode($mapping);
21             if(!file_put_contents($saveConfig['result_temp'], $json)) {
22                 throw new Exception('Result save failed');
23             }
24             rename($saveConfig['result_temp'], $saveConfig['result']);
25         }
26         
27         return $mapping;
28     }
29     
413654 30     function createVehiclesList($fullMapping, $saveConfig = FALSE) {
JK 31         $trips = $this->mapper->getTTSSTrips();
32         
bcd661 33         $lines = [];
JK 34         $vehicles = [];
35         foreach($trips as $trip) {
413654 36             $vehicle = $fullMapping[$trip['id']] ?? [];
bcd661 37             $vehicle += ['trip' => $trip['id']];
JK 38             $lines[$trip['line']][] = [
39                 'trip' => $trip,
40                 'vehicle' => $vehicle,
41             ];
42             $vehicles[$vehicle['type'] ?? '?'][] = $vehicle;
43         }
413654 44         
bcd661 45         foreach($lines as &$line) {
JK 46             usort($line, function($a, $b) {
47                 return (substr($a['vehicle']['num'] ?? '', 2) <=> substr($b['vehicle']['num'] ?? '', 2)); 
48             });
49         }
50         unset($line);
51         ksort($lines);
413654 52         
bcd661 53         foreach($vehicles as &$vehicle) {
JK 54             usort($vehicle, function($a, $b) {
55                 return (substr($a['num'] ?? '', 2) <=> substr($b['num'] ?? '', 2));
56             });
57         }
58         unset($vehicle);
59         ksort($vehicles);
60         
413654 61         $dbMapping = $this->db->getAllByNum();
3d1e54 62         foreach($dbMapping as &$vehicle) {
JK 63             $vehicle['vehicle'] = $this->vehicleTypes->getByNumber($vehicle['num']);
64         }
65         unset($vehicle);
413654 66         ksort($dbMapping);
JK 67         
bcd661 68         if($saveConfig) {
JK 69             $twigLoader = new \Twig\Loader\FilesystemLoader(__DIR__.'/../templates');
70             $twig = new \Twig\Environment($twigLoader);
413654 71             $twig->addExtension(new Twig_Extensions_Extension_Date());
bcd661 72             
JK 73             $vehiclesHtml = $twig->render('vehicles.html', [
74                 'lines' => $lines,
75                 'vehicles' => $vehicles,
76                 'prefix' => $saveConfig['prefix'],
413654 77                 'mapping' => $dbMapping,
bcd661 78             ]);
JK 79             if(!file_put_contents($saveConfig['result_vehicles_temp'], $vehiclesHtml)) {
80                 throw new Exception('Vehicles save failed');
81             }
82             rename($saveConfig['result_vehicles_temp'], $saveConfig['result_vehicles']);
83         }
84         
85         return $lines;
86     }
87 }