<?php

class SongProvider {

    public function provide($key = 0) {
        $songs = $this->read();
        $key = $key != 0 ? $key : array_rand($songs);
        return $songs[$key];
    }

    public function getRandomKey() {
        $songs = $this->read();
        return array_rand($songs);
    }

    public function provideWidest() {
        $songs = $this->read();
        $key = 12;
        return $songs[$key];
    }

    function read() {
        $songs = array();
        if (($handle = fopen(__DIR__."/data.csv", "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                $song = new Song($data[1], $data[2], $data[3]);
                $songs[] = $song;
            }
            fclose($handle);
        }
        array_shift($songs); // remove first element (header)
        return $songs;
    }
}