/** * @param Call $apiCall * @param $json * @return mixed */ private function mapJsonToResponseObject(Call $apiCall, $json) { $jsonMapper = new \JsonMapper(); $responseObject = $jsonMapper->map($json, $apiCall->getResponseObject()); return $responseObject; }
/** * Prepares the test */ public function setUp() { $filename = __DIR__ . '/../../data/JenkinsPayloadAnalyzer/Nasqueron.json'; $mapper = new \JsonMapper(); $this->configuration = $mapper->map(json_decode(file_get_contents($filename)), new JenkinsPayloadAnalyzerConfiguration('Nasqueron')); parent::setUp(); }
public function __construct(array $rawData = array()) { // foreach ($rawData as $_key => $_data) { // $this->{$_key} = $_data; // } $mapper = new \JsonMapper(); $mapper->map($rawData, $this); }
public function testMapCustomArraObjectWithChildType() { $mapper = new \JsonMapper(); $json = '{"users":[{"user":"******"}]}'; $res = $mapper->map(json_decode($json), new UnitData()); $this->assertInstanceOf('\\namespacetest\\UnitData', $res); $this->assertInstanceOf('\\namespacetest\\model\\UserList', $res->users); $this->assertInstanceOf('\\namespacetest\\model\\User', $res->users[0]); }
public function testDecode() { $json = json_decode(file_get_contents('tests/push-reg-body.json')); $mapper = new JsonMapper(); $push = $mapper->map($json, new Push()); $this->assertEquals("Diaspora", $push->repository->name); $this->assertEquals("da1560886d4f094c3e6c9ef40349f7d38b5d27d7", $push->after); $this->assertEquals("fixed readme", $push->commits[1]->message); $this->assertEquals("GitLab dev user", $push->commits[1]->author->name); var_dump($push); }
/** * Gets a coderwall user * @param string $username The username * @param bool $full if your going to need all information from this user * @return User * @throws CoderWallException * @throws \JsonMapper_Exception */ public function getUser($username, $full = true) { if (empty($username)) { throw new CoderWallException("Invalid username"); } try { $json = $this->client->get($username . '.json' . ($full ? "?full=true" : ""))->json(); } catch (ClientException $exception) { if ($exception->getResponse()->getStatusCode() === 404) { throw new CoderWallException("User doesn't exists"); } throw $exception; } $mapper = new \JsonMapper(); return $mapper->map($json, new User()); }
public function toEntity(array $data = null) { $className = $this->getEntityClassName(); $mapper = new \JsonMapper(); if (null === $data) { $data = $this->toArrayWithoutRelations(); } $data = (object) $data; $entity = $mapper->map($data, new $className()); $this->mapEntityRelationships($entity); // entity was just hydrated // this means that any generated event should be removed if (true === method_exists($entity, 'flushEvents')) { $entity->flushEvents(); } return $entity; }
/** * Template Level Qualifying Conditions List * @param array List of Qualifying Conditions */ public function setQcList($qcList) { $mapper = new \JsonMapper(); $this->qcList = array(); if (empty($qcList)) { return; } foreach ($qcList as $idx => $qc) { if (is_array($qc)) { $qcName = $qc['name']; } else { if (isset($qc->name)) { $qcName = $qc->name; } else { // An empty object continue; } } if (strcasecmp($qcName, \Akzo\Scheme\Data\QualifyingCondition\Type::HISTORICAL) === 0) { $this->qcList[] = $mapper->map($qc, new \Akzo\Scheme\Data\QualifyingCondition\HistoricalQC()); } else { if (strcasecmp($qcName, \Akzo\Scheme\Data\QualifyingCondition\Type::GROWTH) === 0) { $this->qcList[] = $mapper->map($qc, new \Akzo\Scheme\Data\QualifyingCondition\GrowthQC()); } else { if (strcasecmp($qcName, \Akzo\Scheme\Data\QualifyingCondition\Type::ACTUAL) === 0) { $this->qcList[] = $mapper->map($qc, new \Akzo\Scheme\Data\QualifyingCondition\ActualQC()); } else { if (strcasecmp($qcName, \Akzo\Scheme\Data\QualifyingCondition\Type::TARGET) === 0) { $this->qcList[] = $mapper->map($qc, new \Akzo\Scheme\Data\QualifyingCondition\TargetQC()); } else { if (strcasecmp($qcName, \Akzo\Scheme\Data\QualifyingCondition\Type::TARGET_ACHIEVEMENT) === 0) { $this->qcList[] = $mapper->map($qc, new \Akzo\Scheme\Data\QualifyingCondition\TargetAchievementQC()); } else { if (strcasecmp($qcName, \Akzo\Scheme\Data\QualifyingCondition\Type::RATIO) === 0) { $this->qcList[] = $mapper->map($qc, new \Akzo\Scheme\Data\QualifyingCondition\RatioQC()); } } } } } } } }
$this->lol = $val; } public function getLol() { return $this->lol; } } class Serializator implements JsonSerializable { public $label = "label", $data = null, $field = null; public function __construct() { $this->data = new DateTime(); $this->field = new Test(42); } function jsonSerialize() { return ['type' => $this->label, 'date' => $this->data->format(DateTime::ISO8601), 'field' => $this->field]; } } $my_json = json_encode(new Serializator()); $decoded_json = json_decode($my_json); $mapper = new JsonMapper(); $serialization = $mapper->map($decoded_json, new Serializator()); $test = new Test(42); $test->getLol(); var_dump($serialization->field->lol); #$lol = $serialization->field->getLol(); echo json_encode(new Serializator()) . "\n"; #echo "\n"; var_dump(json_decode(json_encode(new Serializator())));
function update($path, $body_data, $entity_name) { try { /** * @var $response \GuzzleHttp\Message\AbstractMessage */ $response = $this->client->put($path, array('json' => $body_data)); $data = $response->json(); $mapper = new \JsonMapper(); return $mapper->map($data["response"], new $entity_name()); } catch (\Exception $e) { $this->throwException($e, $entity_name); } }
<?php require_once __DIR__ . '/../src/JsonMapper.php'; require_once 'Contact.php'; require_once 'Address.php'; $json = json_decode(file_get_contents(__DIR__ . '/single.json')); $mapper = new JsonMapper(); $contact = $mapper->map($json, new Contact()); $coords = $contact->address->getGeoCoords(); echo $contact->name . ' lives at coordinates ' . $coords['lat'] . ',' . $coords['lon'] . "\n";
public function testClassMap() { $jm = new JsonMapper(); $jm->classMap['JsonMapperTest_PlainObject'] = 'DateTime'; $sn = $jm->map(json_decode('{"pPlainObject":"2016-04-14T23:15:42+02:00"}'), new JsonMapperTest_Object()); $this->assertInternalType('object', $sn->pPlainObject); $this->assertInstanceOf('DateTime', $sn->pPlainObject); $this->assertEquals('2016-04-14T23:15:42+02:00', $sn->pPlainObject->format('c')); }
/** * Returns the $resultSet from the $response object, or $defaultValue if * the result set is not available * @param stdClass $response an API response object * @param string $resultSet the name of the result set * @param mixed $defaultValue the value to return if the result set is not * available * @param mixed an instance of the class the results should be mapped to, or * null to use stdClass * @return mixed the normalized response */ private static function normalizeResponse($response, $resultSet, $defaultValue, $targetObject = null) { if (isset($response->result->{$resultSet})) { $result = $response->result->{$resultSet}; if ($targetObject !== null) { $mapper = new JsonMapper(); if (is_array($result)) { return $mapper->mapArray($result, new ArrayObject(), $targetObject)->getArrayCopy(); } elseif (is_object($result)) { return $mapper->map($result, $targetObject); } } return $response->result->{$resultSet}; } else { return $defaultValue; } }
public function testSetterIsPreferredOverProperty() { $jm = new JsonMapper(); $sn = $jm->map(json_decode('{"setterPreferredOverProperty":"foo"}'), new JsonMapperTest_Simple()); $this->assertInternalType('string', $sn->setterPreferredOverProperty); $this->assertEquals('set via setter: foo', $sn->setterPreferredOverProperty); }
/** * @expectedException JsonMapper_Exception * @expectedExceptionMessage JSON property "privatePropertyPrivateSetter" has no public setter method in object of type PrivateWithSetter */ public function testPrivatePropertyWithPrivateSetter() { $jm = new JsonMapper(); $jm->bExceptionOnUndefinedProperty = true; $logger = new JsonMapperTest_Logger(); $jm->setLogger($logger); $json = '{"privatePropertyPrivateSetter" : 1}'; $result = $jm->map(json_decode($json), new PrivateWithSetter()); }
public function _buildSchemeDataFromJson($jsonData, $format = 'json') { if (strcasecmp($format, 'json') === 0) { $jsonData = json_decode($jsonData); } if (empty($jsonData)) { return null; } $mapper = new \JsonMapper(); return $mapper->map($jsonData, new \Akzo\Scheme\Data()); }
public function testSettingValueObjects() { $valueObject = new JsonMapperTest_ValueObject('test'); $jm = new JsonMapper(); $sn = $jm->map((object) array('value_object' => $valueObject), new JsonMapperTest_Simple()); $this->assertSame($valueObject, $sn->getValueObject()); }
private function getPhabricatorPayloadAnalyzerConfiguration() { $filename = __DIR__ . '/../../data/PhabricatorPayloadAnalyzer/Nasqueron.json'; $mapper = new \JsonMapper(); return $mapper->map(json_decode(file_get_contents($filename)), new PhabricatorPayloadAnalyzerConfiguration('Nasqueron')); }
"networkPrefix":"79", "countryPrefix":"41" }, "ported":false, "roaming":false, "status":{ "groupId":2, "groupName":"UNDELIVERABLE", "id":9, "name":"UNDELIVERABLE_NOT_DELIVERED", "description":"Message sent not delivered" }, "error":{ "groupId":1, "groupName":"HANDSET_ERRORS", "id":27, "name":"EC_ABSENT_SUBSCRIBER", "description":"Absent Subscriber", "permanent":false } } ] }'; $mapper = new JsonMapper(); $responseObject = $mapper->map(json_decode($responseBody), new NumberContextResponse()); $numberContext = $responseObject->getResults()[0]; echo "Phone number: " . $numberContext->getTo() . "\n"; echo "MccMnc: " . $numberContext->getMccMnc() . "\n"; echo "Original country prefix: " . $numberContext->getOriginalNetwork()->getCountryPrefix() . "\n"; echo "Original network prefix: " . $numberContext->getOriginalNetwork()->getNetworkPrefix() . "\n"; echo "Status: " . $numberContext->getStatus()->getName();
/** * Variable with hyphen and a setter method */ public function testMapSimpleHyphenSetter() { $jm = new JsonMapper(); $sn = $jm->map(json_decode('{"hyphen-value-setter":"blubb"}'), new JsonMapperTest_Simple()); $this->assertInternalType('string', $sn->internalData['hyphen-value-setter']); $this->assertEquals('blubb', $sn->internalData['hyphen-value-setter']); }
/** * Variable with an underscore and a setter method */ public function testMapSimpleUnderscoreSetter() { $jm = new JsonMapper(); $sn = $jm->map(json_decode('{"under_score_setter":"blubb"}'), new JsonMapperTest_Simple()); $this->assertInternalType('string', $sn->internalData['under_score_setter']); $this->assertEquals('blubb', $sn->internalData['under_score_setter']); }
/** * Test a setter method with a namespaced type hint that * is within another namespace than the object itself. */ public function testSetterNamespacedTypeHint() { $mapper = new \JsonMapper(); $json = '{"namespacedTypeHint":"Foo"}'; $res = $mapper->map(json_decode($json), new UnitData()); $this->assertInstanceOf('\\namespacetest\\UnitData', $res); $this->assertInstanceOf('\\othernamespace\\Foo', $res->internalData['namespacedTypeHint']); $this->assertEquals('Foo', $res->internalData['namespacedTypeHint']->name); }
/** * Loads configuration for the analyzer */ public function loadConfiguration() { $fileName = $this->getConfigurationFileName(); $class = $this->getConfigurationClassName(); $mapper = new \JsonMapper(); $this->configuration = $mapper->map(json_decode(Storage::get($fileName)), new $class($this->project)); }
public function testCaseInsensitivePropertyMatching() { $jm = new JsonMapper(); $sn = $jm->map((object) array('PINT' => 2), new JsonMapperTest_Simple()); $this->assertSame(2, $sn->pint); }
* User: nmenkovic * Date: 9/10/15 * Time: 4:44 PM */ use infobip\api\model\sms\mo\reports\MOReportResponse; require_once __DIR__ . '/../../vendor/autoload.php'; $responseBody = '{ "results":[ { "messageId":"ff4804ef-6ab6-4abd-984d-ab3b1387e823", "from":"38598111", "to":"41793026727", "text":"KEY Test message", "cleanText":"Test message", "keyword":"KEY", "receivedAt":"2015-02-15T11:43:20.254+0100", "smsCount":1 } ] }'; $mapper = new JsonMapper(); $responseObject = $mapper->map(json_decode($responseBody), new MOReportResponse()); $result = $responseObject->getResults()[0]; echo "Message ID: " . $result->getMessageId() . "\n"; echo "Received at: " . $result->getReceivedAt()->format('Y-m-d H:i:s P') . "\n"; echo "Sender: " . $result->getFrom() . "\n"; echo "Receiver: " . $result->getTo() . "\n"; echo "Message text: " . $result->getText() . "\n"; echo "Keyword: " . $result->getKeyword() . "\n"; echo "Clean text: " . $result->getCleanText() . "\n"; echo "Sms count: " . $result->getSmsCount();
/** * The TYPO3 autoloader breaks if we autoload a class with a [ or ] * in its name. * * @runInSeparateProcess */ public function testMapTypedArrayObjectDoesNotExist() { $this->assertTrue(spl_autoload_register(array($this, 'mapTypedArrayObjectDoesNotExistAutoloader'))); $jm = new JsonMapper(); $sn = $jm->map(json_decode('{"pTypedArrayObjectNoClass":[{"str":"stringvalue"}]}'), new JsonMapperTest_Broken()); $this->assertInstanceOf('ArrayObject', $sn->pTypedArrayObjectNoClass); $this->assertEquals(1, count($sn->pTypedArrayObjectNoClass)); $this->assertInstanceOf('ThisClassDoesNotExist', $sn->pTypedArrayObjectNoClass[0]); }
/** * Initializes a new instance of the Services class deserializing a JSON file. * * @param $file the JSON file to deserialize * @return Services the deserialized instance */ public static function loadFromJson($file) { $data = json_decode(Storage::get($file)); $mapper = new \JsonMapper(); return $mapper->map($data, new self()); }
private function _collateExecutionResults($result, $existingResult, $code) { $mapper = new \JsonMapper(); $resultsData = $mapper->map($result, new \Akzo\Scheme\ResultData()); // Initialize the result arrays $collatedResult = $this->_initializeResult(array()); // Add the code to this data $collatedResult['code'] = $code; $existingResult = $this->_initializeResult($existingResult); // Collate the results list($collatedResult['inBills'], $existingResult['inBills']) = $this->_collateOutputs($resultsData->inBills, $existingResult['inBills']); list($collatedResult['ppiOutputs'], $existingResult['ppiOutputs']) = $this->_collateOutputs($resultsData->ppiOutputs, $existingResult['ppiOutputs']); list($collatedResult['priOutputs'], $existingResult['priOutputs']) = $this->_collateOutputs($resultsData->priOutputs, $existingResult['priOutputs']); list($collatedResult['slabOutputs'], $existingResult['slabOutputs']) = $this->_collateOutputs($resultsData->slabOutputs, $existingResult['slabOutputs']); list($collatedResult['slabV2Outputs'], $existingResult['slabV2Outputs']) = $this->_collateOutputs($resultsData->slabV2Outputs, $existingResult['slabV2Outputs']); return array($collatedResult, $existingResult); }