Introduce common DataExtractor trait

This commit is contained in:
Yonas Habteab 2022-03-11 16:24:47 +01:00
parent 9e7d0e089e
commit 0848d17a99

View File

@ -0,0 +1,37 @@
<?php
namespace Icinga\Common;
trait DataExtractor
{
/**
* Extract data from array to this class's properties
*
* Unknown properties (no matching setter) are ignored
*
* @param array $data
*
* @return $this
*/
public function fromArray(array $data)
{
foreach ($data as $name => $value) {
$func = 'set'. ucfirst($name);
if (method_exists($this, $func)) {
$this->$func($value);
}
}
return $this;
}
/**
* Get this class's structure as array
*
* @return array
*/
public function toArray()
{
return [];
}
}