Maslosoft Mangan API
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
<?php
/**
* This software package is licensed under AGPL or Commercial license.
*
* @package maslosoft/mangan
* @licence AGPL or Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com>
* @copyright Copyright (c) Maslosoft
* @copyright Copyright (c) Others as mentioned in code
* @link https://maslosoft.com/mangan/
*/
namespace Maslosoft\Mangan\Decorators;
use Maslosoft\Mangan\Helpers\DbRefManager;
use Maslosoft\Mangan\Helpers\PkManager;
use Maslosoft\Mangan\Helpers\RawFinder;
use Maslosoft\Mangan\Interfaces\Decorators\Property\DecoratorInterface;
use Maslosoft\Mangan\Interfaces\Transformators\TransformatorInterface;
use Maslosoft\Mangan\Meta\ManganMeta;
use Maslosoft\Mangan\Model\DbRef;
/**
* DbRefArray
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class DbRefArrayDecorator implements DecoratorInterface
{
public function read($model, $name, &$dbValues, $transformatorClass = TransformatorInterface::class)
{
if (!$dbValues)
{
$fieldMeta = ManganMeta::create($model)->field($name);
$model->$name = $fieldMeta->default;
return;
}
/**
* NOTE: Documents must be sorted as $dbRefs,
* however mongo does not guarantee sorting by list of id's.
* This require sorting in php.
* If document has composite key this must be taken care too
* while comparision for sorting is made.
*/
$refs = [];
$unsortedRefs = [];
$pks = [];
$sort = [];
// Collect primary keys
foreach ($dbValues as $key => $dbValue)
{
$dbValue['_class'] = DbRef::class;
$dbRef = $transformatorClass::toModel($dbValue);
DbRefDecorator::ensureClass($model, $name, $dbRef);
// Collect keys separately for each type
$pks[$dbRef->class][$key] = $dbRef->pk;
$sort[$key] = $dbRef->pk;
}
// Fetch all types of db ref's en masse
$i = 0;
$unsortedPks = [];
foreach ($pks as $referenced => $pkValues)
{
if (empty($referenced))
{
continue;
}
// Find all referenced documents
$refModel = new $referenced;
$found = (new RawFinder($refModel))->findAllByPk($pkValues);
if (!$found)
{
continue;
}
foreach ($found as $document)
{
// Collect unsorted documents
$unsortedRefs[$i] = $document;
// Collect pk's
$unsortedPks[$i] = PkManager::getFromArray($document, $refModel);
$i++;
}
}
// Find existing documents
$existing = [];
foreach ($model->$name as $key => $document)
{
foreach ($sort as $i => $pk)
{
if (PkManager::compare($pk, $document))
{
// Set existing document with key same as in sort
$existing[$i] = $document;
}
}
}
// Sort as stored ref
foreach ($sort as $key => $pk)
{
foreach ($unsortedRefs as $i => $document)
{
if (PkManager::compare($pk, $unsortedPks[$i]))
{
if (array_key_exists($key, $existing))
{
// Update existing instance
$refs[$key] = $transformatorClass::toModel($document, $existing[$key], $existing[$key]);
}
else
{
// Create new instance
$refs[$key] = $transformatorClass::toModel($document);
}
}
}
}
$model->$name = $refs;
}
public function write($model, $name, &$dbValue, $transformatorClass = TransformatorInterface::class)
{
$fieldMeta = ManganMeta::create($model)->field($name);
$dbValue[$name] = $fieldMeta->default;
// Empty
if (!$model->$name)
{
return;
}
// Bogus data
if (!is_array($model->$name))
{
return;
}
// Store DbRefs and optionally referenced model
foreach ($model->$name as $key => $referenced)
{
$dbRef = DbRefManager::extractRef($model, $name, $referenced);
if ($fieldMeta->dbRef->updatable)
{
DbRefManager::save($referenced, $dbRef);
}
$dbValue[$name][$key] = $transformatorClass::fromModel($dbRef, false);
}
}
}
API documentation generated by ApiGen