Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libraries/src/MVC/Model/ListModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public function getActiveFilters()
foreach ($this->filter_fields as $filter) {
$filterName = 'filter.' . $filter;

if (property_exists($this->state, $filterName) && (!empty($this->state->{$filterName}) || is_numeric($this->state->{$filterName}))) {
if (!empty($this->state->get($filterName)) || is_numeric($this->state->get($filterName))) {
$activeFilters[$filter] = $this->state->get($filterName);
}
}
Expand Down
137 changes: 137 additions & 0 deletions libraries/src/MVC/Model/State.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

/**
* Joomla! Content Management System
*
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\CMS\MVC\Model;

use Joomla\Registry\Registry;

// phpcs:disable PSR1.Files.SideEffects
\defined('JPATH_PLATFORM') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
* A simple state holder class. This class acts for transition from CMSObject to Registry
* and should not be used directly. Instead of, use the Registry class.
*
* @since __DEPLOY_VERSION__
*
* @deprecated 7.0 Use the Registry directly
*/
class State extends Registry
{
/**
* Constructor
*
* @param mixed $data The data to bind to the new Registry object.
*
* @since __DEPLOY_VERSION__
*/
public function __construct($data = null)
{
parent::__construct($data);

// To speed up things
$this->separator = null;
}

/**
* Get a registry value.
*
* @param string $path Registry path (e.g. joomla.content.showauthor)
* @param mixed $default Optional default value, returned if the internal value is null.
*
* @return mixed Value of entry or null
*
* @since __DEPLOY_VERSION__
*/
public function get($path, $default = null)
{
if (isset($this->data->$path) && empty($this->data->$path)) {
@trigger_error(
sprintf('Instead of an empty value, the default value will be returned in 7.0 in %s::%s.', __METHOD__, __CLASS__),
E_USER_DEPRECATED
);
return $this->data->$path;
}

return parent::get($path, $default);
}

/**
* Returns an associative array of object properties.
*
* @return array The data array
*
* @since __DEPLOY_VERSION__
*
* @deprecated 7.0 Use toArray instead
*/
public function getProperties()
{
return $this->toArray();
}

/**
* Proxy for internal data access for the given name.
*
* @param string $name The name of the element
*
* @return mixed The value of the element if set, null otherwise
*
* @since __DEPLOY_VERSION__
*
* @deprecated __DEPLOY_VERSION__ will be removed in 7.0
*
*/
public function __get($name)
{
@trigger_error(sprintf('Direct property access will not be supported in 7.0 in %s::%s.', __METHOD__, __CLASS__), E_USER_DEPRECATED);

return $this->get($name);
}

/**
* Proxy for internal data storage for the given name and value.
*
* @param string $name The name of the element
* @param string $value The value
*
* @return void
*
* @since __DEPLOY_VERSION__
*
* @deprecated __DEPLOY_VERSION__ will be removed in 7.0
*
*/
public function __set($name, $value)
{
@trigger_error(sprintf('Direct property access will not be supported in 7.0 in %s::%s.', __METHOD__, __CLASS__), E_USER_DEPRECATED);

return $this->set($name, $value);
}

/**
* Proxy for internal data check for a variable with the given key.
*
* @param string $name The name of the element
*
* @return bool Returns if the internal data storage contains a key with the given
*
* @since __DEPLOY_VERSION__
*
* @deprecated __DEPLOY_VERSION__ will be removed in 7.0
*
*/
public function __isset($name)
{
@trigger_error(sprintf('Direct property access will not be supported in 7.0 in %s::%s.', __METHOD__, __CLASS__), E_USER_DEPRECATED);

return $this->exists($name);
}
}
8 changes: 3 additions & 5 deletions libraries/src/MVC/Model/StateBehaviorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

namespace Joomla\CMS\MVC\Model;

use Joomla\CMS\Object\CMSObject;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
Expand All @@ -33,7 +31,7 @@ trait StateBehaviorTrait
/**
* A state object
*
* @var CMSObject
* @var State
* @since 4.0.0
*/
protected $state = null;
Expand All @@ -51,7 +49,7 @@ trait StateBehaviorTrait
public function getState($property = null, $default = null)
{
if ($this->state === null) {
$this->state = new CMSObject();
$this->state = new State();
}

if (!$this->__state_set) {
Expand All @@ -78,7 +76,7 @@ public function getState($property = null, $default = null)
public function setState($property, $value = null)
{
if ($this->state === null) {
$this->state = new CMSObject();
$this->state = new State();
}

return $this->state->set($property, $value);
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Libraries/Cms/MVC/Model/StateBehaviorTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

namespace Joomla\Tests\Unit\Libraries\Cms\MVC\Model;

use Joomla\CMS\MVC\Model\State;
use Joomla\CMS\MVC\Model\StateBehaviorTrait;
use Joomla\CMS\Object\CMSObject;
use Joomla\Tests\Unit\UnitTestCase;

/**
Expand Down Expand Up @@ -39,7 +39,7 @@ public function testGetEmptyState()
use StateBehaviorTrait;
};

$this->assertInstanceOf(CMSObject::class, $trait->getState());
$this->assertInstanceOf(State::class, $trait->getState());
}

/**
Expand Down
119 changes: 119 additions & 0 deletions tests/Unit/Libraries/Cms/MVC/Model/StateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

/**
* @package Joomla.UnitTest
* @subpackage Base
*
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\Tests\Unit\Libraries\Cms\MVC\Model;

use Joomla\CMS\MVC\Model\State;
use Joomla\Tests\Unit\UnitTestCase;

/**
* Test class for \Joomla\CMS\MVC\Model\State
*
* @package Joomla.UnitTest
* @subpackage MVC
*
* @testdox The State
*
* @since __DEPLOY_VERSION__
*/
class StateTest extends UnitTestCase
{
/**
* @testdox can set and get a property
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function testGetProperties()
{
$state = new State();
$state->set('unit', 'test');

$this->assertCount(1, $state->getProperties());
$this->assertEquals('test', $state->getProperties()['unit']);
}

/**
* @testdox can access property
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function testGetDirectPropertyAccess()
{
$state = new State();
$state->set('unit', 'test');

$this->assertEquals('test', $state->unit);
$this->assertEquals('test', $state->get('unit'));
}

/**
* @testdox can set a value through the direct property
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function testSetDirectPropertyAccess()
{
$state = new State();
$state->unit = 'test';

$this->assertEquals('test', $state->unit);
$this->assertEquals('test', $state->get('unit'));
}

/**
* @testdox can return if a property is set
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function testIsSet()
{
$state = new State();
$state->unit = 'test';

$this->assertTrue(isset($state->unit));
}

/**
* @testdox can return if a property is not set
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function testIsNotSet()
{
$state = new State();

$this->assertFalse(isset($state->unit));
}

/**
* @testdox can set and get an empty value
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function testEmptyValue()
{
$state = new State();
$state->set('unit', '');

$this->assertEquals('', $state->get('unit', 'test'));
}
}