I've been looking to a solution to my problem for a while without success so I'm asking here.
How can we return a json-encoded result on an array of objects (or just an object) containing private properties ?
Indeed, when you use json_encode($myObject), it won't display the private or protected properties, which are present everywhere in the model when using Symfony...
I'm surprised I couldn't find any method like json_encode that would call getters instead of properties themselves.
Any idea ?
EDIT
In that case I would rather do a unique function that looks like :
public function toArray() {
$vars = get_object_vars($this);
$result = array();
foreach ($vars as $key => $value) {
if (is_object($value)) {
$result[$key] = toArray($value);
} else {
$result[$key] = $value;
}
}
return $result;
}
in order to avoid rewriting every property name everytime...
But anyway I think I'll just create an array containing the vars I need, so that I won't touch the model (which is generated code).
Have you try GetSetMethodNormalizer ?
http://api.symfony.com/2.0/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.html
Ex. https://stackoverflow.com/a/6709828/520114
Right now there is no way for this. Only php serialize/unserialize handles the true serialisation of objects.
You'll have to implement them yourselve, or rather let objects return their json values themselves.
You will have to implement your own method toArray() where you expose all your private values in an array:
public function toArray()
{
return array(
'property1' => $this->myproperty1,
'property2' => $this->myproperty2
);
}
And call it like this:
json_encode(MyObject->toArray());
[Edit: this question is not about doctrine, but since you mention both symfony2 and the model, you can consider using Array Hydration for your model: http://www.doctrine-project.org/docs/orm/2.0/en/reference/dql-doctrine-query-language.html#array-hydration ]
Related
I have taken over a WordPress website and have found some of the PHP code a little over my head. The site has an API which calls JSON data from a 3rd party, decodes it into an array, performs some validation checks and then maps it to an array of key=>value pairs before inserting the data into into the database. I understand all the code up until it starts to perform the mapping excercise. The reason I am stuck is that it is using is_callable() and call_user_func_array() on what seems to be the iterator in the foreach loop. I can only find documentation on this function which states it is for testing if there is a function which is callable.
$mapping is the multi-dimension array of key=>value pairs from wp_postmeta we are interested in and $course represents an array of data from the API associated with a 3rd party course which is to be saved to the appropriate key=>value in wp_postmeta. $mapping is defined in the Class in which this function lives.
Can anyone please explain to me what is happening here. Many thanks.
private function make_mapped_value($mapping, $course) {
if(!is_array($mapping)) return $mapping;
$val = $course;
foreach($mapping as $step) {
if(is_callable($step) && !in_array($step, ['min', 'max'])) {
$val = call_user_func_array($step, array(&$val));
}
else {
$val = isset($val[$step]) ? $val[$step] : NULL;
}
}
return $val;
}
I have an array of objects [Article(), Article(), Article()]
Each Article() have plenty of protected properties.
I want to be able to identify which property is an instance of PersistentCollection in order to call ->initialize() method on it.
At the end, I want my new array of 3 objects Array() on which I called the initialize() method only if the property is an instance of PersistentCollection
Do you know an efficient way of doing it dynamically ?
Or is it too expansive in terms of performance and then I should call the methods on the fields I know it is a PersistentCollection ?
Thanks.
Assuming $articles is your initial array you want to process, something like this should do the trick
$closure = function() {
foreach($this as $fieldValue) {
if($fieldValue instanceof PersistentCollection) {
$fieldValue->initialize();
}
}
};
foreach ($articles as $article) {
$initCollections = Closure::bind($closure, $article, get_class($a));
$initCollections();
}
$initCollections = Closure::bind($closure, $a, get_class($a));
$initCollections();
Not sure about perfomance, but don't see any issue with it
_toString() is called when an object is used as string. How can I do something similar for numerical values, something like __toInt(), or __toArray(). Do such methods exist? Is there a work around? Is it a bad idea to use something like that even if there is a workaround for it?
There is no __toArray magic-method (just check the ones that exist here), but then, there shouldn't be, IMO.
Though people have asked for a magic toArray method, it doesn't look like such a method will be implemented any time soon.
Considering what objects are for, and how we use them, a toInt method wouldn't make much sense, and since all objects can be cast to an array, and can be iterated over, I see very little point in using __toArray anyway.
To "convert" on object to an array, you can use either one of the following methods:
$obj = new stdClass;
$obj->foo = 'bar';
var_dump((array) $obj);
//or
var_dump(json_decode(json_encode($obj), true));
This can be done with both custom objects, as stdClass instances alike.
As far as accessing them as an array, I can't see the point. Why write a slow magic method to be able to do something like:
$bar = 'foo';
$obj[$bar];
if you can do:
$obj->{$bar}
or if you can do:
foreach($obj as $property => $value){}
Or, if you need something a tad more specific, just implement any of the Traversable interfaces.
And for those rare cases, where you want an object to produce an array from specific properties in a very particular way, just write a method for that and call that method explicitly.
class ComplexObject
{
private $secret = null;
private $childObject = null;
public $foo = null;
//some methods, then:
public function toArray()
{//custom array representation of object
$data = array();
foreach($this->childObject as $property => $val)
{
if (!is_object($this->childObject->{$property}))
{
$data[$property] = $val;
}
}
$data['foo'] = $this->foo;
return $data;
}
//and even:
public function toJson()
{
return json_encode($this->toArray());
}
}
Ok, you have to call these methods yourself, explicitly, but that's not that hard, really... is it?
I want to call a static method from one of my objects. This is no problem if I know the name of the object, but in this case, I don't.
I have an array with several object names and I want to call the method from one of this arrays elements. So, it looks something like this:
function call_method($key) {
$tmp = array('key1' => 'objectname1','key2' => 'objectname2','key3' => 'objectname3');
if(array_key_exists($key, $tmp)) {
$tmp::static_method();
}
}
But, this solution is not working. Does somebody know how to get this working?
$tmp is an array, so it has no static methods associated with it. You need to look up the value and use that:
// this will work for newer versions of PHP
$klass = $tmp[$key];
$klass::static_method();
// in some older versions, you may need to use call_user_func:
call_user_func(array($klass, 'static_method') /*, your args here */);
Right now, you are trying to call the static function on that array. You should do :
if(array_key_exists($key, $tmp)) {
$tmp[$key]::static_method();
}
Thought you specified the names "object" in the array, I am assuming they are class names. Static functions cannot be called with instances.
I'm looking to create an array or list with elements of a certain type (eg objects the implement a certain interface). I know I can create an object that does the same thing implementing Traversable and Iterator, or override ArrayObject. But maybe there's another way I have missed.
Do you mean something like:
$array=Array();
foreach ($itemsToAdd as $item) {
if ($item instanceof NameOfwantedInterface) {
Array_push($array,$item);
}
}
If you don't, them I'm sorry - it's just that your question isn't too clear.
I would write a custom class that extended ArrayObject and threw an exception if you tried to assign a variable that wasn't the correct type, there's really no better way to do it that I can think of.
PHP as a lanugage is very flexible in terms of type handling and type conversion. You will probably have to put a manual check in if you want any kind of strong type checking, a simple if statement will do.
The array object is designed to be especially flexible (lazy key assignment, automatic increment, string or integer keys, etc.) so you should probably use a custom object of your own.
You could use type hinting:
<?php
interface Shape
{
function draw();
}
class MyArray
{
private $array = array();
function addValue(Shape $shape) //The hinting happens here
{
$array[] = $shape;
}
}
?>
This example is not perfect, but you'll get the idea.
Basically, you are going to want to do a function that checks if the variable you are inserting into the array is an object.
function add($var)
{
if(is_object($var))
{
$this->array[] = $var;
}
}
If you want to make sure it has a specific class name, you would do something like this (for PHP5):
function add(className $var)
{
$this->array[] = $var;
}
or this for previous PHP versions:
function add($var)
{
if($var instanceOf className)
{
$this->array[] = $var
}
}
You might want to look into array_filter() to do this without building an object.
Looking at that page, I've found that you can use array_filter with common functions like is_object. So doing something like this:
$this->array = array_filter($this->array ,'is_object');
Would filter the array to contain only objects.