Skip to content

Instantly share code, notes, and snippets.

@petrowsky
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petrowsky/e30130675e50cd8c066d to your computer and use it in GitHub Desktop.
Save petrowsky/e30130675e50cd8c066d to your computer and use it in GitHub Desktop.
<?php
$theArray = array (
'Order_id' => 'mystore-12656',
'Currency' => 'USD',
'Time' => 'Fri Apr 6 10:46:57 2012 GMT',
'NumericTime' => '1390972125',
'IPAddress' => '127.0.0.1',
'Bill_First' => 'Tësting',
'Bill_Last' => 'Persøn',
'Bill_Company' => 'P&G',
'Bill_Address1' => '1234 Dürty Road',
'Bill_City' => 'Weiterstadt',
'Bill_State' => 'Stateville',
'Bill_Country' => 'DE Germany',
'Bill_Zip' => '92562',
'Bill_Phone' => '049615091854284',
'Bill_Email' => 'name@example.com',
'Items' => array (
'0' => array (
'Id' => 'product-id',
'Code' => 'MAG001',
'Quantity' => '1',
'Unit_Price' => '34.95',
'Description' => 'ISO FileMaker Magazine',
'Url' => 'http://store.filemakertemplates.com/iso-mag.html',
'Taxable' => 'NO',
'Thumb' => '<img border=0 width=70 height=63 src=http://ep.yimg.com/ca/I/isoproductions_2194_963773>',
),
'1' => array (
'Id' => 'videos-rapidramp',
'Code' => 'videos-rapidramp',
'Quantity' => '1',
'Unit_Price' => '178.00',
'Description' => 'Rapid Ramp Up Webinar',
'Url' => 'http://store.filemakertemplates.com/videos-rapidramp.html',
'Taxable' => 'NO',
'Thumb' => '<img border=0 width=70 height=63 src=http://ep.yimg.com/ca/I/isoproductions_2194_963773>',
),
'variable' => 'baz',
),
'Total' => array (
'0' => array (
'Line_type' => 'Total',
'Line_name' => 'Total',
'Line_notes' => '',
'Line_amount' => '24.95',
)
),
'Extra' => 'foobar'
);
// Adding another level for testing
$theArray['Items']['0']['level4'] = array('foo' => 'bar', 'baz' => 'boo');
$desired = <<<EOT
\$Order_id = "mystore-12656";\$Currency = "USD";\$Time = "Fri Apr 6 10:46:57 2012 GMT";\$NumericTime = "1390972125";\$IPAddress = "127.0.0.1";\$Bill_First = "Tësting";\$Bill_Last = "Persøn";\$Bill_Company = "P&G";\$Bill_Address1 = "1234 Dürty Road";\$Bill_City = "Weiterstadt";\$Bill_State = "Stateville";\$Bill_Country = "DE Germany";\$Bill_Zip = "92562";\$Bill_Phone = "049615091854284";\$Bill_Email = "name@example.com";\$Extra = "foobar";
\$Items = "\$0 = \"___Insert > [Items][0]___\";¶\$1 = \"___Insert > [Items][1]___\";¶\$variable = \"baz\";";
\$Total = "\$0=\"___Insert > [Total][0]___\";";
EOT;
/*
FileMaker quote level examples
2 "\"foo\""
3 "\"\\\"foo\\\"\""
4 "\"\\\"\\\\\\\"foo\\\\\\\"\\\"\""
5 "\"\\\"\\\\\\\"\\\\\\\\\\\\\\\"foo\\\\\\\\\\\\\\\"\\\\\\\"\\\"\""
Let (
~var = Quote ( "foo" );
List (
"2 " & Quote ( ~var );
"3 " & Quote ( Quote ( ~var ) );
"4 " & Quote ( Quote ( Quote ( ~var ) ) );
"5 " & Quote ( Quote ( Quote ( Quote ( ~var ) ) ) );
)
)
*/
print '<pre>';
print "----------------------------------\nOriginal Array\n----------------------------------\n";
print_r($theArray);
print "----------------------------------\nThe Objective (see comments)\n----------------------------------\n";
// At each level of an array, a return should be injected so it can be walked with a FileMaker Loop step.
// Because FileMaker does not support multidimensional arrays, <cr>'s must be insterted and converted to ¶
print $desired . "\n";
print "----------------------------------\nCurrent state of conversion\n----------------------------------\n";
$compressed =fmLet($theArray, 'Order');
foreach ($compressed['Order'] as $key => $value) {
if ($key === 'fmpVars') {
$string = $value;
} else {
$string .= "\n" . $value['fmpVars'];
}
}
print_r($compressed);
// This is the string you pass into a FileMaker script for parsing
print "<h2>Final result string</h2>\n\n";
print fmQuote($string);
print "\n\n</pre>\n\n";
function fmLet($arr, $container = 'Main', &$result = array()) {
static $level = 1, $parent = '';
foreach($arr as $key => $value) {
// print $level . "\n";
if (is_array($value)) {
print "[$container] at level $level with key $key\n";
$level++;
// $result .= ''
$parent = $container;
fmLet($value, $key, $result[$container]); // passing the result array as reference for pointer
$level--;
continue;
}
print "[$container] at level $level with key $key\n";
// Append the variable string in quoted fmp format
$varstring .= '$' . $key . ' = ' . fmQuote($value) . ';';
// Inject into tree array
$result[$container]['fmpVars'] = $varstring;
// print $level . "\n";
}
print "\n------------- FUNCTION RESULT -------------\n";
if ($parent === $container) {
print "\n\n>>>>>>>>>>>>>>I SHOULD COMPRESS HERE\n\n";
}
print "Parent container is $parent\n";
print "Current container is $container\n";
print "Level is $level\n";
print "Count of result is " . count($result) . "\n";
print "Result array is\n";
print_r($result[$parent]) . "\n";
// Collect fmpVars when at matching level of entry
if ($parent === $container) {
// Remove the fmpVars because $varstring has it and we don't want to count it
unset($result[$parent]['fmpVars']);
foreach ($result[$parent] as $value) {
$varstring .= (empty($varstring)) ? $value['fmpVars'] : "\n" . $value['fmpVars'];
}
}
// Inject into wrapper
$wrapper = '$' . $container . ' = «INSERT»;';
$wrapper = str_replace('«INSERT»', fmQuote($varstring), $wrapper);
print $wrapper . "\n";
print "\n------------- FUNCTION END -------------\n";
// Append to container
$result[$container]['fmpVars'] = $wrapper;
return $result;
}
/*
* Helper function to fix quotes for FMP compatibility
*/
function fmQuote ($value) {
return '"' . str_replace(array('\\', '"', '¶', "\n"), array('\\\\', '\"', '\\¶', '¶'), $value) . '"';
}
?>
<hr/>
<h2>FileMaker script to parse variables.</h2>
<pre>
# Name: Loopit ( variables )
#----------------------------------
# Capture parameters
#----------------------------------
If [ not #Assign ( #Filter (
Get ( ScriptParameter ) ;
ScriptRequiredParameterList ( "" )
))]
Show Custom Dialog [ Title: "Developer Error!"; Message: Quote ( Get ( ScriptName ) ) & " could not process the required variables."; Default Button: “Darn”, Commit: “Yes” ]
Exit Script [ ]
End If
#----------------------------------
# Walk variables list
#----------------------------------
Set Variable [ $itemCount; Value:ValueCount ( $variables ) ]
Loop
Exit Loop If [ Let ( $i = $i + 1 ; If ( $i > Int ( $itemCount ) ; Let ( $i = "" ; True ) ) ) ]
Set Variable [ $~line; Value:GetValue ( $variables ; $i ) ]
Set Variable [ $~lineIsContainer; Value:GetAsBoolean ( PatternCount ( $~line ; ";\¶" ) ) or
GetAsBoolean ( PatternCount ( $~line ; " = \\\"$" ) ) ]
Set Variable [ $~lineHasEmbeddedVars; Value:GetAsBoolean ( PatternCount ( $~line ; "= \\\"" ) ) ] Set Variable [ $~varsCreated; Value:#Assign ( $~line ) ]
Set Variable [ $~package; Value:"$" & LeftWords ($~line ; 1 ) ]
If [ $~lineIsContainer ]
Perform Script [ “Loopit ( variables )”; Parameter: # ( "variables" ; Evaluate ( $~package ) ) ]
End If
If [ $~lineHasEmbeddedVars ]
Set Variable [ $~varsExplodedOut; Value:#Assign ( Evaluate ( $~package ) ) ]
End If
End Loop
</pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment