File: D:/HostingSpaces/SBogers10/deensekroon.komma-mediadesign.nl/wwwroot/php/miinto/MiintoService.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/php/miinto/MiintoRepository.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/php/miinto/MiintoXmlWriter.php';
class MiintoService
{
protected $repository;
protected $writer;
protected $discountService;
protected $products;
public function __construct()
{
$this->repository = new MiintoRepository();
$this->writer = new MiintoXmlWriter();
}
public function index()
{
// Create data array
$this->products = $this->repository->getProducts();
$this->addVariationToProducts();
$this->writer->write($this->products);
}
/*
* Add variations to existing product array
* @return array
*/
protected function addVariationToProducts()
{
$images = $this->repository->getProductImages();
$stock = $this->repository->getProductStock();
foreach($this->products as &$product)
{
$variation = [];
// Add image variation
if(isset($images[$product->id]))
{
$variation['photos'] = $images[$product->id];
}
else
{
// todo: no image
}
// Add stock variation
if(isset($stock[$product->id])) $variation['sizes'] = $stock[$product->id];
// Color
$variation['color'] = $this->getColor($product->color);
$product->variation = (object) $variation;
}
}
/*
* Return color name
*/
protected function getColor($hex)
{
#color category
if(empty($hex)) $hex = 'ffffff';
$hexR = substr($hex,0,2);
$hexG = substr($hex,2,2);
$hexB = substr($hex,4,2);
$decR = hexdec($hexR);
$decG = hexdec($hexG);
$decB = hexdec($hexB);
$hsl = rgbhsv($decR,$decG,$decB);
return returnColorName($hsl);
}
}