MDL-76221 core: method to filter by defined properties of persistent.
This commit is contained in:
+12
-11
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user