Files
dolibarr/htdocs/includes/restler/Format/UrlEncodedFormat.php
jfefe b503b16f07 NEW : add restler framework
First step to build REST API into Dolibarr
2015-05-01 15:42:05 +02:00

59 lines
1.5 KiB
PHP

<?php
namespace Luracast\Restler\Format;
/**
* URL Encoded String Format
*
* @category Framework
* @package Restler
* @subpackage format
* @author R.Arul Kumaran <arul@luracast.com>
* @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/
* @version 3.0.0rc5
*/
class UrlEncodedFormat extends Format
{
const MIME = 'application/x-www-form-urlencoded';
const EXTENSION = 'post';
public function encode($data, $humanReadable = false)
{
return http_build_query(static::encoderTypeFix($data));
}
public function decode($data)
{
parse_str($data, $r);
return self::decoderTypeFix($r);
}
public static function encoderTypeFix(array $data)
{
foreach ($data as $k => $v) {
if (is_bool($v)) {
$data[$k] = $v = $v ? 'true' : 'false';
} elseif (is_array($v)) {
$data[$k] = $v = static::decoderTypeFix($v);
}
}
return $data;
}
public static function decoderTypeFix(array $data)
{
foreach ($data as $k => $v) {
if ($v === 'true' || $v === 'false') {
$data[$k] = $v = $v === 'true';
} elseif (is_array($v)) {
$data[$k] = $v = static::decoderTypeFix($v);
} elseif (empty($v) && $v != 0) {
unset($data[$k]);
}
}
return $data;
}
}