diff --git a/lib/classes/persistent.php b/lib/classes/persistent.php
index 96402a6fee4..36e0fa5a4ca 100644
--- a/lib/classes/persistent.php
+++ b/lib/classes/persistent.php
@@ -14,26 +14,18 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see .
-/**
- * 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);
}
diff --git a/lib/tests/persistent_test.php b/lib/tests/persistent_test.php
index 151db755130..8d03320569e 100644
--- a/lib/tests/persistent_test.php
+++ b/lib/tests/persistent_test.php
@@ -14,14 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see .
-/**
- * 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
*/
diff --git a/lib/upgrade.txt b/lib/upgrade.txt
index 2de8ef1b884..75e68784cd2 100644
--- a/lib/upgrade.txt
+++ b/lib/upgrade.txt
@@ -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 ===