lukaszmakuch/property-setter

Sets properties of already existing objects.

v0.2 2016-04-19 18:16 UTC

This package is not auto-updated.

Last update: 2024-05-17 17:37:14 UTC


README

travis

PropertySetter

Sets properties of already existing objects.

Usage

Different setters

PropertySetter

Describes how to use any property setter.

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.

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.

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.

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.

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.

use lukaszmakuch\PropertySetter\SettingStrategy\CallSetterMethod;

$strategy = new CallSetterMethod("setParam"); //will call setParam

CallOnlyMethodAsSetter

Calls a setter in order to set a property.

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.

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.

use lukaszmakuch\PropertySetter\ValueSource\UseDirectly;

$valueSource = new UseDirectly(123); //will return 123

Exceptions

UnableToSetProperty

\lukaszmakuch\PropertySetter\Exception\UnableToSetProperty

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).

UnableToGetValue

\lukaszmakuch\PropertySetter\Exception\UnableToGetValue

Thrown when it's impossible to get a value (from a value source). It inherits from the UnableToSetProperty exception.

UnableToGetValue

\lukaszmakuch\PropertySetter\Exception\UnsupportedTarget

Thrown when trying to set properties of an object that is not supported. It inherits from the UnableToSetProperty exception.

Installation

Use composer to get the latest version:

$ composer require lukaszmakuch/property-setter