public static function getThing($type_or_qp, $version = 0) { $thingNames = array_flip(HVRawConnector::$things); $typeId = ''; if ($type_or_qp instanceof Query) { $typeId = $type_or_qp->top()->find('type-id')->text(); } elseif (is_string($type_or_qp)) { $typeId = HealthRecordItemFactory::getTypeId($type_or_qp); $template = __DIR__ . '/HealthRecordItem/XmlTemplates/' . $typeId . '.xml'; if (is_readable($template)) { $type_or_qp = qp(file_get_contents($template), NULL, array('use_parser' => 'xml')); } } else { throw new HVClientException('ThingFactory::getThing must be called with a valid thing name or type id or a QueryPath object representing a thing.'); } if ($typeId) { if ($type_or_qp instanceof Query) { if ($className = HealthRecordItemFactory::convertThingNameToClassName($thingNames[$typeId])) { return new $className($type_or_qp); } else { throw new HVClientException('Things of that type id are not supported yet: ' . $typeId); } } else { throw new HVClientException('Creation of new empty things of that type id is not supported yet: ' . $typeId); } } else { throw new HVClientException('Unable to detect type id.'); } }
/** * @see http://msdn.microsoft.com/en-us/library/dd724265.aspx * * @param $timestamp * @param $weight * @return object File */ public static function createFromData($timestamp, $weight) { $weightMeasurement = HealthRecordItemFactory::getThing('Weight Measurement'); $weightMeasurement->setTimestamp('when', $timestamp); $weightMeasurement->getQp()->find('kg')->text($weight); return $weightMeasurement; }
/** * @see http://msdn.microsoft.com/en-us/library/microsoft.health.itemtypes.file.createfromstream.aspx * * @param resource $stream * @param $name * @param $contentType * @return object File */ public static function createFromStream($stream, $name, $contentType) { if ($content = stream_get_contents($stream)) { $file = HealthRecordItemFactory::getThing('File'); $qp = $file->getQp(); $qp->find('name')->text($name)->top()->find('size')->text(strlen($content))->top()->find('content-type text')->text($contentType)->top()->find('data-other')->text(base64_encode($content))->top(); return $file; } }
public function getThings($thingNameOrTypeId, $recordId, $options = array()) { if ($this->connector) { $typeId = HealthRecordItemFactory::getTypeId($thingNameOrTypeId); $options += array('group max' => 30); $this->connector->authenticatedWcRequest('GetThings', '3', '<group max="' . $options['group max'] . '"><filter><type-id>' . $typeId . '</type-id></filter><format><section>core</section><xml/></format></group>', array('record-id' => $recordId)); $things = array(); $qp = $this->connector->getQueryPathResponse(); $qpThings = $qp->branch()->find('thing'); foreach ($qpThings as $qpThing) { $things[] = HealthRecordItemFactory::getThing(qp('<?xml version="1.0"?>' . $qpThing->xml(), NULL, array('use_parser' => 'xml'))); } return $things; } else { throw new HVClientNotConnectedException(); } }