Skip to content

Commit

Permalink
Merge pull request #2 from wensonsmith/master
Browse files Browse the repository at this point in the history
add variables support
  • Loading branch information
joetannenbaum authored Mar 29, 2017
2 parents 2fef46b + 3372fad commit d587586
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -106,7 +110,11 @@ Results in:
"uid": "linda-belcher",
"valid": true
}
]
],
"variables": {
"fruit": "apple",
"vegetables": "carrots"
}
}
```

Expand Down
20 changes: 20 additions & 0 deletions src/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class Workflow
{
protected $results = [];
protected $variables = [];

/**
* Add a result to the workflow
Expand All @@ -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
*
Expand Down Expand Up @@ -77,6 +93,10 @@ public function output()
}, array_values($this->results)),
];

if(!empty($this->variables)){
$output['variables'] = $this->variables;
};

return json_encode($output);
}
}
64 changes: 64 additions & 0 deletions tests/WorkflowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit d587586

Please sign in to comment.