/** * @test * @dataProvider boolProvider */ public function testBooleans($testValue) { $marshaller = new DocumentFragment\Marshaller(); $fragment = new DocumentFragment("test", $testValue); $marshalledFragment = $marshaller->marshall($fragment); $unmarshalledFragment = $marshaller->unmarshall($marshalledFragment); $this->assertEquals("test", $unmarshalledFragment->getName()); $this->assertEquals($testValue ? 1 : 0, $unmarshalledFragment->getData()); }
/** * @param string $data * @return Document * @throws \Exception */ public function unmarshall(string $data) : Document { $document = json_decode($data, true); $documentVersion = isset($document[self::KEY_VERSION]) ? intval($document[self::KEY_VERSION]) : self::VERSION_1; $unmarshalledFragments = isset($document[self::KEY_FRAGMENTS]) ? $document[self::KEY_FRAGMENTS] : []; $fragments = []; if ($documentVersion != self::VERSION_1) { throw new UnsupportedVersionException(sprintf("Expected a version identifier of '%s', got '%s'", self::VERSION_1, $documentVersion)); } if (count($unmarshalledFragments) > 0) { foreach ($unmarshalledFragments as $unmarshalledFragment) { $fragments[] = $this->fragmentMarshaller->unmarshall($unmarshalledFragment); } } if (isset($document[self::KEY_CHECKSUM])) { $expectedChecksum = $this->calculateFragmentsChecksum($fragments); $actualChecksum = $document[self::KEY_CHECKSUM]; if ($expectedChecksum != $actualChecksum) { throw new ChecksumVerificationException(sprintf("The calculated checksum (%s) does not match the expected checksum (%s)", $expectedChecksum, $actualChecksum)); } } return new Document($fragments); }