Completed
Push — master ( 2bd965...c99558 )
by Peter
07:34
created

FilenameCleaner::clean()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 24
rs 8.9713
cc 3
eloc 14
nc 4
nop 1
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
8
 */
9
namespace AnimeDb\Bundle\CatalogBundle\Service\Storage;
10
11
use Symfony\Component\Finder\SplFileInfo;
12
13
class FilenameCleaner
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $meta = [
19
        // ripping
20
        'CamRip',
21
        'Cam',
22
        'Telesync',
23
        'TS',
24
        'Telecine',
25
        'TC',
26
        'Super Telesync',
27
        'SuperTS',
28
        'Super-TS',
29
        'VHS-Rip',
30
        'VHSRip',
31
        'Screener',
32
        'Scr',
33
        'VHS-Screener',
34
        'VHSScr',
35
        'PPVRip',
36
        'DVD-Screener',
37
        'DVDScr',
38
        'TV-Rip',
39
        'TVRip',
40
        'Sat-Rip',
41
        'SatRip',
42
        'HDTV-Rip',
43
        'HDTVRip',
44
        'PDTVRip',
45
        'HDRip',
46
        'BDRip',
47
        'DVD-Rip',
48
        'DVDRip',
49
        'LaserDisc-RIP',
50
        'LDRip',
51
        'Workprint',
52
        'WP',
53
        'WebRip',
54
        'Web-DL',
55
        'Web-DLRip',
56
        'Remux',
57
        'DCPrip',
58
        // DVD
59
        'DVD',
60
        'DVD5',
61
        'DVD9',
62
        'DVD10',
63
        'DVD18',
64
        // subtitles
65
        'Sub',
66
        'Subtitles',
67
        // display resolutions
68
        '8K',
69
        '5K',
70
        '4K',
71
        '1080p',
72
        '720p',
73
        '480p',
74
    ];
75
76
    public function __construct()
77
    {
78
        $this->meta = array_map('preg_quote', $this->meta);
79
    }
80
81
    /**
82
     * @param SplFileInfo $file
83
     *
84
     * @return string
85
     */
86
    public function clean(SplFileInfo $file)
87
    {
88
        $name = $file->getFilename();
89
        if ($file->isFile()) {
90
            $name = pathinfo($name, PATHINFO_FILENAME);
91
        }
92
93
        $name = str_replace("\t\n\r\0\x0B", ' ', $name); // remove control characters
94
        $name = str_replace('_', ' ', $name);
95
96
        $name = preg_replace('/\[[^\[\]]*\]/', ' ', $name); // remove [...]
97
        $name = preg_replace('/\([^\(\)]*\)/', ' ', $name); // remove (...)
98
99
        // remove all file meta data
100
        $reg = sprintf('/[ .,](?:%s)[ .,]/i', implode('|', $this->meta));
101
        while (preg_match($reg, $name.' ')) {
102
            $name = preg_replace($reg, ' ', $name.' ');
103
        }
104
105
        $name = str_replace('  ', ' ', $name); // remove double spaces
106
        $name = trim($name, ' .,-');
107
108
        return $name;
109
    }
110
}
111