Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 401 and 402]

(no description)

File Size: 388 lines (19 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

Encoder:: (3 methods):
  decode()
  encode()
  decodeXml()


Class: Encoder  - X-Ref

A helper class to easily convert between Value objects and php native values.

decode($xmlrpcVal, $options = array()   X-Ref
Takes an xml-rpc Value in object instance and translates it into native PHP types, recursively.
Works with xml-rpc Request objects as input, too.
Xmlrpc dateTime values will be converted to strings or DateTime objects depending on an $options parameter
Supports i8 and NIL xml-rpc values without the need for specific options.
Both xml-rpc arrays and structs are decoded into PHP arrays, with the exception described below:
Given proper options parameter, can rebuild generic php object instances (provided those have been encoded to
xml-rpc format using a corresponding option in php_xmlrpc_encode()).
PLEASE NOTE that rebuilding php objects involves calling their constructor function.
This means that the remote communication end can decide which php code will get executed on your server, leaving
the door possibly open to 'php-injection' style of attacks (provided you have some classes defined on your server
that might wreak havoc if instances are built outside an appropriate context).
Make sure you trust the remote server/client before enabling this!

param: Value|Request $xmlrpcVal
param: array $options accepted elements:
return: mixed
author: Dan Libby

encode($phpVal, $options = array()   X-Ref
Takes native php types and encodes them into xml-rpc Value objects, recursively.
PHP strings, integers, floats and booleans have a straightforward encoding - note that integers will _not_ be
converted to xml-rpc <i8> elements, even if they exceed the 32-bit range.
PHP arrays will be encoded to either xml-rpc structs or arrays, depending on whether they are hashes
or plain 0..N integer indexed.
PHP objects will be encoded into xml-rpc structs, except if they implement DateTimeInterface, in which case they
will be encoded as dateTime values.
PhpXmlRpc\Value objects will not be double-encoded - which makes it possible to pass in a pre-created base64 Value
as part of a php array.
If given a proper $options parameter, php object instances will be encoded into 'special' xml-rpc values, that can
later be decoded into php object instances by calling php_xmlrpc_decode() with a corresponding option.
PHP resource and NULL variables will be converted into uninitialized Value objects (which will lead to invalid
xml-rpc when later serialized); to support encoding of the latter use the appropriate $options parameter.

param: mixed $phpVal the value to be converted into an xml-rpc value object
param: array $options can include:
return: Value
author: Dan Libby

decodeXml($xmlVal, $options = array()   X-Ref
Convert the xml representation of a method response, method request or single
xml-rpc value into the appropriate object (a.k.a. deserialize).

param: string $xmlVal
param: array $options unused atm
return: Value|Request|Response|false false on error, or an instance of either Value, Request or Response