Cześć!
Biorę udział w rekrutacji na stanowisko PHP dewelopera. Zależy mi na tej pracy, ponieważ bardzo chce się rozwijać w kierunku PHP i najchętniej robiłbym to w pracy.
Napisałem jedno z dwóch zadań rekrutacyjnych - jutro wieczorem je oddaje, jednak zanim oddam chciałbym zasięgnąć języka i zapytać was czy taki sposób jakim rozwiązałem to zadanie będzie dobrze punktowany?
Pytam was, ponieważ nigdy nie miałem okazji pracować w zespole i znam wiele technik, ale nikt mi jeszcze nigdy nie powiedział, które są prawidłowe i których powinno się używać.
Treść zadania, pliki źródłowe i moje rozwiązanie zamieściłem w archiwum w załączniku.
Czekam na wasze opinie!
<?php
function findCategory($id, $list){
foreach($list as $element){
if($element['category_id'] == $id)
return $element;
}
}
function getNameFromElementInfo($translations){
if(@!$lang = $translations["pl_P"])
if(@!$lang = reset($translations))
return null;
return $lang["name"];
}
function setName($array, $list){
foreach($array as $id=>$element){
$categoryInfo = findCategory($element["id"], $list);
$element['name'] = getNameFromElementInfo($categoryInfo["translations"]);
if($childrens = $element['children']){
$element['children'] = setName($childrens, $list);
}
$array[$id] = $element;
}
return $array;
}
function render($tree){
foreach($tree as $el){
$id = $el["id"];
if(@!$name = $el["name"])
$name = null;
echo "$id:$name<br/>";
if($childs = $el['children'])
render($childs);
}
}
$treePath = './tree.json';
$listPath = './list.json';
if(!file_exists($treePath))
return print("can't find file '".$treePath."'");
if(!file_exists($listPath))
return print("can't find file '".$listPath."'");
$treeJson = file_get_contents($treePath);
$tree = json_decode($treeJson,true);
$listJson = file_get_contents($listPath);
$list = json_decode($listJson,true);
$treeWithNames = setName($tree, $list);
render($treeWithNames);
echo "<hr/>";
render($tree);
?>