Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Mar 3, 2023
1 parent 8d83ee0 commit bf55bce
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/back-end.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
fail-fast: true
matrix:
os: ["ubuntu-20.04"]
os: ["ubuntu-22.04"]
php: ["8.1", "8.2"]
stability: ["prefer-stable"]
laravel: ["10.0"]
Expand Down
13 changes: 13 additions & 0 deletions src/Interfaces/Models/PropertyValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Cone\Bazar\Interfaces\Models;

use Illuminate\Database\Eloquent\Relations\BelongsTo;

interface PropertyValue
{
/**
* Get the property for the property value.
*/
public function property(): BelongsTo;
}
45 changes: 20 additions & 25 deletions src/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;
use Illuminate\Database\Query\Builder as QueryBuilder;

class Product extends Model implements Contract, Resourceable
{
Expand Down Expand Up @@ -88,17 +88,6 @@ public function variants(): HasMany
return $this->hasMany(Variant::getProxiedClass());
}

/**
* Get the variants attribute.
*/
public function getVariantsAttribute(): Collection
{
return $this->getRelationValue('variants')->each(function (Variant $variant): void {
$variant->setRelation('product', $this->withoutRelations()->makeHidden('variants'))
->makeHidden('product');
});
}

/**
* Scope the query only to the models that are out of stock.
*/
Expand Down Expand Up @@ -126,19 +115,25 @@ public function scopeInStock(Builder $query): Builder
*/
public function toVariant(array $variation): ?Variant
{
return $this->variants->sortBy(static function (Variant $variant): int {
return array_count_values($variant->variation)['*'] ?? 0;
})->first(function (Variant $variant) use ($variation): bool {
$variation = array_replace(array_fill_keys(array_keys($this->properties), '*'), $variation);

foreach ($variant->variation as $key => $value) {
if ($value === '*') {
$variation[$key] = $value;
}
}

return empty(array_diff_assoc(array_intersect_key($variant->variation, $variation), $variation));
});
return $this->variants()
->getQuery()
->whereHas(
'propertyValues',
static function (Builder $query) use ($variation): Builder {
return $query->whereIn($query->qualifyColumn('value'), $variation)
->whereHas('property', static function (Builder $query) use ($variation): Builder {
return $query->whereIn($query->qualifyColumn('slug'), array_keys($variation));
});
},
'=',
function (QueryBuilder $query): QueryBuilder {
return $query->selectRaw('count(*)')
->from('bazar_buyable_property_value')
->whereIn('bazar_buyable_property_value.buyable_id', $this->variants()->select('bazar_variants.id'))
->where('bazar_buyable_property_value.buyable_type', Variant::class);
}
)
->first();
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/Models/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Cone\Bazar\Interfaces\Models\Property as Contract;
use Cone\Bazar\Resources\PropertyResource;
use Cone\Root\Interfaces\Resourceable;
use Cone\Root\Support\Slug;
use Cone\Root\Traits\InteractsWithProxy;
use Cone\Root\Traits\Sluggable;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -51,6 +52,14 @@ public function values(): HasMany
return $this->hasMany(PropertyValue::class);
}

/**
* Get the slug representation of the model.
*/
public function toSlug(): Slug
{
return (new Slug($this))->from('name')->unique();
}

/**
* Get the resource representation of the model.
*/
Expand Down
3 changes: 2 additions & 1 deletion src/Models/PropertyValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace Cone\Bazar\Models;

use Cone\Bazar\Interfaces\Models\PropertyValue as Contract;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class PropertyValue extends Model
class PropertyValue extends Model implements Contract
{
/**
* The attributes that are mass assignable.
Expand Down

0 comments on commit bf55bce

Please sign in to comment.