Skip to content

Instantly share code, notes, and snippets.

@cebe
Created March 25, 2021 10:35
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 cebe/673feefc0e5ca447a027f90b99504138 to your computer and use it in GitHub Desktop.
Save cebe/673feefc0e5ca447a027f90b99504138 to your computer and use it in GitHub Desktop.
PHP Array exporter for PHPStorm

Install by adding it to

~/.config/JetBrains/PhpStorm2020.3/extensions/com.intellij.database/data/extractors

/*
* Based on IntelliJ PHPStorm JSON exporter.
*
* Available context bindings:
* COLUMNS List<DataColumn>
* ROWS Iterable<DataRow>
* OUT { append() }
* FORMATTER { format(row, col); formatValue(Object, col); getTypeName(Object, col); isStringLiteral(Object, col); }
* TRANSPOSED Boolean
* plus ALL_COLUMNS, TABLE, DIALECT
*
* where:
* DataRow { rowNumber(); first(); last(); data(): List<Object>; value(column): Object }
* DataColumn { columnNumber(), name() }
*/
import static com.intellij.openapi.util.text.StringUtil.escapeStringCharacters as escapeStr
NEWLINE = System.getProperty("line.separator")
INDENT = " "
def printPHPArray(level, col, o) {
switch (o) {
case null: OUT.append("null"); break
case Tuple: printPHPArray(level, o[0], o[1]); break
case Map:
OUT.append("[")
o.entrySet().eachWithIndex { entry, i ->
OUT.append("${i > 0 ? "," : ""}$NEWLINE${INDENT * (level + 1)}")
OUT.append("\"${escapeStr(entry.getKey().toString())}\"")
OUT.append(" => ")
printPHPArray(level + 1, col, entry.getValue())
}
OUT.append("$NEWLINE${INDENT * level}]")
break
case Object[]:
case Iterable:
OUT.append("[")
def plain = true
o.eachWithIndex { item, i ->
plain = item == null || item instanceof Number || item instanceof Boolean || item instanceof String
if (plain) {
OUT.append(i > 0 ? ", " : "")
}
else {
OUT.append("${i > 0 ? "," : ""}$NEWLINE${INDENT * (level + 1)}")
}
printPHPArray(level + 1, col, item)
}
if (plain) OUT.append("]") else OUT.append("$NEWLINE${INDENT * level}]")
break
case Boolean: OUT.append("$o"); break
default:
def str = FORMATTER.formatValue(o, col)
OUT.append(FORMATTER.isStringLiteral(o, col) ? "\"${escapeStr(str)}\"" : str);
break
}
}
printPHPArray(0, null, ROWS.transform { row ->
def map = new LinkedHashMap<String, String>()
COLUMNS.each { col ->
if (row.hasValue(col)) {
def val = row.value(col)
map.put(col.name(), new Tuple(col, val))
}
}
map
})
@cebe
Copy link
Author

cebe commented Mar 25, 2021

2021-03-25_11-34

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment