Skip to content

Commit

Permalink
Added withName and updated $name in MwStack
Browse files Browse the repository at this point in the history
`$name` is now an optional parameter in the MwStack

Signed-off-by: RJ Garcia <rj@bighead.net>
  • Loading branch information
ragboyjr committed Jan 20, 2017
1 parent 60bf16f commit 67fb0d0
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added

- Added `withName` func to MwStack
- Made the `$name` parameter optional in the MwStack

## [0.4.1] - 2017-01-19
### Added
Expand Down
2 changes: 2 additions & 0 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ Closure compose(callable $last = null)
Composes the stack into a handler.
Generator getEntries()
Yields the raw stack entries in the order they were added.
MwStack withName($name)
Creates a clone of the current stack with an updated name.
MwStack withContext(Context $ctx)
Creates a clone of the current stack with an updated context
MwStack withLinkClass($class)
Expand Down
9 changes: 7 additions & 2 deletions src/MwStack.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MwStack implements Countable
private $heap;
private $name_map;

public function __construct($name, Context $ctx = null, $link_class = Link::class) {
public function __construct($name = null, Context $ctx = null, $link_class = Link::class) {
$this->name = $name;
$this->ctx = $ctx;
$this->link_class = $link_class;
Expand Down Expand Up @@ -167,6 +167,11 @@ public function compose($last = null) {
return compose($this->normalize(), $this->ctx, $last, $this->link_class);
}

public function withName($name) {
$stack = clone $this;
$stack->name = $name;
return $stack;
}
public function withContext(Context $ctx) {
$stack = clone $this;
$stack->ctx = $ctx;
Expand All @@ -189,7 +194,7 @@ public function getEntries() {
}
}

public static function createFromEntries($name, $entries, Context $ctx = null, $link_class = Link::class) {
public static function createFromEntries($name = null, $entries, Context $ctx = null, $link_class = Link::class) {
$stack = new static($name, $ctx, $link_class);
foreach ($entries as $entry) {
$stack->insertEntry($entry, 'array_push');
Expand Down
2 changes: 1 addition & 1 deletion src/mw.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ function stackEntry($mw, $sort = 0, $name = null) {
return [$mw, $sort, $name];
}

function stack($name, array $entries = [], Context $context = null, $link_class = Link::class) {
function stack($name = null, array $entries = [], Context $context = null, $link_class = Link::class) {
return MwStack::createFromEntries($name, $entries, $context, $link_class);
}

Expand Down
4 changes: 4 additions & 0 deletions test/mw.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ function() { return 1; },
$handler = $stack->compose();
assert($handler(2) === 1);
});
it('allows you to change name', function() {
$stack = mw\stack()->withName('Stack');
assert($stack->getName() == 'Stack');
});
it('allows you to change context', function() {
$stack = mw\stack('stack')->withContext(new Mw\Context\StdContext(function() { return 1; }));
$stack->push(id());
Expand Down

0 comments on commit 67fb0d0

Please sign in to comment.