Rename Flex Object classes (changed my mind)

This commit is contained in:
Matias Griese
2019-11-15 13:05:51 +02:00
parent 86faceb94f
commit bf2b7dcf7b
10 changed files with 125 additions and 20 deletions

View File

@@ -67,7 +67,7 @@ config:
# Data Configuration
data:
object: 'Grav\Common\Flex\Users\User'
object: 'Grav\Common\Flex\Users\UserObject'
collection: 'Grav\Common\Flex\Users\UserCollection'
index: 'Grav\Common\Flex\Users\UserIndex'
storage:

View File

@@ -138,7 +138,7 @@ config:
# Data Configuration
data:
object: 'Grav\Common\Flex\Pages\Page'
object: 'Grav\Common\Flex\Pages\PageObject'
collection: 'Grav\Common\Flex\Pages\PageCollection'
index: 'Grav\Common\Flex\Pages\PageIndex'
storage:

View File

@@ -67,7 +67,7 @@ config:
# Data Configuration
data:
object: 'Grav\Common\Flex\UserGroups\UserGroup'
object: 'Grav\Common\Flex\UserGroups\UserGroupObject'
collection: 'Grav\Common\Flex\UserGroups\UserGroupCollection'
index: 'Grav\Common\Flex\UserGroups\UserGroupIndex'
storage:

View File

@@ -30,7 +30,7 @@ class PageIndex extends FlexPageIndex
const ORDER_LIST_REGEX = '/(\/\d+)\.[^\/]+/u';
const PAGE_ROUTE_REGEX = '/\/\d+\./u';
/** @var Page|array */
/** @var PageObject|array */
protected $_root;
protected $_params;
@@ -85,7 +85,7 @@ class PageIndex extends FlexPageIndex
/**
* @param string $key
* @return Page|null
* @return PageObject|null
*/
public function get($key)
{
@@ -102,13 +102,13 @@ class PageIndex extends FlexPageIndex
}
/**
* @return Page
* @return PageObject
*/
public function getRoot()
{
$root = $this->_root;
if (is_array($root)) {
/** @var Page $root */
/** @var PageObject $root */
$root = $this->getFlexDirectory()->createObject(['__META' => $root], '/');
$this->_root = $root;
}
@@ -150,6 +150,111 @@ class PageIndex extends FlexPageIndex
return $this->getParams();
}
/**
* Filter pages by given filters.
*
* - search: string
* - page_type: string
* - modular: bool
* - visible: bool
* - routable: bool
* - published: bool
* - page: bool
* - translated: bool
*
* @param array $filters
* @return FlexCollectionInterface
*/
public function filterByNew(array $filters)
{
// TODO
// Normalize filters.
$isDefaultLanguage = $activeLanguage = '';
foreach ($filters as $key => $value) {
if ($value === null || $value === '') {
// Remove empty filters.
unset($filters[$key]);
continue;
}
if ($key === 'translated') {
/** @var Language $language */
$language = Grav::instance()['language'];
$activeLanguage = $language->getLanguage();
$isDefaultLanguage = $activeLanguage === $language->getDefault();
} elseif ($key === 'routable') {
// Routable checks also "not modular and page".
$bool = (bool)$filters['routable'];
$modular = (bool)($filters['modular'] ?? !$bool);
$page = (bool)($filters['page'] ?? $bool);
if ($modular === $bool || $page !== $bool) {
return $this->createFrom([]);
}
$filters['modular'] = $modular;
$filters['page'] = $page;
}
}
$list = [];
foreach ($this->getEntries() as $index => $entry) {
$type = $entry['template'] ?? 'folder';
$path = explode('/', $entry['key']);
$storagePath = explode('/', $entry['storage_key']);
$slug = end($path);
$isModular = $slug[0] === '_';
$storageSlug = end($storagePath);
foreach ($filters as $key => $value) {
switch ($key) {
// case 'search':
// // TODO:
// $matches = $this->search((string)$value);
// break;
case 'page_type':
// Filename specifies the page type.
$types = $value ? explode(',', $value) : [];
$matches = in_array($type, $types, true);
break;
case 'modular':
// All modular pages start with underscore in their slug.
// Modular can also be in the header...
$matches = $isModular === (bool)$value;
break;
case 'visible':
// Visible pages have numeric prefix in their folder name.
// In header
$matches = ($slug !== $storageSlug) === (bool)$value;
break;
// case 'routable':
// // TODO:
// break;
// case 'published':
// // TODO
// break;
case 'page':
// Pages
$matches = ($type !== 'folder') === (bool)$value;
break;
case 'translated':
$matches = isset($entry['markdown'][$activeLanguage]) === (bool)$value;
if (!$matches && $isDefaultLanguage) {
$matches = isset($entry['markdown']['']) === (bool)$value;
}
break;
default:
throw new \RuntimeException('Not implemented');
}
if ($matches === false) {
continue 2;
}
$list[$key] = $value;
}
}
return $this->createFrom($list);
}
/**
* @param array $options
* @return array
@@ -264,7 +369,7 @@ class PageIndex extends FlexPageIndex
$children = $page->children();
/** @var Page $child */
/** @var PageObject $child */
foreach ($children as $child) {
$selected = $child->path() === $extra;
$includeChildren = \is_array($leaf) && !empty($leaf) && $selected;
@@ -304,7 +409,7 @@ class PageIndex extends FlexPageIndex
$child->routable() ? 'routable' : 'non-routable'
];
$lang = $child->findTranslation($language) ?? 'n/a';
/** @var Page $child */
/** @var PageObject $child */
$child = $child->getTranslation($language) ?? $child;
$extras = [
'template' => $child->template(),

View File

@@ -37,7 +37,7 @@ use RocketTheme\Toolbox\Event\Event;
* @property string $template
* @property string $language
*/
class Page extends FlexPageObject
class PageObject extends FlexPageObject
{
use PageContentTrait;
use PageLegacyTrait;
@@ -366,7 +366,7 @@ class Page extends FlexPageObject
throw new \RuntimeException(sprintf('Page %s cannot be moved to %s', '/' . $key, $parentRoute));
}
/** @var Page|null $parent */
/** @var PageObject|null $parent */
$parent = $this->getFlexDirectory()->getObject($parentKey);
if (!$parent) {
// Page cannot be moved to non-existing location.

View File

@@ -24,7 +24,7 @@ use Grav\Framework\Flex\Traits\FlexAuthorizeTrait;
* @property string $groupname
* @property Access $access
*/
class UserGroup extends FlexObject implements UserGroupInterface
class UserGroupObject extends FlexObject implements UserGroupInterface
{
use FlexAuthorizeTrait;

View File

@@ -23,7 +23,7 @@ class UserCollection extends FlexCollection implements UserCollectionInterface
* Always creates user object. To check if user exists, use $this->exists().
*
* @param string $username
* @return User
* @return UserObject
*/
public function load($username): UserInterface
{
@@ -39,7 +39,7 @@ class UserCollection extends FlexCollection implements UserCollectionInterface
$directory = $this->getFlexDirectory();
/** @var User $object */
/** @var UserObject $object */
$object = $directory->createObject(
[
'username' => $username,
@@ -56,7 +56,7 @@ class UserCollection extends FlexCollection implements UserCollectionInterface
*
* @param string $query the query to search for
* @param array $fields the fields to search
* @return User
* @return UserObject
*/
public function find($query, $fields = ['username', 'email']): UserInterface
{

View File

@@ -43,7 +43,7 @@ class UserIndex extends FlexIndex
*
* @param string $username
*
* @return User
* @return UserObject
*/
public function load($username): UserInterface
{
@@ -59,7 +59,7 @@ class UserIndex extends FlexIndex
$directory = $this->getFlexDirectory();
/** @var User $object */
/** @var UserObject $object */
$object = $directory->createObject(
[
'username' => $username,
@@ -76,7 +76,7 @@ class UserIndex extends FlexIndex
*
* @param string $query the query to search for
* @param array $fields the fields to search
* @return User
* @return UserObject
*/
public function find($query, $fields = ['username', 'email']): UserInterface
{

View File

@@ -59,7 +59,7 @@ use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
* @property bool $authenticated
* @property bool $authorized
*/
class User extends FlexObject implements UserInterface, MediaManipulationInterface, \Countable
class UserObject extends FlexObject implements UserInterface, MediaManipulationInterface, \Countable
{
use FlexMediaTrait {
getMedia as private getFlexMedia;

View File

@@ -23,7 +23,7 @@ if (defined('GRAV_USER_INSTANCE') && GRAV_USER_INSTANCE === 'FLEX') {
/**
* @deprecated 1.6 Use $grav['accounts'] instead of static calls. In type hints, please use UserInterface.
*/
class User extends Flex\Users\User
class User extends Flex\Users\UserObject
{
/**
* Load user account.