From 593240b4207742099e20d84826c0ae9533f547b0 Mon Sep 17 00:00:00 2001 From: Thomas Keller Date: Sat, 28 Aug 2010 22:03:47 +0000 Subject: [PATCH] implement IDF_Scm_Monotone_BasicIO::compile() --- src/IDF/Scm/Monotone/BasicIO.php | 55 ++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/src/IDF/Scm/Monotone/BasicIO.php b/src/IDF/Scm/Monotone/BasicIO.php index a0a5fd7..0562c9b 100644 --- a/src/IDF/Scm/Monotone/BasicIO.php +++ b/src/IDF/Scm/Monotone/BasicIO.php @@ -26,7 +26,7 @@ * * @author Thomas Keller */ -class IDF_Scm_Monotone_BasicIO extends IDF_Scm +class IDF_Scm_Monotone_BasicIO { /** * Parses monotone's basic_io format @@ -54,6 +54,7 @@ class IDF_Scm_Monotone_BasicIO extends IDF_Scm } if ($in[$pos] == '[') { + unset($stanzaLine['values']); ++$pos; // opening square bracket $stanzaLine['hash'] = substr($in, $pos, 40); $pos += 40; @@ -61,6 +62,7 @@ class IDF_Scm_Monotone_BasicIO extends IDF_Scm } else { + unset($stanzaLine['hash']); $valCount = 0; while ($in[$pos] == '"') { ++$pos; // opening quote @@ -105,7 +107,56 @@ class IDF_Scm_Monotone_BasicIO extends IDF_Scm */ public static function compile($in) { - throw new IDF_Scm_Exception("not yet implemented"); + $out = ""; + $first = true; + foreach ((array)$in as $sx => $stanza) { + if ($first) + $first = false; + else + $out .= "\n"; + + $maxkeylength = 0; + foreach ((array)$stanza as $lx => $line) { + if (!array_key_exists('key', $line)) { + throw new IDF_Scm_Exception( + '"key" not found in basicio stanza '.$sx.', line '.$lx + ); + } + $maxkeylength = max($maxkeylength, strlen($line['key'])); + } + + foreach ((array)$stanza as $lx => $line) { + $out .= str_pad($line['key'], $maxkeylength, ' ', STR_PAD_LEFT); + + if (array_key_exists('hash', $line)) { + $out .= ' ['.$line['hash'].']'; + } else + if (array_key_exists('values', $line)) { + if (!is_array($line['values']) || count($line['values']) == 0) { + throw new IDF_Scm_Exception( + '"values" must be an array of a size >= 1 '. + 'in basicio stanza '.$sx.', line '.$lx + ); + } + foreach ($line['values'] as $value) { + $out .= ' "'.str_replace( + array("\\", "\""), + array("\\\\", "\\\""), + $value).'"'; + } + } + else + { + throw new IDF_Scm_Exception( + 'neither "hash" nor "values" found in basicio '. + 'stanza '.$sx.', line '.$lx + ); + } + + $out .= "\n"; + } + } + return $out; } }