1: <?php
2:
3: namespace GeoIp2\Record;
4:
5: abstract class AbstractRecord implements \JsonSerializable
6: {
7: private $record;
8:
9: /**
10: * @ignore
11: */
12: public function __construct($record)
13: {
14: $this->record = isset($record) ? $record : array();
15: }
16:
17: /**
18: * @ignore
19: */
20: public function __get($attr)
21: {
22: // XXX - kind of ugly but greatly reduces boilerplate code
23: $key = $this->attributeToKey($attr);
24:
25: if ($this->__isset($attr)) {
26: return $this->record[$key];
27: } elseif ($this->validAttribute($attr)) {
28: return null;
29: } else {
30: throw new \RuntimeException("Unknown attribute: $attr");
31: }
32: }
33:
34: public function __isset($attr)
35: {
36: return $this->validAttribute($attr) &&
37: isset($this->record[$this->attributeToKey($attr)]);
38: }
39:
40: private function attributeToKey($attr)
41: {
42: return strtolower(preg_replace('/([A-Z])/', '_\1', $attr));
43: }
44:
45: private function validAttribute($attr)
46: {
47: return in_array($attr, $this->validAttributes);
48: }
49:
50: public function jsonSerialize()
51: {
52: return $this->record;
53: }
54: }
55: