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