/_releases/apps/swiftmeme-bundle/_nightly/core/Modules/SiCDSInterface/Parser.php

https://github.com/rkabir/Swiftriver · PHP · 121 lines · 56 code · 24 blank · 41 comment · 15 complexity · 7d4a5fca829c57f2f858803130d5ba56 MD5 · raw file

  1. <?php
  2. namespace Swiftriver\SiCDSInterface;
  3. class Parser {
  4. /**
  5. * Given a array of content items, this function will parse
  6. * the diff collections of each item to JSON formtted for the
  7. * SiCDS cloud service interface.
  8. *
  9. * @param \Swiftriver\Core\ObjectModel\Content[] $items
  10. * @param string $apiKey
  11. * @return string
  12. */
  13. public function ParseToRequestJson($items, $apiKey) {
  14. //set up the array to hold the item dtos
  15. $jsonReadyItems = array();
  16. //loop through the content and only take the bits we need
  17. foreach($items as $item) {
  18. $i->id = $item->id;
  19. $i->difcollections = $item->difs;
  20. $jsonReadyItems[] = $i;
  21. unset($i);
  22. }
  23. //Add the json header information
  24. $object->key = $apiKey;
  25. //Add the content item dtos
  26. $object->contentItems = $jsonReadyItems;
  27. //to json the object
  28. $json = json_encode($object);
  29. //return the json
  30. return $json;
  31. }
  32. public function ParseItemToRequestJson($item, $apiKey) {
  33. $i->id = $item->id;
  34. $i->difcollections = $item->difs;
  35. //Add the json header information
  36. $object->key = $apiKey;
  37. //Add the content item dto
  38. $object->contentItems = array($i);
  39. //to json the object
  40. $json = json_encode($object);
  41. //return the json
  42. return $json;
  43. }
  44. /**
  45. * Given the JSON returned from the SiCDS, this function attempts
  46. * to parse and return an array of content item ID's that have been
  47. * classified as unique.
  48. *
  49. * @param string $json
  50. * @return string[]
  51. */
  52. public function ParseResponseFromJsonToUniqueIds($json) {
  53. //decode the json param
  54. $array = json_decode($json, true);
  55. //Null and not array check
  56. if($array == null || !is_array($array))
  57. throw new \InvalidArgumentException("The json was not well formed");
  58. //Ensure required property 'results' is there and is array
  59. if(!key_exists("results", $array) || !is_array($array["results"]))
  60. throw new \InvalidArgumentException("The json did not contain the required property 'results'");
  61. //get the results array
  62. $results = $array["results"];
  63. //set up the return array
  64. $return = array();
  65. //Loop through the resulsts looking for unique content item ids
  66. foreach($results as $result) {
  67. if($result["result"] == "unique") {
  68. $return[] = $result["id"];
  69. }
  70. }
  71. //return the array
  72. return $return;
  73. }
  74. public function ContentIsUnique($json, $id) {
  75. //decode the json param
  76. $array = json_decode($json, true);
  77. //Null and not array check
  78. if($array == null || !is_array($array))
  79. throw new \InvalidArgumentException("The json was not well formed");
  80. //Ensure required property 'results' is there and is array
  81. if(!key_exists("results", $array) || !is_array($array["results"]))
  82. throw new \InvalidArgumentException("The json did not contain the required property 'results'");
  83. //get the results array
  84. $results = $array["results"];
  85. //set up the return array
  86. $return = array();
  87. //Loop through the results looking for unique content item ids
  88. foreach($results as $result) {
  89. if($result["id"] == $id) {
  90. return ($result["result"] == "unique");
  91. }
  92. }
  93. //return default
  94. return true;
  95. }
  96. }
  97. ?>