patternphpMinor
MVC design to fetch, store, and present data from an external source
Viewed 0 times
presentmvcsourcedesignfetchstoreandexternalfromdata
Problem
A couple of weeks ago a company sent me this coding challenge:
Please write a PHP web application and send it back to me as zip file:
NOT provide a DB dump, but a script which automatically transfers the
data from the remote location to the DB)
good testable, best even already contain Unit tests and maybe even
follow the KISS and SOLID principles
AND
by CountryName
Is my solution okay? How can it be improved?
Controller:
```
load->model('country_model');
}
public function index() {
$data = array();
if (isset($_POST['run'])) {
$this->_save_data();
$data['list'] = $this->country_model->get_countries(252)->result();
}
$this->load->helper('form');
$this->load->view('welcome_message', $data);
}
private function _save_data() {
// Get a file into an array. In this example we'll go through HTTP to get
// the HTML source of a URL.
$lines = file('http://pastebin.com/raw.php?i=943PQQ0n');
$lineNo = 0;
$startLine = 4;
$endLine = 255;
// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
$lineNo++;
if ($lineNo >=
Please write a PHP web application and send it back to me as zip file:
- Which waits for a user action, like clicking buttons. According to these actions some data (see further below) should be:
- either shown nicely formatted on the screen
- or downloaded as CSV file
- You can either download the data on each request during the runtime of your PHP program or load the data from a database (in this case do
NOT provide a DB dump, but a script which automatically transfers the
data from the remote location to the DB)
- Preferably the implementation should be written in "clean code", separate concerns using pattern like MVC, be object oriented, very
good testable, best even already contain Unit tests and maybe even
follow the KISS and SOLID principles
AND
- Country list
- The data should be a list of countries with their country code
- Please download the base data from here
- Afterwards you will have to change the whole list from "Country code - Country name" to "CountryName - CountryCode" and sorts the list
by CountryName
Is my solution okay? How can it be improved?
Controller:
```
load->model('country_model');
}
public function index() {
$data = array();
if (isset($_POST['run'])) {
$this->_save_data();
$data['list'] = $this->country_model->get_countries(252)->result();
}
$this->load->helper('form');
$this->load->view('welcome_message', $data);
}
private function _save_data() {
// Get a file into an array. In this example we'll go through HTTP to get
// the HTML source of a URL.
$lines = file('http://pastebin.com/raw.php?i=943PQQ0n');
$lineNo = 0;
$startLine = 4;
$endLine = 255;
// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
$lineNo++;
if ($lineNo >=
Solution
Looks pretty good. Here are just some thoughts but nothing major:
-
It would be more future-proof (and probably efficient) to use regular expressions in lieu of parsing hard-coded line numbers (in case the data has rows added later):
-
For the
This way you do not need to pass limit to
-
You could also use a view helper or template for the output section to better format.
-
It would be more future-proof (and probably efficient) to use regular expressions in lieu of parsing hard-coded line numbers (in case the data has rows added later):
-
For the
_save_data() you could parse like so:$string = '{THE CONTENT FROM THE COUNTRIES LIST PAGE}';
$sub = preg_replace('/.+?(?=AD\s\s\s)/s', '', $string); // this strips the pre-text
$list = preg_split('/$\R?^/m', $sub); // This splits by line
$countries = array();
foreach ($list as $item) {
$arr = explode(' ', $item);
$countries[$arr[0]] = $arr[1];
}
asort($countries); // sort by values
//print_r($countries);
/** Array
(
[AF] => Afghanistan
[AL] => Albania
[DZ] => Algeria
[AS] => American Samoa
[AD] => Andorra
... */This way you do not need to pass limit to
get_countries().-
You could also use a view helper or template for the output section to better format.
can get pretty ugly so I would do a list (``) or something. Overall, it seems pretty logical.Code Snippets
$string = '{THE CONTENT FROM THE COUNTRIES LIST PAGE}';
$sub = preg_replace('/.+?(?=AD\s\s\s)/s', '', $string); // this strips the pre-text
$list = preg_split('/$\R?^/m', $sub); // This splits by line
$countries = array();
foreach ($list as $item) {
$arr = explode(' ', $item);
$countries[$arr[0]] = $arr[1];
}
asort($countries); // sort by values
//print_r($countries);
/** Array
(
[AF] => Afghanistan
[AL] => Albania
[DZ] => Algeria
[AS] => American Samoa
[AD] => Andorra
... */Context
StackExchange Code Review Q#129657, answer score: 4
Revisions (0)
No revisions yet.