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 '';
}
}