<?php

namespace App\Models;

use App\Services\Type;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\ProductCategory
 *
 * @property int $id
 * @property int $product_id
 * @property int $category_id
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \App\Models\Category $category
 * @property-read \App\Models\Product $product
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductCategory whereCategoryId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductCategory whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductCategory whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductCategory whereProductId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductCategory whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductCategory newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductCategory newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductCategory query()
 * @property int|null $pos
 * @property array|null $show_on
 * @method static \Illuminate\Database\Eloquent\Builder|ProductCategory wherePos($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ProductCategory whereShowOn($value)
 * @mixin \Eloquent
 */
class ProductCategory extends Model
{
    protected $table = 'product_categories';

    protected $casts = [
        'show_on' => 'array',
    ];

    protected $fillable = [
        'pos', 'product_id', 'category_id', 'show_on'
    ];

    public function product()
    {
        return $this->belongsTo('App\Models\Product', 'product_id');
    }

    public function category()
    {
        return $this->belongsTo('App\Models\Category', 'category_id');
    }

    public function getShowOnTypes($seperator = false){
        $ret = [];
        if($this->show_on && is_array($this->show_on)){
            foreach($this->show_on as $show){
                $ret[] = isset(Type::$showONs[$show]) ? Type::$showONs[$show] : '-';
            }
        }
        return $seperator ? implode($seperator, $ret) : $ret;
    }

}
