Site du proximo, utilisé pour gérer le stock.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Document.php 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace OpenFoodFacts;
  3. /**
  4. * In mongoDB all element are object, it not possible to define property.
  5. * All property of the mongodb entity are store in one property of this class and the magic call try to access to it
  6. */
  7. class Document
  8. {
  9. use RecursiveSortingTrait;
  10. /**
  11. * the whole data
  12. * @var array
  13. */
  14. private $data;
  15. /**
  16. * the whole data
  17. * @var array
  18. */
  19. private $api;
  20. /**
  21. * Initialization the document and specify from which API it was extract
  22. * @param array $data the whole data
  23. * @param string $api the api name
  24. */
  25. public function __construct(array $data, string $api = null)
  26. {
  27. $this->data = $data;
  28. $this->api = $api;
  29. }
  30. /**
  31. * @inheritDoc
  32. */
  33. public function __get(string $name)
  34. {
  35. return $this->data[$name];
  36. }
  37. /**
  38. * @inheritDoc
  39. */
  40. public function __isset(string $name):bool
  41. {
  42. return isset($this->data[$name]);
  43. }
  44. /**
  45. * Returns a sorted representation of the complete Document Data
  46. * @return array
  47. */
  48. public function getData(): array
  49. {
  50. $this->recursiveSortArray($this->data);
  51. return $this->data;
  52. }
  53. /**
  54. * Returns a Document in the type regarding to the API used.
  55. * May be a Child of "Document" e.g.: FoodDocument or ProductDocument
  56. * @param string $apiIdentifier
  57. * @param array $data
  58. * @return Document
  59. */
  60. public static function createSpecificDocument(string $apiIdentifier, array $data): Document
  61. {
  62. if ($apiIdentifier === '') {
  63. return new Document($data, $apiIdentifier);
  64. }
  65. $className = "OpenFoodFacts\Document\\" . ucfirst($apiIdentifier) . 'Document';
  66. if (class_exists($className) && is_subclass_of($className, Document::class)) {
  67. return new $className($data, $apiIdentifier);
  68. }
  69. return new Document($data, $apiIdentifier);
  70. }
  71. }