Skip to content

Commit eb3e2ca

Browse files
committed
Added comments
1 parent 255bad0 commit eb3e2ca

File tree

8 files changed

+62
-6
lines changed

8 files changed

+62
-6
lines changed

app/Http/Controllers/HistoryController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,23 @@
1414
use Illuminate\Foundation\Auth\Access\AuthorizesResources;
1515
use App\History;
1616
use Illuminate\Http\Request;
17-
use App\Libraries\Converter;
1817

1918
class HistoryController extends BaseController {
2019
use AuthorizesRequests, AuthorizesResources, DispatchesJobs, ValidatesRequests;
2120

2221
function convert( Request $request ) {
22+
// built-in validation
2323
$this->validate( $request, [
2424
'number' => 'required|numeric|min:0|max:3999',
2525
] );
2626

27+
// log this request into database
2728
$history = new History;
2829
$history->number = $request->number;
2930
$history->save();
3031

32+
// redirect to 'home' route with query string 'number'.
33+
// It's GET request which will show the resulting number in decimal na roman
3134
return redirect( route( 'home', array( 'number' => $request->number ) ) );
3235
}
3336
}

app/Http/Controllers/HomeController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,24 @@
1919
class HomeController extends BaseController {
2020
use AuthorizesRequests, AuthorizesResources, DispatchesJobs, ValidatesRequests;
2121

22+
/*
23+
* Displays the homepage
24+
*/
2225
function index( Request $request ) {
2326
$history = History::orderBy( 'created_at', 'desc' )->get();
2427

2528
$data = array(
2629
'history' => $history
2730
);
2831

32+
// If there was 'number' param then show both decimal and roman numbers in their fields
2933
if ( $request->number ) {
3034
$converter = new Converter();
3135
$data['number'] = $request->number;
3236
$data['roman'] = $converter->toRomanNumerals( $request->number );
3337
}
3438

39+
// used when converting numbers in template when displaying history
3540
$data['converter'] = new Converter();
3641

3742
return view( 'converter', $data );

app/Http/routes.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313

1414
Route::get( '/', [ 'as' => 'home', 'uses' => 'HomeController@index' ] );
1515

16-
1716
/**
18-
* Add New History
17+
* Add New History item
1918
*/
2019
Route::post( '/history', [ 'as' => 'history', 'uses' => 'HistoryController@convert' ] );

app/Libraries/Converter.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313

1414
class Converter implements IntegerConversion {
1515

16+
/*
17+
* Validate and convert
18+
*/
1619
public function convert( $number ) {
20+
// standalone validator with default settings
1721
$factory = new ValidatorFactory( new Translator( 'en' ) );
1822
$validation = $factory->make(
1923
array(
@@ -31,6 +35,9 @@ public function convert( $number ) {
3135
return $this->toRomanNumerals( $number );
3236
}
3337

38+
/*
39+
* Convert number into roman numeral
40+
*/
3441
public function toRomanNumerals( $number ) {
3542
$result = '';
3643
$number = (int) $number;
@@ -40,6 +47,8 @@ public function toRomanNumerals( $number ) {
4047
return false;
4148
}
4249

50+
// Dictionary contains only the basic numbers and the
51+
// rest is calculated basing on it
4352
$dictionary = array(
4453
1 => 'I',
4554
5 => 'V',
@@ -50,30 +59,45 @@ public function toRomanNumerals( $number ) {
5059
1000 => 'M'
5160
);
5261

53-
// end early if key exists
62+
// end early if key already exists
5463
if ( array_key_exists( $number, $dictionary ) ) {
5564
return $dictionary[ $number ];
5665
}
5766

67+
// used for $part calculation (more description in loop)
5868
$temp = 0;
5969

70+
/*
71+
* The idea here is to split numbers into chunks so for ex. '1345' becomes 1000, 300, 40, 5
72+
* and then each chunk is looked up in dictionary.
73+
*/
6074
for ( $i = 1000; (int) $i !== 0; $i /= 10 ) {
75+
// current chunk is the difference of given number, $i
76+
// and sum of previous chunks from the loop
6177
$part = $number - ( (int) $number % $i ) - $temp;
6278

79+
// don't calculate when it's not needed
80+
// for ex. 45 doesn't need '1000' chunk
81+
// and when there are no chunks left we can break
6382
if ( $i > $number || $part == 0 ) {
6483
continue;
6584
}
6685

6786
$closest = $this->closest( $dictionary, $part );
87+
88+
// either number is closer to prev character in dictionary or to next one
6889
if ( $closest < $part ) {
90+
// numbers that are lower can have repeated character ( like I, II, III)
6991
$repeat = floor( $part / $closest );
7092
$result .= str_repeat( $dictionary[ $closest ], $repeat );
7193

94+
// should there be something appended (like to V : VI, VII)
7295
$after = $this->toRomanNumerals( $part - ( $closest * $repeat ) );
7396
if ( $after ) {
7497
$result .= $after;
7598
}
7699
} else {
100+
// should there be something prepended (like to V : IV)
77101
$before = $this->toRomanNumerals( $closest - $part );
78102
if ( $before ) {
79103
$result .= $before;
@@ -82,20 +106,30 @@ public function toRomanNumerals( $number ) {
82106
$result .= $dictionary[ $closest ];
83107
}
84108

109+
// sum of all previous chunks
85110
$temp += $part;
86111
}
87112

88113
return $result;
89114
}
90115

91116
function closest( $array, $number ) {
117+
/*
118+
* This ratio was taken from the overall idea how roman numbers work.
119+
* For ex. after I there are 2 numbers that append it and before V there is one that prepends is.
120+
* It works same way for all other roman letters/numbers. The point at which this 'switch' happens
121+
* is exactly after third of all numbers (I, II, III, IV, V) so it happens at fourth of 5 numbers.
122+
* It's position is 4/5 which translates into 0.8.
123+
*/
92124
$ratio = 0.8;
93125

126+
// get the last and first element of array
94127
end( $array );
95128
$next = key( $array );
96129
reset( $array );
97130
$prev = key( $array );
98131

132+
// this way we know which element is before and after the given number
99133
foreach ( $array as $key => $val ) {
100134
if ( $key > $number ) {
101135
$next = $key;
@@ -104,10 +138,12 @@ function closest( $array, $number ) {
104138
$prev = $key;
105139
}
106140

141+
// if the number is closer to the next roman number (IV closer to V)
107142
if ( $number / $next >= $ratio ) {
108143
return $next;
109144
}
110145

146+
// number was closer to previous number (II, III closer to I)
111147
return $prev;
112148
}
113149
}

public/js/app.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/js/app.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readme.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,15 @@ The theme is built using gulp, elixir ans scss. It's designed with simplicity in
2424
- When submitted the form should display both the integer and roman numeral
2525
values
2626
- Support numbers between 0 and 3999, only numeric characters
27-
- Display history of converted numbers along with creation time
27+
- Display history of converted numbers along with creation time
28+
29+
### Important custom files
30+
31+
- `app/Http/routes.php`
32+
- `app/Http/Controllers`
33+
- `app/Http/Libraries`
34+
- `app/History.php`
35+
- `config/app.php` (Include Laravel Collective library Classes)
36+
- `resources/assets` (Sass, JS)
37+
- `resources/views` (Blade templates)
38+
- `tests/ConverterTest.php`

resources/assets/js/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
// import jQuery via browserify
12
window.$ = window.jQuery = require('jquery');

0 commit comments

Comments
 (0)