MDL-76221 core: method to filter by defined properties of persistent.

This commit is contained in:
Paul Holden 2022-11-17 20:53:57 +00:00
parent 12e9d9e1bf
commit 121f5b6300
3 changed files with 30 additions and 21 deletions

View File

@ -14,26 +14,18 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Abstract class for objects saved to the DB.
*
* @package core
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
defined('MOODLE_INTERNAL') || die();
use coding_exception;
use invalid_parameter_exception;
use lang_string;
use ReflectionMethod;
use stdClass;
use renderer_base;
/**
* Abstract class for core objects saved to the DB.
*
* @package core
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@ -328,6 +320,16 @@ abstract class persistent {
return $def;
}
/**
* For a given record, return an array containing only those properties that are defined by the persistent
*
* @param stdClass $record
* @return array
*/
final public static function properties_filter(stdClass $record): array {
return array_intersect_key((array) $record, static::properties_definition());
}
/**
* Gets all the formatted properties.
*
@ -421,8 +423,7 @@ abstract class persistent {
* @return static
*/
final public function from_record(stdClass $record) {
$properties = static::properties_definition();
$record = array_intersect_key((array) $record, $properties);
$record = static::properties_filter($record);
foreach ($record as $property => $value) {
$this->raw_set($property, $value);
}

View File

@ -14,14 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Persistent class tests.
*
* @package core
* @copyright 2015 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
use advanced_testcase;
@ -30,8 +22,6 @@ use dml_missing_record_exception;
use lang_string;
use xmldb_table;
defined('MOODLE_INTERNAL') || die();
/**
* Persistent testcase.
*
@ -173,6 +163,23 @@ class persistent_test extends advanced_testcase {
$this->assertEquals($expected, core_testable_persistent::properties_definition());
}
/**
* Test filtering record properties returns only those defined by the persistent
*/
public function test_properties_filter(): void {
$result = core_testable_persistent::properties_filter((object) [
'idnumber' => '123',
'sortorder' => 1,
'invalidparam' => 'abc',
]);
// We should get back all data except invalid param.
$this->assertEquals([
'idnumber' => '123',
'sortorder' => 1,
], $result);
}
/**
* Test creating persistent instance by specifying record ID in constructor
*/

View File

@ -14,6 +14,7 @@ information provided here is intended especially for developers.
* New DB parameter 'versionfromdb', only available for MySQL and MariaDB drivers. It allows to force the DB version to be
evaluated through an explicit call to VERSION() to skip the PHP client version which appears to be sometimes fooled by the
underlying infrastructure, e.g. PaaS on Azure.
* New `properties_filter` method of persistent class for filtering properties of a record against persistent definition
=== 4.1 ===