0

I know I can specify 'array' as the type for arrays, but what do I use when I want to pass in a dictionary type to a method using Restler 3?

The whole 'outer' block passed to the POST method is of course a dictionary type, but now I want one of the parameters to be a dictionary type as well.

Arul Kumaran
  • 983
  • 7
  • 23
Gargoyle
  • 9,590
  • 16
  • 80
  • 145

1 Answers1

0

Dictionary in Request

Define a dictionary as a class with public properties

class Object
{
    public $property = 'value';

    /** 
     * @var string {@required false} 
     */
    public $optional = 'value';

    //add more properties here
}

Let your api method use it as one of its parameters

class Api
{
    public function method(Object $obj, $name)
    {
        //use $obj->property here
    }
}

Dictionary in Response

You can use an associative array (array with string indexes) or an instance of stdClass for this purpose

return array {

    'name' => 'Gargoyle'
    'object' => array {
        'property' => 'value'
    }

};

or

$r = new stdClass();
$r->name = 'Gargoyle';
$r->object = new stdClass();
$r->object->property = 'value';
return $r;

Both result in an JSON object output

Arul Kumaran
  • 983
  • 7
  • 23
  • I know how to create an assoc array in PHP itself, but I am trying to figure out how to make it work with the restler REST api. – Gargoyle Jun 12 '14 at 15:47
  • I'm not sure I get what you want! Are you trying to pass a dictionary as one of the properties for an API Method and wondering how you will receive it? – Arul Kumaran Jun 12 '14 at 15:56
  • Correct. I'm not sure what to put after the @param in the method comment. – Gargoyle Jun 12 '14 at 22:22
  • I have updated the answer for you! If you need a practical example see http://restler3.luracast.com/examples/_016_forms/readme.html – Arul Kumaran Jun 13 '14 at 03:29
  • @Gargoyle is that the answer you are looking for? – Arul Kumaran Jun 18 '14 at 03:56
  • So it looks like that would work as long as I have a defined 'entity' that I'm passing in and can define something like the Address class directly. – Gargoyle Jun 20 '14 at 03:35
  • Yes, Restler instantiates the entity (Class) and pre populates the properties for you – Arul Kumaran Jun 20 '14 at 13:26
  • Is there a way to make the properties not required? When I pass class into a patch method, for example, I might have only filled out some of the fields. I don't want to make the caller have to explicitly set everything they aren't updating to NULL, for example. – Gargoyle Jul 27 '14 at 00:22
  • Updated the example to include a optional parameter – Arul Kumaran Jul 27 '14 at 14:39
  • 2
    I noticed if I do the @var comment on one line like you do, I get "Unsupported operand types in .../Routes.php on line 559". Making it the multiline format doesn't throw an exception. – Gargoyle Jul 27 '14 at 19:27
  • @Gargoyle Thanks for the comment about a single line var - I would have never figured that our – Indivision Dev Mar 16 '15 at 11:36