-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from conedevelopment/refactor-attribute-bag
Rework attribute bags
- Loading branch information
Showing
11 changed files
with
89 additions
and
229 deletions.
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
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,44 @@ | ||
<?php | ||
|
||
namespace Bazar\Casts; | ||
|
||
use Bazar\Bazar; | ||
use Bazar\Support\AttributeBag; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Str; | ||
|
||
class Prices extends AttributeBag | ||
{ | ||
/** | ||
* Create a new bag instance. | ||
* | ||
* @param array $items | ||
* @return void | ||
*/ | ||
public function __construct(array $items = []) | ||
{ | ||
$items = array_replace_recursive( | ||
array_fill_keys(array_keys(Bazar::currencies()), ['default' => null]), | ||
$items | ||
); | ||
|
||
parent::__construct($items); | ||
} | ||
|
||
/** | ||
* Get the formatted price of the given type. | ||
* | ||
* @param string|null $key | ||
* @return string|null | ||
*/ | ||
public function format(string $key = null): ?string | ||
{ | ||
$currency = $key ? explode('.', $key, 2)[0] : Bazar::currency(); | ||
|
||
$price = Arr::get( | ||
$this->toArray(), $key ?: "{$currency}.default" | ||
); | ||
|
||
return $price ? Str::currency($price, $currency) : null; | ||
} | ||
} |
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
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
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
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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.