Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 165 additions & 0 deletions src/Ting/Repository/HydratorValueObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

/***********************************************************************
*
* Ting - PHP Datamapper
* ==========================================
*
* Copyright (C) 2014 CCM Benchmark Group. (http://www.ccmbenchmark.com)
*
***********************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
**********************************************************************/

namespace CCMBenchmark\Ting\Repository;

use CCMBenchmark\Ting\Driver\ResultInterface;
use CCMBenchmark\Ting\Exception;
use CCMBenchmark\Ting\HydratableMetadataRepository;
use CCMBenchmark\Ting\MetadataRepository;
use CCMBenchmark\Ting\UnitOfWork;

use function reset;

/**
* @template T
*
* @template-implements HydratorInterface<T>
*/
class HydratorValueObject implements HydratorInterface
{
/**
* @var class-string<T>
*/
protected $objectToHydrate;
/**
* @var ResultInterface<T>
*/
protected $result = null;
/**
* @var HydratableMetadataRepository
*/
private $hydrateableMetadataRepository;

/**
* @param class-string<T> $objectToHydrate
*/
public function __construct(string $objectToHydrate)
{
$this->objectToHydrate = $objectToHydrate;
}

/**
* @param MetadataRepository $metadataRepository
* @return void
*/
public function setMetadataRepository(MetadataRepository $metadataRepository)
{
// Useless for this hydrator
}

/**
* @param UnitOfWork $unitOfWork
* @return void
*/
public function setUnitOfWork(UnitOfWork $unitOfWork)
{
// Useless for this hydrator
}

/**
* @param ResultInterface<T> $result
* @return $this
*/
public function setResult(ResultInterface $result)
{
$this->result = $result;
return $this;
}

/**
* @return \Generator<int, T>
*/
#[\ReturnTypeWillChange]
public function getIterator()
{
if (method_exists($this->objectToHydrate, '__construct')) {
foreach ($this->result as $key => $row) {
yield $key => new $this->objectToHydrate(...array_combine(array_column($row, 'name'), array_column($row, 'value')));
}
} else {

$methods = [];
foreach ($this->result as $key => $row) {
foreach ($row as $column) {
$setter = 'set'.str_replace("_", "", ucwords($column['name'], " /_")); // snake case by default in database
if (method_exists($this->objectToHydrate, $setter)) {
$methods[$column['name']] = $setter;
} else {
throw new Exception('There is no setter for column "'.$column['name'].'"');
}
}
break;
}

$this->result->rewind();

foreach ($this->result as $key => $row) {
yield $key => $this->hydrateObject($row, $methods);
}
}
}

/**
* @return int
*/
#[\ReturnTypeWillChange]
public function count()
{
if ($this->result === null) {
return 0;
}

return $this->result->getNumRows();
}

/**
* @throws Exception
*/
private function hydrateObject(array $row, $methods)
{
$object = new $this->objectToHydrate();
foreach ($row as $column) {
$setter = $methods[$column['name']];
$object->$setter($column['value']);
}

return $object;
}

private function getSerializerFromType(string $type)
{

}

/**
* @param HydratableMetadataRepository $metadataRepository
* @return void
*/
public function setHydratableMetadataRepository(HydratableMetadataRepository $metadataRepository)
{

}
}
36 changes: 36 additions & 0 deletions tests/fixtures/ValueObject/Bouh.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace tests\fixtures\ValueObject;

class Bouh
{
private $firstname;

private $name;

/**
* @return mixed
*/
public function getFirstname()
{
return $this->firstname;
}

public function setFirstname($firstname)
{
$this->firstname = $firstname;
}

/**
* @return mixed
*/
public function getName()
{
return $this->name;
}

public function setName($name)
{
$this->name = $name;
}
}
32 changes: 32 additions & 0 deletions tests/fixtures/ValueObject/BouhWithConstruct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace tests\fixtures\ValueObject;

class BouhWithConstruct
{
private $firstname;

private $name;

public function __construct($firstname, $name)
{
$this->firstname = $firstname;
$this->name = $name;
}

/**
* @return mixed
*/
public function getFirstname()
{
return $this->firstname;
}

/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
}
78 changes: 78 additions & 0 deletions tests/fixtures/ValueObject/BouhWithNativeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace tests\fixtures\ValueObject;

class BouhWithNativeType
{
private int $nb;

private float $avg;

private string $myName;

private \DateTime $date;

private \DateTimeImmutable $dateTimeImmutable;

private bool $bool;

public function getNb(): int
{
return $this->nb;
}

public function setNb(int $nb): void
{
$this->nb = $nb;
}

public function getAvg(): float
{
return $this->avg;
}

public function setAvg(float $avg): void
{
$this->avg = $avg;
}

public function getMyName(): string
{
return $this->myName;
}

public function setMyName(string $myName): void
{
$this->myName = $myName;
}

public function getDate(): \DateTime
{
return $this->date;
}

public function setDate(\DateTime $date): void
{
$this->date = $date;
}

public function getDateTimeImmutable(): \DateTimeImmutable
{
return $this->dateTimeImmutable;
}

public function setDateTimeImmutable(\DateTimeImmutable $dateTimeImmutable): void
{
$this->dateTimeImmutable = $dateTimeImmutable;
}

public function isBool(): bool
{
return $this->bool;
}

public function setBool(bool $bool): void
{
$this->bool = $bool;
}
}
24 changes: 24 additions & 0 deletions tests/fixtures/ValueObject/WrongObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace tests\fixtures\ValueObject;

class WrongObject
{
private $wrong;

/**
* @return mixed
*/
public function getWrong()
{
return $this->wrong;
}

/**
* @param mixed $wrong
*/
public function setWrong($wrong): void
{
$this->wrong = $wrong;
}
}
Loading
Loading