Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
FileMode
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 isDefined
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace pvc\storage\filesys;
6
7use ReflectionClass;
8
9class FileMode
10{
11    /**
12     * Open for reading only; place the file pointer at the beginning of the file.
13     */
14    const READ = 'r';
15
16    /**
17     * Open for reading and writing; place the file pointer at the beginning of the file.
18     */
19    const READ_WRITE = 'r+';
20
21    /**
22     * Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length.
23     * If the file does not exist, attempt to create it.
24     */
25    const WRITE = 'w';
26
27    /**
28     * Open for reading and writing; otherwise it has the same behavior as 'w'.
29     */
30    const WRITE_WRITE = 'w+';
31
32    /**
33     * Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to
34     * create it. In this mode, fseek() has no effect, writes are always appended.
35     */
36    const APPEND = 'a';
37
38    /**
39     * Open for reading and writing; place the file pointer at the end of the file. If the file does not exist,
40     * attempt to create it. In this mode, fseek() only affects the reading position, writes are always appended.
41     */
42    const APPEND_READ = 'a+';
43
44    /**
45     * Create and open for writing only; place the file pointer at the beginning of the file. If the file already
46     * exists, the fopen() call will fail by returning false and generating an error of level E_WARNING. If the file
47     * does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying
48     * open(2) system call.
49     */
50    const CREATE_WRITE_NO_OVERWRITE = 'x';
51
52    /**
53     * Create and open for reading and writing; otherwise it has the same behavior as 'x'.
54     */
55    const CREATE_READ_WRITE_NO_OVERWRITE = 'x+';
56
57    /**
58     * Open the file for writing only. If the file does not exist, it is created. If it exists, it is neither
59     * truncated (as opposed to 'w'), nor the call to this function fails (as is the const with 'x'). The file
60     * pointer is positioned on the beginning of the file. This may be useful if it's desired to get an advisory
61     * lock (see flock()) before attempting to modify the file, as using 'w' could truncate the file before the
62     * lock was obtained (if truncation is desired, ftruncate() can be used after the lock is requested).
63     */
64    const CREATE_WRITE_NO_TRUNCATE = 'c';
65
66    /**
67     * Open the file for reading and writing; otherwise it has the same behavior as 'c'.
68     */
69    const CREATE_READ_WRITE_NO_TRUNCATE = 'c+';
70
71    /**
72     * Set close-on-exec flag on the opened file descriptor. Only available in PHP compiled on POSIX.1-2008
73     * conforming systems.
74     */
75    const CLOSE_ON_EXECUTE = 'e';
76
77    public static function isDefined(string $mode): bool
78    {
79        $reflection = new ReflectionClass(__CLASS__);
80        $constants = $reflection->getConstants();
81        return in_array($mode, $constants);
82    }
83}