summaryrefslogtreecommitdiff
path: root/src/Script/Input.php
blob: 772bb8c5adafca647f60a8f3740fe47a6c5aa815 (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php

namespace App\Script;

use Exception;

/**
 * Input handler for CLI arugments and options
 */
class Input
{
    /**
     * Available CLI options
     * f filename - input file with the vendors data
     * d day      - delivery day (dd/mm/yy)
     * t time     - delivery time in 24h format (hh:mm)
     * l location - delivery location (postcode without spaces, e.g. NW43QB)
     * c covers   - number of people to feed
     *
     * @var string
     */
    protected $shortOpts = 'f:d::t::l::c::';

    protected $longOpts = [
        'filename:',
        'day::',
        'time::',
        'location::',
        'covers::'
    ];

    /**
     * The loaded CLI options
     *
     * @var string
     */
    protected $options;

    /**
     * Parse arguments and options
     *
     * @author Phil Burton <phil@pgburton.com>
     */
    public function __construct()
    {
        $this->loadOptions();
    }

    /**
     * Load options, make sure required options are set
     *
     * @author Phil Burton <phil@pgburton.com>
     */
    protected function loadOptions()
    {
        $this->options = getopt($this->getShortOptionString(), $this->getLongOptionString());

        if (!array_key_exists('f', $this->options)) {
            throw new Exception('Filename Option `-f` is required');
        }

        // If only a day or a time is given, we throw exception as we need neither or both
        if ((array_key_exists('d', $this->options) <=> array_key_exists('t', $this->options)) !== 0) {
            throw new Exception('Both day and time options (`-d` and `-t`) are required for time based filtering');
        }

        if (array_key_exists('c', $this->options) && !is_numeric($this->options['c'])) {
            throw new Exception('Value for option `-c` must be an integer');
        }

        if (array_key_exists('l', $this->options) && is_numeric($this->options['l'])) {
            throw new Exception('Value for option `-l` must be a string');
        }
    }

    /**
     * Return the option string for the options we want to load
     *
     * @author Phil Burton <phil@pgburton.com>
     * @return string
     */
    public function getShortOptionString(): string
    {
        return $this->shortOpts;
    }

    /**
     * Return the option string for the options we want to load
     *
     * @author Phil Burton <phil@pgburton.com>
     * @return string
     */
    public function getLongOptionString(): array
    {
        return $this->longOpts;
    }

    /**
     * Return an option value
     * In it doesn't exist return false
     *
     * @author Phil Burton <phil@pgburton.com>
     * @param string $key
     * @return mixed
     */
    public function getOption(string $key)
    {
        if (!array_key_exists($key, $this->options)) {
            return false;
        }

        return $this->options[$key];
    }
}