2017 © Pedro Pelรกez
 

library manipulator

Utilities for manipulating PHP objects

image

nayjest/manipulator

Utilities for manipulating PHP objects

  • Tuesday, September 27, 2016
  • by nayjest
  • Repository
  • 2 Watchers
  • 8 Stars
  • 64,575 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 1 Forks
  • 3 Open issues
  • 13 Versions
  • 8 % Grown

The README.md

Manipulator (mp)

Small library for manipulating PHP objects., (*1)

Build Status Code Coverage Latest Stable Version, (*2)

SensioLabsInsight, (*3)

It's like symfony/property-access with more features, faster (no reflection usage) and without over-engineering (~300 lines of code, few functions)., (*4)

Requirements

  • PHP 5.4+ (hhvm & php7 are supported)

Installation

The recommended way of installing the component is through Composer., (*5)

Run following command:, (*6)

composer require nayjest/manipulator

Usage

Function mp\instantiate

Creates class instance using specified constructor arguments., (*7)

Arguments
  • string $class โ€” Target class name
  • array $arguments โ€” Constructor arguments (optional)
Returned Value

Function returns instantiated object, (*8)

Example
    $user = \mp\instantiate(MyApp\User::class, [$firstArgument, $secondArgument]);

Function mp\setPublicProperties

Assigns values from array to existing public properties of target object., (*9)

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., (*10)

Arguments
  • object $targetObject โ€” target object
  • array $fields โ€” fields to assign, keys must be same as target object property names
  • bool $createProperties โ€” (optional, default value: false) allows to create new properties in target object if value is TRUE
Returned Value

Function returns array containing names of successfully assigned properties., (*11)

Function mp\setValuesUsingSetters

Assigns values from array to corresponding properties of target object using setters., (*12)

This function works similar to mp\setPublicProperties(), but uses setter methods instead of public properties., (*13)

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., (*14)

Fields having no corresponding setters in target object will be ignored., (*15)

This function does not work with magic setters created using __set() php method., (*16)

Arguments
  • object $instance โ€” target object
  • array $fields โ€” fields to assign, keys are used to check availability of corresponding setters in target object
Returned Value

Function returns array containing names of successfully assigned properties., (*17)

Example
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\setValues

Assigns values from $fields array to $target. Target may be object or array., (*18)

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., (*19)

Assigning values using setters can be disabled by removing MP_USE_SETTERS option (it's enabled by default)., (*20)

When target is an array, mp\setValues will call array_merge PHP function., (*21)

Arguments
  • object|array &$target โ€” target object or array
  • array $fields โ€” fields to assign
  • int $options (optional, default value: MP_USE_SETTERS) supported options: MP_USE_SETTERS, MP_CREATE_PROPERTIES
Returned Value

Function returns array containing names of successfully assigned properties., (*22)

Example
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:, (*23)

# 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\getWritable

Returns names of writable properties for objects and classes or existing keys for arrays., (*24)

Only public object properties and properties having setters considered writable., (*25)

For setters, this function will return property names based on setter names (setter names are converted to snake case, 'set' prefixes are removed)., (*26)

Detecting properties by setters can be disabled by specifying second argument as FALSE., (*27)

Arguments
  • object|string|array $target โ€” object or class name or array
  • bool $useSetters โ€” (optional, default value: true) if true, properties having setters will be added to results
Returned Value

Array containing names of writable properties., (*28)

Function mp\getMethodsPrefixedBy

Returns method names from target object/class that starts from specified keyword and followed by uppercase character., (*29)

Arguments
  • string $keyword โ€” method name prefix
  • object|string $target โ€” object or class name
Returned Value

Array containing method names., (*30)

Example
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\getSetters

Returns method names from target object/class that looks like setters., (*31)

Arguments
  • object|string $target โ€” object or class name
Returned Value

Array containing method names., (*32)

Function mp\getGetters

Returns method names from target object/class that looks like setters., (*33)

Arguments
  • object|string $target โ€” object or class name
Returned Value

Array containing method names., (*34)

Function mp\getValues

Returns values of object properties or array elements specified in $propertyNames argument., (*35)

This function supports getters, i. e. value returned by getSomeValue() method of target object can be requested as 'some_value' property., (*36)

Arguments
  • object|array $src
  • string[] $propertyNames
Returned Value

Array containing required values., (*37)

Function mp\getValue

Extracts value specified by property / field / method name from object or array. This function supports property paths (prop1.prop2.prop3) and getters., (*38)

  • For $propertyName = 'prop_name', this function will try to extract data in following order from:, (*39)

  • $src['prop_name'], (*40)

  • $src->prop_name
  • $src->getPropName()
  • $src->prop_name()
  • $src->isPropName()
Arguments
  • array|object $src
  • string $propertyName
  • mixed $default โ€” (optional, default value: null) default value
  • string|null $delimiter โ€” (optional, default value: '.') used to specify property paths

Function mp\getValueByRef

Extracts 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., (*41)

Function mp\setValue

Assigns value, supports property paths (prop1.prop2.prop3)., (*42)

Arguments
  • array|object &$target
  • string $propertyName
  • mixed $value
  • string|null $delimiter โ€” (optional, default value: '.') used to specify property paths
Returned Value

This function returns TRUE if value was successfully assigned, FALSE otherwise, (*43)

Testing

This package bundled with PhpUnit tests., (*44)

Command for running tests:, (*45)

composer test

Contributing

Please see Contributing Guidelines and Code of Conduct for details., (*46)

License

ยฉ 2014 โ€” 2016 Vitalii Stepanenko, (*47)

Licensed under the MIT License., (*48)

Please see License File for more information., (*49)

The Versions

27/09 2016

dev-master

9999999-dev

Utilities for manipulating PHP objects

  Sources   Download

MIT

The Requires

 

The Development Requires

by Vitalii Stepanenko

27/09 2016

v3.1.1

3.1.1.0

Utilities for manipulating PHP objects

  Sources   Download

MIT

The Requires

 

The Development Requires

by Vitalii Stepanenko

27/07 2016

dev-dev

dev-dev

Utilities for manipulating PHP objects

  Sources   Download

MIT

The Requires

 

The Development Requires

by Vitalii Stepanenko

14/03 2016

v3.1.0

3.1.0.0

Utilities for manipulating PHP objects

  Sources   Download

MIT

The Requires

 

The Development Requires

by Vitalii Stepanenko

12/01 2016

v3.0.1

3.0.1.0

Utilities for manipulating PHP objects

  Sources   Download

MIT

The Requires

 

The Development Requires

by Vitalii Stepanenko

11/11 2015

v3.0.0

3.0.0.0

Utilities for manipulating PHP objects

  Sources   Download

MIT

The Requires

 

The Development Requires

by Vitalii Stepanenko

10/11 2015

v2.0.0

2.0.0.0

Utilities for manipulating PHP objects

  Sources   Download

MIT

The Requires

 

The Development Requires

by Vitalii Stepanenko

10/11 2015

v2.0.0-rc.1

2.0.0.0-RC1

Utilities for manipulating PHP objects

  Sources   Download

MIT

The Requires

 

The Development Requires

by Vitalii Stepanenko

23/10 2015

v1.2.1

1.2.1.0

Utilities for manipulating PHP objects

  Sources   Download

MIT

The Requires

 

The Development Requires

by Vitalii Stepanenko

09/09 2015

v1.2.0

1.2.0.0

Utilities for manipulating PHP objects

  Sources   Download

MIT

The Requires

 

The Development Requires

by Vitalii Stepanenko

04/06 2015

v1.1.1

1.1.1.0

Utilities for manipulating PHP objects

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Vitalii Stepanenko

05/05 2015

v1.1.0

1.1.0.0

Utilities for manipulating PHP objects

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Vitalii Stepanenko

22/04 2015

v1.0

1.0.0.0

Utilities for manipulating PHP objects

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Vitalii Stepanenko