diff --git a/library/Icingadb/Model/Customvar.php b/library/Icingadb/Model/Customvar.php index 7019d56d..f568292f 100644 --- a/library/Icingadb/Model/Customvar.php +++ b/library/Icingadb/Model/Customvar.php @@ -9,8 +9,6 @@ use ipl\Orm\Relations; class Customvar extends Model { - protected $accessorsAndMutatorsEnabled = true; - public function getTableName() { return 'customvar'; @@ -60,16 +58,4 @@ class Customvar extends Model $relations->hasMany('customvar_flat', CustomvarFlat::class); } - - protected function mutateValueProperty($json) - { - if (is_string($json)) { - $data = json_decode($json); - if ($data !== null) { - $json = $data; - } - } - - return $json; - } } diff --git a/library/Icingadb/Model/Host.php b/library/Icingadb/Model/Host.php index 3ec25fa7..1030ab1d 100644 --- a/library/Icingadb/Model/Host.php +++ b/library/Icingadb/Model/Host.php @@ -8,8 +8,8 @@ use Icinga\Module\Icingadb\Common\Auth; use Icinga\Module\Icingadb\Model\Behavior\BoolCast; use Icinga\Module\Icingadb\Model\Behavior\ReRoute; use ipl\Orm\Behaviors; +use ipl\Orm\Defaults; use ipl\Orm\Model; -use ipl\Orm\Query; use ipl\Orm\Relations; use ipl\Orm\ResultSet; @@ -20,8 +20,6 @@ class Host extends Model { use Auth; - protected $accessorsAndMutatorsEnabled = true; - public function getTableName() { return 'host'; @@ -147,25 +145,20 @@ class Host extends Model ])); } - /** - * Mutates flattened custom vars to an associative array - * - * @param Query|ResultSet $_ - * - * @return array - */ - public function mutateVarsProperty($_): array + public function createDefaults(Defaults $defaults) { - if (! $this->customvar_flat instanceof ResultSet) { - $this->applyRestrictions($this->customvar_flat); - } + $defaults->add('vars', function (self $subject) { + if (! $subject->customvar_flat instanceof ResultSet) { + $this->applyRestrictions($subject->customvar_flat); + } - $vars = []; - foreach ($this->customvar_flat as $customVar) { - $vars[$customVar->flatname] = $customVar->flatvalue; - } + $vars = []; + foreach ($subject->customvar_flat as $customVar) { + $vars[$customVar->flatname] = $customVar->flatvalue; + } - return $vars; + return $vars; + }); } public function createRelations(Relations $relations) diff --git a/library/Icingadb/Model/Service.php b/library/Icingadb/Model/Service.php index cb5531c3..4ed7b806 100644 --- a/library/Icingadb/Model/Service.php +++ b/library/Icingadb/Model/Service.php @@ -8,8 +8,8 @@ use Icinga\Module\Icingadb\Common\Auth; use Icinga\Module\Icingadb\Model\Behavior\BoolCast; use Icinga\Module\Icingadb\Model\Behavior\ReRoute; use ipl\Orm\Behaviors; +use ipl\Orm\Defaults; use ipl\Orm\Model; -use ipl\Orm\Query; use ipl\Orm\Relations; use ipl\Orm\ResultSet; @@ -17,8 +17,6 @@ class Service extends Model { use Auth; - protected $accessorsAndMutatorsEnabled = true; - public function getTableName() { return 'service'; @@ -137,25 +135,20 @@ class Service extends Model ])); } - /** - * Mutates flattened custom vars to an associative array - * - * @param Query|ResultSet $_ - * - * @return array - */ - public function mutateVarsProperty($_): array + public function createDefaults(Defaults $defaults) { - if (! $this->customvar_flat instanceof ResultSet) { - $this->applyRestrictions($this->customvar_flat); - } + $defaults->add('vars', function (self $subject) { + if (! $subject->customvar_flat instanceof ResultSet) { + $this->applyRestrictions($subject->customvar_flat); + } - $vars = []; - foreach ($this->customvar_flat as $customVar) { - $vars[$customVar->flatname] = $customVar->flatvalue; - } + $vars = []; + foreach ($subject->customvar_flat as $customVar) { + $vars[$customVar->flatname] = $customVar->flatvalue; + } - return $vars; + return $vars; + }); } public function createRelations(Relations $relations) diff --git a/test/php/library/Icingadb/Common/MacrosTest.php b/test/php/library/Icingadb/Common/MacrosTest.php index c515c75f..dd01a01f 100644 --- a/test/php/library/Icingadb/Common/MacrosTest.php +++ b/test/php/library/Icingadb/Common/MacrosTest.php @@ -15,25 +15,25 @@ class MacrosTest extends TestCase { use Macros; + const VARS = [ + 'os' => "Ubuntu", + 'days[0]' => 'mo', + 'days[1]' => 'tue', + 'days[2]' => 'wed', + 'days[3]' => 'thu', + 'days[4]' => 'fr' + ]; + public function testHostMacros() { $mock = \Mockery::mock(Host::class); $mock->name = 'test'; $mock->address = '1.1.1.1'; $mock->address6 = '::1'; + $mock->vars = self::VARS; $mock->hostgroup = new Query(); - $mock->vars = null; - $mock->shouldReceive('mutateVarsProperty')->once()->andReturn([ - 'os' => "Ubuntu", - 'days[0]' => 'mo', - 'days[1]' => 'tue', - 'days[2]' => 'wed', - 'days[3]' => 'thu', - 'days[4]' => 'fr' - ]); - $this->assertEquals($mock->name, $this->expandMacros('$host.name$', $mock)); $this->assertEquals($mock->name, $this->expandMacros('$name$', $mock)); $this->assertEquals($mock->address, $this->expandMacros('$host.address$', $mock)); @@ -64,33 +64,15 @@ class MacrosTest extends TestCase $mock = \Mockery::mock(Service::class); $mock->name = 'test-service'; $mock->description = 'A test service'; + $mock->vars = self::VARS; $mock->servicegroup = new Query(); - $mock->vars = null; - $mock->shouldReceive('mutateVarsProperty')->once()->andReturn([ - 'os' => "Ubuntu", - 'days[0]' => 'mo', - 'days[1]' => 'tue', - 'days[2]' => 'wed', - 'days[3]' => 'thu', - 'days[4]' => 'fr' - ]); - $hostMock = \Mockery::mock(Host::class); $hostMock->name = 'test'; $hostMock->address = '1.1.1.1'; $hostMock->hostgroup = new ResultSet(new \ArrayIterator()); - - $hostMock->vars = null; - $hostMock->shouldReceive('mutateVarsProperty')->once()->andReturn([ - 'os' => "Ubuntu", - 'days[0]' => 'mo', - 'days[1]' => 'tue', - 'days[2]' => 'wed', - 'days[3]' => 'thu', - 'days[4]' => 'fr' - ]); + $hostMock->vars = self::VARS; $mock->host = $hostMock;