Mongolid\Serializer\Type\Converter::toDomainTypes PHP Метод

toDomainTypes() публичный Метод

Example: converts MongoDB\BSON\ObjectID to Type\ObjectID
public toDomainTypes ( array $data ) : mixed
$data array Array to convert.
Результат mixed
    public function toDomainTypes(array $data)
    {
        array_walk_recursive($data, function (&$value) {
            if (!is_object($value)) {
                return;
            }
            $className = $this->getReflectionClass(get_class($value));
            if (!$className) {
                return;
            }
            return $value = new $className($value);
        });
        return $data;
    }

Usage Example

Пример #1
0
 public function testToDomainTypesShouldReplaceMongoTypesByDomainTypes()
 {
     $mongoId = new MongoObjectID();
     $timestamp = time();
     $mongoDate = new MongoUTCDateTime($timestamp * 1000);
     $id = new ObjectID($mongoId);
     $date = new UTCDateTime($timestamp);
     $data = ['_id' => $mongoId, 'created_at' => $mongoDate, 'parents' => [$mongoId, $mongoId, $mongoId], 'comments' => [['author' => 'Jhon', 'date' => $mongoDate], ['author' => 'Doe', 'date' => $mongoDate, 'versions' => [['_id' => $mongoId, 'date' => $mongoDate, 'content' => 'Awsome'], ['_id' => $mongoId, 'date' => $mongoDate, 'content' => 'Great']]]]];
     $expected = ['_id' => $id, 'created_at' => $date, 'parents' => [$id, $id, $id], 'comments' => [['author' => 'Jhon', 'date' => $date], ['author' => 'Doe', 'date' => $date, 'versions' => [['_id' => $id, 'date' => $date, 'content' => 'Awsome'], ['_id' => $id, 'date' => $date, 'content' => 'Great']]]]];
     $this->assertEquals($expected, $this->converter->toDomainTypes($data));
 }
All Usage Examples Of Mongolid\Serializer\Type\Converter::toDomainTypes