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.

70 lines
1.7 KiB

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