<?php
	ini_set('display_errors', 1); 
	ini_set('display_startup_errors', 1); 
	error_reporting(E_ALL);
	abstract class BaseList{
		protected $dataArray;
		protected $index;
		public function __construct(){
			$this->dataArray=[];
			$this->index=0;
		}
		public function delete($id){
			for ($i=0;$i<count($this->dataArray);$i++){
				if ($this->dataArray[$i]->getId()==$id){
					array_splice($this->dataArray, $i, 1); 
				}
			}
		}
		public function displayAll(){
			for ($i=0;$i<count($this->dataArray);$i++){
				echo $this->dataArray[$i]->displayInfo();
			}
		}
	}
	class BackpackList extends BaseList{
		public function add($name, $vendor, $category, $price, $properties){
			$id=++$this->index;
			$nb = new Backpack($id, $name,$vendor, $category, $price, $properties);
			array_push($this->dataArray,$nb);
			return $id;
		}
		public function edit($id, $name, $vendor, $category, $price, $properties){
			for ($i=0;$i<count($this->dataArray);$i++){
				if ($this->dataArray[$i]->getId()==$id){
					$this->dataArray[$i]->edit($name, $vendor, $category, $price, $properties);
				}
			}
		}
		
	}
	class Backpack{
		private $id;
		private $name;
		private $vendor;
		private $category;
		private $price;
		private $properties;
		public function __construct($id, $name, $vendor, $category, $price, $properties){
			$this->id=$id;
			$this->name=$name;	
			$this->vendor=$vendor;
			$this->category=$category;
			$this->price=$price;
			$this->properties=$properties;	
		}
		public function getId(){
			return $this->id;
		}
		
		public function edit($name, $vendor, $category,$price, $properties){
			$this->name=$name;	
			$this->vendor=$vendor;
			$this->category=$category;
			$this->price=$price;
			$this->properties=$properties;
		}
		private function displayProperties(){
			$result='<i>Характеристики:</i></br>';
			foreach($this->properties as $key => $value) {
				$result.=  $key . ": " . $value;
			  	$result.=  "<br>";
			}
			return $result;
		}
		public function displayInfo(){
			return $this->id.". <b>".$this->vendor." ".$this->name."</b></br>
			Ціна: ".$this->price."<br>
			Категорія: ".$this->category."<br>". $this->displayProperties();
		}
		public function __destruct(){
			echo "";	
		}
	}
	
	
	class CategoryList extends BaseList{
		public function add($name){
			$id=++$this->index;
			$nc = new Category($id, $name);
			array_push($this->dataArray,$nc);
			return $id;
		}
		public function edit($id, $name){
			for ($i=0;$i<count($this->dataArray);$i++){
				if ($this->dataArray[$i]->getId()==$id){
					$this->dataArray[$i]->edit($name);
				}
			}
		}
		
	}
	class Category{
		private $id;
		private $name;
		public function __construct($id, $name){
			$this->id=$id;
			$this->name=$name;		
		}
		public function displayInfo(){
			return $this->id.". ".$this->name."</br>";
		}
		public function getId(){
			return $this->id;
		}
		
		public function edit($name){
			$this->name=$name;
		}
		public function __destruct(){
			echo "";	
		}
	}
	
	class PropertyList extends BaseList{
		public function add($name, $units){
			$id=++$this->index;
			$np = new Property($id, $name, $units);
			array_push($this->dataArray,$np);
			return $id;
		}
		public function edit($id, $name, $units){
			for ($i=0;$i<count($this->dataArray);$i++){
				if ($this->dataArray[$i]->getId()==$id){
					$this->dataArray[$i]->edit($name, $units);
				}
			}
		}
		
	}
	
	class Property{
		private $id;
		private $name;
		private $units;
		public function __construct($id, $name,$units){
			$this->id=$id;
			$this->name=$name;
			$this->units=$units;		
		}
		public function getId(){
			return $this->id;
		}
		
		public function edit($name,$units){
			$this->name=$name;
			$this->units=$units;
		}
		public function displayInfo(){
			return $this->id.". ".$this->name." <i>(".$this->units.")</i></br>";
		}
		public function __destruct(){
			echo "";	
		}
	}
	
	
	$cl= new CategoryList();
	$cl->add('Міські рюкзаки');
	$cl->add('Похідні рюкзаки');
	$cl->displayAll();
	echo '</br>';
	$pl= new PropertyList();
	$pl->add('Вага','кг');
	$pl->add('Об\'єм','л');
	$pl->displayAll();
	echo '</br>';
	$bl= new BackpackList();
	$bl->add('Flare', 'Osprey','Міські рюкзаки', '999',['Вага'=>'1.1','Об\'єм'=>'20']);
	$bl->add('Kestrel', 'Osprey','Похідні рюкзаки', '1999',['Вага'=>'1.6','Об\'єм'=>'65']);
	$bl->displayAll();
	echo '</br>';
	$bl->edit(2,'Kestrel', 'Osprey','Похідні рюкзаки', '2999',['Вага'=>'1.6','Об\'єм'=>'65']);
	$bl->displayAll();
?>