Passed
Push — main ( f9aaf7...4197a4 )
by Jonathan
14:34
created
app/Module/GeoDispersion/Services/MapAdapterDataService.php 1 patch
Indentation   +157 added lines, -157 removed lines patch added patch discarded remove patch
@@ -31,171 +31,171 @@
 block discarded – undo
31 31
  */
32 32
 class MapAdapterDataService
33 33
 {
34
-    private MapDefinitionsService $mapdefinition_service;
34
+	private MapDefinitionsService $mapdefinition_service;
35 35
 
36
-    /**
37
-     * Constructor for MapAdapterDataService
38
-     *
39
-     * @param MapDefinitionsService $mapdefinition_service
40
-     */
41
-    public function __construct(MapDefinitionsService $mapdefinition_service)
42
-    {
43
-        $this->mapdefinition_service = $mapdefinition_service;
44
-    }
36
+	/**
37
+	 * Constructor for MapAdapterDataService
38
+	 *
39
+	 * @param MapDefinitionsService $mapdefinition_service
40
+	 */
41
+	public function __construct(MapDefinitionsService $mapdefinition_service)
42
+	{
43
+		$this->mapdefinition_service = $mapdefinition_service;
44
+	}
45 45
 
46
-    /**
47
-     * Find a GeoAnalysisMapAdapter by ID
48
-     *
49
-     * @param int $id
50
-     * @return GeoAnalysisMapAdapter|NULL
51
-     */
52
-    public function find(int $id): ?GeoAnalysisMapAdapter
53
-    {
54
-        return DB::table('maj_geodisp_mapviews')
55
-            ->select('maj_geodisp_mapviews.*')
56
-            ->where('majgm_id', '=', $id)
57
-            ->get()
58
-            ->map($this->mapAdapterMapper())
59
-            ->first();
60
-    }
46
+	/**
47
+	 * Find a GeoAnalysisMapAdapter by ID
48
+	 *
49
+	 * @param int $id
50
+	 * @return GeoAnalysisMapAdapter|NULL
51
+	 */
52
+	public function find(int $id): ?GeoAnalysisMapAdapter
53
+	{
54
+		return DB::table('maj_geodisp_mapviews')
55
+			->select('maj_geodisp_mapviews.*')
56
+			->where('majgm_id', '=', $id)
57
+			->get()
58
+			->map($this->mapAdapterMapper())
59
+			->first();
60
+	}
61 61
 
62
-    /**
63
-     * Get all GeoAnalysisMapAdapters linked to a Map View.
64
-     *
65
-     * @param GeoAnalysisMap $map_view
66
-     * @param bool $show_invalid
67
-     * @return Collection<GeoAnalysisMapAdapter|null>
68
-     */
69
-    public function allForView(GeoAnalysisMap $map_view, bool $show_invalid = false): Collection
70
-    {
71
-        $map_adapters = DB::table('maj_geodisp_mapviews')
72
-            ->select('maj_geodisp_mapviews.*')
73
-            ->where('majgm_majgv_id', '=', $map_view->id())
74
-            ->get()
75
-            ->map($this->mapAdapterMapper());
76
-        return $show_invalid ? $map_adapters : $map_adapters->filter();
77
-    }
62
+	/**
63
+	 * Get all GeoAnalysisMapAdapters linked to a Map View.
64
+	 *
65
+	 * @param GeoAnalysisMap $map_view
66
+	 * @param bool $show_invalid
67
+	 * @return Collection<GeoAnalysisMapAdapter|null>
68
+	 */
69
+	public function allForView(GeoAnalysisMap $map_view, bool $show_invalid = false): Collection
70
+	{
71
+		$map_adapters = DB::table('maj_geodisp_mapviews')
72
+			->select('maj_geodisp_mapviews.*')
73
+			->where('majgm_majgv_id', '=', $map_view->id())
74
+			->get()
75
+			->map($this->mapAdapterMapper());
76
+		return $show_invalid ? $map_adapters : $map_adapters->filter();
77
+	}
78 78
 
79
-    /**
80
-     * Insert a GeoAnalysisMapAdapter in the database.
81
-     *
82
-     * @param GeoAnalysisMapAdapter $map_adapter
83
-     * @return int
84
-     */
85
-    public function insertGetId(GeoAnalysisMapAdapter $map_adapter): int
86
-    {
87
-        return DB::table('maj_geodisp_mapviews')
88
-            ->insertGetId([
89
-                'majgm_majgv_id' => $map_adapter->geoAnalysisViewId(),
90
-                'majgm_map_id' => $map_adapter->map()->id(),
91
-                'majgm_mapper' => get_class($map_adapter->placeMapper()),
92
-                'majgm_feature_prop' => $map_adapter->viewConfig()->mapMappingProperty(),
93
-                'majgm_config' => json_encode($map_adapter->viewConfig()->mapperConfig())
94
-            ]);
95
-    }
79
+	/**
80
+	 * Insert a GeoAnalysisMapAdapter in the database.
81
+	 *
82
+	 * @param GeoAnalysisMapAdapter $map_adapter
83
+	 * @return int
84
+	 */
85
+	public function insertGetId(GeoAnalysisMapAdapter $map_adapter): int
86
+	{
87
+		return DB::table('maj_geodisp_mapviews')
88
+			->insertGetId([
89
+				'majgm_majgv_id' => $map_adapter->geoAnalysisViewId(),
90
+				'majgm_map_id' => $map_adapter->map()->id(),
91
+				'majgm_mapper' => get_class($map_adapter->placeMapper()),
92
+				'majgm_feature_prop' => $map_adapter->viewConfig()->mapMappingProperty(),
93
+				'majgm_config' => json_encode($map_adapter->viewConfig()->mapperConfig())
94
+			]);
95
+	}
96 96
 
97
-    /**
98
-     * Update a GeoAnalysisMapAdapter in the database.
99
-     *
100
-     * @param GeoAnalysisMapAdapter $map_adapter
101
-     * @return int
102
-     */
103
-    public function update(GeoAnalysisMapAdapter $map_adapter): int
104
-    {
105
-        return DB::table('maj_geodisp_mapviews')
106
-            ->where('majgm_id', '=', $map_adapter->id())
107
-            ->update([
108
-                'majgm_map_id' => $map_adapter->map()->id(),
109
-                'majgm_mapper' => get_class($map_adapter->placeMapper()),
110
-                'majgm_feature_prop' => $map_adapter->viewConfig()->mapMappingProperty(),
111
-                'majgm_config' => json_encode($map_adapter->placeMapper()->config())
112
-            ]);
113
-    }
97
+	/**
98
+	 * Update a GeoAnalysisMapAdapter in the database.
99
+	 *
100
+	 * @param GeoAnalysisMapAdapter $map_adapter
101
+	 * @return int
102
+	 */
103
+	public function update(GeoAnalysisMapAdapter $map_adapter): int
104
+	{
105
+		return DB::table('maj_geodisp_mapviews')
106
+			->where('majgm_id', '=', $map_adapter->id())
107
+			->update([
108
+				'majgm_map_id' => $map_adapter->map()->id(),
109
+				'majgm_mapper' => get_class($map_adapter->placeMapper()),
110
+				'majgm_feature_prop' => $map_adapter->viewConfig()->mapMappingProperty(),
111
+				'majgm_config' => json_encode($map_adapter->placeMapper()->config())
112
+			]);
113
+	}
114 114
 
115
-    /**
116
-     * Delete a GeoAnalysisMapAdapter from the database.
117
-     *
118
-     * @param GeoAnalysisMapAdapter $map_adapter
119
-     * @return int
120
-     */
121
-    public function delete(GeoAnalysisMapAdapter $map_adapter): int
122
-    {
123
-        return DB::table('maj_geodisp_mapviews')
124
-            ->where('majgm_id', '=', $map_adapter->id())
125
-            ->delete();
126
-    }
115
+	/**
116
+	 * Delete a GeoAnalysisMapAdapter from the database.
117
+	 *
118
+	 * @param GeoAnalysisMapAdapter $map_adapter
119
+	 * @return int
120
+	 */
121
+	public function delete(GeoAnalysisMapAdapter $map_adapter): int
122
+	{
123
+		return DB::table('maj_geodisp_mapviews')
124
+			->where('majgm_id', '=', $map_adapter->id())
125
+			->delete();
126
+	}
127 127
 
128
-    /**
129
-     * Delete invalid GeoAnalysisMapAdapters from the database.
130
-     *
131
-     * @param AbstractGeoAnalysisView $view
132
-     * @param Collection<int> $valid_map_adapters
133
-     * @return int
134
-     */
135
-    public function deleteInvalid(AbstractGeoAnalysisView $view, Collection $valid_map_adapters): int
136
-    {
137
-        return DB::table('maj_geodisp_mapviews')
138
-            ->where('majgm_majgv_id', '=', $view->id())
139
-            ->whereNotIn('majgm_id', $valid_map_adapters)
140
-            ->delete();
141
-    }
128
+	/**
129
+	 * Delete invalid GeoAnalysisMapAdapters from the database.
130
+	 *
131
+	 * @param AbstractGeoAnalysisView $view
132
+	 * @param Collection<int> $valid_map_adapters
133
+	 * @return int
134
+	 */
135
+	public function deleteInvalid(AbstractGeoAnalysisView $view, Collection $valid_map_adapters): int
136
+	{
137
+		return DB::table('maj_geodisp_mapviews')
138
+			->where('majgm_majgv_id', '=', $view->id())
139
+			->whereNotIn('majgm_id', $valid_map_adapters)
140
+			->delete();
141
+	}
142 142
 
143
-    /**
144
-     * Get the closure to create a GeoAnalysisMapAdapter object from a row in the database.
145
-     * It returns null if the classes stored in the DB cannot be loaded through the Laravel container,
146
-     * or if the types do not match with the ones expected.
147
-     *
148
-     * @return Closure(\stdClass $row):?GeoAnalysisMapAdapter
149
-     */
150
-    private function mapAdapterMapper(): Closure
151
-    {
152
-        return function (stdClass $row): ?GeoAnalysisMapAdapter {
153
-            if (null === $map = $this->mapdefinition_service->find($row->majgm_map_id)) {
154
-                return null;
155
-            }
156
-            try {
157
-                $mapper = app($row->majgm_mapper);
158
-                if (!($mapper instanceof PlaceMapperInterface)) {
159
-                    return null;
160
-                }
143
+	/**
144
+	 * Get the closure to create a GeoAnalysisMapAdapter object from a row in the database.
145
+	 * It returns null if the classes stored in the DB cannot be loaded through the Laravel container,
146
+	 * or if the types do not match with the ones expected.
147
+	 *
148
+	 * @return Closure(\stdClass $row):?GeoAnalysisMapAdapter
149
+	 */
150
+	private function mapAdapterMapper(): Closure
151
+	{
152
+		return function (stdClass $row): ?GeoAnalysisMapAdapter {
153
+			if (null === $map = $this->mapdefinition_service->find($row->majgm_map_id)) {
154
+				return null;
155
+			}
156
+			try {
157
+				$mapper = app($row->majgm_mapper);
158
+				if (!($mapper instanceof PlaceMapperInterface)) {
159
+					return null;
160
+				}
161 161
 
162
-                return new GeoAnalysisMapAdapter(
163
-                    (int) $row->majgm_id,
164
-                    (int) $row->majgm_majgv_id,
165
-                    $map,
166
-                    app($row->majgm_mapper),
167
-                    new MapViewConfig($row->majgm_feature_prop, $this->mapperConfigDecoder($row->majgm_config))
168
-                );
169
-            } catch (BindingResolutionException $ex) {
170
-                return null;
171
-            }
172
-        };
173
-    }
162
+				return new GeoAnalysisMapAdapter(
163
+					(int) $row->majgm_id,
164
+					(int) $row->majgm_majgv_id,
165
+					$map,
166
+					app($row->majgm_mapper),
167
+					new MapViewConfig($row->majgm_feature_prop, $this->mapperConfigDecoder($row->majgm_config))
168
+				);
169
+			} catch (BindingResolutionException $ex) {
170
+				return null;
171
+			}
172
+		};
173
+	}
174 174
 
175
-    /**
176
-     * Create a PlaceMapperConfigInterface object from a JSON column value.
177
-     * Returns null if the JSON string is invalid/empty or if the extracted mapper class cannot be loaded
178
-     * through the Laravel container or if the type do not match with the one expected.
179
-     *
180
-     * @param string $json_config
181
-     * @return PlaceMapperConfigInterface|NULL
182
-     */
183
-    private function mapperConfigDecoder(?string $json_config): ?PlaceMapperConfigInterface
184
-    {
185
-        $config = $json_config === null ? [] : json_decode($json_config, true);
186
-        $class = $config['class'] ?? null;
187
-        $json_mapper_config = $config['config'] ?? null;
188
-        if ($class === null || $json_mapper_config === null) {
189
-            return null;
190
-        }
191
-        try {
192
-            $mapper_config = app($class);
193
-            if (!$mapper_config instanceof PlaceMapperConfigInterface) {
194
-                return null;
195
-            }
196
-            return $mapper_config->jsonDeserialize($json_mapper_config);
197
-        } catch (BindingResolutionException $ex) {
198
-            return null;
199
-        }
200
-    }
175
+	/**
176
+	 * Create a PlaceMapperConfigInterface object from a JSON column value.
177
+	 * Returns null if the JSON string is invalid/empty or if the extracted mapper class cannot be loaded
178
+	 * through the Laravel container or if the type do not match with the one expected.
179
+	 *
180
+	 * @param string $json_config
181
+	 * @return PlaceMapperConfigInterface|NULL
182
+	 */
183
+	private function mapperConfigDecoder(?string $json_config): ?PlaceMapperConfigInterface
184
+	{
185
+		$config = $json_config === null ? [] : json_decode($json_config, true);
186
+		$class = $config['class'] ?? null;
187
+		$json_mapper_config = $config['config'] ?? null;
188
+		if ($class === null || $json_mapper_config === null) {
189
+			return null;
190
+		}
191
+		try {
192
+			$mapper_config = app($class);
193
+			if (!$mapper_config instanceof PlaceMapperConfigInterface) {
194
+				return null;
195
+			}
196
+			return $mapper_config->jsonDeserialize($json_mapper_config);
197
+		} catch (BindingResolutionException $ex) {
198
+			return null;
199
+		}
200
+	}
201 201
 }
Please login to merge, or discard this patch.
app/Module/GeoDispersion/Services/GeoAnalysisService.php 2 patches
Indentation   +34 added lines, -34 removed lines patch added patch discarded remove patch
@@ -25,41 +25,41 @@
 block discarded – undo
25 25
  */
26 26
 class GeoAnalysisService
27 27
 {
28
-    private ModuleService $module_service;
28
+	private ModuleService $module_service;
29 29
 
30
-    /**
31
-     * Constructor for MapDefinitionsService
32
-     *
33
-     * @param ModuleService $module_service
34
-     */
35
-    public function __construct(ModuleService $module_service)
36
-    {
37
-        $this->module_service = $module_service;
38
-    }
30
+	/**
31
+	 * Constructor for MapDefinitionsService
32
+	 *
33
+	 * @param ModuleService $module_service
34
+	 */
35
+	public function __construct(ModuleService $module_service)
36
+	{
37
+		$this->module_service = $module_service;
38
+	}
39 39
 
40
-    /**
41
-     * Get all available geographical dispersion analyses.
42
-     *
43
-     * {@internal The list is generated based on the modules exposing ModuleGeoAnalysisProviderInterface
44
-     *
45
-     * @param bool $include_disabled
46
-     * @return Collection<GeoAnalysisInterface>
47
-     */
48
-    public function all(bool $include_disabled = false): Collection
49
-    {
50
-        /** @var Collection<GeoAnalysisInterface> $geoanalyses */
51
-        $geoanalyses = $this->module_service
52
-            ->findByInterface(ModuleGeoAnalysisProviderInterface::class, $include_disabled)
53
-            ->flatMap(fn(ModuleGeoAnalysisProviderInterface $module) => $module->listGeoAnalyses())
54
-            ->map(static function (string $analysis_class): ?GeoAnalysisInterface {
55
-                try {
56
-                    $analysis = app($analysis_class);
57
-                    return $analysis instanceof GeoAnalysisInterface ? $analysis : null;
58
-                } catch (BindingResolutionException $ex) {
59
-                    return null;
60
-                }
61
-            })->filter();
40
+	/**
41
+	 * Get all available geographical dispersion analyses.
42
+	 *
43
+	 * {@internal The list is generated based on the modules exposing ModuleGeoAnalysisProviderInterface
44
+	 *
45
+	 * @param bool $include_disabled
46
+	 * @return Collection<GeoAnalysisInterface>
47
+	 */
48
+	public function all(bool $include_disabled = false): Collection
49
+	{
50
+		/** @var Collection<GeoAnalysisInterface> $geoanalyses */
51
+		$geoanalyses = $this->module_service
52
+			->findByInterface(ModuleGeoAnalysisProviderInterface::class, $include_disabled)
53
+			->flatMap(fn(ModuleGeoAnalysisProviderInterface $module) => $module->listGeoAnalyses())
54
+			->map(static function (string $analysis_class): ?GeoAnalysisInterface {
55
+				try {
56
+					$analysis = app($analysis_class);
57
+					return $analysis instanceof GeoAnalysisInterface ? $analysis : null;
58
+				} catch (BindingResolutionException $ex) {
59
+					return null;
60
+				}
61
+			})->filter();
62 62
 
63
-        return $geoanalyses;
64
-    }
63
+		return $geoanalyses;
64
+	}
65 65
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -51,7 +51,7 @@
 block discarded – undo
51 51
         $geoanalyses = $this->module_service
52 52
             ->findByInterface(ModuleGeoAnalysisProviderInterface::class, $include_disabled)
53 53
             ->flatMap(fn(ModuleGeoAnalysisProviderInterface $module) => $module->listGeoAnalyses())
54
-            ->map(static function (string $analysis_class): ?GeoAnalysisInterface {
54
+            ->map(static function(string $analysis_class): ?GeoAnalysisInterface {
55 55
                 try {
56 56
                     $analysis = app($analysis_class);
57 57
                     return $analysis instanceof GeoAnalysisInterface ? $analysis : null;
Please login to merge, or discard this patch.
app/Module/GeoDispersion/Services/PlaceMapperService.php 2 patches
Indentation   +34 added lines, -34 removed lines patch added patch discarded remove patch
@@ -25,41 +25,41 @@
 block discarded – undo
25 25
  */
26 26
 class PlaceMapperService
27 27
 {
28
-    private ModuleService $module_service;
28
+	private ModuleService $module_service;
29 29
 
30
-    /**
31
-     * Constructor for PlaceMapperService
32
-     *
33
-     * @param ModuleService $module_service
34
-     */
35
-    public function __construct(ModuleService $module_service)
36
-    {
37
-        $this->module_service = $module_service;
38
-    }
30
+	/**
31
+	 * Constructor for PlaceMapperService
32
+	 *
33
+	 * @param ModuleService $module_service
34
+	 */
35
+	public function __construct(ModuleService $module_service)
36
+	{
37
+		$this->module_service = $module_service;
38
+	}
39 39
 
40
-    /**
41
-     * Get all place mappers available.
42
-     *
43
-     * {@internal The list is generated based on the modules exposing ModulePlaceMapperProviderInterface}
44
-     *
45
-     * @param bool $include_disabled
46
-     * @return Collection<PlaceMapperInterface>
47
-     */
48
-    public function all(bool $include_disabled = false): Collection
49
-    {
50
-        /** @var Collection<PlaceMapperInterface> $place_mappers */
51
-        $place_mappers =  $this->module_service
52
-            ->findByInterface(ModulePlaceMapperProviderInterface::class, $include_disabled)
53
-            ->flatMap(fn(ModulePlaceMapperProviderInterface $module) => $module->listPlaceMappers())
54
-            ->map(static function (string $mapper_class): ?PlaceMapperInterface {
55
-                try {
56
-                    $mapper = app($mapper_class);
57
-                    return $mapper instanceof PlaceMapperInterface ? $mapper : null;
58
-                } catch (BindingResolutionException $ex) {
59
-                    return null;
60
-                }
61
-            })->filter();
40
+	/**
41
+	 * Get all place mappers available.
42
+	 *
43
+	 * {@internal The list is generated based on the modules exposing ModulePlaceMapperProviderInterface}
44
+	 *
45
+	 * @param bool $include_disabled
46
+	 * @return Collection<PlaceMapperInterface>
47
+	 */
48
+	public function all(bool $include_disabled = false): Collection
49
+	{
50
+		/** @var Collection<PlaceMapperInterface> $place_mappers */
51
+		$place_mappers =  $this->module_service
52
+			->findByInterface(ModulePlaceMapperProviderInterface::class, $include_disabled)
53
+			->flatMap(fn(ModulePlaceMapperProviderInterface $module) => $module->listPlaceMappers())
54
+			->map(static function (string $mapper_class): ?PlaceMapperInterface {
55
+				try {
56
+					$mapper = app($mapper_class);
57
+					return $mapper instanceof PlaceMapperInterface ? $mapper : null;
58
+				} catch (BindingResolutionException $ex) {
59
+					return null;
60
+				}
61
+			})->filter();
62 62
 
63
-        return $place_mappers;
64
-    }
63
+		return $place_mappers;
64
+	}
65 65
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -48,10 +48,10 @@
 block discarded – undo
48 48
     public function all(bool $include_disabled = false): Collection
49 49
     {
50 50
         /** @var Collection<PlaceMapperInterface> $place_mappers */
51
-        $place_mappers =  $this->module_service
51
+        $place_mappers = $this->module_service
52 52
             ->findByInterface(ModulePlaceMapperProviderInterface::class, $include_disabled)
53 53
             ->flatMap(fn(ModulePlaceMapperProviderInterface $module) => $module->listPlaceMappers())
54
-            ->map(static function (string $mapper_class): ?PlaceMapperInterface {
54
+            ->map(static function(string $mapper_class): ?PlaceMapperInterface {
55 55
                 try {
56 56
                     $mapper = app($mapper_class);
57 57
                     return $mapper instanceof PlaceMapperInterface ? $mapper : null;
Please login to merge, or discard this patch.
app/Module/GeoDispersion/Services/GeoAnalysisDataService.php 1 patch
Indentation   +56 added lines, -56 removed lines patch added patch discarded remove patch
@@ -26,64 +26,64 @@
 block discarded – undo
26 26
  */
27 27
 class GeoAnalysisDataService
28 28
 {
29
-    /**
30
-     * Yields indviduals and family records for a specified tree.
31
-     *
32
-     * @param Tree $tree
33
-     * @return \Generator<\Fisharebest\Webtrees\GedcomRecord>
34
-     */
35
-    public function individualsAndFamilies(Tree $tree): Generator
36
-    {
37
-        yield from DB::table('individuals')
38
-            ->where('i_file', '=', $tree->id())
39
-            ->select(['individuals.*'])
40
-            ->get()
41
-            ->map(Registry::individualFactory()->mapper($tree))
42
-            ->filter(GedcomRecord::accessFilter())
43
-            ->all();
29
+	/**
30
+	 * Yields indviduals and family records for a specified tree.
31
+	 *
32
+	 * @param Tree $tree
33
+	 * @return \Generator<\Fisharebest\Webtrees\GedcomRecord>
34
+	 */
35
+	public function individualsAndFamilies(Tree $tree): Generator
36
+	{
37
+		yield from DB::table('individuals')
38
+			->where('i_file', '=', $tree->id())
39
+			->select(['individuals.*'])
40
+			->get()
41
+			->map(Registry::individualFactory()->mapper($tree))
42
+			->filter(GedcomRecord::accessFilter())
43
+			->all();
44 44
 
45
-        yield from DB::table('families')
46
-            ->where('f_file', '=', $tree->id())
47
-            ->select(['families.*'])
48
-            ->get()
49
-            ->map(Registry::familyFactory()->mapper($tree))
50
-            ->filter(GedcomRecord::accessFilter())
51
-            ->all();
52
-    }
45
+		yield from DB::table('families')
46
+			->where('f_file', '=', $tree->id())
47
+			->select(['families.*'])
48
+			->get()
49
+			->map(Registry::familyFactory()->mapper($tree))
50
+			->filter(GedcomRecord::accessFilter())
51
+			->all();
52
+	}
53 53
 
54
-    /**
55
-     * Returns an example of the place hierarchy, from a place within the GEDCOM file, looking for the deepest
56
-     * hierarchy found. The part order is reversed compared to the normal GEDCOM structure (largest first).
57
-     *
58
-     * {@internal The places are taken only from the individuals and families records.}
59
-     *
60
-     * @param Tree $tree
61
-     * @return array<int, string[]>
62
-     */
63
-    public function placeHierarchyExample(Tree $tree): array
64
-    {
65
-        $query_individuals = DB::table('individuals')
66
-            ->select(['i_gedcom AS g_gedcom'])
67
-            ->where('i_file', '=', $tree->id())
68
-            ->where('i_gedcom', 'like', '%2 PLAC %');
54
+	/**
55
+	 * Returns an example of the place hierarchy, from a place within the GEDCOM file, looking for the deepest
56
+	 * hierarchy found. The part order is reversed compared to the normal GEDCOM structure (largest first).
57
+	 *
58
+	 * {@internal The places are taken only from the individuals and families records.}
59
+	 *
60
+	 * @param Tree $tree
61
+	 * @return array<int, string[]>
62
+	 */
63
+	public function placeHierarchyExample(Tree $tree): array
64
+	{
65
+		$query_individuals = DB::table('individuals')
66
+			->select(['i_gedcom AS g_gedcom'])
67
+			->where('i_file', '=', $tree->id())
68
+			->where('i_gedcom', 'like', '%2 PLAC %');
69 69
 
70
-        $query_families = DB::table('families')
71
-            ->select(['f_gedcom AS g_gedcom'])
72
-            ->where('f_file', '=', $tree->id())
73
-            ->where('f_gedcom', 'like', '%2 PLAC %');
70
+		$query_families = DB::table('families')
71
+			->select(['f_gedcom AS g_gedcom'])
72
+			->where('f_file', '=', $tree->id())
73
+			->where('f_gedcom', 'like', '%2 PLAC %');
74 74
 
75
-        return $query_individuals->unionAll($query_families)
76
-            ->get()->pluck('g_gedcom')
77
-            ->flatMap(static function (string $gedcom): array {
78
-                preg_match_all('/\n2 PLAC (.+)/', $gedcom, $matches);
79
-                return $matches[1] ?? [];
80
-            })
81
-            ->sort(I18N::comparator())->reverse()
82
-            ->mapWithKeys(static function (string $place): array {
83
-                $place_array = array_reverse(array_filter(array_map('trim', explode(",", $place))));
84
-                return [ count($place_array) => $place_array ];
85
-            })
86
-            ->sortKeys()
87
-            ->last();
88
-    }
75
+		return $query_individuals->unionAll($query_families)
76
+			->get()->pluck('g_gedcom')
77
+			->flatMap(static function (string $gedcom): array {
78
+				preg_match_all('/\n2 PLAC (.+)/', $gedcom, $matches);
79
+				return $matches[1] ?? [];
80
+			})
81
+			->sort(I18N::comparator())->reverse()
82
+			->mapWithKeys(static function (string $place): array {
83
+				$place_array = array_reverse(array_filter(array_map('trim', explode(",", $place))));
84
+				return [ count($place_array) => $place_array ];
85
+			})
86
+			->sortKeys()
87
+			->last();
88
+	}
89 89
 }
Please login to merge, or discard this patch.
app/Module/GeoDispersion/PlaceMappers/PlaceMapperTrait.php 1 patch
Indentation   +57 added lines, -57 removed lines patch added patch discarded remove patch
@@ -24,67 +24,67 @@
 block discarded – undo
24 24
  */
25 25
 trait PlaceMapperTrait
26 26
 {
27
-    private ?PlaceMapperConfigInterface $config = null;
27
+	private ?PlaceMapperConfigInterface $config = null;
28 28
 
29
-    /** @var array<string, mixed> $data */
30
-    private array $data = [];
29
+	/** @var array<string, mixed> $data */
30
+	private array $data = [];
31 31
 
32
-    /**
33
-     * Implementation of PlaceMapperInterface::boot
34
-     *
35
-     * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::boot()
36
-     */
37
-    public function boot(): void
38
-    {
39
-    }
32
+	/**
33
+	 * Implementation of PlaceMapperInterface::boot
34
+	 *
35
+	 * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::boot()
36
+	 */
37
+	public function boot(): void
38
+	{
39
+	}
40 40
 
41
-    /**
42
-     * Implementation of PlaceMapperInterface::config
43
-     *
44
-     * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::config()
45
-     *
46
-     * @return PlaceMapperConfigInterface
47
-     */
48
-    public function config(): PlaceMapperConfigInterface
49
-    {
50
-        return $this->config ?? new NullPlaceMapperConfig();
51
-    }
41
+	/**
42
+	 * Implementation of PlaceMapperInterface::config
43
+	 *
44
+	 * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::config()
45
+	 *
46
+	 * @return PlaceMapperConfigInterface
47
+	 */
48
+	public function config(): PlaceMapperConfigInterface
49
+	{
50
+		return $this->config ?? new NullPlaceMapperConfig();
51
+	}
52 52
 
53
-    /**
54
-     * Implementation of PlaceMapperInterface::setConfig
55
-     *
56
-     * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::setConfig()
57
-     *
58
-     * @param PlaceMapperConfigInterface $config
59
-     */
60
-    public function setConfig(PlaceMapperConfigInterface $config): void
61
-    {
62
-        $this->config = $config;
63
-    }
53
+	/**
54
+	 * Implementation of PlaceMapperInterface::setConfig
55
+	 *
56
+	 * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::setConfig()
57
+	 *
58
+	 * @param PlaceMapperConfigInterface $config
59
+	 */
60
+	public function setConfig(PlaceMapperConfigInterface $config): void
61
+	{
62
+		$this->config = $config;
63
+	}
64 64
 
65
-    /**
66
-     * Implementation of PlaceMapperInterface::data
67
-     *
68
-     * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::data()
69
-     *
70
-     * @param string $key
71
-     * @return NULL|mixed
72
-     */
73
-    public function data(string $key)
74
-    {
75
-        return $this->data[$key] ?? null;
76
-    }
65
+	/**
66
+	 * Implementation of PlaceMapperInterface::data
67
+	 *
68
+	 * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::data()
69
+	 *
70
+	 * @param string $key
71
+	 * @return NULL|mixed
72
+	 */
73
+	public function data(string $key)
74
+	{
75
+		return $this->data[$key] ?? null;
76
+	}
77 77
 
78
-    /**
79
-     * Implementation of PlaceMapperInterface::setData
80
-     *
81
-     * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::setData()
82
-     *
83
-     * @param string $key
84
-     * @param mixed|null $data
85
-     */
86
-    public function setData(string $key, $data): void
87
-    {
88
-        $this->data[$key] = $data;
89
-    }
78
+	/**
79
+	 * Implementation of PlaceMapperInterface::setData
80
+	 *
81
+	 * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::setData()
82
+	 *
83
+	 * @param string $key
84
+	 * @param mixed|null $data
85
+	 */
86
+	public function setData(string $key, $data): void
87
+	{
88
+		$this->data[$key] = $data;
89
+	}
90 90
 }
Please login to merge, or discard this patch.
app/Module/GeoDispersion/PlaceMappers/TopFilteredPlaceMapperTrait.php 1 patch
Indentation   +57 added lines, -57 removed lines patch added patch discarded remove patch
@@ -21,65 +21,65 @@
 block discarded – undo
21 21
  */
22 22
 trait TopFilteredPlaceMapperTrait
23 23
 {
24
-    /**
25
-     * @var Place[] $top_places
26
-     */
27
-    private array $top_places = [];
24
+	/**
25
+	 * @var Place[] $top_places
26
+	 */
27
+	private array $top_places = [];
28 28
 
29
-    /**
30
-     * Get the list of top level places.
31
-     *
32
-     * @return Place[]
33
-     */
34
-    public function topPlaces(): array
35
-    {
36
-        return $this->top_places;
37
-    }
29
+	/**
30
+	 * Get the list of top level places.
31
+	 *
32
+	 * @return Place[]
33
+	 */
34
+	public function topPlaces(): array
35
+	{
36
+		return $this->top_places;
37
+	}
38 38
 
39
-    /**
40
-     * Set the list of defined top level places.
41
-     *
42
-     * @param Place[] $top_places
43
-     */
44
-    public function setTopPlaces(array $top_places): void
45
-    {
46
-        $this->top_places = collect($top_places)
47
-            ->filter(
48
-                /** @psalm-suppress MissingClosureParamType */
49
-                fn($top_places): bool => $top_places instanceof Place
50
-            )->toArray();
51
-    }
39
+	/**
40
+	 * Set the list of defined top level places.
41
+	 *
42
+	 * @param Place[] $top_places
43
+	 */
44
+	public function setTopPlaces(array $top_places): void
45
+	{
46
+		$this->top_places = collect($top_places)
47
+			->filter(
48
+				/** @psalm-suppress MissingClosureParamType */
49
+				fn($top_places): bool => $top_places instanceof Place
50
+			)->toArray();
51
+	}
52 52
 
53
-    /**
54
-     * Check whether a string ($haystack) ends with another string ($needle)
55
-     *
56
-     * {@internal This is redundant with the function str_ends_with in PHP8}
57
-     *
58
-     * @param string $haystack
59
-     * @param string $needle
60
-     * @return bool
61
-     */
62
-    private function endsWith(string $haystack, string $needle): bool
63
-    {
64
-        return substr_compare($haystack, $needle, -strlen($needle)) === 0;
65
-    }
53
+	/**
54
+	 * Check whether a string ($haystack) ends with another string ($needle)
55
+	 *
56
+	 * {@internal This is redundant with the function str_ends_with in PHP8}
57
+	 *
58
+	 * @param string $haystack
59
+	 * @param string $needle
60
+	 * @return bool
61
+	 */
62
+	private function endsWith(string $haystack, string $needle): bool
63
+	{
64
+		return substr_compare($haystack, $needle, -strlen($needle)) === 0;
65
+	}
66 66
 
67
-    /**
68
-     * Check whether a Place belongs to one of the defined top places.
69
-     *
70
-     * @param Place $place
71
-     * @return bool
72
-     */
73
-    protected function belongsToTopLevels(Place $place): bool
74
-    {
75
-        foreach ($this->top_places as $top_place) {
76
-            if (
77
-                $top_place->tree()->id() === $place->tree()->id() &&
78
-                $this->endsWith($place->gedcomName(), $top_place->gedcomName())
79
-            ) {
80
-                    return true;
81
-            }
82
-        }
83
-        return false;
84
-    }
67
+	/**
68
+	 * Check whether a Place belongs to one of the defined top places.
69
+	 *
70
+	 * @param Place $place
71
+	 * @return bool
72
+	 */
73
+	protected function belongsToTopLevels(Place $place): bool
74
+	{
75
+		foreach ($this->top_places as $top_place) {
76
+			if (
77
+				$top_place->tree()->id() === $place->tree()->id() &&
78
+				$this->endsWith($place->gedcomName(), $top_place->gedcomName())
79
+			) {
80
+					return true;
81
+			}
82
+		}
83
+		return false;
84
+	}
85 85
 }
Please login to merge, or discard this patch.
app/Module/GeoDispersion/PlaceMappers/CoordinatesPlaceMapper.php 1 patch
Indentation   +180 added lines, -180 removed lines patch added patch discarded remove patch
@@ -36,184 +36,184 @@
 block discarded – undo
36 36
  */
37 37
 class CoordinatesPlaceMapper implements PlaceMapperInterface
38 38
 {
39
-    use PlaceMapperTrait;
40
-
41
-    private ?string $cache_key = null;
42
-
43
-    /**
44
-     * {@inheritDoc}
45
-     * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::title()
46
-     */
47
-    public function title(): string
48
-    {
49
-        return I18N::translate('Mapping on place coordinates');
50
-    }
51
-
52
-    /**
53
-     * {@inheritDoc}
54
-     *
55
-     * {@internal The Place is associated to a Point only.
56
-     * PlaceLocation can calculate a BoundingBox.
57
-     * Using a BoundingBox could make the mapping more complex and potentially arbitary.
58
-     * Furthermore, when no coordinate is found for the place or its children, then it bubbles up to the parents.
59
-     * This could create the unwanted side effect of a very large area to consider}
60
-     *
61
-     * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::map()
62
-     */
63
-    public function map(Place $place, string $feature_property): ?string
64
-    {
65
-        $location = new PlaceLocation($place->gedcomName());
66
-        $longitude = $location->longitude();
67
-        $latitude = $location->latitude();
68
-        if ($longitude === null || $latitude === null) {
69
-            return null;
70
-        }
71
-
72
-        $features_index = $this->featuresIndex();
73
-        if ($features_index === null) {
74
-            return null;
75
-        }
76
-
77
-        $place_point = Point::xy($longitude, $latitude, $features_index['SRID']);
78
-        $grid_box = $this->getGridCell(
79
-            $place_point,
80
-            $features_index['map_NE'],
81
-            $features_index['map_SW'],
82
-            $features_index['nb_columns']
83
-        );
84
-        if ($grid_box === null || !$this->setGeometryEngine()) {
85
-            return null;
86
-        }
87
-        $features = $features_index['grid'][$grid_box[0]][$grid_box[1]];
88
-        foreach ($features as $feature) {
89
-            $geometry = $feature->getGeometry();
90
-            if ($geometry !== null && $place_point->SRID() === $geometry->SRID() && $geometry->contains($place_point)) {
91
-                return $feature->getProperty($feature_property);
92
-            }
93
-        }
94
-        return null;
95
-    }
96
-
97
-    /**
98
-     * Return the XY coordinates in a bounded grid of the cell containing a specific point.
99
-     *
100
-     * @param Point $point Point to find
101
-     * @param Point $grid_NE North-East point of the bounded grid
102
-     * @param Point $grid_SW South-West point fo the bounded grid
103
-     * @param int $grid_columns Number of columns/rows in the grid
104
-     * @return int[]|NULL
105
-     */
106
-    protected function getGridCell(Point $point, Point $grid_NE, Point $grid_SW, int $grid_columns): ?array
107
-    {
108
-        list($x, $y) = $point->toArray();
109
-        list($x_max, $y_max) = $grid_NE->toArray();
110
-        list($x_min, $y_min) = $grid_SW->toArray();
111
-
112
-        $x_step = ($x_max - $x_min) / $grid_columns;
113
-        $y_step = ($y_max - $y_min) / $grid_columns;
114
-
115
-        if ($x_min <= $x && $x <= $x_max && $y_min <= $y && $y <= $y_max) {
116
-            return [
117
-                $x === $x_max ? $grid_columns - 1 : intval(($x - $x_min) / $x_step),
118
-                $y === $y_max ? $grid_columns - 1 : intval(($y - $y_min) / $y_step)
119
-            ];
120
-        }
121
-        return null;
122
-    }
123
-
124
-    /**
125
-     * Get an indexed array of the features of the map.
126
-     *
127
-     * {@internal The map is divided in a grid, eacg cell containing the features which bounding box overlaps that cell.
128
-     * The grid is computed once for each map, and cached.}
129
-     *
130
-     * @phpcs:ignore Generic.Files.LineLength.TooLong
131
-     * @return array{grid: array<int, array<int, \Brick\Geo\IO\GeoJSON\Feature[]>>, nb_columns: int, map_NE: \Brick\Geo\Point, map_SW: \Brick\Geo\Point, SRID: int}|NULL
132
-     */
133
-    protected function featuresIndex(): ?array
134
-    {
135
-        $cacheKey = $this->cacheKey();
136
-        if ($cacheKey === null) {
137
-            return null;
138
-        }
139
-        return Registry::cache()->array()->remember($cacheKey, function (): ?array {
140
-            $map_def = $this->data('map');
141
-            if (
142
-                !$this->setGeometryEngine()
143
-                || $map_def === null
144
-                || !($map_def instanceof MapDefinitionInterface)
145
-            ) {
146
-                return null;
147
-            }
148
-            $bounding_boxes = [];
149
-            $map_bounding_box = new BoundingBox();
150
-            $srid = 0;
151
-            foreach ($map_def->features() as $feature) {
152
-                $geometry = $feature->getGeometry();
153
-                if ($geometry === null) {
154
-                    continue;
155
-                }
156
-                $srid = $geometry->SRID();
157
-                $bounding_box = $geometry->getBoundingBox();
158
-                $bounding_boxes[] = [$feature, $bounding_box];
159
-                $map_bounding_box = $map_bounding_box->extendedWithBoundingBox($bounding_box);
160
-            }
161
-            $grid_columns = count($bounding_boxes);
162
-            $grid = array_fill(0, $grid_columns, array_fill(0, $grid_columns, []));
163
-            $map_NE = $map_bounding_box->getNorthEast();
164
-            $map_SW = $map_bounding_box->getSouthWest();
165
-            foreach ($bounding_boxes as $item) {
166
-                $grid_box_SW = $this->getGridCell($item[1]->getSouthWest(), $map_NE, $map_SW, $grid_columns) ?? [1, 1];
167
-                $grid_box_NE = $this->getGridCell($item[1]->getNorthEast(), $map_NE, $map_SW, $grid_columns) ?? [0, 0];
168
-                for ($i = $grid_box_SW[0]; $i <= $grid_box_NE[0]; $i++) {
169
-                    for ($j = $grid_box_SW[1]; $j <= $grid_box_NE[1]; $j++) {
170
-                        $grid[$i][$j][] = $item[0];
171
-                    }
172
-                }
173
-            }
174
-            return [
175
-                'grid'          =>  $grid,
176
-                'nb_columns'    =>  $grid_columns,
177
-                'map_NE'        =>  $map_NE,
178
-                'map_SW'        =>  $map_SW,
179
-                'SRID'          =>  $srid
180
-            ];
181
-        });
182
-    }
183
-
184
-    /**
185
-     * Set the Brick Geo Engine to use the database for geospatial computations.
186
-     * The engine is set only if it has not been set beforehand.
187
-     *
188
-     * @return bool
189
-     */
190
-    protected function setGeometryEngine(): bool
191
-    {
192
-        try {
193
-            if (!GeometryEngineRegistry::has()) {
194
-                GeometryEngineRegistry::set(new PDOEngine(DB::connection()->getPdo()));
195
-            }
196
-            $point = Point::xy(1, 1);
197
-            return $point->equals($point);
198
-        } catch (Throwable $ex) {
199
-        }
200
-        return false;
201
-    }
202
-
203
-    /**
204
-     * Get the key to cache the indexed grid of features.
205
-     *
206
-     * @return string|NULL
207
-     */
208
-    protected function cacheKey(): ?string
209
-    {
210
-        if ($this->cache_key === null) {
211
-            $map_def = $this->data('map');
212
-            if ($map_def === null || !($map_def instanceof MapDefinitionInterface)) {
213
-                return null;
214
-            }
215
-            return spl_object_id($this) . '-map-' . $map_def->id();
216
-        }
217
-        return $this->cache_key;
218
-    }
39
+	use PlaceMapperTrait;
40
+
41
+	private ?string $cache_key = null;
42
+
43
+	/**
44
+	 * {@inheritDoc}
45
+	 * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::title()
46
+	 */
47
+	public function title(): string
48
+	{
49
+		return I18N::translate('Mapping on place coordinates');
50
+	}
51
+
52
+	/**
53
+	 * {@inheritDoc}
54
+	 *
55
+	 * {@internal The Place is associated to a Point only.
56
+	 * PlaceLocation can calculate a BoundingBox.
57
+	 * Using a BoundingBox could make the mapping more complex and potentially arbitary.
58
+	 * Furthermore, when no coordinate is found for the place or its children, then it bubbles up to the parents.
59
+	 * This could create the unwanted side effect of a very large area to consider}
60
+	 *
61
+	 * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::map()
62
+	 */
63
+	public function map(Place $place, string $feature_property): ?string
64
+	{
65
+		$location = new PlaceLocation($place->gedcomName());
66
+		$longitude = $location->longitude();
67
+		$latitude = $location->latitude();
68
+		if ($longitude === null || $latitude === null) {
69
+			return null;
70
+		}
71
+
72
+		$features_index = $this->featuresIndex();
73
+		if ($features_index === null) {
74
+			return null;
75
+		}
76
+
77
+		$place_point = Point::xy($longitude, $latitude, $features_index['SRID']);
78
+		$grid_box = $this->getGridCell(
79
+			$place_point,
80
+			$features_index['map_NE'],
81
+			$features_index['map_SW'],
82
+			$features_index['nb_columns']
83
+		);
84
+		if ($grid_box === null || !$this->setGeometryEngine()) {
85
+			return null;
86
+		}
87
+		$features = $features_index['grid'][$grid_box[0]][$grid_box[1]];
88
+		foreach ($features as $feature) {
89
+			$geometry = $feature->getGeometry();
90
+			if ($geometry !== null && $place_point->SRID() === $geometry->SRID() && $geometry->contains($place_point)) {
91
+				return $feature->getProperty($feature_property);
92
+			}
93
+		}
94
+		return null;
95
+	}
96
+
97
+	/**
98
+	 * Return the XY coordinates in a bounded grid of the cell containing a specific point.
99
+	 *
100
+	 * @param Point $point Point to find
101
+	 * @param Point $grid_NE North-East point of the bounded grid
102
+	 * @param Point $grid_SW South-West point fo the bounded grid
103
+	 * @param int $grid_columns Number of columns/rows in the grid
104
+	 * @return int[]|NULL
105
+	 */
106
+	protected function getGridCell(Point $point, Point $grid_NE, Point $grid_SW, int $grid_columns): ?array
107
+	{
108
+		list($x, $y) = $point->toArray();
109
+		list($x_max, $y_max) = $grid_NE->toArray();
110
+		list($x_min, $y_min) = $grid_SW->toArray();
111
+
112
+		$x_step = ($x_max - $x_min) / $grid_columns;
113
+		$y_step = ($y_max - $y_min) / $grid_columns;
114
+
115
+		if ($x_min <= $x && $x <= $x_max && $y_min <= $y && $y <= $y_max) {
116
+			return [
117
+				$x === $x_max ? $grid_columns - 1 : intval(($x - $x_min) / $x_step),
118
+				$y === $y_max ? $grid_columns - 1 : intval(($y - $y_min) / $y_step)
119
+			];
120
+		}
121
+		return null;
122
+	}
123
+
124
+	/**
125
+	 * Get an indexed array of the features of the map.
126
+	 *
127
+	 * {@internal The map is divided in a grid, eacg cell containing the features which bounding box overlaps that cell.
128
+	 * The grid is computed once for each map, and cached.}
129
+	 *
130
+	 * @phpcs:ignore Generic.Files.LineLength.TooLong
131
+	 * @return array{grid: array<int, array<int, \Brick\Geo\IO\GeoJSON\Feature[]>>, nb_columns: int, map_NE: \Brick\Geo\Point, map_SW: \Brick\Geo\Point, SRID: int}|NULL
132
+	 */
133
+	protected function featuresIndex(): ?array
134
+	{
135
+		$cacheKey = $this->cacheKey();
136
+		if ($cacheKey === null) {
137
+			return null;
138
+		}
139
+		return Registry::cache()->array()->remember($cacheKey, function (): ?array {
140
+			$map_def = $this->data('map');
141
+			if (
142
+				!$this->setGeometryEngine()
143
+				|| $map_def === null
144
+				|| !($map_def instanceof MapDefinitionInterface)
145
+			) {
146
+				return null;
147
+			}
148
+			$bounding_boxes = [];
149
+			$map_bounding_box = new BoundingBox();
150
+			$srid = 0;
151
+			foreach ($map_def->features() as $feature) {
152
+				$geometry = $feature->getGeometry();
153
+				if ($geometry === null) {
154
+					continue;
155
+				}
156
+				$srid = $geometry->SRID();
157
+				$bounding_box = $geometry->getBoundingBox();
158
+				$bounding_boxes[] = [$feature, $bounding_box];
159
+				$map_bounding_box = $map_bounding_box->extendedWithBoundingBox($bounding_box);
160
+			}
161
+			$grid_columns = count($bounding_boxes);
162
+			$grid = array_fill(0, $grid_columns, array_fill(0, $grid_columns, []));
163
+			$map_NE = $map_bounding_box->getNorthEast();
164
+			$map_SW = $map_bounding_box->getSouthWest();
165
+			foreach ($bounding_boxes as $item) {
166
+				$grid_box_SW = $this->getGridCell($item[1]->getSouthWest(), $map_NE, $map_SW, $grid_columns) ?? [1, 1];
167
+				$grid_box_NE = $this->getGridCell($item[1]->getNorthEast(), $map_NE, $map_SW, $grid_columns) ?? [0, 0];
168
+				for ($i = $grid_box_SW[0]; $i <= $grid_box_NE[0]; $i++) {
169
+					for ($j = $grid_box_SW[1]; $j <= $grid_box_NE[1]; $j++) {
170
+						$grid[$i][$j][] = $item[0];
171
+					}
172
+				}
173
+			}
174
+			return [
175
+				'grid'          =>  $grid,
176
+				'nb_columns'    =>  $grid_columns,
177
+				'map_NE'        =>  $map_NE,
178
+				'map_SW'        =>  $map_SW,
179
+				'SRID'          =>  $srid
180
+			];
181
+		});
182
+	}
183
+
184
+	/**
185
+	 * Set the Brick Geo Engine to use the database for geospatial computations.
186
+	 * The engine is set only if it has not been set beforehand.
187
+	 *
188
+	 * @return bool
189
+	 */
190
+	protected function setGeometryEngine(): bool
191
+	{
192
+		try {
193
+			if (!GeometryEngineRegistry::has()) {
194
+				GeometryEngineRegistry::set(new PDOEngine(DB::connection()->getPdo()));
195
+			}
196
+			$point = Point::xy(1, 1);
197
+			return $point->equals($point);
198
+		} catch (Throwable $ex) {
199
+		}
200
+		return false;
201
+	}
202
+
203
+	/**
204
+	 * Get the key to cache the indexed grid of features.
205
+	 *
206
+	 * @return string|NULL
207
+	 */
208
+	protected function cacheKey(): ?string
209
+	{
210
+		if ($this->cache_key === null) {
211
+			$map_def = $this->data('map');
212
+			if ($map_def === null || !($map_def instanceof MapDefinitionInterface)) {
213
+				return null;
214
+			}
215
+			return spl_object_id($this) . '-map-' . $map_def->id();
216
+		}
217
+		return $this->cache_key;
218
+	}
219 219
 }
Please login to merge, or discard this patch.
app/Module/GeoDispersion/Views/AbstractGeoAnalysisView.php 1 patch
Indentation   +220 added lines, -220 removed lines patch added patch discarded remove patch
@@ -26,224 +26,224 @@
 block discarded – undo
26 26
  */
27 27
 abstract class AbstractGeoAnalysisView
28 28
 {
29
-    private int $id;
30
-    private Tree $tree;
31
-    private bool $enabled;
32
-    private string $description;
33
-    private GeoAnalysisInterface $geoanalysis;
34
-    private int $depth;
35
-    private int $detailed_top_places;
36
-    private bool $use_flags;
37
-
38
-    /**
39
-     * Constructor for AbstractGeoAnalysisView
40
-     *
41
-     * @param int $id
42
-     * @param Tree $tree
43
-     * @param bool $enabled
44
-     * @param string $description
45
-     * @param GeoAnalysisInterface $geoanalysis
46
-     * @param int $depth
47
-     * @param int $detailed_top_places
48
-     * @param bool $use_flags
49
-     */
50
-    final public function __construct(
51
-        int $id,
52
-        Tree $tree,
53
-        bool $enabled,
54
-        string $description,
55
-        GeoAnalysisInterface $geoanalysis,
56
-        int $depth,
57
-        int $detailed_top_places = 0,
58
-        bool $use_flags = false
59
-    ) {
60
-        $this->id = $id;
61
-        $this->tree = $tree;
62
-        $this->enabled = $enabled;
63
-        $this->description = $description;
64
-        $this->geoanalysis = $geoanalysis;
65
-        $this->depth = $depth;
66
-        $this->detailed_top_places = $detailed_top_places;
67
-        $this->use_flags = $use_flags;
68
-    }
69
-
70
-    /**
71
-     * Create a copy of the view with a new ID.
72
-     *
73
-     * @param int $id
74
-     * @return static
75
-     */
76
-    public function withId(int $id): self
77
-    {
78
-        $new = clone $this;
79
-        $new->id = $id;
80
-        return $new;
81
-    }
82
-
83
-    /**
84
-     * Create a copy of the view with new properties.
85
-     *
86
-     * @param bool $enabled
87
-     * @param string $description
88
-     * @param GeoAnalysisInterface $geoanalysis
89
-     * @param int $depth
90
-     * @param int $detailed_top_places
91
-     * @param bool $use_flags
92
-     * @return static
93
-     */
94
-    public function with(
95
-        bool $enabled,
96
-        string $description,
97
-        GeoAnalysisInterface $geoanalysis,
98
-        int $depth,
99
-        int $detailed_top_places = 0,
100
-        bool $use_flags = false
101
-    ): self {
102
-        $new = clone $this;
103
-        $new->enabled = $enabled;
104
-        $new->description = $description;
105
-        $new->geoanalysis = $geoanalysis;
106
-        $new->depth = $depth;
107
-        $new->detailed_top_places = $detailed_top_places;
108
-        $new->use_flags = $use_flags;
109
-        return $new;
110
-    }
111
-
112
-    /**
113
-     * Get the view ID
114
-     *
115
-     * @return int
116
-     */
117
-    public function id(): int
118
-    {
119
-        return $this->id;
120
-    }
121
-
122
-    /**
123
-     * Get the view type for display
124
-     *
125
-     * @return string
126
-     */
127
-    abstract public function type(): string;
128
-
129
-    /**
130
-     * Get the icon for the view type
131
-     *
132
-     * @param ModuleInterface $module
133
-     * @return string
134
-     */
135
-    abstract public function icon(ModuleInterface $module): string;
136
-
137
-    /**
138
-     * Return the content of the global settings section of the config page
139
-     *
140
-     * @param ModuleInterface $module
141
-     * @return string
142
-     */
143
-    abstract public function globalSettingsContent(ModuleInterface $module): string;
144
-
145
-    /**
146
-     * Return a view with global settings updated according to the view rules
147
-     *
148
-     * @param ServerRequestInterface $request
149
-     * @return static
150
-     */
151
-    abstract public function withGlobalSettingsUpdate(ServerRequestInterface $request): self;
152
-
153
-    /**
154
-     * Returns the content of the view global tab
155
-     *
156
-     * @param ModuleInterface $module
157
-     * @param GeoAnalysisResult $result
158
-     * @param array<string, mixed> $params
159
-     * @return string
160
-     */
161
-    abstract public function globalTabContent(
162
-        ModuleInterface $module,
163
-        GeoAnalysisResult $result,
164
-        array $params
165
-    ): string;
166
-
167
-    /**
168
-     * Returns the content of the view detailed tab
169
-     *
170
-     * @param ModuleInterface $module
171
-     * @param Collection<string, GeoAnalysisResult> $results
172
-     * @param array<string, mixed> $params
173
-     * @return string
174
-     */
175
-    public function detailedTabContent(ModuleInterface $module, Collection $results, array $params): string
176
-    {
177
-        return view($module->name() . '::geoanalysisview-tab-detailed', $params + [ 'results'   =>  $results ]);
178
-    }
179
-
180
-    /**
181
-     * Get the tree to which the view belongs
182
-     *
183
-     * @return Tree
184
-     */
185
-    public function tree(): Tree
186
-    {
187
-        return $this->tree;
188
-    }
189
-
190
-    /**
191
-     * Get the description of the view
192
-     *
193
-     * @return string
194
-     */
195
-    public function description(): string
196
-    {
197
-        return $this->description;
198
-    }
199
-
200
-    /**
201
-     * Get whether the view is enabled
202
-     *
203
-     * @return bool
204
-     */
205
-    public function isEnabled(): bool
206
-    {
207
-        return $this->enabled;
208
-    }
209
-
210
-    /**
211
-     * Get the geographical dispersion analysis for the view
212
-     *
213
-     * @return GeoAnalysisInterface
214
-     */
215
-    public function analysis(): GeoAnalysisInterface
216
-    {
217
-        return $this->geoanalysis;
218
-    }
219
-
220
-    /**
221
-     * Get the place hierarchy depth for the view
222
-     *
223
-     * @return int
224
-     */
225
-    public function placesDepth(): int
226
-    {
227
-        return $this->depth;
228
-    }
229
-
230
-    /**
231
-     * Get the number of places to display in the detailed tab
232
-     *
233
-     * @return int
234
-     */
235
-    public function numberTopPlaces(): int
236
-    {
237
-        return $this->detailed_top_places;
238
-    }
239
-
240
-    /**
241
-     * Get whether flags should be used in the detailed tab
242
-     *
243
-     * @return bool
244
-     */
245
-    public function useFlags(): bool
246
-    {
247
-        return $this->use_flags;
248
-    }
29
+	private int $id;
30
+	private Tree $tree;
31
+	private bool $enabled;
32
+	private string $description;
33
+	private GeoAnalysisInterface $geoanalysis;
34
+	private int $depth;
35
+	private int $detailed_top_places;
36
+	private bool $use_flags;
37
+
38
+	/**
39
+	 * Constructor for AbstractGeoAnalysisView
40
+	 *
41
+	 * @param int $id
42
+	 * @param Tree $tree
43
+	 * @param bool $enabled
44
+	 * @param string $description
45
+	 * @param GeoAnalysisInterface $geoanalysis
46
+	 * @param int $depth
47
+	 * @param int $detailed_top_places
48
+	 * @param bool $use_flags
49
+	 */
50
+	final public function __construct(
51
+		int $id,
52
+		Tree $tree,
53
+		bool $enabled,
54
+		string $description,
55
+		GeoAnalysisInterface $geoanalysis,
56
+		int $depth,
57
+		int $detailed_top_places = 0,
58
+		bool $use_flags = false
59
+	) {
60
+		$this->id = $id;
61
+		$this->tree = $tree;
62
+		$this->enabled = $enabled;
63
+		$this->description = $description;
64
+		$this->geoanalysis = $geoanalysis;
65
+		$this->depth = $depth;
66
+		$this->detailed_top_places = $detailed_top_places;
67
+		$this->use_flags = $use_flags;
68
+	}
69
+
70
+	/**
71
+	 * Create a copy of the view with a new ID.
72
+	 *
73
+	 * @param int $id
74
+	 * @return static
75
+	 */
76
+	public function withId(int $id): self
77
+	{
78
+		$new = clone $this;
79
+		$new->id = $id;
80
+		return $new;
81
+	}
82
+
83
+	/**
84
+	 * Create a copy of the view with new properties.
85
+	 *
86
+	 * @param bool $enabled
87
+	 * @param string $description
88
+	 * @param GeoAnalysisInterface $geoanalysis
89
+	 * @param int $depth
90
+	 * @param int $detailed_top_places
91
+	 * @param bool $use_flags
92
+	 * @return static
93
+	 */
94
+	public function with(
95
+		bool $enabled,
96
+		string $description,
97
+		GeoAnalysisInterface $geoanalysis,
98
+		int $depth,
99
+		int $detailed_top_places = 0,
100
+		bool $use_flags = false
101
+	): self {
102
+		$new = clone $this;
103
+		$new->enabled = $enabled;
104
+		$new->description = $description;
105
+		$new->geoanalysis = $geoanalysis;
106
+		$new->depth = $depth;
107
+		$new->detailed_top_places = $detailed_top_places;
108
+		$new->use_flags = $use_flags;
109
+		return $new;
110
+	}
111
+
112
+	/**
113
+	 * Get the view ID
114
+	 *
115
+	 * @return int
116
+	 */
117
+	public function id(): int
118
+	{
119
+		return $this->id;
120
+	}
121
+
122
+	/**
123
+	 * Get the view type for display
124
+	 *
125
+	 * @return string
126
+	 */
127
+	abstract public function type(): string;
128
+
129
+	/**
130
+	 * Get the icon for the view type
131
+	 *
132
+	 * @param ModuleInterface $module
133
+	 * @return string
134
+	 */
135
+	abstract public function icon(ModuleInterface $module): string;
136
+
137
+	/**
138
+	 * Return the content of the global settings section of the config page
139
+	 *
140
+	 * @param ModuleInterface $module
141
+	 * @return string
142
+	 */
143
+	abstract public function globalSettingsContent(ModuleInterface $module): string;
144
+
145
+	/**
146
+	 * Return a view with global settings updated according to the view rules
147
+	 *
148
+	 * @param ServerRequestInterface $request
149
+	 * @return static
150
+	 */
151
+	abstract public function withGlobalSettingsUpdate(ServerRequestInterface $request): self;
152
+
153
+	/**
154
+	 * Returns the content of the view global tab
155
+	 *
156
+	 * @param ModuleInterface $module
157
+	 * @param GeoAnalysisResult $result
158
+	 * @param array<string, mixed> $params
159
+	 * @return string
160
+	 */
161
+	abstract public function globalTabContent(
162
+		ModuleInterface $module,
163
+		GeoAnalysisResult $result,
164
+		array $params
165
+	): string;
166
+
167
+	/**
168
+	 * Returns the content of the view detailed tab
169
+	 *
170
+	 * @param ModuleInterface $module
171
+	 * @param Collection<string, GeoAnalysisResult> $results
172
+	 * @param array<string, mixed> $params
173
+	 * @return string
174
+	 */
175
+	public function detailedTabContent(ModuleInterface $module, Collection $results, array $params): string
176
+	{
177
+		return view($module->name() . '::geoanalysisview-tab-detailed', $params + [ 'results'   =>  $results ]);
178
+	}
179
+
180
+	/**
181
+	 * Get the tree to which the view belongs
182
+	 *
183
+	 * @return Tree
184
+	 */
185
+	public function tree(): Tree
186
+	{
187
+		return $this->tree;
188
+	}
189
+
190
+	/**
191
+	 * Get the description of the view
192
+	 *
193
+	 * @return string
194
+	 */
195
+	public function description(): string
196
+	{
197
+		return $this->description;
198
+	}
199
+
200
+	/**
201
+	 * Get whether the view is enabled
202
+	 *
203
+	 * @return bool
204
+	 */
205
+	public function isEnabled(): bool
206
+	{
207
+		return $this->enabled;
208
+	}
209
+
210
+	/**
211
+	 * Get the geographical dispersion analysis for the view
212
+	 *
213
+	 * @return GeoAnalysisInterface
214
+	 */
215
+	public function analysis(): GeoAnalysisInterface
216
+	{
217
+		return $this->geoanalysis;
218
+	}
219
+
220
+	/**
221
+	 * Get the place hierarchy depth for the view
222
+	 *
223
+	 * @return int
224
+	 */
225
+	public function placesDepth(): int
226
+	{
227
+		return $this->depth;
228
+	}
229
+
230
+	/**
231
+	 * Get the number of places to display in the detailed tab
232
+	 *
233
+	 * @return int
234
+	 */
235
+	public function numberTopPlaces(): int
236
+	{
237
+		return $this->detailed_top_places;
238
+	}
239
+
240
+	/**
241
+	 * Get whether flags should be used in the detailed tab
242
+	 *
243
+	 * @return bool
244
+	 */
245
+	public function useFlags(): bool
246
+	{
247
+		return $this->use_flags;
248
+	}
249 249
 }
Please login to merge, or discard this patch.
app/Module/GeoDispersion/Model/GeoAnalysisMapAdapter.php 1 patch
Indentation   +185 added lines, -185 removed lines patch added patch discarded remove patch
@@ -29,189 +29,189 @@
 block discarded – undo
29 29
  */
30 30
 class GeoAnalysisMapAdapter
31 31
 {
32
-    private int $id;
33
-    private int $view_id;
34
-    private MapDefinitionInterface $map;
35
-    private PlaceMapperInterface $place_mapper;
36
-    private MapViewConfigInterface $config;
37
-
38
-    /**
39
-     * Constructor for GeoAnalysisMapAdapter
40
-     *
41
-     * @param int $id
42
-     * @param MapDefinitionInterface $map
43
-     * @param PlaceMapperInterface $mapper
44
-     * @param MapViewConfigInterface $config
45
-     */
46
-    public function __construct(
47
-        int $id,
48
-        int $view_id,
49
-        MapDefinitionInterface $map,
50
-        PlaceMapperInterface $mapper,
51
-        MapViewConfigInterface $config
52
-    ) {
53
-        $this->id = $id;
54
-        $this->view_id = $view_id;
55
-        $this->map = $map;
56
-        $this->place_mapper = $mapper;
57
-        $this->config = $config;
58
-        $this->place_mapper->setConfig($this->config->mapperConfig());
59
-        $this->place_mapper->setData('map', $map);
60
-        $this->place_mapper->boot();
61
-    }
62
-
63
-    /**
64
-     * Create a copy of the GeoAnalysisMapAdapter with new properties.
65
-     *
66
-     * @param MapDefinitionInterface $map
67
-     * @param PlaceMapperInterface $mapper
68
-     * @param string $mapping_property
69
-     * @return static
70
-     */
71
-    public function with(
72
-        MapDefinitionInterface $map,
73
-        PlaceMapperInterface $mapper,
74
-        string $mapping_property
75
-    ): self {
76
-        $new = clone $this;
77
-        $new->map = $map;
78
-        $new->place_mapper = $mapper;
79
-        $new->config = $this->config->with($mapping_property, $mapper->config());
80
-        return $new;
81
-    }
82
-
83
-    /**
84
-     * Get the GeoAnalysisMapAdapter ID
85
-     *
86
-     * @return int
87
-     */
88
-    public function id(): int
89
-    {
90
-        return $this->id;
91
-    }
92
-
93
-    /**
94
-     * Get the ID of the associated GeoAnalysisView
95
-     *
96
-     * @return int
97
-     */
98
-    public function geoAnalysisViewId(): int
99
-    {
100
-        return $this->view_id;
101
-    }
102
-
103
-    /**
104
-     * Get the associated target map
105
-     *
106
-     * @return MapDefinitionInterface
107
-     */
108
-    public function map(): MapDefinitionInterface
109
-    {
110
-        return $this->map;
111
-    }
112
-
113
-    /**
114
-     * Get the Place Mapper used for the mapping
115
-     *
116
-     * @return PlaceMapperInterface
117
-     */
118
-    public function placeMapper(): PlaceMapperInterface
119
-    {
120
-        return $this->place_mapper;
121
-    }
122
-
123
-    /**
124
-     * Get the configuration of the Map View.
125
-     *
126
-     * @return MapViewConfigInterface
127
-     */
128
-    public function viewConfig(): MapViewConfigInterface
129
-    {
130
-        return $this->config;
131
-    }
132
-
133
-    /**
134
-     * Convert the geographical analysis result to a MapAdapter result for usage in the Map View
135
-     *
136
-     * @param GeoAnalysisResult $result
137
-     * @return MapAdapterResult
138
-     */
139
-    public function convert(GeoAnalysisResult $result): MapAdapterResult
140
-    {
141
-        $result = $result->copy();
142
-
143
-        $features = [];
144
-        list($features_data, $result) = $this->featureAnalysisData($result);
145
-
146
-        $places_found = $result->countFound();
147
-        foreach ($this->map->features() as $feature) {
148
-            $feature_id = $this->featureId($feature);
149
-            if ($feature_id !== null && $features_data->has($feature_id)) {
150
-                /** @var MapFeatureAnalysisData $feature_data */
151
-                $feature_data = $features_data->get($feature_id)->tagAsExisting();
152
-                $place_count = $feature_data->count();
153
-                $features[] = $feature
154
-                    ->withProperty('count', $place_count)
155
-                    ->withProperty('ratio', $places_found > 0 ? $place_count / $places_found : 0)
156
-                    ->withProperty(
157
-                        'places',
158
-                        $feature_data->places()
159
-                            ->map(fn(GeoAnalysisPlace $place): string => $place->place()->firstParts(1)->first())
160
-                            ->sort(I18N::comparator())
161
-                            ->toArray()
162
-                    );
163
-            } else {
164
-                $features[] = $feature;
165
-            }
166
-        }
167
-
168
-        $features_data
169
-            ->filter(fn(MapFeatureAnalysisData $data) => !$data->existsInMap())
170
-            ->each(
171
-                fn (MapFeatureAnalysisData $data) =>
172
-                    $data->places()->each(
173
-                        fn(GeoAnalysisPlace $place) => $result->exclude($place)
174
-                    )
175
-            );
176
-
177
-        return new MapAdapterResult($result, $features);
178
-    }
179
-
180
-    /**
181
-     * Populate the map features with the mapped Places and total count
182
-     *
183
-     * @param GeoAnalysisResult $result
184
-     * @return mixed[]
185
-     */
186
-    protected function featureAnalysisData(GeoAnalysisResult $result): array
187
-    {
188
-        $features_mapping = new Collection();
189
-
190
-        $byplaces = $result->knownPlaces();
191
-        $byplaces->each(function (GeoAnalysisResultItem $item) use ($features_mapping, $result): void {
192
-            $id = $this->place_mapper->map($item->place()->place(), $this->config->mapMappingProperty());
193
-
194
-            if ($id !== null && mb_strlen($id) > 0) {
195
-                $features_mapping->put(
196
-                    $id,
197
-                    $features_mapping->get($id, new MapFeatureAnalysisData($id))->add($item->place(), $item->count())
198
-                );
199
-            } else {
200
-                $result->exclude($item->place());
201
-            }
202
-        });
203
-
204
-        return [ $features_mapping, $result];
205
-    }
206
-
207
-    /**
208
-     * Get the value of the feature property used for the mapping
209
-     *
210
-     * @param Feature $feature
211
-     * @return string|NULL
212
-     */
213
-    protected function featureId(Feature $feature): ?string
214
-    {
215
-        return $feature->getProperty($this->config->mapMappingProperty());
216
-    }
32
+	private int $id;
33
+	private int $view_id;
34
+	private MapDefinitionInterface $map;
35
+	private PlaceMapperInterface $place_mapper;
36
+	private MapViewConfigInterface $config;
37
+
38
+	/**
39
+	 * Constructor for GeoAnalysisMapAdapter
40
+	 *
41
+	 * @param int $id
42
+	 * @param MapDefinitionInterface $map
43
+	 * @param PlaceMapperInterface $mapper
44
+	 * @param MapViewConfigInterface $config
45
+	 */
46
+	public function __construct(
47
+		int $id,
48
+		int $view_id,
49
+		MapDefinitionInterface $map,
50
+		PlaceMapperInterface $mapper,
51
+		MapViewConfigInterface $config
52
+	) {
53
+		$this->id = $id;
54
+		$this->view_id = $view_id;
55
+		$this->map = $map;
56
+		$this->place_mapper = $mapper;
57
+		$this->config = $config;
58
+		$this->place_mapper->setConfig($this->config->mapperConfig());
59
+		$this->place_mapper->setData('map', $map);
60
+		$this->place_mapper->boot();
61
+	}
62
+
63
+	/**
64
+	 * Create a copy of the GeoAnalysisMapAdapter with new properties.
65
+	 *
66
+	 * @param MapDefinitionInterface $map
67
+	 * @param PlaceMapperInterface $mapper
68
+	 * @param string $mapping_property
69
+	 * @return static
70
+	 */
71
+	public function with(
72
+		MapDefinitionInterface $map,
73
+		PlaceMapperInterface $mapper,
74
+		string $mapping_property
75
+	): self {
76
+		$new = clone $this;
77
+		$new->map = $map;
78
+		$new->place_mapper = $mapper;
79
+		$new->config = $this->config->with($mapping_property, $mapper->config());
80
+		return $new;
81
+	}
82
+
83
+	/**
84
+	 * Get the GeoAnalysisMapAdapter ID
85
+	 *
86
+	 * @return int
87
+	 */
88
+	public function id(): int
89
+	{
90
+		return $this->id;
91
+	}
92
+
93
+	/**
94
+	 * Get the ID of the associated GeoAnalysisView
95
+	 *
96
+	 * @return int
97
+	 */
98
+	public function geoAnalysisViewId(): int
99
+	{
100
+		return $this->view_id;
101
+	}
102
+
103
+	/**
104
+	 * Get the associated target map
105
+	 *
106
+	 * @return MapDefinitionInterface
107
+	 */
108
+	public function map(): MapDefinitionInterface
109
+	{
110
+		return $this->map;
111
+	}
112
+
113
+	/**
114
+	 * Get the Place Mapper used for the mapping
115
+	 *
116
+	 * @return PlaceMapperInterface
117
+	 */
118
+	public function placeMapper(): PlaceMapperInterface
119
+	{
120
+		return $this->place_mapper;
121
+	}
122
+
123
+	/**
124
+	 * Get the configuration of the Map View.
125
+	 *
126
+	 * @return MapViewConfigInterface
127
+	 */
128
+	public function viewConfig(): MapViewConfigInterface
129
+	{
130
+		return $this->config;
131
+	}
132
+
133
+	/**
134
+	 * Convert the geographical analysis result to a MapAdapter result for usage in the Map View
135
+	 *
136
+	 * @param GeoAnalysisResult $result
137
+	 * @return MapAdapterResult
138
+	 */
139
+	public function convert(GeoAnalysisResult $result): MapAdapterResult
140
+	{
141
+		$result = $result->copy();
142
+
143
+		$features = [];
144
+		list($features_data, $result) = $this->featureAnalysisData($result);
145
+
146
+		$places_found = $result->countFound();
147
+		foreach ($this->map->features() as $feature) {
148
+			$feature_id = $this->featureId($feature);
149
+			if ($feature_id !== null && $features_data->has($feature_id)) {
150
+				/** @var MapFeatureAnalysisData $feature_data */
151
+				$feature_data = $features_data->get($feature_id)->tagAsExisting();
152
+				$place_count = $feature_data->count();
153
+				$features[] = $feature
154
+					->withProperty('count', $place_count)
155
+					->withProperty('ratio', $places_found > 0 ? $place_count / $places_found : 0)
156
+					->withProperty(
157
+						'places',
158
+						$feature_data->places()
159
+							->map(fn(GeoAnalysisPlace $place): string => $place->place()->firstParts(1)->first())
160
+							->sort(I18N::comparator())
161
+							->toArray()
162
+					);
163
+			} else {
164
+				$features[] = $feature;
165
+			}
166
+		}
167
+
168
+		$features_data
169
+			->filter(fn(MapFeatureAnalysisData $data) => !$data->existsInMap())
170
+			->each(
171
+				fn (MapFeatureAnalysisData $data) =>
172
+					$data->places()->each(
173
+						fn(GeoAnalysisPlace $place) => $result->exclude($place)
174
+					)
175
+			);
176
+
177
+		return new MapAdapterResult($result, $features);
178
+	}
179
+
180
+	/**
181
+	 * Populate the map features with the mapped Places and total count
182
+	 *
183
+	 * @param GeoAnalysisResult $result
184
+	 * @return mixed[]
185
+	 */
186
+	protected function featureAnalysisData(GeoAnalysisResult $result): array
187
+	{
188
+		$features_mapping = new Collection();
189
+
190
+		$byplaces = $result->knownPlaces();
191
+		$byplaces->each(function (GeoAnalysisResultItem $item) use ($features_mapping, $result): void {
192
+			$id = $this->place_mapper->map($item->place()->place(), $this->config->mapMappingProperty());
193
+
194
+			if ($id !== null && mb_strlen($id) > 0) {
195
+				$features_mapping->put(
196
+					$id,
197
+					$features_mapping->get($id, new MapFeatureAnalysisData($id))->add($item->place(), $item->count())
198
+				);
199
+			} else {
200
+				$result->exclude($item->place());
201
+			}
202
+		});
203
+
204
+		return [ $features_mapping, $result];
205
+	}
206
+
207
+	/**
208
+	 * Get the value of the feature property used for the mapping
209
+	 *
210
+	 * @param Feature $feature
211
+	 * @return string|NULL
212
+	 */
213
+	protected function featureId(Feature $feature): ?string
214
+	{
215
+		return $feature->getProperty($this->config->mapMappingProperty());
216
+	}
217 217
 }
Please login to merge, or discard this patch.