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/shop.komma.nl/app/Products/ProductComposite/ProductComposite.php
<?php

namespace App\Products\ProductComposite;

use App\Products\AbstractProductable;
use App\Products\ProductableEnum;
use App\Properties\Models\PropertizableTrait;
use Komma\KMS\Documents\Kms\DocumentableInterface;
use Komma\KMS\Documents\Models\Document;
use Komma\KMS\Core\Entities\DisplayNameInterface;
use Komma\KMS\Core\Entities\DisplayNameTrait;
use App\Categories\Kms\CategorizableInterface;
use App\Categories\Models\Category;
use App\Products\ProductGroup\ProductGroup;
use App\Properties\Models\PropertizableInterface;
use App\Properties\Models\Property;
use Komma\KMS\Sites\Models\Site;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Laravel\Scout\Searchable;

/**
 * App\Products\ProductComposite\ProductComposite
 *
 * @property int $id
 * @property int $active
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \Illuminate\Database\Eloquent\Collection|Category[] $categories
 * @property-read int|null $categories_count
 * @property-read \Illuminate\Database\Eloquent\Collection|Document[] $documents
 * @property-read int|null $documents_count
 * @property-read \Illuminate\Database\Eloquent\Collection|ProductGroup[] $groups
 * @property-read int|null $groups_count
 * @property-read \Illuminate\Database\Eloquent\Collection|Property[] $properties
 * @property-read int|null $properties_count
 * @property-read \Illuminate\Database\Eloquent\Collection|Site[] $sites
 * @property-read int|null $sites_count
 * @property-read \App\Products\ProductComposite\ProductCompositeTranslation|null $translation
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Products\ProductComposite\ProductCompositeTranslation[] $translations
 * @property-read int|null $translations_count
 * @method static \Illuminate\Database\Eloquent\Builder|ProductComposite newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|ProductComposite newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|ProductComposite query()
 * @method static \Illuminate\Database\Eloquent\Builder|ProductComposite whereActive($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ProductComposite whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ProductComposite whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ProductComposite whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class ProductComposite extends AbstractProductable implements CategorizableInterface, DocumentableInterface, PropertizableInterface
{
    use Searchable;
    use PropertizableTrait;

    public function searchableAs()
    {
        return 'products_composite_index';
    }

    public function toSearchableArray()
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
        ];
    }

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'active'];

    public function groups()
    {
        return $this->belongsToMany(ProductGroup::class);
    }

    public function translations(): HasMany
    {
        return $this->hasMany(ProductCompositeTranslation::class);
    }

    /**
     * Get the documents from the current model
     *
     * @return MorphMany
     */
    public function documents():MorphMany
    {
        return $this->morphMany(Document::class, 'documentable');
    }

    /**
     * Returns the lowest price possible for the composite.
     * Also known as "entry price"
     *
     * @return float
     */
    public function getPrice(): float
    {
        $price = 0;
        $groups = $this->groups()->get();
        $groups->each(function(ProductGroup $group) use (&$price) {
            $price += $group->getPrice();
        });

        return $price;
    }

    /**
     * @return int
     */
    public function enum(): int
    {
        return ProductableEnum::PRODUCT_GROUP;
    }
}