These files are free software
see WARRANTY DISCLAIMER and COPYRIGHT
in top of each file
<?php
function decode_pgarray($pg_array,$fields =
array(),&$i = 0)
{
if (is_null($pg_array))
return NULL;
$field_count = 0;
$in_quotes = false;
$in_fields = false;
$is_string = false;
$return_from_child = false;
if (
strlen($pg_array) == 0
|| $pg_array{0} != "
{"
|| $pg_array{(
strlen($pg_array) - 1)} != "
}"
|| $pg_array == "
{}"
)
{
$is_string = true;
$fields = "
";
}
for ($i; $i <
strlen($pg_array) ; $i++)
{
$tmp =
substr($pg_array,$i,1);
if (!
isset($fields[$field_count])
&& $in_quotes == false
&& $tmp !="
}"
&& !$is_string
)
$fields[$field_count] = "
";
if ( $tmp == "
{"
&& !$in_fields
&& !$in_quotes
&& !$is_string)
$in_fields = 1;
elseif ($tmp === '
,' && !$in_quotes)
if(!$return_from_child)
$field_count++;
else
$return_from_child = false;
elseif ($in_fields
&& $tmp == '
}'
&& !$in_quotes)
return $fields;
elseif($tmp == '
{'
&& !$in_quotes
&& !$is_string)
{
$value = decode_pgarray($pg_array,$fields[$field_count],$i);
$return_from_child = 1;
$fields[$field_count] = $value;
$field_count ++;
}
elseif ( $tmp == '
"'
&& !$in_quotes
&& $is_string == false
&&
strlen($fields[$field_count]) == 0)
$in_quotes = true;
elseif ($tmp == '
"'
&& $in_quotes)
$in_quotes = false;
elseif ($tmp == "
\\")
if ($is_string)
$fields .= $tmp;
else
$fields[$field_count] .=
substr($pg_array,++$i,1);
else
if ($is_string)
$fields .= $tmp;
else
$fields[$field_count] .= $tmp;
}
if (
is_string($fields))
return $fields;
else
throw new pgarray_exception('
unattended error while scanning array',12);
}
function encode_pgarray($array,$type = "
STRING")
{
if (!
is_array($array))
{
throw new pgarray_exception(
sprintf("
value supplied is not an array\n"),1);
return false;
}
array_dims($array,$result);
while(array_key_exists('
dims',$result))
{
$max_keys[] = $result['
max'];
$result = $result['
dims'];
}
fill_empty_pg_array_keys($array,$max_keys);
$result = serialize_pg_array($array,$type);
return sprintf("
'%s'",$result);
}
function array_dims(&$array,&$result,$max=0)
{
if (
is_array($array))
$max =
max(
max(
array_keys($array)),$max);
$result['
max'] = $max;
if (
is_array($array))
{
$result['
dims'] =
array();
$max = 0;
foreach($array as $slice)
if(
is_array($slice))
{
$result['
dims']['
max'] = 0;
array_dims($slice,$result['
dims'],$max);
$max =
max($result['
dims']['
max'],$max);
}
}
}
function fill_empty_pg_array_keys(&$array,$max_keys,$level=0)
{
$max_key = $max_keys[$level];
if(!
is_array($array))
throw new pgarray_exception(
"
cannot mix types 'arrays' and other types in an array slice"
,1);
$keys =
array_keys($array);
foreach($keys as $key)
if (!
is_int($key))
throw new pgarray_exception(
sprintf("
key \"%s\" is not integer\n",$key),1);
if(!array_key_exists($level + 1,$max_keys))
{
for($i = 0; $i <= $max_key ; $i++)
if (!
isset($array[$i]))
$array[$i] = "
";
return true;
}
if(!
isset($array))
$array =
array("
");
for($i = 0; $i <= $max_key ; $i++)
{
if (!array_key_exists($i,$array))
$array[$i] =
array();
$slice = $array[$i];
$slicetype =
is_array($slice) ? true : false;
if (!$slicetype)
throw new pgarray_exception(
sprintf("
cannot mix types 'arrays' and other types in an array slice\n",$key),1);
$shift = ($i == 0) ? true : false;
fill_empty_pg_array_keys($array[$i],$max_keys,$level + 1);
}
}
function serialize_pg_array($array,$type)
{
$search=
array('
"',"
\\","
'");
$replace=
array('
\\"','
\\\\\\\\',"
''");
for($i = 0; $i <=
max(
array_keys($array)) ; $i++)
{
$content = $array[$i];
if (!
isset($result))
$result = "
";
else
$result .= "
,";
if (!
is_array($content))
switch($type)
{
case "
INTEGER":
if ($content == "
")
{
trigger_error("
encode_pgarray: not set integer value will be fill with 0",E_USER_NOTICE);
$result .= '
0';
}
else
if (!
is_int($content))
throw new pgarray_exception(
sprintf("
%s: value supplied is not an integer\n",
$content),1);
else
$result .= $content;
continue;
case "
FLOAT":
if ($content == "
")
{
trigger_error("
encode_pgarray: not set float value will be fill with 0",E_USER_NOTICE);
$result .= '
0';
}
else
if (!
is_float($content))
throw new pgarray_exception(
sprintf("
%s: value supplied is not a float\n",
$content),1);
else
$result .= $content;
continue;
default:
$result .=
sprintf('
"%s"',
str_replace($search,$replace,$content));
continue;
}
else
$result .=
sprintf("
%s",serialize_pg_array($content,$type));
}
return sprintf('
{%s}',$result);
}
class pgarray_exception extends Exception
{
public function __construct($message, $code = 0)
{
switch($code)
{
case 1:
$color='
red';
$level='
Warning';
$function='
encode_pgarray';
break;
case 2:
$color='
green';
$level='
Notice';
$function='
encode_pgarray';
break;
case 3:
$color='
blue';
$level='
Warning';
$function='
encode_pgarray';
break;
case 11:
$color='
red';
$level='
Warning';
$function='
decode_pgarray';
break;
case 12:
$color='
green';
$level='
Notice';
$function='
decode_pgarray';
break;
case 13:
$color='
blue';
$level='
Warning';
$function='
decode_pgarray';
break;
default:
$level ='
Warning';
$color='
red';
$function='
encode_pgarray / decode_pgarray';
break;
}
$this->message =
sprintf("
<div style='color: %s'>%s: function %s: %s</div>",$color,$level,$function,$message);
parent::__construct($this->message, $code);
}
public function __toString()
{
return "
{$this->message}\n";
}
}
?>