Hi Woody,
Hi Ben,
Obviously this package is not suitable when you deal with single properties of concrete class instances.
It's useful for creating general-purpose code, i. e. code that will deal with any unknown objects or if you need mass-operations.
Few use-cases where manipulator can be handy:
Use-case 1: General purpose decorators, proxy objects, wrappers, data structures, etc
I will list few concrete examples to clarify what I mean:
* Decorator that will sanitize values received from or written to decorated object to protect from XSS, SQL Injections, etc.
* State monad implementation (JQuery-like) or collection that deals with properties of stored elements ( $collection->filterByProperty('role', 'Admin')->findByProperty('
company.name', 'eBay'), )
Use-case 2: Mass-operations
* Example 2.1: Configuring objects.
You have class with many setters and its configuration, you can just call mp\setValues($myObject, $config) to configure it. Or even instantiate and configure, $inst = mp\instantiate($config['class'], $config['constructorArguments']); mp\setValues($inst, $config);
* Example 2.2: Data Export / Import / Serialization / API's.
You need export data from object or you need to serialize object that can't be serialized using standard way because of closures & resources inside.
$values = mp\getValues($target, mp\getWritable($target));
// then filter and export
// or apply to object again
mp\setValues($anotherInstance, $values);
Use-case 3: You decided to implement your own var_dump or another kind of UI component that can display any object or even form to edit it.
It can be useful for those who already use symfony property-access. Manipulator will be faster because there is no Reflection usage and simplify code (IMHO: using factory to create property-access instance to read some property looks little bit overcomplicated for me). I want to note here that authors of symfony definitely had reasons to include property-access to symfony with their own use-cases.
In my work I used it for fast implementation of API's and in following packages:
-
collections -- to deal with properties of collection elements
-
builder -- for instantiating and initializing objects using configurations
-
data grids -- for extracting fields from data rows that format can vary becouse package supports Laravel and Symfony models / query builders and raw arrays as data sources (it's possible to specify field name '
blog_post.author.company.name' for column and it will work for any data source, symfony property access was not suitable there because it has different syntax for accessing array elements and properties)