1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under `AGPL-3.0-only, proprietary` license[s]. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/manganel |
7
|
|
|
* @license AGPL-3.0-only, proprietary |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) Peter Maselkowski <[email protected]> |
10
|
|
|
* @link https://maslosoft.com/manganel/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Maslosoft\Manganel\Decorators; |
14
|
|
|
|
15
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
16
|
|
|
use Maslosoft\Mangan\Helpers\Sanitizer\Sanitizer; |
17
|
|
|
use Maslosoft\Mangan\Interfaces\Decorators\Model\ModelDecoratorInterface; |
18
|
|
|
use Maslosoft\Mangan\Interfaces\Transformators\TransformatorInterface; |
19
|
|
|
use Maslosoft\Manganel\Meta\ManganelMeta; |
20
|
|
|
use Maslosoft\Manganel\SearchArray; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* ElasticSearch 2.x does not allow `_id` fields, this is to prevent storing such fields. |
24
|
|
|
* |
25
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
26
|
|
|
*/ |
27
|
|
|
class UnderscoreIdFieldDecorator implements ModelDecoratorInterface |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Key used instead of `_id` |
32
|
|
|
*/ |
33
|
|
|
const Key = '_mongo_id_'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* This will be called when getting value. |
37
|
|
|
* This should return end user value. |
38
|
|
|
* @param AnnotatedInterface $model Document model which will be decorated |
39
|
|
|
* @param mixed $dbValues |
40
|
|
|
* @param string $transformatorClass Transformator class used |
41
|
|
|
* @return bool Return true if value should be assigned to model |
42
|
|
|
*/ |
43
|
24 |
|
public function read($model, &$dbValues, $transformatorClass = TransformatorInterface::class) |
44
|
|
|
{ |
45
|
24 |
|
if (isset($dbValues[self::Key]) && isset($model->_id)) |
46
|
|
|
{ |
47
|
24 |
|
$id = $dbValues[self::Key]; |
48
|
|
|
// Need to sanitize value, as $dbValues contains raw value |
49
|
24 |
|
$sanitizer = new Sanitizer($model, SearchArray::class, ManganelMeta::create($model)); |
50
|
24 |
|
$model->_id = $sanitizer->read('_id', $id); |
51
|
24 |
|
unset($dbValues[self::Key]); |
52
|
|
|
} |
53
|
24 |
|
return true; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* This will be called when setting value. |
58
|
|
|
* This should return db acceptable value |
59
|
|
|
* @param AnnotatedInterface $model Model which is about to be decorated |
60
|
|
|
* @param mixed[] $dbValues Whole model values from database. This is associative array with keys same as model properties (use $name param to access value). This is passed by reference. |
61
|
|
|
* @param string $transformatorClass Transformator class used |
62
|
|
|
* @return bool Return true to store value to database |
63
|
|
|
*/ |
64
|
53 |
|
public function write($model, &$dbValues, $transformatorClass = TransformatorInterface::class) |
65
|
|
|
{ |
66
|
53 |
|
if (isset($dbValues['_id'])) |
67
|
|
|
{ |
68
|
53 |
|
$dbValues[self::Key] = $dbValues['_id']; |
69
|
53 |
|
unset($dbValues['_id']); |
70
|
|
|
} |
71
|
53 |
|
return true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|