Add where and whereComponent to CMS objects

This commit is contained in:
Samuel Georges 2015-10-08 07:27:38 +11:00
parent f99ff4a363
commit 3531793ca3
3 changed files with 171 additions and 4 deletions

View File

@ -16,14 +16,81 @@ class CmsObjectCollection extends CollectionBase
* @param string|array $components
* @return static
*/
public function withComponent($components)
public function withComponent($components, $callback = null)
{
return $this->filter(function($object) use ($components) {
return $this->filter(function($object) use ($components, $callback) {
$hasComponent = false;
foreach ((array) $components as $component) {
if ($object->hasComponent($component)) {
foreach ((array) $components as $componentName) {
if (!$callback && $object->hasComponent($componentName)) {
$hasComponent = true;
}
if ($callback && ($component = $object->getComponent($componentName))) {
$hasComponent = call_user_func($callback, $component) ?: $hasComponent;
}
}
return $hasComponent;
});
}
/**
* Returns objects whose properties match the supplied value.
* @param string $property
* @param string $value
* @return static
*/
public function where($property, $value, $strict = true)
{
return $this->filter(function($object) use ($property, $value, $strict) {
if (!array_key_exists($property, $object->settings)) {
return false;
}
return $strict
? $object->settings[$property] === $value
: $object->settings[$property] == $value;
});
}
/**
* Returns objects whose component properties match the supplied value.
* @param string $property
* @param string $value
* @return static
*/
public function whereComponent($components, $property, $value, $strict = true)
{
return $this->filter(function($object) use ($components, $property, $value, $strict) {
$hasComponent = false;
foreach ((array) $components as $componentName) {
if (!$componentAlias = $object->hasComponent($componentName)) {
continue;
}
$componentSettings = array_get($object->settings, 'components', []);
if (!array_key_exists($componentAlias, $componentSettings)) {
continue;
}
$settings = $componentSettings[$componentAlias];
if (!array_key_exists($property, $settings)) {
continue;
}
if (
($strict && $settings[$property] === $value) ||
(!$strict && $settings[$property] == $value)
) {
$hasComponent = true;
}
}
@ -31,4 +98,5 @@ class CmsObjectCollection extends CollectionBase
return $hasComponent;
});
}
}

View File

@ -40,6 +40,10 @@ class CmsObjectQuery
*/
public function inTheme($theme)
{
if (is_string($theme)) {
$theme = Theme::load($theme);
}
$this->theme = $theme;
return $this;
}

View File

@ -0,0 +1,95 @@
<?php
use Cms\Classes\Page;
use Cms\Classes\Layout;
class CmsObjectQueryTest extends TestCase
{
public function testWhere()
{
$page = Page::where('layout', 'caramba')->first();
$this->assertEquals('/no-layout', $page->url);
}
public function testWhereComponent()
{
include_once base_path() . '/tests/fixtures/plugins/october/tester/components/Archive.php';
$pages = Page::whereComponent('testArchive', 'posts-per-page', '6');
$this->assertCount(1, $pages->all());
$page = $pages->first();
$this->assertEquals('/with-components', $page->url);
}
public function testWithComponent()
{
$pages = Page::withComponent('testArchive')->all();
$this->assertCount(2, $pages);
foreach ($pages as $page) {
$this->assertTrue(!!$page->hasComponent('testArchive'));
}
}
public function testWithComponentCallback()
{
include_once base_path() . '/tests/fixtures/plugins/october/tester/components/Archive.php';
$pages = Page::withComponent('testArchive', function($component) {
return $component->property('posts-per-page') == '69';
})->all();
$this->assertCount(1, $pages);
}
public function testLists()
{
// Default theme: test
$pages = Page::lists('baseFileName');
$this->assertEquals([
"404",
"a/a-page",
"ajax-test",
"authors",
"b/b-page",
"blog-archive",
"blog-post",
"code-namespaces",
"component-custom-render",
"component-partial-nesting",
"component-partial-override",
"component-partial",
"cycle-test",
"index",
"no-component-class",
"no-component",
"no-layout",
"no-partial",
"optional-full-php-tags",
"optional-short-php-tags",
"throw-php",
"with-component",
"with-components",
"with-content",
"with-layout",
"with-partials",
"with-placeholder",
], $pages);
$layouts = Layout::lists('baseFileName');
$this->assertEquals([
"a/a-layout",
"ajax-test",
"content",
"cycle-test",
"no-php",
"partials",
"php-parser-test",
"placeholder",
"sidebar",
], $layouts);
$pages = Page::inTheme('NON_EXISTENT_THEME')->lists('baseFileName');
$this->assertEmpty($pages);
}
}