Completed
Push — develop ( 9f140d...7abee3 )
by Arkadiusz
02:42
created

FilesDataset::scanDir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
declare (strict_types = 1);
4
5
namespace Phpml\Dataset;
6
7
use Phpml\Exception\DatasetException;
8
9
class FilesDataset extends ArrayDataset
10
{
11
    /**
12
     * @param string $rootPath
13
     *
14
     * @throws DatasetException
15
     */
16
    public function __construct(string $rootPath)
17
    {
18
        if (!is_dir($rootPath)) {
19
            throw DatasetException::missingFolder($rootPath);
20
        }
21
22
        $this->scanRootPath($rootPath);
23
    }
24
25
    /**
26
     * @param string $rootPath
27
     */
28
    private function scanRootPath(string $rootPath)
29
    {
30
        foreach (glob($rootPath.DIRECTORY_SEPARATOR.'*', GLOB_ONLYDIR) as $dir) {
31
            $this->scanDir($dir);
32
        }
33
    }
34
35
    /**
36
     * @param string $dir
37
     */
38
    private function scanDir(string $dir)
39
    {
40
        $target = basename($dir);
41
42
        foreach (array_filter(glob($dir.DIRECTORY_SEPARATOR.'*'), 'is_file') as $file) {
43
            $this->samples[] = [file_get_contents($file)];
44
            $this->targets[] = $target;
45
        }
46
    }
47
}
48