Peekmo\JsonPath\JsonStore::setData PHP Method

setData() public method

Sets JsonStore's manipulated data
public setData ( string | array | stdClass $data )
$data string | array | stdClass
    public function setData($data)
    {
        $this->data = $data;
        if (is_string($this->data)) {
            $this->data = json_decode($this->data, true);
        } else {
            if (is_object($data)) {
                $this->data = json_decode(json_encode($this->data), true);
            } else {
                if (!is_array($data)) {
                    throw new \InvalidArgumentException(sprintf('Invalid data type in JsonStore. Expected object, array or string, got %s', gettype($data)));
                }
            }
        }
    }

Usage Example

Beispiel #1
0
 public function testSetData()
 {
     $this->assertEquals($this->jsonStore->toArray(), json_decode($this->json, true));
     $new = ['a' => 'b'];
     $this->jsonStore->setData($new);
     $this->assertEquals($this->jsonStore->toArray(), $new);
     $this->assertNotEquals($this->jsonStore->toArray(), json_decode($this->json, true));
 }