Skip to content

Commit

Permalink
Merge pull request #5 from chialab/add-contains-improve-mysql-wkb-filter
Browse files Browse the repository at this point in the history
  • Loading branch information
edoardocavazza authored May 3, 2023
2 parents 40cb49d + 22d8546 commit 12ee6bc
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
uses: 'shivammathur/setup-php@v2'
with:
php-version: '8.1'
tools: 'composer, phpstan'
tools: 'composer'
extensions: 'mbstring, intl'
coverage: 'none'

Expand All @@ -80,5 +80,4 @@ jobs:
run: 'composer install --prefer-dist --no-interaction'

- name: 'Run PHP STAN'
run: |
phpstan analyse --no-progress src
run: 'composer run-script stan'
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
},
"require-dev": {
"cakephp/cakephp-codesniffer": "^4.2",
"phpunit/phpunit": "^8.5 || ^9.3"
"phpunit/phpunit": "^8.5 || ^9.3",
"phpstan/phpstan": "^1.10"
},
"autoload": {
"psr-4": {
Expand Down
12 changes: 12 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
parameters:
bootstrapFiles:
- tests/bootstrap.php
paths:
- src
- tests
phpVersion: 70400
level: 4
ignoreErrors:
- message: '#Call to static method map\(\) on an unknown class Cake\\Database\\Type.#'
paths:
- src/Plugin.php
4 changes: 2 additions & 2 deletions src/Geometry.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Geometry implements \JsonSerializable
*
* @param \Brick\Geo\Geometry $geometry The geometry instance.
*/
public function __construct(BrickGeometry $geometry)
final public function __construct(BrickGeometry $geometry)
{
$this->geometry = $geometry;
}
Expand Down Expand Up @@ -63,7 +63,7 @@ public static function parse($geometry): self
// Not a WKB string.
}

if (strlen($geometry) > 4) {
if (strspn($geometry, '01', 0, 4) === 4) {
try {
[$wkb, $srid] = [substr($geometry, 4), bindec(substr($geometry, 0, 4))];

Expand Down
12 changes: 11 additions & 1 deletion src/Model/Behavior/GeometryBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function initialize(array $config): void
*/
public function findGeo(Query $query, array $options): Query
{
$options = array_intersect_key($options, array_flip(['intersects', 'within']));
$options = array_intersect_key($options, array_flip(['intersects', 'within', 'contains']));

$dbField = $this->_table->aliasField($this->getConfigOrFail('geometryField'));
$dbGeom = [$dbField => 'identifier'];
Expand Down Expand Up @@ -85,6 +85,16 @@ public function findGeo(Query $query, array $options): Query
->add(new FunctionExpression('ST_Within', array_merge($dbGeom, ['test' => $geom]), ['test' => 'geometry'])));

break;

case 'contains':
$geom = Geometry::parse($geom)->getGeometry()->withSRID(0);

$query = $query->where(fn (QueryExpression $exp) => $exp
->isNotNull($dbField)
->notEq($dbField, '', 'string')
->add(new FunctionExpression('ST_Contains', array_merge($dbGeom, ['test' => $geom]), ['test' => 'geometry'])));

break;
}
}

Expand Down

0 comments on commit 12ee6bc

Please sign in to comment.