Skip to content

Instantly share code, notes, and snippets.

@Xsaven
Last active September 17, 2016 10:11
Show Gist options
  • Save Xsaven/d416817a0a6ba767d05d053a36e701c8 to your computer and use it in GitHub Desktop.
Save Xsaven/d416817a0a6ba767d05d053a36e701c8 to your computer and use it in GitHub Desktop.
Тестовое задание
<?php
/**
* @author Xsaven
* @copyright 2016
*/
$factory = new Factory();
$factory->addRobot(new robot()) //Добовление роботов
->addRobot(new robot(array(
'type' => 'MAZDA',
'weight' => 250.3,
'speed' => 130.2,
'model' => 'A2'
)))
->addRobot(new robot(array(
'type' => 'BMW',
'weight' => 280.1,
'speed' => 11,
'model' => 'A3'
)));
$serchRobots = $factory->serch('BMW'); //Вывидет массив всех роботов типа 'BMW'
echo $factory->getSpeed(); //Скорость всех роботов
echo $factory->getSpeed($serchRobots); //Скорость всех роботов с результата поиска
echo $factory->getWeight(); //Вес всех роботов
echo $factory->getWeight($serchRobots); //Вес всех роботов с результата поиска
$allRobost = $factory->robots; //Массив всех роботов
class Factory
{
public $robots = array();
public function addRobot($obj){
$this->robots[] = array(
'type' => $obj->type,
'weight' => $obj->weight,
'speed' => $obj->speed,
'model' => $obj->model,
);
return $this;
}
public function serch($serch){
$finded = array();
foreach($this->robots as $array){
$key = array_search($serch, $array);
if($key!==FALSE) $finded[] = $array;
}
if(count($finded)) return $finded;
else return false;
}
public function getSpeed($serchResult = array()){
$speed = 0;
if(count($serchResult)) foreach($serchResult as $array) $speed += $array['speed'];
else foreach($this->robots as $array) $speed += $array['speed'];
return $speed;
}
public function getWeight($serchResult = array()){
$weight = 0;
if(count($serchResult)) foreach($serchResult as $array) $weight += $array['weight'];
else foreach($this->robots as $array) $weight += $array['weight'];
return $weight;
}
}
class robot
{
public $type = 'BMW';
public $weight = 200;
public $speed = 1.5;
public $model = 'A1';
public function __construct($data=array()) {
foreach($data as $key => $val) $this->$key = $val;
}
public function getParams($paramName){
if(isset($this->$paramName)) return $this->$paramName;
else return '';
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment