8000 Extending XmlFormat? · Issue #59 · Luracast/Restler · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Extending XmlFormat? #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
GaryJones opened this issue Jul 3, 2012 · 2 comments
Closed

Extending XmlFormat? #59

GaryJones opened this issue Jul 3, 2012 · 2 comments

Comments

@GaryJones
Copy link

For a method in a class, for which I want the XML root name and list of values that should be attributes to be changed, I know I can do:

<?php
/**
 * @class XmlFormat(root_name=foobar&attribute_names=xmlns,id)
 */

...before the method. However, I'm potentially going to have hundreds of methods across various classes, and this duplication seems somewhat redundant as I'll want the same root name across all of them, and same attribute sets across most of them.

What I'd like to do (but I couldn't get working), is to create my own format, that extends XmlFormat, but sets new default values for properties. Something like:

<?php
require 'restler/xmlformat.php';

class MyXmlFormat extends XmlFormat {
    public static $root_name = 'foobar';
    public static $attribute_names = array('xmlns', 'id');
}

or

<?php
require 'restler/xmlformat.php';

class MyXmlFormat extends XmlFormat {
    public __construct() {
        self::$root_name = 'foobar';
        self::$attribute_names = array('xmlns', 'id');
    }
}

...with

<?php
$r->setSupportedFormats('JsonFormat', 'MyXmlFormat');

in the index.php.

Is such a thing possible / am I heading along the right lines? Some use of var_dump() showed that a MyXmlFormat object was being used, but the static properties being returned via self::$... were not picking up the new values.

@ghost ghost assigned Luracast Jul 4, 2012
@Luracast
Copy link
Collaborator
Luracast commented Jul 4, 2012

There is a much simpler way to achieve that. :)

You can use the following in index.php or a config php file that you will include in index.php

<?php
XmlFormat::$root_name = 'result';
XmlFormat::$attribute_names = array (
        'bmi',
        'message',
        'height',
        'weight' 
);

which will result with the following xml for the Multi-fomat example

<result bmi="31.77" message="Obesity">
    <metric height="162.6 centimeter" weight="84 kilograms"/>
    <imperial height="5 feet 4 inches" weight="185.19 pounds"/>
</result>

If you still prefer creating your own subclass you can write it as shown below

<?php
class MyXmlFormat extends XmlFormat {

    public function __construct() {
        parent::$root_name = 'result';
        parent::$attribute_names = array (
                'bmi',
                'message',
                'height',
                'weight' 
        );
    }
}

We found a bug with the auto-loader that comes with restler.php that will make it difficult to create a subclass of a format class (you need to manually include the class at the moment). We will release an update to fix this and then close this issue :)

Luracast pushed a commit that referenced this issue Jul 4, 2012
…allowing us to subclass format classes with out manually including them
@Luracast Luracast closed this as completed Jul 4, 2012
@GaryJones
Copy link
Author

There is a much simpler way to achieve that. :)

Thought there should be - thanks for the pointers, and for Restler in general - it fits in exactly with the project I'm doing at the moment :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant
0