, (*1)
PropertySetter
Sets properties of already existing objects., (*2)
Usage
Different setters
PropertySetter
Describes how to use any property setter., (*3)
use lukaszmakuch\PropertySetter\PropertySetter;
/* @var $propetySetter PropertySetter */
$propetySetter->setPropertiesOf($someObject);
SimplePropertySetter
Injects values obtained from a value source to objects described by a target specifier using some property setting strategy., (*4)
use lukaszmakuch\PropertySetter\SimplePropertySetter;
use lukaszmakuch\PropertySetter\SettingStrategy\CallSetterMethod;
use lukaszmakuch\PropertySetter\TargetSpecifier\PickByClass;
use lukaszmakuch\PropertySetter\ValueSource\UseDirectly;
$setter = new SimplePropertySetter(
new PickByClass(SomeClass::class),
new CallSetterMethod("setParam"),
new UseDirectly("param value")
);
SilentPropertySetter
It's a decorator that prevents the decorated setter from throwing the UnsupportedTarget exception if some object is not supported., (*5)
use lukaszmakuch\PropertySetter\SilentPropertySetter;
use lukaszmakuch\PropertySetter\PropertySetter;
/* @var $actualSetter PropertySetter */
$silentSetter = new SilentPropertySetter($actualSetter);
SimpleChainOfPropertySetters
It tries to set properties of an object using all of its setters. It doesn't prevent any exceptions., (*6)
use lukaszmakuch\PropertySetter\SimpleChainOfPropertySetters;
use lukaszmakuch\PropertySetter\PropertySetter;
/* @var $firstSetter PropertySetter */
/* @var $anotherSetter PropertySetter */
$chain = (new SimpleChainOfPropertySetters())
->add($firstSetter)
->add($anotherSetter);
SilentChainOfPropertySetters
It's a decorator that ignores a situation when some setter doesn't support objects of some type. It prevents throwing the UnsupportedTarget exception. When one of its setter throws an exception, it keeps trying to use other setters., (*7)
use lukaszmakuch\PropertySetter\SilentChainOfPropertySetters;
use lukaszmakuch\PropertySetter\ChainOfPropertySetters;
/* @var $actualChain ChainOfPropertySetters */
$silentChain = new SilentChainOfPropertySetters($actualChain);
Setting strategies
CallSetterMethod
Calls a setter in order to set a property., (*8)
use lukaszmakuch\PropertySetter\SettingStrategy\CallSetterMethod;
$strategy = new CallSetterMethod("setParam"); //will call setParam
CallOnlyMethodAsSetter
Calls a setter in order to set a property., (*9)
use lukaszmakuch\PropertySetter\SettingStrategy\CallOnlyMethodAsSetter;
interface ObjectThatHasOnlyOnePublicMethod
{
public function setParam($newValue);
}
$strategy = new CallOnlyMethodAsSetter(ObjectThatHasOnlyOnePublicMethod::class); //will call setParam
Target specifiers
PickByClass
Selects targets by their classes., (*10)
use lukaszmakuch\PropertySetter\TargetSpecifier\PickByClass;
$specifier = new PickByClass(\DateTime::class); //will support \DateTime
Value Sources
UseDirectly
Simply holds some value without modyfing it before it's returned., (*11)
use lukaszmakuch\PropertySetter\ValueSource\UseDirectly;
$valueSource = new UseDirectly(123); //will return 123
Exceptions
UnableToSetProperty
\lukaszmakuch\PropertySetter\Exception\UnableToSetProperty, (*12)
It's the parent of any exception that may be thrown by the setPropertiesOf method.
When the setter method throws an exception, it's wrapped in this one (and becomes available by calling the getPrevious method)., (*13)
UnableToGetValue
\lukaszmakuch\PropertySetter\Exception\UnableToGetValue, (*14)
Thrown when it's impossible to get a value (from a value source). It inherits from the UnableToSetProperty exception., (*15)
UnableToGetValue
\lukaszmakuch\PropertySetter\Exception\UnsupportedTarget, (*16)
Thrown when trying to set properties of an object that is not supported. It inherits from the UnableToSetProperty exception., (*17)
Installation
Use composer to get the latest version:, (*18)
$ composer require lukaszmakuch/property-setter