Skip to content

Commit

Permalink
feat: add copy as PHP for get directly an array (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephaneBour committed Apr 24, 2018
1 parent c887602 commit afe4a77
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
class="icon-wrench"></i></a>
<ul class="dropdown-menu">
<li><a id="copy_as_curl" tabindex="-1" href="#">Copy as cURL</a></li>
<li><a id="copy_as_php" tabindex="-1" href="#">Copy as PHP</a></li>
<li><a id="query_save" tabindex="-1" href="#">Save query</a></li>
<li><a id="auto_indent" tabindex="-1" href="#">Auto indent</a></li>
</ul>
Expand Down
37 changes: 37 additions & 0 deletions src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,38 @@ function copyAsCURL() {

copyAsCURL = autoRetryIfTokenizing(copyAsCURL, true);

function copyAsPhp() {
var req = sense.utils.getCurrentRequest();
if (!req) return;

// parsing URL for get index and type

if(req.url.charAt(0) == '/') {
req.url = req.url.slice(1);
}
var data = req.url.split('/');
var index = data[0];
var type = '';
if(data[1].slice(1) !== '_')
type = data[1];

var php = '[' + "\n\t" + '\'index\' => \'' + index + '\',' + "\n\t";
if(type !== '')
php = php + '\'type\' => \'' + type + '\',' + "\n\t";

php = php + '\'body\' => ' + "\n\t\t";

if (req.data && req.data.length) {
php += req.data.join("\n").replace(/"/g, '\'').replace(/{/g, '[').replace(/}/g, ']').replace(/:/g, ' =>');
if (req.data.length > 1) php += "\n"; // end with a new line
}
php = php + '];';

//console.log(php);
copyToClipboard(php);

}


function handleCURLPaste(text) {
var curlInput = sense.curl.parseCURL(text);
Expand Down Expand Up @@ -515,6 +547,11 @@ function init() {
e.preventDefault();
});

$("#copy_as_php").click(function (e) {
copyAsPhp();
e.preventDefault();
});

$("#query_save").click(function (e) {
querySave();
e.preventDefault();
Expand Down

0 comments on commit afe4a77

Please sign in to comment.