Skip to content

Commit

Permalink
Merge pull request #10 from joetannenbaum/json-util
Browse files Browse the repository at this point in the history
add json util
  • Loading branch information
joetannenbaum authored Oct 10, 2022
2 parents e2c6252 + 0b498af commit 7a517f9
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/ItemParam/HasConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Alfred\Workflows\ItemParam;

trait HasConfig
{
use HasParams;

/**
* The `config` object enables dynamic (and overriding) configuration of
* the workflow objects connected to the output of the JSON Utility.
*
* The easiest way to find out which configuration fields are available
* for an object is to copy the object configuration from the right-click
* popup menu for the selected workflow object on the canvas.
*
* Only the included fields will be overridden, allowing for partial dynamic
* configuration of a workflow object.
*
* The fields are generally self-explanatory, but if you have trouble identifying
* a field, set it to a unique value in the object's configuration sheet and copy
* the configuration again. You'll see the value you set.
*
* @param string|array $key
* @param mixed $value
*
* @link https://www.alfredapp.com/help/workflows/utilities/json/
*/
public function config($key, $value = null): self
{
if (is_array($key)) {
$this->mergeParam('config', $key);
return $this;
}

$this->mergeParam('config', [$key => $value]);

return $this;
}
}
27 changes: 27 additions & 0 deletions src/RunScript.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Alfred\Workflows;

use Alfred\Workflows\ItemParam\HasArguments;
use Alfred\Workflows\ItemParam\HasConfig;
use Alfred\Workflows\ItemParam\HasVariables;

class RunScript
{
use HasVariables;
use HasArguments;
use HasConfig;

public function output(bool $echo = true): string
{
$json = json_encode([
'alfredworkflow' => $this->params,
]);

if ($echo) {
echo $json;
}

return $json;
}
}
11 changes: 11 additions & 0 deletions src/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ public function item(): Item
return $this->items->add();
}

/**
* Set variables, arguments, and config from a Script Action
*
* @link https://www.alfredapp.com/help/workflows/utilities/json/
* @return RunScript
*/
public function setFromRunScript()
{
return new RunScript();
}

/**
* Access the collection of items
*/
Expand Down
37 changes: 37 additions & 0 deletions tests/Unit/RunScriptTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

it('can add variables from a run script', function () {
$output = $this->workflow->setFromRunScript()->variables([
'color' => 'green',
])->output(false);

expect($output)->toBe(json_encode(['alfredworkflow' => ['variables' => ['color' => 'green']]]));
});

it('can add an argument from a run script', function () {
$output = $this->workflow->setFromRunScript()->arg('just passing through')->output(false);

expect($output)->toBe(json_encode(['alfredworkflow' => ['arg' => 'just passing through']]));
});

it('can add a config from a run script', function () {
$output = $this->workflow->setFromRunScript()->config([
'url' => '{query}',
])->output(false);

expect($output)->toBe(json_encode(['alfredworkflow' => ['config' => ['url' => '{query}']]]));
});

it('can add a combination from a run script', function () {
$output = $this->workflow->setFromRunScript()->config([
'url' => '{query}',
])->variables([
'color' => 'green',
])->arg('just passing through')->output(false);

expect($output)->toBe(json_encode(['alfredworkflow' => [
'config' => ['url' => '{query}'],
'variables' => ['color' => 'green'],
'arg' => 'just passing through',
]]));
});

0 comments on commit 7a517f9

Please sign in to comment.