Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Nov 7, 2023
1 parent 8bfdb1e commit 3590dd1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
51 changes: 51 additions & 0 deletions src/Fields/Price.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Cone\Bazar\Fields;

use Cone\Bazar\Bazar;
use Cone\Root\Fields\Meta;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class Price extends Meta
{
/**
* The price currency.
*/
protected string $currency;

/**
* Create a new price field instance.
*/
public function __construct(string $label, string $modelAttribute = null, string $currency = null)
{
parent::__construct($label, $modelAttribute);

$this->currency = $currency ?: Bazar::getCurrency();
}

/**
* Set the currency attribute.
*/
public function currency(string $value): static
{
$this->currency = $value;

return $this;
}

/**
* {@inheritdoc}
*/
public function resolveFormat(Request $request, Model $model): mixed
{
if (is_null($this->formatResolver)) {
$this->formatResolver = function (Request $request, Model $model, mixed $value): ?string {
return is_null($value) ? null : Str::currency($value, $this->currency);
};
}

return parent::resolveFormat($request, $model);
}
}
2 changes: 1 addition & 1 deletion src/Traits/HasPrices.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function getFormattedPrice(string $currency = null): ?string

$price = $this->getPrice($currency);

return $price ? Str::currency($price, $currency) : null;
return is_null($price) ? null : Str::currency($price, $currency);
}

/**
Expand Down

0 comments on commit 3590dd1

Please sign in to comment.