FOF30\Hal\Document::addData PHP Method

addData() public method

Add data to the document
public addData ( stdClass $data, boolean $overwrite = true ) : void
$data stdClass The data to add
$overwrite boolean Should I overwrite existing data?
return void
    public function addData($data, $overwrite = true)
    {
        if (is_array($data)) {
            $data = (object) $data;
        }
        if ($overwrite) {
            $this->_data = $data;
        } else {
            if (!is_array($this->_data)) {
                $this->_data = array($this->_data);
            }
            $this->_data[] = $data;
        }
    }

Usage Example

コード例 #1
0
ファイル: DocumentTest.php プロジェクト: Joal01/fof
 /**
  * @covers FOF30\Hal\Document::getLinks
  */
 public function testGetLinks()
 {
     $data = array('test1' => 'one', 'test2' => 'two', 'testArray' => array('testUno' => 'uno', 'testDue' => 'Due'));
     $document = new Document($data);
     $myLink = new Link('http://www.example.com/foo.json', false, 'test', null, 'A test link');
     $document->addLink('foo', $myLink);
     // Make sure trying to add links with replace=false adds, doesn't replace, links
     $myOtherLink = new Link('http://www.example.com/bar.json', false, 'test', null, 'Another test link');
     $document->addLink('foo', $myOtherLink, false);
     $extraData = array('newData' => 'something');
     $document->addData($extraData, false);
     $allLinks = $document->getLinks();
     $this->assertInternalType('array', $allLinks, 'Line: ' . __LINE__ . '.');
     $this->assertArrayHasKey('foo', $allLinks, 'Line: ' . __LINE__ . '.');
     $links = $document->getLinks('foo');
     $this->assertInternalType('array', $links, 'Line: ' . __LINE__ . '.');
     $this->assertEquals($links, $allLinks['foo'], 'Line: ' . __LINE__ . '.');
     $this->assertCount(2, $links, 'Line: ' . __LINE__ . '.');
     $this->assertTrue($links[0] instanceof Link, 'Line: ' . __LINE__ . '.');
     $this->assertEquals('http://www.example.com/foo.json', $links[0]->href, 'Line: ' . __LINE__ . '.');
     $this->assertTrue($links[1] instanceof Link, 'Line: ' . __LINE__ . '.');
     $this->assertEquals('http://www.example.com/bar.json', $links[1]->href, 'Line: ' . __LINE__ . '.');
 }