nayjest/manipulator
Fast, lightweight PHP object manipulation helpers—like symfony/property-access but simpler (~300 LOC) and no reflection. Instantiate classes, set public properties, and assign values via setters from arrays (snake/camel case), with optional property creation.
Small library for manipulating PHP objects.
It's like symfony/property-access with more features, faster (no reflection usage) and without over-engineering (~300 lines of code, few functions).
The recommended way of installing the component is through Composer.
Run following command:
composer require nayjest/manipulator
Function mp\instantiateCreates class instance using specified constructor arguments.
Function returns instantiated object
$user = \mp\instantiate(MyApp\User::class, [$firstArgument, $secondArgument]);
Function mp\setPublicPropertiesAssigns values from array to existing public properties of target object.
By default this function ignores fields having no corresponding properties in target object, but this behavior can be changed if TRUE will be passed to third argument.
Function returns array containing names of successfully assigned properties.
Function mp\setValuesUsingSettersAssigns values from array to corresponding properties of target object using setters.
This function works similar to mp\setPublicProperties(), but uses setter methods instead of public properties.
Field names may be in snake or camel case, it will be converted to camel case and prefixed by 'set' to check availability of corresponding setter in target object.
Fields having no corresponding setters in target object will be ignored.
This function does not work with magic setters created using __set() php method.
Function returns array containing names of successfully assigned properties.
use mp;
class Target
{
private $somePropery;
public function setSomeProperty($value)
{
$this->someProperty = $value;
}
public function getSomeProperty()
{
return $this->someProperty;
}
}
$target = new Target;
$result = mp\setValuesUsingSetters($target, [
'some_property' => 1, // 'someProperty' => 1 will also work
'some_other_property' => 2
]);
# $target->setSomeProperty(1) will be called.
# Value of 'some_other_property' will be ignored since $target has no 'setSomeOtherProperty' setter.
echo $target->getSomeProperty(); // 1
var_dump($result); // array(0 => 'some_property')
Function mp\setValuesAssigns values from $fields array to $target. Target may be object or array.
By default mp\setValues ignores fields having no corresponding properties or setters in target object but this behavior can be changed if MP_CREATE_PROPERTIES option is used.
Assigning values using setters can be disabled by removing MP_USE_SETTERS option (it's enabled by default).
When target is an array, mp\setValues will call array_merge PHP function.
Function returns array containing names of successfully assigned properties.
use mp;
class Target
{
private $property1;
public $property2;
public function setProperty1($value)
{
$this->property1 = $value;
}
}
$target1 = new Target;
$target2 = new Target;
$target3 = new Target;
$target4 = new Target;
$fieldsToSet = [
'property1' => 1,
'property2' => 2,
'property3' => 3,
];
$result1 = mp\setValues($target1, $fieldsToSet); // MP_USE_SETTERS by default
$result2 = mp\setValues($target1, $fieldsToSet, MP_USE_SETTERS | MP_CREATE_PROPERTIES);
$result3 = mp\setValues($target1, $fieldsToSet, MP_CREATE_PROPERTIES);
$result4 = mp\setValues($target1, $fieldsToSet, 0);
Results:
| # | Options | Assigned properties |
|---|---|---|
| 1 | not specified (MP_USE_SETTERS by default) | property1, property2 |
| 2 | MP_USE_SETTERS | MP_CREATE_PROPERTIES | property1, property2, property3 (created) |
| 3 | MP_CREATE_PROPERTIES \ | property2, property3 (created) |
| 4 | 0 | property2 |
Function mp\getWritableReturns names of writable properties for objects and classes or existing keys for arrays.
Only public object properties and properties having setters considered writable.
For setters, this function will return property names based on setter names (setter names are converted to snake case, 'set' prefixes are removed).
Detecting properties by setters can be disabled by specifying second argument as FALSE.
Array containing names of writable properties.
Function mp\getMethodsPrefixedByReturns method names from target object/class that starts from specified keyword and followed by uppercase character.
Array containing method names.
class MyClass {
public function getProperty1(){};
public function getProperty2(){};
}
$objectMethodNames = \mp\getMethodsPrefixedBy('get', $obj); // will return methods of $obj that looks like getters
$classMethodNames = \mp\getMethodsPrefixedBy('get', 'MyClass'); // will return methods of 'MyClass' class that looks like getters.
// $classMethodNames will contain ['getProperty1', 'getProperty2']
Function mp\getSettersReturns method names from target object/class that looks like setters.
Array containing method names.
Function mp\getGettersReturns method names from target object/class that looks like setters.
Array containing method names.
Function mp\getValuesReturns values of object properties or array elements specified in $propertyNames argument.
This function supports getters, i. e. value returned by getSomeValue() method of target object can be requested as 'some_value' property.
Array containing required values.
Function mp\getValueExtracts value specified by property / field / method name from object or array. This function supports property paths (prop1.prop2.prop3) and getters.
$src['prop_name']$src->prop_name$src->getPropName()$src->prop_name()$src->isPropName()Function mp\getValueByRefExtracts value specified by property / field / method name from object or array by reference if possible.
This function acts like mp\getValue with only difference that value will be returned by reference if possible.
Function mp\setValueAssigns value, supports property paths (prop1.prop2.prop3).
This function returns TRUE if value was successfully assigned, FALSE otherwise
This package bundled with PhpUnit tests.
Command for running tests:
composer test
Please see Contributing Guidelines and Code of Conduct for details.
© 2014 — 2016 Vitalii Stepanenko
Licensed under the MIT License.
Please see License File for more information.
How can I help you explore Laravel packages today?