Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Accessible Laravel Package

delormejonathan/accessible

Accessible is a PHP library that uses docblock annotations to automate class behavior: generates getters/setters, validates setter arguments with Symfony Assert, initializes properties in constructors, and manages collections and associations with add/remove helpers.

View on GitHub
Deep Wiki
Context7

How to define the class constructor

Sometimes your classes __construct simply consist in taking some arguments and applying them directly as the values of the class' properties; eventually you could also give a fixed initial value to some properties. In these cases you may find useful the [@Construct](https://github.com/Construct), [@Initialize](https://github.com/Initialize) and [@InitializeObject](https://github.com/InitializeObject) annotations, which will make your class more concise and readable.

The [@Construct](https://github.com/Construct) annotation

The [@Construct](https://github.com/Construct) annotation defines which properties will be initialized by the constructor. Take this example:

use Accessible\Annotation\Construct;

/**
 * [@Construct](https://github.com/Construct)({"bar", "baz"})
 */
class Foo
{
  use Accessible\AutomatedBehaviorTrait;

  /**
   * [@Access](https://github.com/Access)({Access::GET})
   */
  private $bar;

  /**
   * [@Access](https://github.com/Access)({Access::GET})
   */
  private $baz;
}

If you want to create an instance of Foo, you will do it like this:

$foo = new Foo(42, "sandwich");
$foo->getBar(); // 42
$foo->getBaz(); // "sandwich"

The object will be initialized with $bar having the value 42 and $bar having the value "sandwich". Simple.

If constraints on properties are present, the value you give to the constructor will be checked.

class Foo
{
  // ...

  /**
   * [@Access](https://github.com/Access)({Access::GET})
   * [@Assert](https://github.com/Assert)("string")
   */
  private $bar;

  // ...
}

$foo = new Foo(42, "sandwich"); // Throws an \InvalidArgumentException as 42 is not a string

If no [@Construct](https://github.com/Construct) has been added, the constructor will be the equivalent of public function __construct() {}.

The [@Initialize](https://github.com/Initialize) and [@InitializeObject](https://github.com/InitializeObject) annotations

For some properties, you may want to fix a value when the object is instanciated. The [@Initialize](https://github.com/Initialize) and [@InitializeObject](https://github.com/InitializeObject) annotations are useful in this situation.

[@Initialize](https://github.com/Initialize)

Simply add these annotations to the concerned properties. Let's begin with [@Initialize](https://github.com/Initialize):

use Accessible\Annotation\Initialize;

class Foo
{
  use Accessible\AutomatedBehaviorTrait;

  /**
   * [@Initialize](https://github.com/Initialize)("baz")
   */
  private $bar;
}

$foo = new Foo();
$foo->getBar(); // "baz"

The advantage of using [@Initialize](https://github.com/Initialize) over the native way to set the default value of the property is that the constructor will check that the given value respects the property's constraints.

class Foo
{
  use Accessible\AutomatedBehaviorTrait;

  /**
   * [@Assert](https://github.com/Assert)\Type("string")
   */
  private $bar = true; // The value of this property will not be checked

  /**
   * [@Assert](https://github.com/Assert)\Type("string")
   * [@Initialize](https://github.com/Initialize)(true)
   */
  private $baz; // An \InvalidArgumentException will be thrown
}

[@InitializeObject](https://github.com/InitializeObject)

For the properties which initial value is an object, if this object has to be instancied with a simple new Something() without argument, you can use the [@InitializeObject](https://github.com/InitializeObject) annotation.

use Doctrine\Common\Collections\ArrayCollection;

class Foo
{
  use Accessible\AutomatedBehaviorTrait;

  /**
   * [@InitializeObject](https://github.com/InitializeObject)(ArrayCollection::class)
   */
  private $bar;
}

A new instance of ArrayCollection will be set to $bar when instanciating a Foo object.

Disable the constraints validation for [@Initialize](https://github.com/Initialize) and [@InitializeObject](https://github.com/InitializeObject)

As the values given to [@Initialize](https://github.com/Initialize) and [@InitializeObject](https://github.com/InitializeObject) are fixed, they should be validated only in development. To enable or disable their validation, use the Accessible\Configuration::setInitializeValuesValidationEnabled() method:

$debug = true;
Accessible\Configuration::setInitializeValuesValidationEnabled($debug);

Use a custom constructor

If you want to add a custom __construct method in your class but want to use the annotations described in this page, you will simply need to add a call to initializeProperties(). Here is an example:

/**
 * [@Construct](https://github.com/Construct)({"bar"})
 */
class Foo
{
  use AutomatedBehaviorTrait;

  /** [@Access](https://github.com/Access)({Access::GET}) **/
  private $bar;

  /**
   * [@Access](https://github.com/Access)({Access::GET})
   * [@Initialize](https://github.com/Initialize)(42)
   */
  private $baz;

  public function __construct()
  {
    $this->initializeProperties(['blablabla']); // This will set $bar to 'blablabla' and $baz to 42
    // custom code
  }
}
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
roundly-consulting/kubernetes-api-for-laravel
roundly-consulting/plausible-for-laravel
gvp/gvp-laravel-template
metasyncsite/laravel-metasync-client
roundly-consulting/onboarding-for-laravel
andydefer/laravel-notification
adachsoft/cli-process-tester
jhaskell13/laraflow
sebastienheyd/boilerplate-media-manager
ccast/tagixo-primix
pretendtrue/laravel-builder
sawmainek/laravel-scout-tntsearch-driver
tomshaw/laravel-calendar-table
dibakar/laravel-ownership
steadrun/laravel-monitor
labyrinthe/connect-laravel
elmogy/layan
drewdan/sentinel-client
dskripchenko/laravel-llm
fatryst/nuonuo