-
-
Notifications
You must be signed in to change notification settings - Fork 514
Expand file tree
/
Copy pathAbstractReplace.php
More file actions
54 lines (44 loc) · 1.67 KB
/
AbstractReplace.php
File metadata and controls
54 lines (44 loc) · 1.67 KB
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
<?php
declare(strict_types=1);
namespace Doctrine\ODM\MongoDB\Aggregation\Stage;
use Doctrine\ODM\MongoDB\Aggregation\Builder;
use Doctrine\ODM\MongoDB\Aggregation\Expr;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Persisters\DocumentPersister;
use function array_map;
use function is_array;
use function is_string;
use function substr;
abstract class AbstractReplace extends Operator
{
/** @param string|mixed[]|Expr|null $expression */
final public function __construct(Builder $builder, protected DocumentManager $dm, protected ClassMetadata $class, protected $expression = null)
{
Operator::__construct($builder);
}
private function getDocumentPersister(): DocumentPersister
{
return $this->dm->getUnitOfWork()->getDocumentPersister($this->class->name);
}
/** @return array<string, mixed>|string */
protected function getReplaceExpression()
{
return $this->expression !== null ? $this->convertExpression($this->expression) : $this->expr->getExpression();
}
/**
* @param mixed[]|string|mixed $expression
*
* @return mixed[]|string|mixed
*/
private function convertExpression($expression)
{
if (is_array($expression)) {
return array_map($this->convertExpression(...), $expression);
}
if (is_string($expression) && substr($expression, 0, 1) === '$') {
return '$' . $this->getDocumentPersister()->prepareFieldName(substr($expression, 1));
}
return $this->dm->getConfiguration()->getTypeRegistry()->convertToDatabaseValue(Expr::convertExpression($expression));
}
}