Skip to content

Commit ba0fdd2

Browse files
committed
refactor: Align Marshaller with CakePHP ORM patterns
- Delegate entity/nested creation to Index classes - Add Model.afterMarshal event and fieldValue() helper - Add Embedded::getIndex/setIndex for marshaller delegation - Improve dirty flag handling for clean embedded entities
1 parent ec7006b commit ba0fdd2

5 files changed

Lines changed: 488 additions & 101 deletions

File tree

phpstan.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ parameters:
77
ignoreErrors:
88
- identifier: missingType.iterableValue
99
- identifier: missingType.generics
10+
-
11+
identifier: instanceof.alwaysTrue
12+
path: src/Marshaller.php
1013
-
1114
message: '#^Trait Cake\\ElasticSearch\\Datasource\\IndexLocatorAwareTrait is used zero times and is not analysed\.$#'
1215
identifier: trait.unused

src/Association/Embedded.php

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
namespace Cake\ElasticSearch\Association;
55

66
use Cake\Core\App;
7+
use Cake\Datasource\FactoryLocator;
78
use Cake\ElasticSearch\Document;
89
use Cake\ElasticSearch\Exception\MissingDocumentException;
910
use Cake\ElasticSearch\Index;
1011
use Cake\Utility\Inflector;
12+
use InvalidArgumentException;
1113

1214
/**
1315
* Represents an embedded document.
@@ -36,7 +38,7 @@ abstract class Embedded
3638
protected string $alias;
3739

3840
/**
39-
* The class to use for the embeded document.
41+
* The class to use for the embedded document.
4042
*/
4143
protected string $entityClass;
4244

@@ -50,6 +52,11 @@ abstract class Embedded
5052
*/
5153
protected string $indexClass;
5254

55+
/**
56+
* Index instance this embed is linked to
57+
*/
58+
protected ?Index $index = null;
59+
5360
/**
5461
* Constructor
5562
*
@@ -174,15 +181,30 @@ public function getIndexClass(): string
174181
/**
175182
* Set the index class used for this embed.
176183
*
177-
* @param \Cake\ElasticSearch\Index|string|null $name The class name to set.
184+
* @param \Cake\ElasticSearch\Index|string|null $className The class name to set.
178185
* @return $this
186+
* @throws \InvalidArgumentException In case the class name is set after the target index has been
187+
* resolved, and it doesn't match the target index's class name.
179188
*/
180-
public function setIndexClass(string|Index|null $name)
189+
public function setIndexClass(string|Index|null $className)
181190
{
182-
if ($name instanceof Index) {
183-
$this->indexClass = get_class($name);
184-
} elseif (is_string($name)) {
185-
$class = App::className($name, 'Model/Index');
191+
if ($className instanceof Index) {
192+
$this->index = $className;
193+
$this->indexClass = get_class($className);
194+
} elseif (is_string($className)) {
195+
$class = App::className($className, 'Model/Index');
196+
if (
197+
$class !== null &&
198+
$this->index instanceof Index &&
199+
get_class($this->index) !== $class
200+
) {
201+
throw new InvalidArgumentException(sprintf(
202+
"The class name `%s` doesn't match the target index class name of `%s`.",
203+
$className,
204+
$this->index::class,
205+
));
206+
}
207+
186208
if ($class !== null) {
187209
$this->indexClass = $class;
188210
}
@@ -191,6 +213,33 @@ public function setIndexClass(string|Index|null $name)
191213
return $this;
192214
}
193215

216+
/**
217+
* Sets the index instance for the target side of the association.
218+
*
219+
* @param \Cake\ElasticSearch\Index $index the instance to be assigned as target side
220+
* @return $this
221+
*/
222+
public function setIndex(Index $index)
223+
{
224+
$this->index = $index;
225+
226+
return $this;
227+
}
228+
229+
/**
230+
* Gets the index instance for the target side of the association.
231+
*/
232+
public function getIndex(): Index
233+
{
234+
if (!$this->index instanceof Index) {
235+
/** @var \Cake\ElasticSearch\Index $index */
236+
$index = FactoryLocator::get('Elastic')->get($this->getIndexClass());
237+
$this->index = $index;
238+
}
239+
240+
return $this->index;
241+
}
242+
194243
/**
195244
* Get the alias for this embed.
196245
*/

0 commit comments

Comments
 (0)