Completed
Pull Request — master (#3)
by
unknown
08:58
created

Builder::update()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.1755

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 16
ccs 7
cts 9
cp 0.7778
crap 4.1755
rs 9.9666
1
<?php
2
3
namespace LaravelSpatial\Eloquent;
4
5
use GeoJson\Geometry\Geometry;
6
use geoPHP\geoPHP;
7
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
8
use LaravelSpatial\Exceptions\SpatialParseException;
9
10
/**
11
 * Class Builder
12
 *
13
 * @package LaravelSpatial\Eloquent
14
 */
15
class Builder extends EloquentBuilder
16
{
17
	/**
18
	 * @inheritDoc
19
	 */
20 12
    public function update(array $values)
21
    {
22 12
        foreach ($values as $key => &$value) {
23 12
            if ($value instanceof Geometry) {
24
	            try {
25 12
		            $wkt = geoPHP::load(json_decode(json_encode($value->jsonSerialize()), FALSE), 'json')
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
26 12
		                         ->out('wkt');
27
	            } catch (\Exception $e) {
28
	            	throw new SpatialParseException(\sprintf('Unable to parse geometry data for column %s.',$key), 0, $e);
29
	            }
30
31 12
	            $value = $this->getQuery()->raw("ST_GeomFromText('{$wkt}')");
32
            }
33
        }
34
35 12
        return parent::update($values);
36
    }
37
}
38