Skip to content

Commit

Permalink
polish docs
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Dec 18, 2024
1 parent b48f6cc commit d4182bc
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,7 @@ These default values are applied to both the **constructor arguments** and the
will first attempt to set a constructor argument called ``$title``. If that doesn't
exist, the `PropertyAccess <https://symfony.com/doc/current/components/property_access.html>`_
component will be used to call the ``setTitle()`` method or directly set the public
``$title`` property. If all these methods fail, PHP Reflection will be used
to set the value of the property.
``$title`` property. More about this in the :ref:`instantiation and hydration <instantiation>` section.

.. tip::

Expand Down Expand Up @@ -631,15 +630,16 @@ You can override your factory's ``initialize()`` method to add default state/log

.. _instantiation:

Instantiation
~~~~~~~~~~~~~
Object Instantiation & Hydration
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

By default, objects are instantiated in the normal fashion, by using the object's constructor. Attributes
that match constructor arguments are used. Remaining attributes are set to the object using Symfony's
`PropertyAccess <https://symfony.com/doc/current/components/property_access.html>`_ component
that match constructor arguments are used. Remaining attributes are used in the hydration phase and set to the object
using Symfony's `PropertyAccess <https://symfony.com/doc/current/components/property_access.html>`_ component

Check failure on line 638 in docs/index.rst

View workflow job for this annotation

GitHub Actions / Lint Documentation

Please add 4 spaces for every indention.
(setters/public properties). Any extra attributes cause an exception to be thrown.

You can customize the instantiator in several ways:
You can customize the instantiator in several ways, so that Foundry will instantiate and hydrate your objects, using the
attributes provided:

::

Expand Down Expand Up @@ -670,20 +670,25 @@ You can customize the instantiator in several ways:
// use a "namedConstructor"
->instantiateWith(Instantiator::namedConstructor("methodName"))

// use a callable
// use a callable: it will be passed the attributes matching its parameters names,
// remaining attributes will be used in the hydration phase
->instantiateWith(Instantiator::use(function(string $title): object {
return new Post($title); // ... your own logic
return new Post($title); // ... your own instantiation logic
}))
;

If this does not suit your needs, the instantiator is just a callable. You can provide your own to have complete control
over instantiation and hydration phases:

::

// the instantiator is just a callable, you can provide your own
->instantiateWith(function(array $attributes, string $class): object {
return new Post(); // ... your own logic
})
;

.. warning::

The ``instantiateWith()`` method fully replaces the default instantiation
The ``instantiateWith(callable(...))`` method fully replaces the default instantiation
and object hydration system. Attributes defined in the ``defaults()`` method,
as well as any states defined with the ``with()`` method, **will not be
applied automatically**. However, they are available as arguments to the
Expand Down

0 comments on commit d4182bc

Please sign in to comment.