summaryrefslogtreecommitdiff
path: root/src/File/Handler.php
blob: e8e30b04912998b9a63506baea9545e723deb102 (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
<?php

namespace App\File;

use Exception;

/**
 * File handler
 *
 * @author Phil Burton <phil@pgburton.com>
 */
class Handler
{
    /**
     * Filename
     *
     * @var string
     */
    protected $filename;

    /**
     * Contents of file
     *
     * @var string
     */
    protected $file;

    /**
     * Check file can be read
     * Read and parse file into array
     *
     * @author Phil Burton <phil@pgburton.com>
     * @param string $filename string
     */
    public function __construct(string $filename)
    {
        $this->filename = $filename;
        if (!is_readable($filename)) {
            throw new Exception('Cannot read from file: ' . $filename);
        }

        $this->load();
    }

    /**
     * Load the file
     *
     * @author Phil Burton <phil@pgburton.com>
     */
    public function load()
    {
        $this->file = file_get_contents($this->filename);
    }

    /**
     * Retun the raw file contents
     *
     * @author Phil Burton <phil@pgburton.com>
     * @return string
     */
    public function getFileContents(): string
    {
        return $this->file;
    }
}