Installation:
composer require antares/accessible
Register the Doctrine annotation loader (once in your project):
$loader = require __DIR__ . '/vendor/autoload.php';
Doctrine\Common\Annotations\AnnotationRegistry::registerLoader([$loader, 'loadClass']);
Basic Usage:
Add AutomatedBehaviorTrait to your model and annotate properties:
use Accessible\AutomatedBehaviorTrait;
class User {
use AutomatedBehaviorTrait;
/**
* @Access({Access::GET, Access::SET})
* @Assert\NotBlank
*/
private $name;
}
Now getName()/setName() work automatically with validation.
First Use Case: Replace repetitive getter/setter boilerplate for a single property:
$user = new User();
$user->setName('John'); // Validates via @Assert\NotBlank
echo $user->getName(); // "John"
Property Automation:
@Access annotations.Validation Integration:
Assert annotations (e.g., @Assert\Email) directly in docblocks./**
* @Access({Access::SET})
* @Assert\Email
*/
private $email;
Collections:
@ListBehavior, @SetBehavior, or @MapBehavior./**
* @Access({Access::GET})
* @ListBehavior
* @Initialize({})
*/
private $orders;
addOrder()/removeOrder() methods.Associations:
@Referenced/@InCollection.class Author {
/**
* @Access({Access::GET, Access::SET})
* @ListBehavior
* @Referenced(className=Book::class, propertyName="author")
*/
private $books;
}
Constructor Automation:
@Construct to auto-map constructor args to properties./**
* @Construct({"name", "email"})
*/
class User { ... }
$user = new User('Alice', 'alice@example.com');
Eloquent Models:
AutomatedBehaviorTrait alongside Model:
use Illuminate\Database\Eloquent\Model;
use Accessible\AutomatedBehaviorTrait;
class Post extends Model {
use AutomatedBehaviorTrait;
// Annotations here...
}
setAttribute).Form Requests:
class StorePostRequest extends FormRequest {
/**
* @Assert\NotBlank
* @Assert\MaxLength(255)
*/
public $title;
}
API Resources:
@Access({Access::GET}) to control serialization:
class PostResource extends JsonResource {
/**
* @Access({Access::GET})
*/
public $customField;
}
Service Layer:
class CreateUserCommand {
/**
* @Assert\Email
*/
public $email;
}
Annotation Loading:
bootstrap/app.php):
$loader = require __DIR__.'/vendor/autoload.php';
Doctrine\Common\Annotations\AnnotationRegistry::registerLoader([$loader, 'loadClass']);
Method Conflicts:
setAttribute) override auto-generated setters.@Access({Access::GET, Access::SET}) and avoid naming collisions.Validation Timing:
@Assert annotations run only during setX() or constructor initialization.$object->validateProperty('propertyName').Circular References:
@Referenced/@InCollection) can cause infinite loops.updatePropertyAssociation() with caution or disable auto-updates:
$this->updatePropertyAssociation('property', ['newValue' => $value], false);
PHP 7+ Compatibility:
Undefined class constant errors for Access::GET.@Access({\Accessible\Annotation\Access::GET, \Accessible\Annotation\Access::SET})
Enable Debug Mode:
Accessible\AutomatedBehaviorTrait::$debug = true to log generated methods.Inspect Generated Code:
get_class_methods($object) to verify auto-generated methods.Validation Errors:
ConstraintViolationList for detailed validation failures:
try {
$user->setEmail('invalid');
} catch (\InvalidArgumentException $e) {
$violations = $e->getConstraintViolations();
}
Custom Annotations:
Accessible\Annotation\Annotation to create domain-specific rules.Override Default Behavior:
Accessible\AutomatedBehaviorTrait to modify method generation:
class CustomBehaviorTrait extends AutomatedBehaviorTrait {
protected function generateGetter($property) {
// Custom logic
}
}
Integration with Laravel Events:
setX() to trigger events:
$user->setName('Bob');
event(new UserNameUpdated($user));
Dynamic Property Handling:
@Dynamic to generate methods for properties not defined in the class (advanced).Annotation Parsing:
config/cache.php):
'stores' => [
'file' => [
'path' => storage_path('framework/cache'),
'tag' => 'accessible_annotations',
],
],
Avoid Over-Annotation:
@Access to only necessary properties to reduce reflection overhead.Disable for Non-CRUD Classes:
AutomatedBehaviorTrait in read-only DTOs or value objects.Model Events:
saving/updated events.public function setNameAttribute($value) {
$this->attributes['name'] = $value;
// Trigger custom logic
}
API Resources:
@Access annotations to control serialization.Database Hydration:
$fillable or $guarded:
protected $fillable = ['name']; // Only allow mass assignment for annotated properties
How can I help you explore Laravel packages today?