-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
52 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters