diff --git a/README.md b/README.md index 20ac3cc..d4a6bb7 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,10 @@ require 'vendor/autoload.php'; $workflow = new Workflow; +// add variables +$workflow->variable('fruit','apple') + ->variable('vegetables','carrots'); + $workflow->result() ->uid('bob-belcher') ->title('Bob') @@ -106,7 +110,11 @@ Results in: "uid": "linda-belcher", "valid": true } - ] + ], + "variables": { + "fruit": "apple", + "vegetables": "carrots" + } } ``` diff --git a/src/Workflow.php b/src/Workflow.php index e088f28..c52db54 100644 --- a/src/Workflow.php +++ b/src/Workflow.php @@ -5,6 +5,7 @@ class Workflow { protected $results = []; + protected $variables = []; /** * Add a result to the workflow @@ -20,6 +21,21 @@ public function result() return $result; } + /** + * Add a variables to the workflow + * + * @param string $key + * @param string $value + * + * @return \Alfred\Workflows\Workflow + */ + public function variable($key, $value) + { + $this->variables[$key] = $value; + + return $this; + } + /** * Sort the current results * @@ -77,6 +93,10 @@ public function output() }, array_values($this->results)), ]; + if(!empty($this->variables)){ + $output['variables'] = $this->variables; + }; + return json_encode($output); } } diff --git a/tests/WorkflowTest.php b/tests/WorkflowTest.php index cc66b6b..dfd6fe6 100644 --- a/tests/WorkflowTest.php +++ b/tests/WorkflowTest.php @@ -418,4 +418,68 @@ public function it_can_filter_results_by_a_different_key() $this->assertSame(json_encode($expected), $workflow->filterResults('ID 2', 'uid')->output()); } + + /** @test */ + public function it_can_add_variables() + { + $workflow = new Workflow; + + $workflow->variable('fruit','apple') + ->variable('vegetables','carrots'); + + $workflow->result() + ->uid('THE ID') + ->title('Item Title') + ->subtitle('Item Subtitle') + ->quicklookurl('https://www.google.com') + ->type('file') + ->arg('ARGUMENT') + ->valid(false) + ->icon('icon.png') + ->mod('cmd', 'Do Something Different', 'something-different') + ->mod('shift', 'Another Different', 'another-different', false) + ->copy('Please copy this') + ->largetype('This will be huge') + ->autocomplete('AutoComplete This'); + + $expected = [ + 'items' => [ + [ + 'arg' => 'ARGUMENT', + 'autocomplete' => 'AutoComplete This', + 'icon' => [ + 'path' => 'icon.png', + ], + 'mods' => [ + 'cmd' => [ + 'subtitle' => 'Do Something Different', + 'arg' => 'something-different', + 'valid' => true, + ], + 'shift' => [ + 'subtitle' => 'Another Different', + 'arg' => 'another-different', + 'valid' => false, + ], + ], + 'quicklookurl' => 'https://www.google.com', + 'subtitle' => 'Item Subtitle', + 'text' => [ + 'copy' => 'Please copy this', + 'largetype' => 'This will be huge', + ], + 'title' => 'Item Title', + 'type' => 'file', + 'uid' => 'THE ID', + 'valid' => false, + ], + ], + 'variables' => [ + 'fruit' => 'apple', + 'vegetables' => 'carrots' + ] + ]; + + $this->assertSame(json_encode($expected), $workflow->output()); + } }