Skip to content

Instantly share code, notes, and snippets.

@mrbig00
Last active November 26, 2020 14:21
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 mrbig00/1da1d3da490065b49510a23f105b782a to your computer and use it in GitHub Desktop.
Save mrbig00/1da1d3da490065b49510a23f105b782a to your computer and use it in GitHub Desktop.
Enhanced PHP array export
/*
* Available context bindings:
* COLUMNS List<DataColumn>
* ROWS Iterable<DataRow>
* OUT { append() }
* FORMATTER { format(row, col); formatValue(Object, col) }
* TRANSPOSED Boolean
* plus ALL_COLUMNS, TABLE, DIALECT
*
* where:
* DataRow { rowNumber(); first(); last(); data(): List<Object>; value(column): Object }
* DataColumn { columnNumber(), name() }
*/
SEPARATOR = ","
QUOTE = "\'"
NEWLINE = System.getProperty("line.separator")
count = 0;
OUT.append("[").append(NEWLINE)
def printRow = { values, valueToString ->
OUT.append("\t[").append(NEWLINE)
values.eachWithIndex { value, idx ->
def str = valueToString(value)
str = str.replace("'","\\'")
OUT.append("\t\t'").append(value.name()).append("' => ")
if(str == "NULL") {
OUT.append(str).append(",")
} else {
OUT.append("'").append(str).append("',")
}
OUT.append(NEWLINE)
}
OUT.append("\t],")
OUT.append(NEWLINE)
}
ROWS.each { row -> printRow(COLUMNS, { FORMATTER.format(row, it) }) }
OUT.append("];")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment