-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.php
59 lines (48 loc) · 1.3 KB
/
solution.php
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
<?php
class Problem
{
private array $patterns;
private string $encodedImage;
public function __construct()
{
$this->patterns = ['(', ')', '~', '#', '%', '*', '!', '^', '@'];
$this->encodedImage = $this->loadData();
}
public function solve()
{
return $this->writeToFile()
? "Success!"
: "Error!";
}
private function writeToFile()
{
/**
* $data[0] is base64 encoded file format
* $data[1] is actual base64 encoded file
*/
$data = explode(',', $this->decodeImage());
return file_put_contents(
$this->generateFileName('result', $data[0]),
base64_decode($data[1])
);
}
private function generateFileName(string $name, string $format)
{
return $name . '.' . $this->getExtension($format);
}
private function getExtension($format)
{
$start = strpos($format, '/') + 1;
$length = strpos($format, ';') - $start;
return substr($format, $start, $length);
}
private function decodeImage()
{
return str_replace($this->patterns, '', $this->encodedImage);
}
private function loadData()
{
return file_get_contents('data.txt');
}
}
echo (new Problem())->solve();