Skip to content

Commit

Permalink
Merge pull request #120 from conedevelopment/refactor-attribute-bag
Browse files Browse the repository at this point in the history
Rework attribute bags
  • Loading branch information
iamgergo authored Feb 19, 2021
2 parents ddff61b + 5968767 commit f8c7e3b
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 229 deletions.
7 changes: 4 additions & 3 deletions src/Support/Bags/Inventory.php → src/Casts/Inventory.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<?php

namespace Bazar\Support\Bags;
namespace Bazar\Casts;

use Bazar\Support\AttributeBag;
use Illuminate\Support\Facades\Config;

class Inventory extends Bag
class Inventory extends AttributeBag
{
/**
* The bag items.
*
* @var array
*/
protected $items = [
protected $defaults = [
'files' => [],
'sku' => null,
'width' => null,
Expand Down
44 changes: 44 additions & 0 deletions src/Casts/Prices.php
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;
}
}
6 changes: 2 additions & 4 deletions src/Concerns/InteractsWithStock.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function price(string $type = 'default', string $currency = null): ?float
{
$currency = $currency ?: Bazar::currency();

return $this->prices[$currency][$type];
return $this->prices->get("{$currency}.{$type}");
}

/**
Expand All @@ -51,9 +51,7 @@ public function formattedPrice(string $type = 'default', string $currency = null
{
$currency = $currency ?: Bazar::currency();

$price = $this->prices[$currency];

return $price ? $price->format($type) : null;
return $this->prices->format("{$currency}.{$type}");
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Bazar\Models;

use Bazar\Casts\Inventory;
use Bazar\Casts\Prices;
use Bazar\Concerns\BazarRoutable;
use Bazar\Concerns\Filterable;
use Bazar\Concerns\HasMedia;
Expand All @@ -15,8 +17,6 @@
use Bazar\Proxies\Category as CategoryProxy;
use Bazar\Proxies\Order as OrderProxy;
use Bazar\Proxies\Variant as VariantProxy;
use Bazar\Support\Bags\Inventory;
use Bazar\Support\Bags\Prices;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
Expand Down
6 changes: 3 additions & 3 deletions src/Models/Variant.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Bazar\Models;

use Bazar\Bazar;
use Bazar\Casts\Inventory;
use Bazar\Casts\Prices;
use Bazar\Concerns\BazarRoutable;
use Bazar\Concerns\Filterable;
use Bazar\Concerns\HasMedia;
Expand All @@ -11,8 +13,6 @@
use Bazar\Contracts\Models\Variant as Contract;
use Bazar\Contracts\Stockable;
use Bazar\Proxies\Product as ProductProxy;
use Bazar\Support\Bags\Inventory;
use Bazar\Support\Bags\Prices;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand Down Expand Up @@ -132,7 +132,7 @@ public function price(string $type = 'default', string $currency = null): ?float
{
$currency = $currency ?: Bazar::currency();

return $this->prices[$currency][$type] ?: $this->product->price($type, $currency);
return $this->prices->get("{$currency}.{$type}") ?: $this->product->price($type, $currency);
}

/**
Expand Down
106 changes: 21 additions & 85 deletions src/Support/Bags/Bag.php → src/Support/AttributeBag.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
<?php

namespace Bazar\Support\Bags;
namespace Bazar\Support;

use ArrayAccess;
use ArrayIterator;
use ArrayObject;
use Illuminate\Contracts\Database\Eloquent\Castable;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use IteratorAggregate;
use Illuminate\Support\Arr;
use JsonSerializable;
use Stringable;

abstract class Bag implements Arrayable, ArrayAccess, Castable, IteratorAggregate, Jsonable, JsonSerializable, Stringable
abstract class AttributeBag extends ArrayObject implements Arrayable, Castable, Jsonable, JsonSerializable, Stringable
{
/**
* The bag items.
*
* @var array
*/
protected $items = [];
protected $defaults = [];

/**
* Create a new bag instance.
Expand All @@ -29,7 +28,7 @@ abstract class Bag implements Arrayable, ArrayAccess, Castable, IteratorAggregat
*/
public function __construct(array $items = [])
{
$this->items = array_replace($this->items, $items);
parent::__construct(array_replace($this->defaults, $items));
}

/**
Expand All @@ -39,9 +38,13 @@ public function __construct(array $items = [])
* @param mixed $value
* @return $this
*/
public function set(string $key, $value): Bag
public function set(string $key, $value): AttributeBag
{
$this->offsetSet($key, $value);
$items = $this->toArray();

Arr::set($items, $key, $value);

$this->exchangeArray($items);

return $this;
}
Expand All @@ -55,18 +58,7 @@ public function set(string $key, $value): Bag
*/
public function get(string $key, $default = null)
{
return $this->offsetGet($key) ?: $default;
}

/**
* Remove the given key from the items.
*
* @param string $key
* @return void
*/
public function remove(string $key): void
{
$this->offsetUnset($key);
return Arr::get($this->toArray(), $key, $default);
}

/**
Expand All @@ -76,9 +68,7 @@ public function remove(string $key): void
*/
public function toArray(): array
{
return array_map(static function ($item) {
return $item instanceof Arrayable ? $item->toArray() : $item;
}, $this->items);
return $this->getArrayCopy();
}

/**
Expand All @@ -103,66 +93,7 @@ public function jsonSerialize(): array
}

/**
* Determine if the offset exists in the items.
*
* @param string|int $key
* @return bool
*/
public function offsetExists($key): bool
{
return isset($this->items[$key]);
}

/**
* Get the value of the given offset.
*
* @param string|int $key
* @return mixed
*/
public function offsetGet($key)
{
return $this->items[$key] ?? null;
}

/**
* Get the value of the given offset.
*
* @param string|int|null $key
* @param mixed $value
* @return void
*/
public function offsetSet($key, $value): void
{
if (is_null($key)) {
$this->items[] = $value;
} else {
$this->items[$key] = $value;
}
}

/**
* Unset the value of the given offset.
*
* @param string|int $key
* @return void
*/
public function offsetUnset($key): void
{
unset($this->items[$key]);
}

/**
* Get the iterator for the items.
*
* @return \ArrayIterator
*/
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->items);
}

/**
* Convert the object to its string representation.
* Convert the object to its JSON representation.
*
* @return string
*/
Expand Down Expand Up @@ -233,7 +164,7 @@ public function __construct(string $class)
$this->class = $class;
}

public function get($model, string $key, $value, array $attributes): Bag
public function get($model, string $key, $value, array $attributes): AttributeBag
{
$class = $this->class;

Expand All @@ -246,6 +177,11 @@ public function set($model, string $key, $value, array $attributes): string
{
return json_encode($value, JSON_NUMERIC_CHECK);
}

public function serialize($model, string $key, $value, array $attributes): array
{
return $value->getArrayCopy();
}
};
}
}
62 changes: 0 additions & 62 deletions src/Support/Bags/Price.php

This file was deleted.

Loading

0 comments on commit f8c7e3b

Please sign in to comment.