Advertisement
Guest User

Untitled

a guest
Aug 4th, 2015
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. #!/usr/bin/env php
  2. <?php
  3.  
  4. class RecordFile {
  5.  
  6. var $parser;
  7.  
  8. var $records = [];
  9.  
  10. function __construct($filename = null) {
  11. if (file_exists($filename) && $filename !== null){
  12. $this->parser = simplexml_load_file($filename);
  13. foreach ($this->parser->children() as $name => $child) {
  14. $record = new Record($child);
  15. if (!empty($record->type)) {
  16. $this->records[] = $record;
  17. }
  18. }
  19. } else {
  20. throw new Exception("File does not exist");
  21. }
  22.  
  23. }
  24.  
  25. function __toString() {
  26. $toReturn = [];
  27. foreach($this->records as $record) {
  28. $toReturn[$record->type] .= $record->type." ".$record->__toString() . PHP_EOL ;
  29. }
  30. return join(PHP_EOL, $toReturn);
  31. }
  32. }
  33.  
  34.  
  35. class Record {
  36.  
  37. var $element;
  38. var $type;
  39. var $date;
  40. var $value;
  41.  
  42. function __construct(SimpleXMLElement $child) {
  43. $type = @$child->attributes()['type'];
  44. if (empty($type)){
  45. return null;
  46. } else {
  47. $this->type = str_replace("HKQuantityTypeIdentifier", "", $type);;
  48. $this->element = $child;
  49. $this->parseElement();
  50. }
  51. }
  52.  
  53. function parseElement() {
  54. $this->date = $this->iOSStringToDateTime(@$this->element->attributes()['startDate']);
  55. $this->value = @$this->element->attributes()['average'];
  56. if (empty($this->value)){
  57. $this->value = @$this->element->attributes()['value'];
  58. }
  59. }
  60.  
  61. function iOSStringToDateTime($incoming) {
  62. $year = substr($incoming, 0, 4);
  63. $month = substr($incoming, 4, 2);
  64. $day = substr($incoming, 6, 2);
  65. $hour = substr($incoming, 8, 2);
  66. $minute = substr($incoming, 10, 2);
  67. return new DateTime($year."-".$month."-".$day." ".$hour.":".$minute.":"."00", new DateTimeZone("America/Los_Angeles"));
  68. }
  69.  
  70. function __toString() {
  71. return (string) $this->date->format("l Y-m-d h:iA") . " " . $this->value;
  72. }
  73.  
  74. }
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81. $args = $argv;
  82.  
  83. $scriptname = array_shift($args);
  84. if (count($args) <= 0) {
  85. throw new Exception("This script requires a filename to parse. e.g. > ".$scriptname." ./file.xml");
  86. }
  87.  
  88. $recordFile = new RecordFile(array_shift($args));
  89.  
  90. print($recordFile->__toString());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement