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/knijter/KnijterService.php
<?php




namespace Knijter;


include_once $_SERVER['DOCUMENT_ROOT'] . '/php/knijter/KnijterRepository.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/php/knijter/KnijterXmlWriter.php';


class KnijterService
{
    private $repository;

    private $writer;

    public function __construct()
    {
        $this->repository = new KnijterRepository();
        $this->writer = new KnijterXmlWriter();
    }

    /**
     * Return all product variants with images
     *
     * @return array
     */
    public function products()
    {
        // Get all products and all images
        $products = $this->repository->products();
        $images = $this->repository->productImages();

        // Get all categories
        $categories = $this->repository->categories();
        $productsAsObject = [];

        // Loop through all products
        foreach($products as $product)
        {
            // Add images to product
            isset($images[$product['id']]) ? $product['images'] = $images[$product['id']] : $product['images'] = [];

            // Decide whether the product is male, female or unisex by category
            isset($categories[$product['shop_category_id']]) ?
                $category = $categories[$product['shop_category_id']] :
                $category = null;
            $product['gender'] = $this->addGender($category);

            // Convert array to object
            $productsAsObject[] = (object) $product;
        }
        return $productsAsObject;
    }

    /**
     * Write XML file with products
     *
     * @param $products
     */
    public function write($products)
    {
        $this->writer->write($products);
    }

    /**
     * Decide whether the product is male, female or unisex
     *
     * @param $category
     * @return string
     */
    private function addGender($category)
    {
        // Make sure category is in lowercase
        $category = strtolower($category->category);
        $category = trim($category);

        // Definitely male
        $maleCategories = ['kleding heren'];
        if(in_array($category, $maleCategories)) return 'male';

        // Definitely female
        $femaleCategories = ['kleding dames','sieraden'];
        if(in_array($category, $femaleCategories)) return 'female';

        // Unknown
        return '';
    }

}