-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathModel.php
executable file
·179 lines (150 loc) · 5.57 KB
/
Model.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
namespace ElevenLab\GeoLaravel\Eloquent;
use ElevenLab\GeoLaravel\Database\Query\Builder;
use ElevenLab\PHPOGC\OGCObject;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Eloquent\Model as IlluminateModel;
class Model extends IlluminateModel
{
protected $geometries = [];
protected static $geotypes = [
'points' => 'ElevenLab\PHPOGC\DataTypes\Point',
'multipoints' => 'ElevenLab\PHPOGC\DataTypes\MultiPoints',
'linestrings' => 'ElevenLab\PHPOGC\DataTypes\LineString',
'multilinestrings' => 'ElevenLab\PHPOGC\DataTypes\MultiLineString',
'polygons' => 'ElevenLab\PHPOGC\DataTypes\Polygon',
'multipolygons' => 'ElevenLab\PHPOGC\DataTypes\MultiPolygon',
'geometrycollection' => 'ElevenLab\PHPOGC\DataTypes\GeometryCollection'
];
public $tmp_geo = [];
/**
* Overriding the "booting" method of the model.
*
* @return void
*/
protected static function boot()
{
parent::boot();
static::creating(function($model){
self::updateGeoAttributes($model);
});
static::created(function($model){
self::updateGeoAttributes($model);
});
static::updating(function($model){
self::updateGeoAttributes($model);
});
static::updated(function($model){
self::updateGeoAttributes($model);
});
}
/**
* Get a new custom query builder instance for the connection.
*
* @return \Illuminate\Database\Query\Builder
*/
protected function newBaseQueryBuilder()
{
$conn = $this->getConnection();
$grammar = $conn->getQueryGrammar();
return new Builder($conn, $grammar, $conn->getPostProcessor());
}
/**
* @param $geotype
* @return bool
*/
private static function isValidGeotype($geotype)
{
return (in_array($geotype, array_keys(static::$geotypes)));
}
/**
* @param $model
* @param $attribute
* @return mixed
* @throws \Exception
*/
private static function getGeoType($model, $attribute)
{
foreach ($model->geometries as $geometry => $attributes) {
if(in_array($attribute, $attributes))
return $geometry;
}
throw new \Exception('Given attribute has no geotype.');
}
/**
* @param $model
* @throws \Exception
*/
public static function updateGeoAttributes($model)
{
if( ! isset($model->geometries) ) return;
foreach($model->geometries as $geotype => $attrnames){
if(!self::isValidGeotype($geotype))
throw new \Exception('Unknown geotype: ' . $geotype);
$classname = static::$geotypes[$geotype];
foreach ($attrnames as $attrname){
if( isset($model->$attrname) ){
if($model->$attrname instanceof Expression){
$model->setAttribute( $attrname, $model->tmp_geo[$attrname] );
unset($model->tmp_geo[$attrname]);
}else if($model->$attrname instanceof $classname){
$model->tmp_geo[$attrname] = $model->$attrname;
$model->setAttribute( $attrname, \DB::rawGeo( $model->$attrname ));
}else{
throw new \Exception('Geometry attribute ' . $attrname .' must be an instance of ' . $classname);
}
}
}
}
}
/**
* @param array $attributes
* @param null $connection
* @return \Illuminate\Database\Eloquent\Model
* @throws \Exception
*/
public function newFromBuilder($attributes = [], $connection = null)
{
$model = parent::newFromBuilder($attributes, $connection);
if( ! isset($model->geometries) ) return;
foreach($model->geometries as $geotype => $attrnames) {
if(!self::isValidGeotype($geotype))
throw new \Exception('Unknown geotype: ' . $geotype);
foreach ($attrnames as $attrname) {
if (isset($model->$attrname)) {
$model->$attrname; // use the magic __get to instantiate element
}
}
}
return $model;
}
/**
* @param string $key
* @return mixed
*/
public function __get($key)
{
if(
in_array($key, array_flatten($this->geometries)) && // if the attribute is a geometry
! parent::__get($key) instanceof OGCObject && // if it wasn't still converted to geo object
! parent::__get($key) instanceof Expression && // if it is not in DB Expression form
parent::__get($key) != "" // if it is not empty
){
$geotype = self::getGeoType($this, $key);
$classname = self::$geotypes[$geotype];
$data = parent::__get($key);
# here we have 3 possible value for $data:
# 1) binary: we probably have a BLOB from MySQL
# 2) hex: PgSQL gives an hex WKB output for geo data
# 3) WKT: else
#
if(!ctype_print($data) or ctype_xdigit($data)){
$wkb = \DB::fromRawToWKB(parent::__get($key));
$this->setAttribute($key, $classname::fromWKB($wkb));
}else{ // assuming that it is in WKT
$this->setAttribute($key, $classname::fromWKT($data));
}
}
return parent::__get($key);
}
}