1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace Performance; |
3
|
|
|
|
4
|
|
|
Class File { |
5
|
|
|
protected $path; |
6
|
|
|
public function __construct($path) { |
7
|
|
|
$this->path = $path; |
8
|
|
|
} |
9
|
|
|
public function read() { |
10
|
|
|
return file_get_contents($this->path); |
11
|
|
|
} |
12
|
|
|
public function write($content) { |
13
|
|
|
file_put_contents($this->path, $content); |
14
|
|
|
} |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
class Formatter { |
|
|
|
|
18
|
|
|
public function format($words) { |
19
|
|
|
$text = ''; |
20
|
|
|
foreach ($words as $occ => $list) { |
21
|
|
|
$text .= "{$occ}: {$this->formatList($list)}\n"; |
22
|
|
|
} |
23
|
|
|
return $text; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
protected function formatList($list) { |
27
|
|
|
$values = array_map(function(Word $w) { |
28
|
|
|
return $w->value(); |
29
|
|
|
}, $list); |
30
|
|
|
return implode(', ', $values); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
class Word { |
|
|
|
|
35
|
|
|
protected $value; |
36
|
|
|
protected $occurrences; |
37
|
|
|
|
38
|
|
|
public function __construct($value, $occurrences) { |
39
|
|
|
$this->value = $value; |
40
|
|
|
$this->occurrences = $occurrences; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function value() { |
44
|
|
|
return $this->value; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function occurrences() { |
48
|
|
|
return $this->occurrences; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
class Parser { |
|
|
|
|
53
|
|
|
protected $file; |
54
|
|
|
protected $tokens; |
55
|
|
|
|
56
|
|
|
public function __construct(File $file) { |
57
|
|
|
$this->file = $file; |
58
|
|
|
$this->tokens = []; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function words() { |
62
|
|
|
$this->fillTokens(); |
63
|
|
|
$words = []; |
64
|
|
|
foreach (array_count_values($this->tokens) as $value => $occ) { |
65
|
|
|
$words[] = new Word($value, $occ); |
66
|
|
|
} |
67
|
|
|
return $words; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function fillTokens() { |
71
|
|
|
$text = $this->file->read(); |
72
|
|
|
$text = trim(preg_replace('/[^a-zA-Z0-9]+/', ' ', $text)); |
73
|
|
|
$this->tokens = explode(' ', $text); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
class Collection { |
|
|
|
|
79
|
|
|
protected $list; |
80
|
|
|
|
81
|
|
|
public function __construct($list) { |
82
|
|
|
$this->list = $list; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function groupByOccurrences() { |
86
|
|
|
$occurrencesValues = $this->getOccurrencesValues(); |
87
|
|
|
arsort($occurrencesValues); |
88
|
|
|
$result = []; |
89
|
|
|
foreach ($occurrencesValues as $occ) { |
90
|
|
|
$result[$occ] = $this->getWordsHavingOccurrences($occ); |
91
|
|
|
} |
92
|
|
|
return $result; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function getOccurrencesValues() { |
96
|
|
|
$result = []; |
97
|
|
|
foreach ($this->list as $word) { |
98
|
|
|
$result[$word->occurrences()] = true; |
99
|
|
|
} |
100
|
|
|
return array_keys($result); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function getWordsHavingOccurrences($number) { |
104
|
|
|
$result = []; |
105
|
|
|
foreach ($this->list as $word) { |
106
|
|
|
if ($word->occurrences() == $number) |
107
|
|
|
$result[] = $word; |
108
|
|
|
} |
109
|
|
|
usort($result, function(Word $w1, Word $w2) { |
110
|
|
|
return strcmp($w1->value(), $w2->value()); |
111
|
|
|
}); |
112
|
|
|
return $result; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
class Program { |
|
|
|
|
118
|
|
|
public static function run() { |
119
|
|
|
$input = new File('php://stdin'); |
120
|
|
|
$output = new File('php://stdout'); |
121
|
|
|
|
122
|
|
|
$parser = new Parser($input); |
123
|
|
|
$words = $parser->words(); |
124
|
|
|
|
125
|
|
|
$collection = new Collection($words); |
126
|
|
|
|
127
|
|
|
$formatter = new Formatter; |
128
|
|
|
$output->write($formatter->format($collection->groupByOccurrences())); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
Program::run(); |
133
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.