Skip to content

Commit d587586

Browse files
Merge pull request #2 from wensonsmith/master
add variables support
2 parents 2fef46b + 3372fad commit d587586

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ require 'vendor/autoload.php';
2626

2727
$workflow = new Workflow;
2828

29+
// add variables
30+
$workflow->variable('fruit','apple')
31+
->variable('vegetables','carrots');
32+
2933
$workflow->result()
3034
->uid('bob-belcher')
3135
->title('Bob')
@@ -106,7 +110,11 @@ Results in:
106110
"uid": "linda-belcher",
107111
"valid": true
108112
}
109-
]
113+
],
114+
"variables": {
115+
"fruit": "apple",
116+
"vegetables": "carrots"
117+
}
110118
}
111119
```
112120

src/Workflow.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
class Workflow
66
{
77
protected $results = [];
8+
protected $variables = [];
89

910
/**
1011
* Add a result to the workflow
@@ -20,6 +21,21 @@ public function result()
2021
return $result;
2122
}
2223

24+
/**
25+
* Add a variables to the workflow
26+
*
27+
* @param string $key
28+
* @param string $value
29+
*
30+
* @return \Alfred\Workflows\Workflow
31+
*/
32+
public function variable($key, $value)
33+
{
34+
$this->variables[$key] = $value;
35+
36+
return $this;
37+
}
38+
2339
/**
2440
* Sort the current results
2541
*
@@ -77,6 +93,10 @@ public function output()
7793
}, array_values($this->results)),
7894
];
7995

96+
if(!empty($this->variables)){
97+
$output['variables'] = $this->variables;
98+
};
99+
80100
return json_encode($output);
81101
}
82102
}

tests/WorkflowTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,4 +418,68 @@ public function it_can_filter_results_by_a_different_key()
418418

419419
$this->assertSame(json_encode($expected), $workflow->filterResults('ID 2', 'uid')->output());
420420
}
421+
422+
/** @test */
423+
public function it_can_add_variables()
424+
{
425+
$workflow = new Workflow;
426+
427+
$workflow->variable('fruit','apple')
428+
->variable('vegetables','carrots');
429+
430+
$workflow->result()
431+
->uid('THE ID')
432+
->title('Item Title')
433+
->subtitle('Item Subtitle')
434+
->quicklookurl('https://www.google.com')
435+
->type('file')
436+
->arg('ARGUMENT')
437+
->valid(false)
438+
->icon('icon.png')
439+
->mod('cmd', 'Do Something Different', 'something-different')
440+
->mod('shift', 'Another Different', 'another-different', false)
441+
->copy('Please copy this')
442+
->largetype('This will be huge')
443+
->autocomplete('AutoComplete This');
444+
445+
$expected = [
446+
'items' => [
447+
[
448+
'arg' => 'ARGUMENT',
449+
'autocomplete' => 'AutoComplete This',
450+
'icon' => [
451+
'path' => 'icon.png',
452+
],
453+
'mods' => [
454+
'cmd' => [
455+
'subtitle' => 'Do Something Different',
456+
'arg' => 'something-different',
457+
'valid' => true,
458+
],
459+
'shift' => [
460+
'subtitle' => 'Another Different',
461+
'arg' => 'another-different',
462+
'valid' => false,
463+
],
464+
],
465+
'quicklookurl' => 'https://www.google.com',
466+
'subtitle' => 'Item Subtitle',
467+
'text' => [
468+
'copy' => 'Please copy this',
469+
'largetype' => 'This will be huge',
470+
],
471+
'title' => 'Item Title',
472+
'type' => 'file',
473+
'uid' => 'THE ID',
474+
'valid' => false,
475+
],
476+
],
477+
'variables' => [
478+
'fruit' => 'apple',
479+
'vegetables' => 'carrots'
480+
]
481+
];
482+
483+
$this->assertSame(json_encode($expected), $workflow->output());
484+
}
421485
}

0 commit comments

Comments
 (0)