PHPackages                             nayjest/manipulator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. nayjest/manipulator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

nayjest/manipulator
===================

Utilities for manipulating PHP objects

v3.1.1(9y ago)10307.9k↓51.9%1[3 issues](https://github.com/Nayjest/manipulator/issues)4MITPHPPHP &gt;=5.4

Since Apr 22Pushed 9y ago2 watchersCompare

[ Source](https://github.com/Nayjest/manipulator)[ Packagist](https://packagist.org/packages/nayjest/manipulator)[ RSS](/packages/nayjest-manipulator/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (2)Versions (13)Used By (4)

Manipulator (mp)
================

[](#manipulator-mp)

Small library for manipulating PHP objects.

[![Build Status](https://camo.githubusercontent.com/9271be8690df20abc326dc17b5a8826fe9c06ccfc90df494ff9e6dfa24b2d03a/68747470733a2f2f7472617669732d63692e6f72672f4e61796a6573742f4275696c6465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Nayjest/Builder)[![Code Coverage](https://camo.githubusercontent.com/202970f3c4b5300dce89fc096d818b525694ba788770f4d47d51b22d8b615f32/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4e61796a6573742f6d616e6970756c61746f722f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Nayjest/manipulator/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/92bd92c3e2964ef4a700c7eb15fd867134fe970c074ab4f53b44ca3baa21a050/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6e61796a6573742f6d616e6970756c61746f722e737667)](https://packagist.org/packages/nayjest/manipulator)

[![SensioLabsInsight](https://camo.githubusercontent.com/43f78bff673527b7cb2a77897447ada0614b62df7df6b107dd84a5f718675a30/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f34633462336161342d653336362d343536652d383036352d3637303333643261383038302f6269672e706e67)](https://insight.sensiolabs.com/projects/4c4b3aa4-e366-456e-8065-67033d2a8080)

It's like [symfony/property-access](http://symfony.com/doc/current/components/property_access/index.html)with more features, faster (no reflection usage) and without over-engineering (~300 lines of code, few functions).

Requirements
------------

[](#requirements)

- PHP 5.4+ (hhvm &amp; php7 are supported)

Installation
------------

[](#installation)

The recommended way of installing the component is through [Composer](https://getcomposer.org).

Run following command:

```
composer require nayjest/manipulator
```

Usage
-----

[](#usage)

### `Function mp\instantiate`

[](#function-mpinstantiate)

Creates class instance using specified constructor arguments.

##### Arguments

[](#arguments)

- string $class — Target class name
- array $arguments — Constructor arguments (optional)

##### Returned Value

[](#returned-value)

Function returns instantiated object

##### Example

[](#example)

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

### `Function mp\setPublicProperties`

[](#function-mpsetpublicproperties)

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

##### Arguments

[](#arguments-1)

- 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

[](#returned-value-1)

Function returns array containing names of successfully assigned properties.

### `Function mp\setValuesUsingSetters`

[](#function-mpsetvaluesusingsetters)

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

##### Arguments

[](#arguments-2)

- object $instance — target object
- array $fields — fields to assign, keys are used to check availability of corresponding setters in target object

##### Returned Value

[](#returned-value-2)

Function returns array containing names of successfully assigned properties.

##### Example

[](#example-1)

```
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`

[](#function-mpsetvalues)

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

##### Arguments

[](#arguments-3)

- object|array &amp;$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

[](#returned-value-3)

Function returns array containing names of successfully assigned properties.

##### Example

[](#example-2)

```
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:

\#OptionsAssigned properties1not specified (MP\_USE\_SETTERS by default)property1, property22MP\_USE\_SETTERS | MP\_CREATE\_PROPERTIESproperty1, property2, property3 (created)3MP\_CREATE\_PROPERTIES \\property2, property3 (created)40property2### `Function mp\getWritable`

[](#function-mpgetwritable)

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

##### Arguments

[](#arguments-4)

- 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

[](#returned-value-4)

Array containing names of writable properties.

### `Function mp\getMethodsPrefixedBy`

[](#function-mpgetmethodsprefixedby)

Returns method names from target object/class that starts from specified keyword and followed by uppercase character.

##### Arguments

[](#arguments-5)

- string $keyword — method name prefix
- object|string $target — object or class name

##### Returned Value

[](#returned-value-5)

Array containing method names.

##### Example

[](#example-3)

```
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`

[](#function-mpgetsetters)

Returns method names from target object/class that looks like setters.

##### Arguments

[](#arguments-6)

- object|string $target — object or class name

##### Returned Value

[](#returned-value-6)

Array containing method names.

### `Function mp\getGetters`

[](#function-mpgetgetters)

Returns method names from target object/class that looks like setters.

##### Arguments

[](#arguments-7)

- object|string $target — object or class name

##### Returned Value

[](#returned-value-7)

Array containing method names.

### `Function mp\getValues`

[](#function-mpgetvalues)

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

##### Arguments

[](#arguments-8)

- object|array $src
- string\[\] $propertyNames

##### Returned Value

[](#returned-value-8)

Array containing required values.

### `Function mp\getValue`

[](#function-mpgetvalue)

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

- For $propertyName = 'prop\_name', this function will try to extract data in following order from:

- `$src['prop_name']`
- `$src->prop_name`
- `$src->getPropName()`
- `$src->prop_name()`
- `$src->isPropName()`

##### Arguments

[](#arguments-9)

- 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`

[](#function-mpgetvaluebyref)

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.

### `Function mp\setValue`

[](#function-mpsetvalue)

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

##### Arguments

[](#arguments-10)

- array|object &amp;$target
- string $propertyName
- mixed $value
- string|null $delimiter — (optional, default value: '.') used to specify property paths

##### Returned Value

[](#returned-value-9)

This function returns TRUE if value was successfully assigned, FALSE otherwise

Testing
-------

[](#testing)

This package bundled with PhpUnit tests.

Command for running tests:

```
composer test
```

Contributing
------------

[](#contributing)

Please see [Contributing Guidelines](contributing.md) and [Code of Conduct](code_of_conduct.md) for details.

License
-------

[](#license)

© 2014 — 2016 Vitalii Stepanenko

Licensed under the MIT License.

Please see [License File](LICENSE) for more information.

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance14

Infrequent updates — may be unmaintained

Popularity41

Moderate usage in the ecosystem

Community18

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 97% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~52 days

Recently: every ~80 days

Total

11

Last Release

3566d ago

Major Versions

v1.2.1 → v2.0.0-rc.12015-11-10

v2.0.0 → v3.0.02015-11-11

### Community

Maintainers

![](https://www.gravatar.com/avatar/f774898eb3341eff33d07e1b05bccfb69d084831b7ac151d3079e322d0ff6155?d=identicon)[nayjest](/maintainers/nayjest)

---

Top Contributors

[![Nayjest](https://avatars.githubusercontent.com/u/153999?v=4)](https://github.com/Nayjest "Nayjest (65 commits)")[![globotech-acc](https://avatars.githubusercontent.com/u/10062496?v=4)](https://github.com/globotech-acc "globotech-acc (1 commits)")[![hexusdev](https://avatars.githubusercontent.com/u/8880373?v=4)](https://github.com/hexusdev "hexusdev (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/nayjest-manipulator/health.svg)

```
[![Health](https://phpackages.com/badges/nayjest-manipulator/health.svg)](https://phpackages.com/packages/nayjest-manipulator)
```

###  Alternatives

[am-impact/amnav

Navigation Plugin for Craft

16726.5k](/packages/am-impact-amnav)[jxlwqq/quill

quill editor for laravel-admin

2428.6k](/packages/jxlwqq-quill)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
