HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
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);
    }
}