Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Nov 19, 2024
1 parent 9ceeb68 commit 3ef12c6
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 108 deletions.
15 changes: 1 addition & 14 deletions actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,11 @@ use Illuminate\Http\Request;
public function actions(Request $request): array
{
return [
new SendPasswordResetNotification(),
new SendPasswordResetNotification,
];
}
```

Alternatively, you can use `withActions` method on an object that resolves actions. It can be useful when you just want to hook into the object for some reason.

```php
use App\Root\Actions\SendPasswordResetNotification;
use Illuminate\Http\Request;

$resource->withActions(static function (Request $request): array {
return [
new SendPasswordResetNotification(),
];
});
```

## Configuration

### Fields
Expand Down
70 changes: 32 additions & 38 deletions bazar.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Also, you may add your custom driver easily that implements the required logic o
Registering gateways works almost the same as registering shipping methods. All custom drivers should extend the `Bazar\Gateway\Driver` class.

```php
use Bazar\Gateway\Driver;
use Cone\Bazar\Gateway\Driver;

class CreditCardDriver extends Driver
{
Expand All @@ -80,7 +80,7 @@ class CreditCardDriver extends Driver
Now, let's register the driver using the `Bazar\Support\Facades\Gateway` facade:

```php
use Bazar\Support\Facades\Gateway;
use Cone\Bazar\Support\Facades\Gateway;

Gateway::extend('credit-card', static function ($app): CreditCardDriver {
return new CreditCardDriver(
Expand All @@ -102,7 +102,7 @@ The class that implements the interface must implement the `shipping` method, wh
> By default, the `Bazar\Models\Order` and the `Bazar\Models\Cart` models are implementing the contract.
```php
use Bazar\Models\Shipping;
use Cone\Bazar\Models\Shipping;
use Illuminate\Database\Eloquent\Relations\MorphOne;

public function shipping(): MorphOne
Expand All @@ -122,8 +122,8 @@ Registering shipping methods works almost the same as registering gateways metho
Let's create a simple driver as an example:

```php
use Bazar\Contracts\Shippable;
use Bazar\Shipping\Driver;
use Cone\Bazar\Contracts\Shippable;
use Cone\Bazar\Shipping\Driver;

class FedexDriver extends Driver
{
Expand All @@ -137,7 +137,7 @@ class FedexDriver extends Driver
Now, let's register the driver using the `Bazar\Support\Facades\Shipping` facade:

```php
use Bazar\Support\Facades\Shipping;
use Cone\Bazar\Support\Facades\Shipping;

Shipping::extend('fedex', static function ($app): FedexDriver {
return new FedexDriver(
Expand Down Expand Up @@ -174,14 +174,12 @@ Cart::removeItem($item->id);

### Taxes

> Before moving on, you may check the [tax documentation](/docs/tax) about managing taxes.
The `Cart` model uses the `Itemable` trait, which allows the model to interact with its `Taxable` models. Taxes are stored on the `Item` and `Shipping` models.

There are several methods to retrieve the aggregated tax for the model:

```php
use Bazar\Support\Facades\Cart;
use Cone\Bazar\Support\Facades\Cart;

// Aggregate the calculated taxes
$tax = Cart::getModel()->getTax();
Expand All @@ -195,12 +193,10 @@ $tax = Cart::getModel()->calculateTax(false);

### Discounts

> Before moving on, you may check the [discount documentation](/docs/discount) about managing discounts.
Unlike TAXes, discounts are stored directly on the model as an aggregated value.
Unlike Taxes, discounts are stored directly on the model as an aggregated value.

```php
use Bazar\Support\Facades\Cart;
use Cone\Bazar\Support\Facades\Cart;

// Get the discount attribute
$tax = Cart::getModel()->getDiscount();
Expand All @@ -214,10 +210,8 @@ $tax = Cart::getModel()->calculateDiscount(false);

### Shipping

> Before moving on, you may check the [shipping documentation](/docs/shipping) about managing shipping methods.
```php
use Bazar\Support\Facades\Cart;
use Cone\Bazar\Support\Facades\Cart;

// Get the calculated shipping cost
Cart::getModel()->shipping->getCost();
Expand All @@ -238,7 +232,7 @@ However, this behavior can be controlled by the lock/unlock mechanism. When the
> Note, you may retrieve the `Cart` model using the `Cart` facade for this feature.
```php
use Bazar\Models\Cart;
use Cone\Bazar\Models\Cart;

$cart = Cart::first();

Expand All @@ -264,7 +258,7 @@ This can be extremely useful in the checkout process. Locking the cart can make
To keep the database clean, carts without owners **expire in 3 days**. To retrieve the expired carts, you may use the `expired()` query scope on the `Cart` model:

```php
use Bazar\Models\Cart;
use Cone\Bazar\Models\Cart;

$expired = Cart::expired()->get();
```
Expand Down Expand Up @@ -309,8 +303,8 @@ Like shipping and payment gateway, Bazar manages multiple cart drivers as well.
Registering cart drivers works almost the same as registering shipping methods or payment gateways. All custom drivers should extend the `Bazar\Cart\Driver` class, which holds one abstract method: `resolve()`.

```php
use Bazar\Cart\Driver;
use Bazar\Models\Cart;
use Cone\Bazar\Cart\Driver;
use Cone\Bazar\Models\Cart;

class TokenDriver extends Driver
{
Expand All @@ -333,7 +327,7 @@ class TokenDriver extends Driver
Now, let's register the driver using the `Bazar\Support\Facades\Cart` facade:

```php
use Bazar\Support\Facades\Cart;
use Cone\Bazar\Support\Facades\Cart;

Cart::extend('custom', function ($app): CustomDriver {
return new CustomDriver(
Expand All @@ -353,17 +347,17 @@ Taxes are stored on the `Item` and the `Shipping` models. Both implement the `Ta
You may register taxes using the `Tax` facade. You can pass a number, a `Closure`, or a class (that implements the `Bazar\Contracts\Tax` interface) along with the name of the tax.

```php
use Bazar\Support\Facades\Tax;
use Cone\Bazar\Support\Facades\Tax;

// Fix tax
Tax::register('fix-20', 20);
```

```php
// Custom closure tax
use Bazar\Models\Shipping;
use Bazar\Contracts\LineItem;
use Bazar\Support\Facades\Tax;
use Cone\Bazar\Models\Shipping;
use Cone\Bazar\Contracts\LineItem;
use Cone\Bazar\Support\Facades\Tax;

Tax::register('custom-percent', function (LineItem $model) {
return $model->getPrice() * ($model instanceof Shipping ? 0.3 : 0.27);
Expand All @@ -372,10 +366,10 @@ Tax::register('custom-percent', function (LineItem $model) {

```php
// Class tax
use Bazar\Contracts\Tax as Contract;
use Bazar\Contracts\LineItem;
use Bazar\Models\Shipping;
use Bazar\Support\Facades\Tax;
use Cone\Bazar\Contracts\Tax as Contract;
use Cone\Bazar\Contracts\LineItem;
use Cone\Bazar\Models\Shipping;
use Cone\Bazar\Support\Facades\Tax;

class CustomTax implements Contract
{
Expand All @@ -395,7 +389,7 @@ Tax::register('complex-tax', new CustomTax);
You may remove registered taxes using the `Tax` facade.

```php
use Bazar\Support\Facades\Tax;
use Cone\Bazar\Support\Facades\Tax;

Tax::remove('complex-tax');
```
Expand All @@ -405,7 +399,7 @@ Tax::remove('complex-tax');
You may disable tax calculation globally in some scenarios. To do so, call the `disable` method on the `Tax` facade.

```php
use Bazar\Support\Facades\Tax;
use Cone\Bazar\Support\Facades\Tax;

Tax::disable();
```
Expand All @@ -423,15 +417,15 @@ Discounts are stored on the `Item` and the `Shipping` models. Both implement the
You may register discounts using the `Discount` facade. You can pass a number, a `Closure`, or a class (that implements the `Bazar\Contracts\Discount` interface) along with the name of the discount.

```php
use Bazar\Support\Facades\Discount;
use Cone\Bazar\Support\Facades\Discount;

// Fix discount
Discount::register('fix-20', 20);
```

```php
// Custom closure discount
use Bazar\Support\Facades\Discount;
use Cone\Bazar\Support\Facades\Discount;

Discount::register('custom-percent', function (Discountable $model) {
return $model->getTotal() * 0.3;
Expand All @@ -440,9 +434,9 @@ Discount::register('custom-percent', function (Discountable $model) {

```php
// Class discount
use Bazar\Contracts\Discount as Contract;
use Bazar\Contracts\Discountable;
use Bazar\Support\Facades\Discount;
use Cone\Bazar\Contracts\Discount as Contract;
use Cone\Bazar\Contracts\Discountable;
use Cone\Bazar\Support\Facades\Discount;

class CustomDiscount implements Contract
{
Expand All @@ -462,7 +456,7 @@ Discount::register('complex-discount', new CustomDiscount);
You may remove registered discounts using the `Discount` facade.

```php
use Bazar\Support\Facades\Discount;
use Cone\Bazar\Support\Facades\Discount;

Discount::remove('complex-discount');
```
Expand All @@ -472,7 +466,7 @@ Discount::remove('complex-discount');
You may disable discount calculation globally in some scenarios. To do so, call the `disable` method on the `Discount` facade.

```php
use Bazar\Support\Facades\Discount;
use Cone\Bazar\Support\Facades\Discount;

Discount::disable();
```
Expand Down
5 changes: 3 additions & 2 deletions core-concepts.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ After you created your customizable model, you can swap the container binding fo
use App\Models\Product;
use Cone\Bazar\Contracts\Models\Product as Contract;

public function register()
public function register(): void
{
$this->app->bind(Contract::class, Product::class);
}
Expand All @@ -53,6 +53,7 @@ Proxies are representing the classes that are currently bound to the container.

```php
use Cone\Bazar\Models\Product;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

// Available proxy methods
Product::proxy();
Expand All @@ -63,7 +64,7 @@ Product::getProxiedContract();
Product::proxy()->newQuery()->where(...);

// Dynamic usage of bound classes
public function product()
public function product(): BelongsTo
{
$this->belongsTo(Product::getProxiedClass());
}
Expand Down
2 changes: 1 addition & 1 deletion dashboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class RootServiceProvider extends RootApplicationServiceProvider
protected function widgets(): array
{
return [
PostCount::make(),
new PostCount,
];
}
}
Expand Down
45 changes: 42 additions & 3 deletions fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function fields(Request $request): array
}
```

### Configuration
## Configuration

### Value Resolution

Expand Down Expand Up @@ -86,7 +86,7 @@ Number::make('Price')
});
```

## Authorization
### Authorization

You may allow/disallow interaction with fields. To do so, you can call the `authorize` method on the field instance:

Expand Down Expand Up @@ -526,6 +526,18 @@ Relation fields are representing Eloquent relation definitions on the resource m

#### Searchable & Sortable Columns

When using the `searchable` and `sortable` methods on the relation fields the target columns should be defined.

```php
$field->searchable(columns: [
'id', 'name',
]);
```

```php
$field->sortable(column: 'name');
```

#### Customizing the Query

You may customize the relatable model's query. This is possible defining global scopes per field type or locally on the field definition.
Expand Down Expand Up @@ -582,7 +594,7 @@ $field->aggregate('count', 'id');
$field->aggregate('sum', 'tax');
```

> The available aggregate functions: `count`, `min`, `max`, `sum`, `avg`.
> The available aggregate functions: `count`, `min`, `max`, `sum` and `avg`.
#### Grouping

Expand All @@ -602,6 +614,14 @@ $field->groupOptionsBy(static function (Model $model): string {

#### Subresources

When relation fields need more robust management you might convert it as a subresource. That means, instead of rendering a `<select>` on the resource form, the relation field gets its own CRUD interfaces as it would be a resource.

```php
$field->asSubResource();
```

Subresources appear on the parent resource models's show route.

### BelongsTo

The `BelongsTo` field is typically a handler for an existing `Illuminate\Database\Eloquent\Relations\BelongsTo` relation:
Expand All @@ -616,6 +636,25 @@ $field = BelongsTo::make(__('Author'), 'author');

The `BelongsToMany` field is typically a handler for a `Illuminate\Database\Eloquent\Relations\BelongsToMany` relation:

```php
$field = BelongsToMany::make(__('Teams'), 'teams');
```

When using as subresource, you may define editable pivot fields:

> Only existing models can be attached, creating relatable models from the subresource is not supported.
```php
$field->withPivotFields(static function (Request $request): array {
return [
Select::make('Role', 'role')->options([
'admin' => 'Admin',
'member' => 'Member',
]),
];
});
```

### HasMany

The `HasMany` field is typically a handler for a `Illuminate\Database\Eloquent\Relations\HasMany` relation:
Expand Down
Loading

0 comments on commit 3ef12c6

Please sign in to comment.