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: if (preg_match('/^is_/', $key)) {
29: return false;
30: } else {
31: return null;
32: }
33: } else {
34: throw new \RuntimeException("Unknown attribute: $attr");
35: }
36: }
37:
38: public function __isset($attr)
39: {
40: return $this->validAttribute($attr) &&
41: isset($this->record[$this->attributeToKey($attr)]);
42: }
43:
44: private function attributeToKey($attr)
45: {
46: return strtolower(preg_replace('/([A-Z])/', '_\1', $attr));
47: }
48:
49: private function validAttribute($attr)
50: {
51: return in_array($attr, $this->validAttributes);
52: }
53:
54: public function jsonSerialize()
55: {
56: return $this->record;
57: }
58: }
59: