Skip to main content

Class-Based Features

Instead of referencing feature keys as magic strings like 'exports' or 'projects', you can define feature classes for type safety, IDE autocompletion, and safe refactoring.

Creating a feature class

Implement Pixelworxio\Subscribd\Contracts\Feature:
<?php

namespace App\Features;

use Pixelworxio\Subscribd\Contracts\Feature;

class ExportsFeature implements Feature
{
    public function key(): string
    {
        return 'exports';
    }
}

Checking with a feature class

Pass the class or an instance directly to allows() and limit():
use App\Features\ExportsFeature;
use App\Features\ProjectsFeature;
use Pixelworxio\Subscribd\Facades\Entitlements;

Entitlements::for($user)->allows(ExportsFeature::class);      // bool
Entitlements::for($user)->allows(new ExportsFeature());       // same

Entitlements::for($user)->limit(ProjectsFeature::class);      // int|null

Feature class with a default limit

Define a default limit on the class itself. When the plan does not specify a value for the feature, this default is used as a fallback:
<?php

namespace App\Features;

use Pixelworxio\Subscribd\Contracts\Feature;

class ProjectsFeature implements Feature
{
    public function key(): string
    {
        return 'projects';
    }

    public function default(): int
    {
        return 1; // free users get 1 project if no plan feature is defined
    }
}

Organising feature classes

A simple approach is a App\Features\ namespace with one class per feature:
app/
└── Features/
    ├── ExportsFeature.php
    ├── ProjectsFeature.php
    ├── ApiAccessFeature.php
    └── SupportTierFeature.php

Next steps