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