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.

72 lines
1.6 KiB

7 days ago
7 days ago
7 days ago
7 days ago
7 days ago
  1. <?php
  2. namespace OSM\Element;
  3. use OSM\Element\Member\Member;
  4. class Element
  5. {
  6. public ?int $id = null;
  7. public array $members = [];
  8. public array $tags = [];
  9. public static function createFromArray($array)
  10. {
  11. assert(is_array($array));
  12. $hasType = isset($array['type']);
  13. assert($hasType);
  14. $className = __NAMESPACE__.'\\'.ucfirst($array['type']);
  15. $hasClass = class_exists($className);
  16. $instance = new $className();
  17. $hasId = isset($array['id']);
  18. assert($hasId);
  19. $instance->id = (int) $array['id'];
  20. $hasMembers = isset($array['members']);
  21. if ($hasMembers) {
  22. $items = $array['members'];
  23. foreach ($items as $item) {
  24. $member = Member::createFromArray($item);
  25. $instance->members[] = $member;
  26. }
  27. }
  28. $hasTags = isset($array['tags']);
  29. if ($hasTags) {
  30. $items = $array['tags'];
  31. foreach ($items as $itemKey => $itemValue) {
  32. $tag = Tag::createFromValues($itemKey, $itemValue);
  33. $instance->tags[] = $tag;
  34. }
  35. }
  36. $instance->completeFromArray($array);
  37. return $instance;
  38. }
  39. public function completeFromArray(array $array): static
  40. {
  41. return $this;
  42. }
  43. public function getTagValue(string $key): ?string
  44. {
  45. $foundTags = array_filter($this->tags, function ($tag) use ($key) {
  46. return $tag->key === $key;
  47. });
  48. if (1 !== count($foundTags)) {
  49. return null;
  50. }
  51. $tag = reset($foundTags);
  52. return $tag->value;
  53. }
  54. }