-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'refs/remotes/origin/dev'
- Loading branch information
Showing
10 changed files
with
200 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php # -*- coding: utf-8 -*- | ||
/* | ||
* This file is part of the Context package. | ||
* | ||
* (c) Giuseppe Mazzapica | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Brain\Context; | ||
|
||
/** | ||
* @author Giuseppe Mazzapica <giuseppe.mazzapica@gmail.com> | ||
* @package Context | ||
* @license http://opensource.org/licenses/MIT MIT | ||
*/ | ||
final class ArrayMergeContextCollector implements ContextCollectorInterface | ||
{ | ||
use ArrayMergeContextCollectorTrait; | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function provide() | ||
{ | ||
if (!$this->query instanceof \WP_Query) { | ||
return []; | ||
} | ||
|
||
$context = []; | ||
while (!$this->providers->isEmpty()) { | ||
/** @var ContextProviderInterface|UpdatableContextProviderInterface $provider */ | ||
$provider = $this->providers->dequeue(); | ||
if (!$provider->accept($this->query)) { | ||
continue; | ||
} | ||
|
||
$context = array_merge($context, $provider->provide()); | ||
if ($provider instanceof UpdatableContextProviderInterface) { | ||
$context = $provider->update($context); | ||
} | ||
} | ||
|
||
return $context; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php # -*- coding: utf-8 -*- | ||
/* | ||
* This file is part of the Context package. | ||
* | ||
* (c) Giuseppe Mazzapica | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Brain\Context; | ||
|
||
/** | ||
* @author Giuseppe Mazzapica <giuseppe.mazzapica@gmail.com> | ||
* @package Context | ||
* @license http://opensource.org/licenses/MIT MIT | ||
*/ | ||
final class ArrayMergeRecursiveContextCollector implements ContextCollectorInterface | ||
{ | ||
|
||
use ArrayMergeContextCollectorTrait; | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function provide() | ||
{ | ||
if (!$this->query instanceof \WP_Query) { | ||
return []; | ||
} | ||
|
||
$context = []; | ||
while (!$this->providers->isEmpty()) { | ||
/** @var ContextProviderInterface|UpdatableContextProviderInterface $provider */ | ||
$provider = $this->providers->dequeue(); | ||
if (!$provider->accept($this->query)) { | ||
continue; | ||
} | ||
|
||
$context = array_merge_recursive($context, $provider->provide()); | ||
if ($provider instanceof UpdatableContextProviderInterface) { | ||
$context = $provider->update($context); | ||
} | ||
} | ||
|
||
return $context; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php # -*- coding: utf-8 -*- | ||
/* | ||
* This file is part of the Context package. | ||
* | ||
* (c) Giuseppe Mazzapica | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Brain\Context\Tests; | ||
|
||
use Brain\Context\ArrayMergeRecursiveContextCollector; | ||
use Brain\Context\ContextProviderInterface; | ||
use Brain\Context\ArrayMergeContextCollector; | ||
use Brain\Monkey\WP\Actions; | ||
|
||
/** | ||
* @author Giuseppe Mazzapica <giuseppe.mazzapica@gmail.com> | ||
* @package Context | ||
* @license http://opensource.org/licenses/MIT MIT | ||
*/ | ||
class ArrayMergeRecursiveContextCollectorTest extends TestCase | ||
{ | ||
|
||
public function testProvide() | ||
{ | ||
$collector = new ArrayMergeRecursiveContextCollector(); | ||
|
||
$query = \Mockery::mock('WP_Query'); | ||
|
||
$context_a = \Mockery::mock(ContextProviderInterface::class); | ||
$context_b = clone $context_a; | ||
$context_c = clone $context_b; | ||
|
||
$context_a->shouldReceive('accept')->once()->with($query)->andReturn(true); | ||
$context_b->shouldReceive('accept')->once()->with($query)->andReturn(false); | ||
$context_c->shouldReceive('accept')->once()->with($query)->andReturn(true); | ||
|
||
$context_a->shouldReceive('provide')->once()->andReturn([ | ||
'message' => 'Hello from A!', | ||
'letters' => ['a'] | ||
]); | ||
|
||
$context_b->shouldReceive('provide')->once()->andReturn([ | ||
'message' => 'Goodbye from B!', | ||
'meh' => 'meh' | ||
]); | ||
|
||
$context_c->shouldReceive('provide')->once()->andReturn([ | ||
'message' => 'Hello from C!', | ||
'letters' => ['b', 'c', 'd'], | ||
'color' => 'yellow' | ||
]); | ||
|
||
Actions::expectFired('brain.context.added') | ||
->times(3) | ||
->with(\Mockery::type(ContextProviderInterface::class), \Mockery::type('SplQueue')); | ||
|
||
$collector | ||
->addProvider($context_a) | ||
->addProvider($context_b) | ||
->addProvider($context_c); | ||
|
||
$collector->accept($query); | ||
|
||
$expected = [ | ||
'message' => ['Hello from A!', 'Hello from C!'], | ||
'letters' => ['a', 'b', 'c', 'd'], | ||
'color' => 'yellow' | ||
]; | ||
|
||
assertSame($expected, $collector->provide()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters