public function test() { $link = new Link(); $link->setHelp('TESThelp'); $link->setTitle('TESTtitle'); $link->setType('TESTtype'); $link->setUrl('TESTurl'); $linkData = ['help' => 'TEST2help', 'title' => 'TEST2title', 'type' => 'TEST2type', 'url' => 'TEST2url']; $this->assertEquals('TESThelp', $link->getHelp()); $this->assertEquals('TESTtitle', $link->getTitle()); $this->assertEquals('TESTtype', $link->getType()); $this->assertEquals('TESTurl', $link->getUrl()); $this->assertJson(json_encode($link)); $link2 = new Link(); $link2->populate($linkData); $this->assertEquals($linkData['url'], $link2->getUrl()); $link2->populate($link); $this->assertEquals($link->getUrl(), $link2->getUrl()); try { $link2->populate('nope'); } catch (\RcmUser\Exception\RcmUserException $e) { $this->assertInstanceOf('\\RcmUser\\Exception\\RcmUserException', $e); return; } $this->fail("Expected exception not thrown"); }
/** * populate * * @param array|Link $data data to populate this object with * * @return void * @throws RcmUserException */ public function populate($data = []) { if ($data instanceof Link) { $this->setType($data->getType()); $this->setTitle($data->getTitle()); $this->setUrl($data->getUrl()); $this->setHelp($data->getHelp()); return; } if (is_array($data)) { if (isset($data['type'])) { $this->setType($data['type']); } if (isset($data['title'])) { $this->setTitle($data['title']); } if (isset($data['url'])) { $this->setUrl($data['url']); } if (isset($data['help'])) { $this->setHelp($data['help']); } return; } throw new RcmUserException('Link data could not be populated, date format not supported'); }