Skip to content

Radio Class (Version 2)

Radio.php - OOP in PHP: Complete Code Listing

 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
<?php
    class Radio
    {
        const   MIN_VOLUME = 0;
        const   MAX_VOLUME = 10;

        const   MIN_AM_FREQ = 535;
        const   MAX_AM_FREQ = 1700;

        const   MIN_FM_FREQ = 87.5;
        const   MAX_FM_FREQ = 108;

        private $powered;   // true or false
        private $volume;    // 0 - 10
        private $channel;   // 535kHz - 1700kHz, 87.5MHz - 108MHz

        // Getters
        public function getPowered()
        {
            return $this->powered;
        }

        public function getVolume()
        {
            return $this->volume;
        }

        public function getChannel()
        {
            return $this->channel;
        }

        // Setters
        public function setPowered($powered)
        {
            $this->powered = $powered;
        }

        public function setVolume($volume)
        {
            if ($volume < self::MIN_VOLUME)
            {
                $volume = self::MIN_VOLUME;
            }
            else if ($volume > self::MAX_VOLUME)
            {
                $volume = self::MAX_VOLUME;
            }

            $this->volume = $volume;
        }

        public function setChannel($channel)
        {
            if ($channel < self::MIN_FM_FREQ)
            {
                $channel = self::MIN_FM_FREQ;
            }
            else if ($channel > self::MAX_FM_FREQ && $channel < self::MIN_AM_FREQ
                    && $channel < (self::MIN_AM_FREQ - self::MAX_FM_FREQ))
            {
                $channel = self::MAX_FM_FREQ;
            }
            else if ($channel > self::MAX_FM_FREQ && $channel < self::MIN_AM_FREQ
                    && $channel >= (self::MIN_AM_FREQ - self::MAX_FM_FREQ))
            {
                $channel = self::MIN_AM_FREQ;
            }
            else if ($channel > self::MAX_AM_FREQ)
            {
                $channel = self::MAX_AM_FREQ;
            }

            $this->channel = $channel;
        }

        // General Methods
        public function scanChannels() 
        {
            // find the next channel with a strong signal
        }

        public function getCurrentSong() 
        {
            // reads live metadata from current channel about current song
            // being played and returns song title, etc...
        }
    }