mirror of
https://github.com/getgrav/grav.git
synced 2026-05-07 06:35:30 +02:00
More phpstan fixes
This commit is contained in:
@@ -20,7 +20,7 @@
|
||||
* Fixed multi-language saving issues with default language in `Flex Pages`
|
||||
* Grav 1.7: Fixed PHP 7.1 compatibility issues
|
||||
* Selfupgrade CLI: Fixed broken selfupgrade assets reference [#2681](https://github.com/getgrav/grav/issues/2681)
|
||||
|
||||
|
||||
# v1.7.0-beta.10
|
||||
## 10/03/2019
|
||||
|
||||
|
||||
@@ -20,4 +20,25 @@ interface MediaCollectionInterface extends \Grav\Framework\Media\Interfaces\Medi
|
||||
* @return string|null
|
||||
*/
|
||||
public function getPath();
|
||||
|
||||
/**
|
||||
* Get a list of all media.
|
||||
*
|
||||
* @return MediaObjectInterface[]
|
||||
*/
|
||||
public function all();
|
||||
|
||||
/**
|
||||
* Set file modification timestamps (query params) for all the media files.
|
||||
*
|
||||
* @param string|int|null $timestamp
|
||||
* @return $this
|
||||
*/
|
||||
public function setTimestamps($timestamp = null);
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param MediaObjectInterface $file
|
||||
*/
|
||||
public function add($name, $file);
|
||||
}
|
||||
|
||||
@@ -40,7 +40,10 @@ class PageCollection extends FlexPageCollection implements PageCollectionInterfa
|
||||
*/
|
||||
public function getRoot()
|
||||
{
|
||||
return $this->getIndex()->getRoot();
|
||||
/** @var PageIndex $index */
|
||||
$index = $this->getIndex();
|
||||
|
||||
return $index->getRoot();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -82,7 +85,7 @@ class PageCollection extends FlexPageCollection implements PageCollectionInterfa
|
||||
*
|
||||
* @param PageInterface $page
|
||||
*
|
||||
* @return self
|
||||
* @return static
|
||||
*/
|
||||
public function addPage(PageInterface $page)
|
||||
{
|
||||
@@ -101,7 +104,7 @@ class PageCollection extends FlexPageCollection implements PageCollectionInterfa
|
||||
* Merge another collection with the current collection
|
||||
*
|
||||
* @param PageCollectionInterface $collection
|
||||
* @return self
|
||||
* @return static
|
||||
*/
|
||||
public function merge(PageCollectionInterface $collection)
|
||||
{
|
||||
@@ -112,7 +115,7 @@ class PageCollection extends FlexPageCollection implements PageCollectionInterfa
|
||||
* Intersect another collection with the current collection
|
||||
*
|
||||
* @param PageCollectionInterface $collection
|
||||
* @return self
|
||||
* @return static
|
||||
*/
|
||||
public function intersect(PageCollectionInterface $collection)
|
||||
{
|
||||
@@ -148,7 +151,7 @@ class PageCollection extends FlexPageCollection implements PageCollectionInterfa
|
||||
* Pick one or more random entries.
|
||||
*
|
||||
* @param int $num Specifies how many entries should be picked.
|
||||
* @return self
|
||||
* @return static
|
||||
*/
|
||||
public function random($num = 1)
|
||||
{
|
||||
@@ -160,7 +163,7 @@ class PageCollection extends FlexPageCollection implements PageCollectionInterfa
|
||||
*
|
||||
* @param array $items Items to be appended. Existing keys will be overridden with the new values.
|
||||
*
|
||||
* @return self
|
||||
* @return static
|
||||
*/
|
||||
public function append($items)
|
||||
{
|
||||
@@ -171,7 +174,7 @@ class PageCollection extends FlexPageCollection implements PageCollectionInterfa
|
||||
* Split collection into array of smaller collections.
|
||||
*
|
||||
* @param int $size
|
||||
* @return self[]
|
||||
* @return static[]
|
||||
*/
|
||||
public function batch($size): array
|
||||
{
|
||||
@@ -186,7 +189,7 @@ class PageCollection extends FlexPageCollection implements PageCollectionInterfa
|
||||
* @param array $manual
|
||||
* @param string $sort_flags
|
||||
*
|
||||
* @return self
|
||||
* @return static
|
||||
*/
|
||||
public function order($by, $dir = 'asc', $manual = null, $sort_flags = null)
|
||||
{
|
||||
@@ -203,7 +206,7 @@ class PageCollection extends FlexPageCollection implements PageCollectionInterfa
|
||||
* @param string|bool $endDate
|
||||
* @param string|null $field
|
||||
*
|
||||
* @return self
|
||||
* @return static
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function dateRange($startDate, $endDate = false, $field = null)
|
||||
@@ -230,7 +233,7 @@ class PageCollection extends FlexPageCollection implements PageCollectionInterfa
|
||||
/**
|
||||
* Creates new collection with only visible pages
|
||||
*
|
||||
* @return self The collection with only visible pages
|
||||
* @return static The collection with only visible pages
|
||||
*/
|
||||
public function visible()
|
||||
{
|
||||
@@ -247,7 +250,7 @@ class PageCollection extends FlexPageCollection implements PageCollectionInterfa
|
||||
/**
|
||||
* Creates new collection with only non-visible pages
|
||||
*
|
||||
* @return self The collection with only non-visible pages
|
||||
* @return static The collection with only non-visible pages
|
||||
*/
|
||||
public function nonVisible()
|
||||
{
|
||||
@@ -264,7 +267,7 @@ class PageCollection extends FlexPageCollection implements PageCollectionInterfa
|
||||
/**
|
||||
* Creates new collection with only modular pages
|
||||
*
|
||||
* @return self The collection with only modular pages
|
||||
* @return static The collection with only modular pages
|
||||
*/
|
||||
public function modular()
|
||||
{
|
||||
@@ -281,7 +284,7 @@ class PageCollection extends FlexPageCollection implements PageCollectionInterfa
|
||||
/**
|
||||
* Creates new collection with only non-modular pages
|
||||
*
|
||||
* @return self The collection with only non-modular pages
|
||||
* @return static The collection with only non-modular pages
|
||||
*/
|
||||
public function nonModular()
|
||||
{
|
||||
@@ -298,7 +301,7 @@ class PageCollection extends FlexPageCollection implements PageCollectionInterfa
|
||||
/**
|
||||
* Creates new collection with only published pages
|
||||
*
|
||||
* @return self The collection with only published pages
|
||||
* @return static The collection with only published pages
|
||||
*/
|
||||
public function published()
|
||||
{
|
||||
@@ -315,7 +318,7 @@ class PageCollection extends FlexPageCollection implements PageCollectionInterfa
|
||||
/**
|
||||
* Creates new collection with only non-published pages
|
||||
*
|
||||
* @return self The collection with only non-published pages
|
||||
* @return static The collection with only non-published pages
|
||||
*/
|
||||
public function nonPublished()
|
||||
{
|
||||
@@ -332,7 +335,7 @@ class PageCollection extends FlexPageCollection implements PageCollectionInterfa
|
||||
/**
|
||||
* Creates new collection with only routable pages
|
||||
*
|
||||
* @return self The collection with only routable pages
|
||||
* @return static The collection with only routable pages
|
||||
*/
|
||||
public function routable()
|
||||
{
|
||||
@@ -349,7 +352,7 @@ class PageCollection extends FlexPageCollection implements PageCollectionInterfa
|
||||
/**
|
||||
* Creates new collection with only non-routable pages
|
||||
*
|
||||
* @return self The collection with only non-routable pages
|
||||
* @return static The collection with only non-routable pages
|
||||
*/
|
||||
public function nonRoutable()
|
||||
{
|
||||
@@ -368,7 +371,7 @@ class PageCollection extends FlexPageCollection implements PageCollectionInterfa
|
||||
*
|
||||
* @param string $type
|
||||
*
|
||||
* @return self The collection
|
||||
* @return static The collection
|
||||
*/
|
||||
public function ofType($type)
|
||||
{
|
||||
@@ -387,7 +390,7 @@ class PageCollection extends FlexPageCollection implements PageCollectionInterfa
|
||||
*
|
||||
* @param string[] $types
|
||||
*
|
||||
* @return self The collection
|
||||
* @return static The collection
|
||||
*/
|
||||
public function ofOneOfTheseTypes($types)
|
||||
{
|
||||
@@ -406,7 +409,7 @@ class PageCollection extends FlexPageCollection implements PageCollectionInterfa
|
||||
*
|
||||
* @param array $accessLevels
|
||||
*
|
||||
* @return self The collection
|
||||
* @return static The collection
|
||||
*/
|
||||
public function ofOneOfTheseAccessLevels($accessLevels)
|
||||
{
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace Grav\Common\Page\Flex;
|
||||
|
||||
use Grav\Common\File\CompiledJsonFile;
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\Page\Interfaces\PageInterface;
|
||||
use Grav\Framework\Flex\FlexDirectory;
|
||||
use Grav\Framework\Flex\Interfaces\FlexObjectInterface;
|
||||
use Grav\Framework\Flex\Interfaces\FlexStorageInterface;
|
||||
@@ -32,6 +33,10 @@ class PageIndex extends FlexPageIndex
|
||||
protected $_root;
|
||||
protected $_params;
|
||||
|
||||
/**
|
||||
* @param array $entries
|
||||
* @param FlexDirectory|null $directory
|
||||
*/
|
||||
public function __construct(array $entries = [], FlexDirectory $directory = null)
|
||||
{
|
||||
// Remove root if it's taken.
|
||||
@@ -43,6 +48,11 @@ class PageIndex extends FlexPageIndex
|
||||
parent::__construct($entries, $directory);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $entries
|
||||
* @param string|null $keyField
|
||||
* @return $this|FlexPageIndex
|
||||
*/
|
||||
protected function createFrom(array $entries, string $keyField = null)
|
||||
{
|
||||
/** @var static $index */
|
||||
@@ -72,6 +82,10 @@ class PageIndex extends FlexPageIndex
|
||||
return static::updateIndexFile($storage, $index['index'], $entries, ['include_missing' => true]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return FlexObjectInterface|PageInterface|null
|
||||
*/
|
||||
public function get($key)
|
||||
{
|
||||
if (mb_strpos($key, '|') !== false) {
|
||||
@@ -86,6 +100,9 @@ class PageIndex extends FlexPageIndex
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return FlexObjectInterface|PageInterface
|
||||
*/
|
||||
public function getRoot()
|
||||
{
|
||||
$root = $this->_root;
|
||||
@@ -130,6 +147,10 @@ class PageIndex extends FlexPageIndex
|
||||
return $this->getParams();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FlexStorageInterface $storage
|
||||
* @return CompiledJsonFile|\Grav\Common\File\CompiledYamlFile|null
|
||||
*/
|
||||
protected static function getIndexFile(FlexStorageInterface $storage)
|
||||
{
|
||||
// Load saved index file.
|
||||
|
||||
@@ -322,7 +322,7 @@ class PageObject extends FlexPageObject
|
||||
|
||||
$children = $page->children();
|
||||
|
||||
/** @var PageInterface|PageObject $child */
|
||||
/** @var PageObject $child */
|
||||
foreach ($children as $child) {
|
||||
if ($field) {
|
||||
$payload = [
|
||||
@@ -487,6 +487,7 @@ class PageObject extends FlexPageObject
|
||||
throw new \RuntimeException(sprintf('Page %s cannot be moved to %s', '/' . $key, $parentRoute));
|
||||
}
|
||||
|
||||
/** @var PageObject $parent */
|
||||
$parent = $this->getFlexDirectory()->getObject($parentKey);
|
||||
if (!$parent) {
|
||||
// Page cannot be moved to non-existing location.
|
||||
|
||||
@@ -295,19 +295,18 @@ class PageStorage extends FolderStorage
|
||||
*/
|
||||
protected function saveRow(string $key, array $row): array
|
||||
{
|
||||
$grav = Grav::instance();
|
||||
|
||||
/** @var Debugger $debugger */
|
||||
$debugger = $grav['debugger'];
|
||||
// Initialize all key-related variables.
|
||||
$newKeys = $this->extractKeysFromRow($row);
|
||||
$newKey = $this->buildStorageKey($newKeys);
|
||||
$newFolder = $this->buildFolder($newKeys);
|
||||
$newFilename = $this->buildFilename($newKeys);
|
||||
$newFilepath = "{$newFolder}/{$newFilename}";
|
||||
|
||||
try {
|
||||
// Initialize all key-related variables.
|
||||
$newKeys = $this->extractKeysFromRow($row);
|
||||
$newKey = $this->buildStorageKey($newKeys);
|
||||
$newFolder = $this->buildFolder($newKeys);
|
||||
$newFilename = $this->buildFilename($newKeys);
|
||||
$newFilepath = "{$newFolder}/{$newFilename}";
|
||||
$grav = Grav::instance();
|
||||
|
||||
/** @var Debugger $debugger */
|
||||
$debugger = $grav['debugger'];
|
||||
$debugger->addMessage("Save page: {$newKey}", 'debug');
|
||||
|
||||
// Check if the row already exists.
|
||||
@@ -380,7 +379,9 @@ class PageStorage extends FolderStorage
|
||||
$locator->clearCache();
|
||||
}
|
||||
} catch (\RuntimeException $e) {
|
||||
throw new \RuntimeException(sprintf('Flex saveRow(%s): %s', $file->filename(), $e->getMessage()));
|
||||
$name = isset($file) ? $file->filename() : $newKey;
|
||||
|
||||
throw new \RuntimeException(sprintf('Flex saveRow(%s): %s', $name, $e->getMessage()));
|
||||
}
|
||||
|
||||
$row['__META'] = $this->getObjectMeta($newKey, true);
|
||||
|
||||
@@ -137,10 +137,9 @@ trait PageLegacyTrait
|
||||
/** @var Pages $pages */
|
||||
$pages = Grav::instance()['pages'];
|
||||
|
||||
/** @var Pages $pages */
|
||||
$inherited = $pages->inherited($this->getProperty('parent_route'), $field);
|
||||
$inheritedParams = $inherited ? (array)$inherited->value('header.' . $field) : [];
|
||||
$currentParams = (array)$this->value('header.' . $field);
|
||||
$currentParams = (array)$this->getFormValue('header.' . $field);
|
||||
if ($inheritedParams && is_array($inheritedParams)) {
|
||||
$currentParams = array_replace_recursive($inheritedParams, $currentParams);
|
||||
}
|
||||
@@ -185,7 +184,7 @@ trait PageLegacyTrait
|
||||
|
||||
if (is_string($params)) {
|
||||
// Look into a page header field.
|
||||
$params = (array)$this->value('header.' . $params);
|
||||
$params = (array)$this->getFormValue('header.' . $params);
|
||||
} elseif (!is_array($params)) {
|
||||
throw new \InvalidArgumentException('Argument should be either header variable name or array of parameters');
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ use Grav\Common\Utils;
|
||||
use Grav\Framework\Flex\Flex;
|
||||
use Grav\Framework\Flex\FlexDirectory;
|
||||
use Grav\Framework\Flex\Interfaces\FlexObjectInterface;
|
||||
use Grav\Framework\Flex\Interfaces\FlexTranslateInterface;
|
||||
use Grav\Plugin\Admin;
|
||||
use RocketTheme\Toolbox\Event\Event;
|
||||
use RocketTheme\Toolbox\Event\EventDispatcher;
|
||||
@@ -1166,7 +1167,7 @@ class Pages
|
||||
{
|
||||
$accessLevels = [];
|
||||
foreach ($this->all() as $page) {
|
||||
if (isset($page, $page->header()->access)) {
|
||||
if ($page && isset($page->header()->access)) {
|
||||
if (\is_array($page->header()->access)) {
|
||||
foreach ($page->header()->access as $index => $accessLevel) {
|
||||
if (\is_array($accessLevel)) {
|
||||
@@ -1374,8 +1375,9 @@ class Pages
|
||||
foreach ($collection as $key => $page) {
|
||||
$path = $page->path();
|
||||
|
||||
// FIXME: We really need to do better than this.
|
||||
$page = $page->hasTranslation() ? $page->getTranslation() : null;
|
||||
if ($page instanceof FlexTranslateInterface) {
|
||||
$page = $page && $page->hasTranslation() ? $page->getTranslation() : null;
|
||||
}
|
||||
|
||||
if (!$page || $path === $root_path) {
|
||||
continue;
|
||||
|
||||
@@ -122,10 +122,11 @@ trait CacheTrait
|
||||
if ($keys instanceof \Traversable) {
|
||||
$keys = iterator_to_array($keys, false);
|
||||
} elseif (!\is_array($keys)) {
|
||||
$isObject = \is_object($keys);
|
||||
throw new InvalidArgumentException(
|
||||
sprintf(
|
||||
'Cache keys must be array or Traversable, "%s" given',
|
||||
\is_object($keys) ? \get_class($keys) : \gettype($keys)
|
||||
$isObject ? \get_class($keys) : \gettype($keys)
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -137,6 +138,9 @@ trait CacheTrait
|
||||
$this->validateKeys($keys);
|
||||
$keys = array_unique($keys);
|
||||
$keys = array_combine($keys, $keys);
|
||||
if (empty($keys)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$list = $this->doGetMultiple($keys, $this->miss);
|
||||
|
||||
@@ -162,10 +166,11 @@ trait CacheTrait
|
||||
if ($values instanceof \Traversable) {
|
||||
$values = iterator_to_array($values, true);
|
||||
} elseif (!is_array($values)) {
|
||||
$isObject = \is_object($values);
|
||||
throw new InvalidArgumentException(
|
||||
sprintf(
|
||||
'Cache values must be array or Traversable, "%s" given',
|
||||
\is_object($values) ? \get_class($values) : \gettype($values)
|
||||
$isObject ? \get_class($values) : \gettype($values)
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -193,10 +198,11 @@ trait CacheTrait
|
||||
if ($keys instanceof \Traversable) {
|
||||
$keys = iterator_to_array($keys, false);
|
||||
} elseif (!is_array($keys)) {
|
||||
$isObject = \is_object($keys);
|
||||
throw new InvalidArgumentException(
|
||||
sprintf(
|
||||
'Cache keys must be array or Traversable, "%s" given',
|
||||
\is_object($keys) ? \get_class($keys) : \gettype($keys)
|
||||
$isObject ? \get_class($keys) : \gettype($keys)
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -247,7 +253,7 @@ trait CacheTrait
|
||||
|
||||
/**
|
||||
* @param array $values
|
||||
* @param int $ttl
|
||||
* @param int|null $ttl
|
||||
* @return bool
|
||||
*/
|
||||
public function doSetMultiple($values, $ttl)
|
||||
@@ -323,7 +329,7 @@ trait CacheTrait
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null|int|\DateInterval|mixed $ttl
|
||||
* @param null|int|\DateInterval $ttl
|
||||
* @return int|null
|
||||
* @throws \Psr\SimpleCache\InvalidArgumentException|InvalidArgumentException
|
||||
*/
|
||||
@@ -338,7 +344,8 @@ trait CacheTrait
|
||||
}
|
||||
|
||||
if ($ttl instanceof \DateInterval) {
|
||||
$ttl = (int) \DateTime::createFromFormat('U', '0')->add($ttl)->format('U');
|
||||
$date = \DateTime::createFromFormat('U', '0');
|
||||
$ttl = $date ? (int)$date->add($ttl)->format('U') : 0;
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException(
|
||||
|
||||
@@ -199,7 +199,7 @@ abstract class AbstractIndexCollection implements CollectionInterface
|
||||
{
|
||||
$key = $this->isAllowedElement($element) ? $this->getCurrentKey($element) : null;
|
||||
|
||||
return $key && isset($this->entries[$key]) ? $key : null;
|
||||
return $key && isset($this->entries[$key]) ? $key : false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -252,7 +252,7 @@ class FlexForm implements FlexFormInterface
|
||||
/**
|
||||
* Get form flash object.
|
||||
*
|
||||
* @return FlexFormFlash|FormFlashInterface
|
||||
* @return FlexFormFlash
|
||||
*/
|
||||
public function getFlash()
|
||||
{
|
||||
@@ -481,9 +481,9 @@ class FlexForm implements FlexFormInterface
|
||||
/**
|
||||
* Filter validated data.
|
||||
*
|
||||
* @param \ArrayAccess|Data $data
|
||||
* @param \ArrayAccess|Data|null $data
|
||||
*/
|
||||
protected function filterData($data): void
|
||||
protected function filterData($data = null): void
|
||||
{
|
||||
if ($data instanceof Data) {
|
||||
$data->filter(true, true);
|
||||
|
||||
@@ -34,10 +34,18 @@ interface FlexTranslateInterface
|
||||
*/
|
||||
public function getTranslation(string $languageCode = null, bool $fallback = null);
|
||||
|
||||
/**
|
||||
* Returns all translated languages.
|
||||
*
|
||||
* @param bool $includeDefault If set to true, return separate entries for '' and 'en' (default) language.
|
||||
* @return array
|
||||
*/
|
||||
public function getLanguages(bool $includeDefault = false): array;
|
||||
|
||||
/**
|
||||
* Get used language.
|
||||
*
|
||||
* @return string|null
|
||||
* @return string
|
||||
*/
|
||||
public function getLanguage(): ?string;
|
||||
public function getLanguage(): string;
|
||||
}
|
||||
|
||||
@@ -38,16 +38,20 @@ class FlexPageCollection extends FlexCollection
|
||||
|
||||
/**
|
||||
* @param bool $bool
|
||||
* @return FlexCollection|FlexPageCollection
|
||||
* @return FlexCollectionInterface|FlexPageCollection
|
||||
*/
|
||||
public function withPublished(bool $bool = true): FlexCollectionInterface
|
||||
public function withPublished(bool $bool = true)
|
||||
{
|
||||
$list = array_keys(array_filter($this->call('isPublished', [$bool])));
|
||||
|
||||
return $this->select($list);
|
||||
}
|
||||
|
||||
public function withVisible(bool $bool = true): FlexCollectionInterface
|
||||
/**
|
||||
* @param bool $bool
|
||||
* @return FlexCollectionInterface|FlexPageCollection
|
||||
*/
|
||||
public function withVisible(bool $bool = true)
|
||||
{
|
||||
$list = array_keys(array_filter($this->call('isVisible', [$bool])));
|
||||
|
||||
|
||||
@@ -281,7 +281,7 @@ trait PageContentTrait
|
||||
'process',
|
||||
$var,
|
||||
function ($value) {
|
||||
$value = array_replace(Grav::instance()['config']->get('system.pages.process', []), is_array($value) ? $value : []);
|
||||
$value = array_replace(Grav::instance()['config']->get('system.pages.process', []), is_array($value) ? $value : []) ?? [];
|
||||
foreach ($value as $process => $status) {
|
||||
$value[$process] = (bool)$status;
|
||||
}
|
||||
@@ -300,7 +300,21 @@ trait PageContentTrait
|
||||
'slug',
|
||||
$var,
|
||||
function ($value) {
|
||||
return $value ?: static::normalizeRoute(preg_replace(PAGE_ORDER_PREFIX_REGEX, '', $this->folder()));
|
||||
if (is_string($value)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
$folder = $this->folder();
|
||||
if (null === $folder) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$folder = preg_replace(PAGE_ORDER_PREFIX_REGEX, '', $folder);
|
||||
if (null === $folder) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return static::normalizeRoute($folder);
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -315,7 +329,10 @@ trait PageContentTrait
|
||||
$var,
|
||||
function ($value) {
|
||||
if (null === $value) {
|
||||
preg_match(static::PAGE_ORDER_REGEX, $this->folder(), $order);
|
||||
$folder = $this->folder();
|
||||
if (null !== $folder) {
|
||||
preg_match(static::PAGE_ORDER_REGEX, $folder, $order);
|
||||
}
|
||||
|
||||
$value = $order[1] ?? false;
|
||||
}
|
||||
@@ -526,7 +543,9 @@ trait PageContentTrait
|
||||
case 'ordering':
|
||||
return (bool)$this->order();
|
||||
case 'folder':
|
||||
return preg_replace(PAGE_ORDER_PREFIX_REGEX, '', $this->folder());
|
||||
$folder = $this->folder();
|
||||
|
||||
return null !== $folder ? preg_replace(PAGE_ORDER_PREFIX_REGEX, '', $folder) : '';
|
||||
case 'slug':
|
||||
return $this->slug();
|
||||
case 'published':
|
||||
@@ -754,11 +773,13 @@ trait PageContentTrait
|
||||
*/
|
||||
protected function processMarkdown($content, array $options = []): string
|
||||
{
|
||||
/** @var PageInterface $this */
|
||||
$excerpts = new Excerpts($this, $options);
|
||||
/** @var PageInterface $self */
|
||||
$self = $this;
|
||||
|
||||
$excerpts = new Excerpts($self, $options);
|
||||
|
||||
// Initialize the preferred variant of markdown parser.
|
||||
if (isset($defaults['extra'])) {
|
||||
if (isset($options['extra'])) {
|
||||
$parsedown = new ParsedownExtra($excerpts);
|
||||
} else {
|
||||
$parsedown = new Parsedown($excerpts);
|
||||
|
||||
@@ -20,8 +20,8 @@ use Grav\Common\Yaml;
|
||||
use Grav\Framework\Cache\CacheInterface;
|
||||
use Grav\Framework\File\Formatter\MarkdownFormatter;
|
||||
use Grav\Framework\File\Formatter\YamlFormatter;
|
||||
use Grav\Framework\Flex\Interfaces\FlexIndexInterface;
|
||||
use Grav\Framework\Flex\Interfaces\FlexObjectInterface;
|
||||
use Grav\Framework\Flex\Pages\FlexPageIndex;
|
||||
use RocketTheme\Toolbox\File\MarkdownFile;
|
||||
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
|
||||
|
||||
@@ -87,7 +87,7 @@ trait PageLegacyTrait
|
||||
}
|
||||
|
||||
$storage = $this->getFlexDirectory()->getStorage();
|
||||
if (method_exists($storage, 'readRaw')) {
|
||||
if (method_exists($storage, 'readFrontmatter')) {
|
||||
return $storage->readFrontmatter($this->getStorageKey());
|
||||
}
|
||||
|
||||
@@ -100,8 +100,8 @@ trait PageLegacyTrait
|
||||
/**
|
||||
* Modify a header value directly
|
||||
*
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @param string $key
|
||||
* @param string|array $value
|
||||
*/
|
||||
public function modifyHeader($key, $value): void
|
||||
{
|
||||
@@ -173,7 +173,7 @@ trait PageLegacyTrait
|
||||
// Content meta is generated during the content is being rendered, so make sure we have done it.
|
||||
$this->content();
|
||||
|
||||
return $this->getContentMeta();
|
||||
return $this->_content_meta ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -259,7 +259,8 @@ trait PageLegacyTrait
|
||||
if ($this->route() === $parent->route()) {
|
||||
throw new \RuntimeException('Failed: Cannot set page parent to self');
|
||||
}
|
||||
if (Utils::startsWith($parent->rawRoute(), $this->rawRoute())) {
|
||||
$rawRoute = $this->rawRoute();
|
||||
if ($rawRoute && Utils::startsWith($parent->rawRoute(), $rawRoute)) {
|
||||
throw new \RuntimeException('Failed: Cannot set page parent to a child of current page');
|
||||
}
|
||||
|
||||
@@ -280,9 +281,8 @@ trait PageLegacyTrait
|
||||
public function copy(PageInterface $parent = null)
|
||||
{
|
||||
$parentStorageKey = ltrim(dirname("/{$this->getStorageKey(true)}"), '/');
|
||||
$relocate = false;
|
||||
|
||||
/** @var FlexIndexInterface $index */
|
||||
/** @var FlexPageIndex $index */
|
||||
$index = $this->getFlexDirectory()->getIndex();
|
||||
|
||||
if ($parent) {
|
||||
@@ -290,7 +290,6 @@ trait PageLegacyTrait
|
||||
$k = $parent->getStorageKey(true);
|
||||
if ($k !== $parentStorageKey) {
|
||||
$parentStorageKey = $k;
|
||||
$relocate = true;
|
||||
}
|
||||
} else {
|
||||
throw new \RuntimeException('Cannot copy page, parent is of unknown type');
|
||||
@@ -391,7 +390,7 @@ trait PageLegacyTrait
|
||||
{
|
||||
return [
|
||||
'header' => (array)$this->header(),
|
||||
'content' => (string)$this->value('content')
|
||||
'content' => (string)$this->getFormValue('content')
|
||||
];
|
||||
}
|
||||
|
||||
@@ -412,7 +411,12 @@ trait PageLegacyTrait
|
||||
*/
|
||||
public function toJson(): string
|
||||
{
|
||||
return json_encode($this->toArray());
|
||||
$json = json_encode($this->toArray());
|
||||
if (!is_string($json)) {
|
||||
throw new \RuntimeException('Internal error');
|
||||
}
|
||||
|
||||
return $json;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -897,7 +901,7 @@ trait PageLegacyTrait
|
||||
*
|
||||
* @param int $direction either -1 or +1
|
||||
*
|
||||
* @return PageInterface|bool the sibling page
|
||||
* @return PageInterface|false the sibling page
|
||||
*/
|
||||
public function adjacentSibling($direction = 1)
|
||||
{
|
||||
@@ -909,7 +913,7 @@ trait PageLegacyTrait
|
||||
/**
|
||||
* Helper method to return an ancestor page.
|
||||
*
|
||||
* @param bool $lookup Name of the parent folder
|
||||
* @param bool|null $lookup Name of the parent folder
|
||||
*
|
||||
* @return PageInterface|null page you were looking for if it exists
|
||||
*/
|
||||
@@ -967,7 +971,7 @@ trait PageLegacyTrait
|
||||
/** @var Pages $pages */
|
||||
$inherited = $pages->inherited($this->getProperty('parent_route'), $field);
|
||||
$inheritedParams = $inherited ? (array)$inherited->value('header.' . $field) : [];
|
||||
$currentParams = (array)$this->value('header.' . $field);
|
||||
$currentParams = (array)$this->getFormValue('header.' . $field);
|
||||
if ($inheritedParams && is_array($inheritedParams)) {
|
||||
$currentParams = array_replace_recursive($inheritedParams, $currentParams);
|
||||
}
|
||||
@@ -1004,7 +1008,7 @@ trait PageLegacyTrait
|
||||
{
|
||||
if (is_string($params)) {
|
||||
// Look into a page header field.
|
||||
$params = (array)$this->value('header.' . $params);
|
||||
$params = (array)$this->getFormValue('header.' . $params);
|
||||
} elseif (!is_array($params)) {
|
||||
throw new \InvalidArgumentException('Argument should be either header variable name or array of parameters');
|
||||
}
|
||||
@@ -1091,6 +1095,7 @@ trait PageLegacyTrait
|
||||
* @return CacheInterface
|
||||
*/
|
||||
abstract public function getCache(string $namespace = null);
|
||||
abstract public function parent(PageInterface $var = null);
|
||||
|
||||
abstract protected function exists(): bool;
|
||||
abstract protected function getStorageFolder();
|
||||
|
||||
@@ -263,8 +263,9 @@ trait PageRoutableTrait
|
||||
|
||||
/** @var UniformResourceLocator $locator */
|
||||
$locator = Grav::instance()['locator'];
|
||||
$path = $locator->findResource($folder, false);
|
||||
|
||||
return $locator->findResource($folder, false);
|
||||
return is_string($path) ? $path : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -288,10 +289,13 @@ trait PageRoutableTrait
|
||||
$folder = $this->getStorageFolder();
|
||||
}
|
||||
|
||||
/** @var UniformResourceLocator $locator */
|
||||
$locator = Grav::instance()['locator'];
|
||||
if ($folder) {
|
||||
/** @var UniformResourceLocator $locator */
|
||||
$locator = Grav::instance()['locator'];
|
||||
$folder = $locator($folder);
|
||||
}
|
||||
|
||||
return $folder ? $locator($folder) : null;
|
||||
return is_string($folder) ? $folder : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -354,8 +358,14 @@ trait PageRoutableTrait
|
||||
}
|
||||
|
||||
$parentKey = ltrim(dirname("/{$this->getKey()}"), '/');
|
||||
$directory = $this->getFlexDirectory();
|
||||
if ($parentKey) {
|
||||
return $directory->getObject($parentKey);
|
||||
}
|
||||
|
||||
return $parentKey ? $this->getFlexDirectory()->getObject($parentKey) : $this->getFlexDirectory()->getIndex()->getRoot();
|
||||
$index = $directory->getIndex();
|
||||
|
||||
return $index->getRoot();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -386,8 +396,8 @@ trait PageRoutableTrait
|
||||
{
|
||||
$parent = $this->parent();
|
||||
$collection = $parent ? $parent->collection('content', false) : null;
|
||||
if ($collection instanceof PageCollectionInterface) {
|
||||
return $collection->currentPosition($this->path());
|
||||
if ($collection instanceof PageCollectionInterface && $path = $this->path()) {
|
||||
return $collection->currentPosition($path);
|
||||
}
|
||||
|
||||
return 1;
|
||||
@@ -422,7 +432,7 @@ trait PageRoutableTrait
|
||||
$routes = $pages->routes();
|
||||
|
||||
if (isset($routes[$uri_path])) {
|
||||
/** @var PageInterface $child_page */
|
||||
/** @var PageInterface $child_page|null */
|
||||
$child_page = $pages->dispatch($uri->route())->parent();
|
||||
if ($child_page) {
|
||||
while (!$child_page->root()) {
|
||||
|
||||
@@ -12,6 +12,7 @@ namespace Grav\Framework\Flex\Pages\Traits;
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\Language\Language;
|
||||
use Grav\Common\Page\Interfaces\PageInterface;
|
||||
use Grav\Framework\Flex\Interfaces\FlexObjectInterface;
|
||||
|
||||
/**
|
||||
* Implements PageTranslateInterface
|
||||
@@ -39,7 +40,7 @@ trait PageTranslateTrait
|
||||
/**
|
||||
* @param string|null $languageCode
|
||||
* @param bool|null $fallback
|
||||
* @return static|null
|
||||
* @return FlexObjectInterface|null
|
||||
*/
|
||||
public function getTranslation(string $languageCode = null, bool $fallback = null)
|
||||
{
|
||||
@@ -84,7 +85,12 @@ trait PageTranslateTrait
|
||||
$languages = array_fill_keys($languages, false);
|
||||
$translated = array_fill_keys(array_keys($translated), true);
|
||||
|
||||
return array_replace($languages, $translated);
|
||||
$all = array_replace($languages, $translated);
|
||||
if (null === $all) {
|
||||
throw new \RuntimeException('Unexpected result');
|
||||
}
|
||||
|
||||
return $all;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -109,6 +115,9 @@ trait PageTranslateTrait
|
||||
return array_keys($languages);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLanguage(): string
|
||||
{
|
||||
return $this->language() ?? '';
|
||||
@@ -116,7 +125,7 @@ trait PageTranslateTrait
|
||||
|
||||
/**
|
||||
* @param string|null $languageCode
|
||||
* @param array|null $fallback
|
||||
* @param bool|null $fallback
|
||||
* @return string|null
|
||||
*/
|
||||
protected function findTranslation(string $languageCode = null, bool $fallback = null): ?string
|
||||
@@ -146,7 +155,6 @@ trait PageTranslateTrait
|
||||
* Return an array with the routes of other translated languages
|
||||
*
|
||||
* @param bool $onlyPublished only return published translations
|
||||
*
|
||||
* @return array the page translated languages
|
||||
*/
|
||||
public function translatedLanguages($onlyPublished = false): array
|
||||
@@ -178,7 +186,6 @@ trait PageTranslateTrait
|
||||
* Return an array listing untranslated languages available
|
||||
*
|
||||
* @param bool $includeUnpublished also list unpublished translations
|
||||
*
|
||||
* @return array the page untranslated languages
|
||||
*/
|
||||
public function untranslatedLanguages($includeUnpublished = false): array
|
||||
@@ -197,8 +204,7 @@ trait PageTranslateTrait
|
||||
/**
|
||||
* Get page language
|
||||
*
|
||||
* @param $var
|
||||
*
|
||||
* @param string $var
|
||||
* @return string|null
|
||||
*/
|
||||
public function language($var = null): ?string
|
||||
|
||||
@@ -26,7 +26,7 @@ trait FlexAuthorizeTrait
|
||||
public function isAuthorized(string $action, string $scope = null, UserInterface $user = null) : bool
|
||||
{
|
||||
if (null === $user) {
|
||||
/** @var UserInterface $user */
|
||||
/** @var UserInterface|null $user */
|
||||
$user = Grav::instance()['user'] ?? null;
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,6 @@ use Grav\Common\Filesystem\Folder;
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\Media\Interfaces\MediaCollectionInterface;
|
||||
use Grav\Common\Media\Traits\MediaTrait;
|
||||
use Grav\Common\Page\Medium\AbstractMedia;
|
||||
use Grav\Common\Page\Medium\Medium;
|
||||
use Grav\Common\Page\Medium\MediumFactory;
|
||||
use Grav\Common\Utils;
|
||||
@@ -66,15 +65,13 @@ trait FlexMediaTrait
|
||||
public function getMedia()
|
||||
{
|
||||
if ($this->media === null) {
|
||||
/** @var AbstractMedia $media */
|
||||
$updated = false;
|
||||
$media = $this->getExistingMedia();
|
||||
|
||||
// Include uploaded media to the object media.
|
||||
/** @var FormFlashFile|null $upload */
|
||||
$updated = false;
|
||||
foreach ($this->getUpdatedMedia() as $filename => $upload) {
|
||||
// Just make sure we do not include removed or moved media.
|
||||
if ($upload && $upload->getError() === \UPLOAD_ERR_OK && !$upload->isMoved()) {
|
||||
if (null !== $upload && $upload->getError() === \UPLOAD_ERR_OK && !$upload->isMoved()) {
|
||||
$updated = true;
|
||||
$media->add($filename, MediumFactory::fromUploadedFile($upload));
|
||||
}
|
||||
@@ -110,7 +107,7 @@ trait FlexMediaTrait
|
||||
throw new RuntimeException($language->translate('PLUGIN_ADMIN.UNKNOWN_ERRORS'), 400);
|
||||
}
|
||||
|
||||
$filename = $uploadedFile->getClientFilename();
|
||||
$filename = $uploadedFile->getClientFilename() ?? '';
|
||||
|
||||
if (!Utils::checkFilename($filename)) {
|
||||
throw new RuntimeException(sprintf($language->translate('PLUGIN_ADMIN.FILEUPLOAD_UNABLE_TO_UPLOAD'), $filename, 'Bad filename'), 400);
|
||||
@@ -164,7 +161,7 @@ trait FlexMediaTrait
|
||||
}
|
||||
|
||||
if ($locator->isStream($path)) {
|
||||
$path = $locator->findResource($path, true, true);
|
||||
$path = (string)$locator->findResource($path, true, true);
|
||||
$locator->clearCache($path);
|
||||
}
|
||||
|
||||
@@ -180,7 +177,7 @@ trait FlexMediaTrait
|
||||
}
|
||||
if ($uploadedFile->getError() === \UPLOAD_ERR_OK) {
|
||||
$uploadedFile->moveTo($filepath);
|
||||
} elseif (!file_exists($filepath) && $pos = strpos($filename, '/')) {
|
||||
} elseif ($filename && !file_exists($filepath) && $pos = strpos($filename, '/')) {
|
||||
$origpath = sprintf('%s/%s', $path, substr($filename, $pos));
|
||||
if (file_exists($origpath)) {
|
||||
copy($origpath, $filepath);
|
||||
@@ -223,8 +220,8 @@ trait FlexMediaTrait
|
||||
$targetPath = $path . '/' . $dirname;
|
||||
$targetFile = $path . '/' . $filename;
|
||||
if ($locator->isStream($targetFile)) {
|
||||
$targetPath = $locator->findResource($targetPath, true, true);
|
||||
$targetFile = $locator->findResource($targetFile, true, true);
|
||||
$targetPath = (string)$locator->findResource($targetPath, true, true);
|
||||
$targetFile = (string)$locator->findResource($targetFile, true, true);
|
||||
$locator->clearCache($targetPath);
|
||||
$locator->clearCache($targetFile);
|
||||
}
|
||||
@@ -243,15 +240,20 @@ trait FlexMediaTrait
|
||||
}
|
||||
|
||||
// Remove Extra Files
|
||||
foreach (scandir($targetPath, SCANDIR_SORT_NONE) as $file) {
|
||||
$dir = scandir($targetPath, SCANDIR_SORT_NONE);
|
||||
if (false === $dir) {
|
||||
throw new \RuntimeException('Internal error');
|
||||
}
|
||||
|
||||
foreach ($dir as $file) {
|
||||
$preg_name = preg_quote($fileParts['filename'], '`');
|
||||
$preg_ext =preg_quote($fileParts['extension'], '`');
|
||||
$preg_ext = preg_quote($fileParts['extension'] ?? '.', '`');
|
||||
$preg_filename = preg_quote($basename, '`');
|
||||
|
||||
if (preg_match("`({$preg_name}@\d+x\.{$preg_ext}(?:\.meta\.yaml)?$|{$preg_filename}\.meta\.yaml)$`", $file)) {
|
||||
$testPath = $targetPath . '/' . $file;
|
||||
if ($locator->isStream($testPath)) {
|
||||
$testPath = $locator->findResource($testPath, true, true);
|
||||
$testPath = (string)$locator->findResource($testPath, true, true);
|
||||
$locator->clearCache($testPath);
|
||||
}
|
||||
|
||||
@@ -274,11 +276,11 @@ trait FlexMediaTrait
|
||||
{
|
||||
$list = [];
|
||||
foreach ($files as $field => $group) {
|
||||
if ($field === '' || \strpos($field, '/')) {
|
||||
if ($field === '' || \strpos((string)$field, '/')) {
|
||||
continue;
|
||||
}
|
||||
foreach ($group as $filename => $file) {
|
||||
$list[$filename] = $file;
|
||||
$list[(string)$filename] = $file;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -286,7 +288,7 @@ trait FlexMediaTrait
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<UploadedFileInterface,null>
|
||||
* @return array<UploadedFileInterface|null>
|
||||
*/
|
||||
protected function getUpdatedMedia(): array
|
||||
{
|
||||
@@ -323,7 +325,7 @@ trait FlexMediaTrait
|
||||
|
||||
$file = $uri && $locator->isStream($uri) ? $locator->findResource($uri) : $uri;
|
||||
|
||||
return $file && file_exists($file) ? MediumFactory::fromFile($file) : null;
|
||||
return is_string($file) && file_exists($file) ? MediumFactory::fromFile($file) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -26,6 +26,7 @@ use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
use Twig\Error\LoaderError;
|
||||
use Twig\Error\SyntaxError;
|
||||
use Twig\Template;
|
||||
use Twig\TemplateWrapper;
|
||||
|
||||
/**
|
||||
@@ -434,7 +435,7 @@ trait FormTrait
|
||||
/** @var Grav $grav */
|
||||
$grav = Grav::instance();
|
||||
|
||||
/** @var SessionInterface $session */
|
||||
/** @var SessionInterface|null $session */
|
||||
$session = $grav['session'] ?? null;
|
||||
|
||||
$this->sessionid = $session ? ($session->getId() ?? '') : '';
|
||||
@@ -457,7 +458,7 @@ trait FormTrait
|
||||
{
|
||||
$grav = Grav::instance();
|
||||
|
||||
/** @var UserInterface $user */
|
||||
/** @var UserInterface|null $user */
|
||||
$user = $grav['user'] ?? null;
|
||||
$userExists = $user && $user->exists();
|
||||
$username = $userExists ? $user->username : null;
|
||||
@@ -520,7 +521,7 @@ trait FormTrait
|
||||
|
||||
/**
|
||||
* @param string $layout
|
||||
* @return TemplateWrapper
|
||||
* @return Template|TemplateWrapper
|
||||
* @throws LoaderError
|
||||
* @throws SyntaxError
|
||||
*/
|
||||
@@ -587,11 +588,11 @@ trait FormTrait
|
||||
/**
|
||||
* Validate data and throw validation exceptions if validation fails.
|
||||
*
|
||||
* @param \ArrayAccess|Data $data
|
||||
* @param \ArrayAccess|Data|null $data
|
||||
* @throws ValidationException
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function validateData($data): void
|
||||
protected function validateData($data = null): void
|
||||
{
|
||||
if ($data instanceof Data) {
|
||||
$data->validate();
|
||||
@@ -601,9 +602,9 @@ trait FormTrait
|
||||
/**
|
||||
* Filter validated data.
|
||||
*
|
||||
* @param \ArrayAccess|Data $data
|
||||
* @param \ArrayAccess|Data|null $data
|
||||
*/
|
||||
protected function filterData($data): void
|
||||
protected function filterData($data = null): void
|
||||
{
|
||||
if ($data instanceof Data) {
|
||||
$data->filter();
|
||||
@@ -639,7 +640,7 @@ trait FormTrait
|
||||
// Handle bad filenames.
|
||||
$filename = $file->getClientFilename();
|
||||
|
||||
if (!Utils::checkFilename($filename)) {
|
||||
if ($filename && !Utils::checkFilename($filename)) {
|
||||
$grav = Grav::instance();
|
||||
throw new \RuntimeException(
|
||||
sprintf($grav['language']->translate('PLUGIN_FORM.FILEUPLOAD_UNABLE_TO_UPLOAD', null, true), $filename, 'Bad filename')
|
||||
@@ -662,6 +663,9 @@ trait FormTrait
|
||||
// Decode JSON encoded fields and merge them to data.
|
||||
if (isset($data['_json'])) {
|
||||
$data = array_replace_recursive($data, $this->jsonDecode($data['_json']));
|
||||
if (null === $data) {
|
||||
throw new \RuntimeException(__METHOD__ . '(): Unexpected error');
|
||||
}
|
||||
unset($data['_json']);
|
||||
}
|
||||
|
||||
|
||||
@@ -54,7 +54,7 @@ trait NestedPropertyCollectionTrait
|
||||
|
||||
/**
|
||||
* @param string $property Object property to be updated.
|
||||
* @param string $value New value.
|
||||
* @param mixed $value New value.
|
||||
* @param string $separator Separator, defaults to '.'
|
||||
* @return $this
|
||||
*/
|
||||
|
||||
@@ -73,7 +73,7 @@ trait NestedPropertyTrait
|
||||
|
||||
/**
|
||||
* @param string $property Object property to be updated.
|
||||
* @param string $value New value.
|
||||
* @param mixed $value New value.
|
||||
* @param string $separator Separator, defaults to '.'
|
||||
* @return $this
|
||||
* @throws \RuntimeException
|
||||
|
||||
@@ -54,7 +54,7 @@ trait ObjectCollectionTrait
|
||||
|
||||
/** @var ObjectInterface $element */
|
||||
foreach ($this->getIterator() as $id => $element) {
|
||||
$list[$id] = $element->hasProperty($property);
|
||||
$list[$id] = (bool)$element->hasProperty($property);
|
||||
}
|
||||
|
||||
return $list;
|
||||
@@ -135,8 +135,8 @@ trait ObjectCollectionTrait
|
||||
* @var ObjectInterface $element
|
||||
*/
|
||||
foreach ($this->getIterator() as $id => $element) {
|
||||
$callable = method_exists($element, $method) ? [$element, $method] : null;
|
||||
$list[$id] = $callable ? \call_user_func_array($callable, $arguments) : null;
|
||||
$callable = [$element, $method];
|
||||
$list[$id] = is_callable($callable) ? \call_user_func_array($callable, $arguments) : null;
|
||||
}
|
||||
|
||||
return $list;
|
||||
|
||||
@@ -66,7 +66,7 @@ trait ObjectTrait
|
||||
|
||||
/**
|
||||
* @param string $property Object property name.
|
||||
* @return bool|bool[] True if property has been defined (can be null).
|
||||
* @return bool True if property has been defined (can be null).
|
||||
*/
|
||||
public function hasProperty($property)
|
||||
{
|
||||
@@ -76,7 +76,7 @@ trait ObjectTrait
|
||||
/**
|
||||
* @param string $property Object property to be fetched.
|
||||
* @param mixed $default Default value if property has not been set.
|
||||
* @return mixed|mixed[] Property value.
|
||||
* @return mixed Property value.
|
||||
*/
|
||||
public function getProperty($property, $default = null)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Grav\Framework\Object
|
||||
*
|
||||
* @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
|
||||
* @license MIT License; see LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace Grav\Framework\Object\Interfaces;
|
||||
|
||||
/**
|
||||
* Common Interface for both Objects and Collections
|
||||
* @package Grav\Framework\Object
|
||||
*/
|
||||
interface NestedObjectCollectionInterface extends ObjectCollectionInterface
|
||||
{
|
||||
/**
|
||||
* @param string $property Object property name.
|
||||
* @param string|null $separator Separator, defaults to '.'
|
||||
* @return bool[] List of [key => bool] pairs.
|
||||
*/
|
||||
public function hasNestedProperty($property, $separator = null);
|
||||
|
||||
/**
|
||||
* @param string $property Object property to be fetched.
|
||||
* @param mixed|null $default Default value if property has not been set.
|
||||
* @param string|null $separator Separator, defaults to '.'
|
||||
* @return mixed[] List of [key => value] pairs.
|
||||
*/
|
||||
public function getNestedProperty($property, $default = null, $separator = null);
|
||||
|
||||
/**
|
||||
* @param string $property Object property to be updated.
|
||||
* @param mixed $value New value.
|
||||
* @param string|null $separator Separator, defaults to '.'
|
||||
* @return $this
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function setNestedProperty($property, $value, $separator = null);
|
||||
|
||||
/**
|
||||
* @param string $property Object property to be defined.
|
||||
* @param mixed $default Default value.
|
||||
* @param string|null $separator Separator, defaults to '.'
|
||||
* @return $this
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function defNestedProperty($property, $default, $separator = null);
|
||||
|
||||
/**
|
||||
* @param string $property Object property to be unset.
|
||||
* @param string|null $separator Separator, defaults to '.'
|
||||
* @return $this
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function unsetNestedProperty($property, $separator = null);
|
||||
}
|
||||
@@ -16,14 +16,17 @@ use Grav\Framework\Collection\CollectionInterface;
|
||||
* ObjectCollection Interface
|
||||
* @package Grav\Framework\Collection
|
||||
*/
|
||||
interface ObjectCollectionInterface extends CollectionInterface, Selectable, ObjectInterface
|
||||
interface ObjectCollectionInterface extends CollectionInterface, Selectable, \Serializable
|
||||
{
|
||||
/**
|
||||
* Create a copy from this collection by cloning all objects in the collection.
|
||||
*
|
||||
* @return static
|
||||
* @return string
|
||||
*/
|
||||
public function copy();
|
||||
public function getType();
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getKey();
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
@@ -31,6 +34,46 @@ interface ObjectCollectionInterface extends CollectionInterface, Selectable, Obj
|
||||
*/
|
||||
public function setKey($key);
|
||||
|
||||
/**
|
||||
* @param string $property Object property name.
|
||||
* @return bool[] List of [key => bool] pairs.
|
||||
*/
|
||||
public function hasProperty($property);
|
||||
|
||||
/**
|
||||
* @param string $property Object property to be fetched.
|
||||
* @param mixed|null $default Default value if property has not been set.
|
||||
* @return mixed[] List of [key => value] pairs.
|
||||
*/
|
||||
public function getProperty($property, $default = null);
|
||||
|
||||
/**
|
||||
* @param string $property Object property to be updated.
|
||||
* @param mixed $value New value.
|
||||
* @return $this
|
||||
*/
|
||||
public function setProperty($property, $value);
|
||||
|
||||
/**
|
||||
* @param string $property Object property to be defined.
|
||||
* @param mixed $default Default value.
|
||||
* @return $this
|
||||
*/
|
||||
public function defProperty($property, $default);
|
||||
|
||||
/**
|
||||
* @param string $property Object property to be unset.
|
||||
* @return $this
|
||||
*/
|
||||
public function unsetProperty($property);
|
||||
|
||||
/**
|
||||
* Create a copy from this collection by cloning all objects in the collection.
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function copy();
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
|
||||
@@ -27,14 +27,14 @@ interface ObjectInterface extends \Serializable, \JsonSerializable
|
||||
|
||||
/**
|
||||
* @param string $property Object property name.
|
||||
* @return bool|bool[] True if property has been defined (can be null).
|
||||
* @return bool True if property has been defined (property can be null).
|
||||
*/
|
||||
public function hasProperty($property);
|
||||
|
||||
/**
|
||||
* @param string $property Object property to be fetched.
|
||||
* @param mixed|null $default Default value if property has not been set.
|
||||
* @return mixed|mixed[] Property value.
|
||||
* @return mixed Property value.
|
||||
*/
|
||||
public function getProperty($property, $default = null);
|
||||
|
||||
|
||||
@@ -15,13 +15,12 @@ use Grav\Framework\Collection\ArrayCollection;
|
||||
use Grav\Framework\Object\Access\NestedPropertyCollectionTrait;
|
||||
use Grav\Framework\Object\Base\ObjectCollectionTrait;
|
||||
use Grav\Framework\Object\Collection\ObjectExpressionVisitor;
|
||||
use Grav\Framework\Object\Interfaces\NestedObjectInterface;
|
||||
use Grav\Framework\Object\Interfaces\ObjectCollectionInterface;
|
||||
use Grav\Framework\Object\Interfaces\NestedObjectCollectionInterface;
|
||||
|
||||
/**
|
||||
* Class contains a collection of objects.
|
||||
*/
|
||||
class ObjectCollection extends ArrayCollection implements ObjectCollectionInterface, NestedObjectInterface
|
||||
class ObjectCollection extends ArrayCollection implements NestedObjectCollectionInterface
|
||||
{
|
||||
use ObjectCollectionTrait;
|
||||
use NestedPropertyCollectionTrait {
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace Grav\Framework\Object;
|
||||
|
||||
use Doctrine\Common\Collections\Criteria;
|
||||
use Grav\Framework\Collection\AbstractIndexCollection;
|
||||
use Grav\Framework\Object\Interfaces\NestedObjectInterface;
|
||||
use Grav\Framework\Object\Interfaces\NestedObjectCollectionInterface;
|
||||
use Grav\Framework\Object\Interfaces\ObjectCollectionInterface;
|
||||
|
||||
/**
|
||||
@@ -21,7 +21,7 @@ use Grav\Framework\Object\Interfaces\ObjectCollectionInterface;
|
||||
* This is an abstract class and has some protected abstract methods to load objects which you need to implement in
|
||||
* order to use the class.
|
||||
*/
|
||||
abstract class ObjectIndex extends AbstractIndexCollection implements ObjectCollectionInterface, NestedObjectInterface
|
||||
abstract class ObjectIndex extends AbstractIndexCollection implements NestedObjectCollectionInterface
|
||||
{
|
||||
/** @var string */
|
||||
protected static $type;
|
||||
@@ -68,7 +68,7 @@ abstract class ObjectIndex extends AbstractIndexCollection implements ObjectColl
|
||||
|
||||
/**
|
||||
* @param string $property Object property name.
|
||||
* @return array True if property has been defined (can be null).
|
||||
* @return bool[] True if property has been defined (can be null).
|
||||
*/
|
||||
public function hasProperty($property)
|
||||
{
|
||||
@@ -78,7 +78,7 @@ abstract class ObjectIndex extends AbstractIndexCollection implements ObjectColl
|
||||
/**
|
||||
* @param string $property Object property to be fetched.
|
||||
* @param mixed $default Default value if property has not been set.
|
||||
* @return array Property values.
|
||||
* @return mixed[] Property values.
|
||||
*/
|
||||
public function getProperty($property, $default = null)
|
||||
{
|
||||
@@ -117,7 +117,7 @@ abstract class ObjectIndex extends AbstractIndexCollection implements ObjectColl
|
||||
/**
|
||||
* @param string $property Object property name.
|
||||
* @param string $separator Separator, defaults to '.'
|
||||
* @return bool True if property has been defined (can be null).
|
||||
* @return bool[] True if property has been defined (can be null).
|
||||
*/
|
||||
public function hasNestedProperty($property, $separator = null)
|
||||
{
|
||||
@@ -128,7 +128,7 @@ abstract class ObjectIndex extends AbstractIndexCollection implements ObjectColl
|
||||
* @param string $property Object property to be fetched.
|
||||
* @param mixed $default Default value if property has not been set.
|
||||
* @param string $separator Separator, defaults to '.'
|
||||
* @return mixed Property value.
|
||||
* @return mixed[] Property values.
|
||||
*/
|
||||
public function getNestedProperty($property, $default = null, $separator = null)
|
||||
{
|
||||
@@ -137,7 +137,7 @@ abstract class ObjectIndex extends AbstractIndexCollection implements ObjectColl
|
||||
|
||||
/**
|
||||
* @param string $property Object property to be updated.
|
||||
* @param string $value New value.
|
||||
* @param mixed $value New value.
|
||||
* @param string $separator Separator, defaults to '.'
|
||||
* @return ObjectCollectionInterface
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user