Skip to content
This repository has been archived by the owner on Nov 29, 2022. It is now read-only.

Commit

Permalink
Added scalar result and fixed odmtoclass function for performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Erman Titiz committed Dec 1, 2017
1 parent 8a55b0a commit 5bf405a
Showing 4 changed files with 145 additions and 49 deletions.
7 changes: 5 additions & 2 deletions Odm/Repository/BaseRepository.php
Original file line number Diff line number Diff line change
@@ -662,14 +662,17 @@ public function getResult()
{
return $this->response->getResult();
}

/**
* @param $result
* @return RepositoryResponse
*/
public function setResult($result){
$response = new RepositoryResponse();
$response->raw = $result;
foreach($result as &$row) $row = $this->cm->getDataManipulator()->convertRecordToOdmObject($row,$this->getBundle());
$response->setResult($result);
$data=$this->cm->getDataManipulator()->odmToClass($result);
$response->setResult($data);
$response->raw =$this->cm->getDataManipulator()->odmToClass($result,false);

return $response;
}
8 changes: 5 additions & 3 deletions Odm/Responses/RepositoryResponse.php
Original file line number Diff line number Diff line change
@@ -72,13 +72,15 @@ public function getSingularResult()
return $this->getCount() >0 ? $this->result[0] : null;
}

public function getScalarResult()
{
return $this->raw;
}

public function getResult()
{
return $this->result;
}
/**
* @param $result
*/
public function setResult($result){
$this->result = $result;
return $this;
133 changes: 132 additions & 1 deletion Services/ClassDataManipulator.php
Original file line number Diff line number Diff line change
@@ -10,6 +10,8 @@
use BiberLtd\Bundle\Phorient\Odm\Entity\BaseClass;
use BiberLtd\Bundle\Phorient\Odm\Types\BaseType;
use BiberLtd\Bundle\Phorient\Odm\Types\ORecordId;
use Doctrine\DBAL\Schema\Column;
use PhpOrient\Protocols\Binary\Data\ID;
use PhpOrient\Protocols\Binary\Data\Record;

class ClassDataManipulator
@@ -245,7 +247,6 @@ public function convertRecordToOdmObject($record,$bundle)
$entityClass->setRid($record->getRid());
return $entityClass;
}

private function arrayToObject($arrayObject,$bundle)
{

@@ -254,4 +255,134 @@ private function arrayToObject($arrayObject,$bundle)

return $arrayObject;
}

public function odmToClass($record,$toClass=true)
{
if(!is_object($record) && !is_array($record)){
$obj = $record;
return $obj;
}

if ($this->checkisRecord($record)) {
if (is_array($record)) {
$oClass = $record['@class'];

unset($record['@class']);
unset($record['@type']);
$record['rid'] = array_key_exists('@rid',$record) ? $record['@rid'] : null;
unset($record['@rid']);
unset($record['@version']);
unset($record['@fieldTypes']);
}elseif ($record instanceof Record)
{
$oClass = $record->getOClass();
$record = $record->getOData();
$record['rid'] = $record->getRid();
}else{
$oClass = null;
}
$obj = $this->dataToClass($record,$oClass,$toClass);
}else{
$obj = [];
foreach ($record as $key => $value) {

if (!empty($value))
{
$obj[$key] = $this->odmToClass($value,$toClass);
}
else
{
$obj[$key] = $value;
}
}
}

return $obj;
}
private function dataToClass($recordData, $oClass,$toClass=true)
{
if(!$toClass)
{
foreach($recordData as $key => &$value)
{
$value = $this->odmToClass($recordData[$key],$toClass);
}
return $recordData;
}
$class = $this->cm->getEntityPath().$oClass;
if (!class_exists($class)) return $recordData;
$entityClass = new $class;
$metadata = $this->cm->getMetadata($entityClass);
foreach ($metadata->getColumns()->toArray() as $propName => $annotations)
{

if(array_key_exists($propName, $recordData)) {
$fieldValue = $this->odmToClass($recordData[$propName],$toClass);
if(property_exists($annotations,'type') && $annotations->type=="ODateTime")
{
$fieldValue = \DateTime::createFromFormat('Y-m-d H:i:s',$recordData[$propName]);
}
$methodName = 'set' . ucfirst( $propName );
if ( method_exists( $entityClass, $methodName ) ) {
$entityClass->{$methodName}( $fieldValue);
} elseif( property_exists( $entityClass, $propName ) ) {
$entityClass->{$key} = $fieldValue;
} else {
// skip not existent configuration params
}

}
}
if(method_exists($entityClass,'setRid'))
$entityClass->setRid(new ID($recordData['rid']));

return $entityClass;
}


public function objToArray($obj, &$arr){

if(!is_object($obj) && !is_array($obj)){
$arr = $obj;
return $arr;
}

if(is_object($obj))
{
$class = get_class($obj);
/**
* @var Metadata $metadata;
*/
$metadata = $this->cm->getMetadata($class);
$columns = $metadata->getColumns();
$probs = $metadata->getProps();
foreach ($columns as $key => $value)
{
$arr[$key] = array();

$this->objToArray(
method_exists($obj,'get'.ucfirst($key)) ?
$obj->{'get'.ucfirst($key)}() :
(
property_exists($obj,$key) && $value instanceof Column ?
$obj->$key :
null
)
, $arr[$key]);
}
}
foreach ($obj as $key => $value)
{
if (!empty($value))
{
$arr[$key] = array();
$this->objToArray($value, $arr[$key]);
}
else
{
$arr[$key] = $value;
}
}
return $arr;
}
}
46 changes: 3 additions & 43 deletions Services/ClassManager.php
Original file line number Diff line number Diff line change
@@ -31,6 +31,7 @@ class ClassManager
public $currentDb;
private $entityPath;
private $dataManipulator;
private $currentBundle;

public function __construct(ContainerInterface $container = null, CMConfig $config=null)
{
@@ -109,12 +110,13 @@ public function getRepository($entityName)

public function setEntityPath($bundleName,$path)
{
$this->currentBundle = $bundleName;
$this->entityPath[$bundleName]=$path;
}

public function getEntityPath($bundleName=null)
{
if(is_null($bundleName)) $bundleName = $this->container->getRequest()->attributes->get('_template')->get('bundle');
$bundleName= $bundleName ?? $this->currentBundle;
return $this->entityPath[$bundleName];
}

@@ -128,48 +130,6 @@ public function getMetadata($entityClass)
return $this->cMetadataFactory->getMetadata($this,$entityClass);

}
public function convertRecordToOdmObject($record,$bundle)
{
if(is_array($record) && array_key_exists('@class',$record))
{
$record = $this->dataManipulator->objectToRecord($record);
}
$oClass = $record->getOClass();
$oData = $record->getOData();;
$class = $this->getEntityPath($bundle).$oClass;
if (!class_exists($class)) return $record->getOData();
$entityClass = new $class;
$metadata = $this->getMetadata($entityClass);
$recordData = $oData;
foreach ($metadata->getColumns()->toArray() as $propName => $annotations)
{

if(array_key_exists($propName, $recordData)) {
$value = $this->dataManipulator->checkisRecord($recordData[$propName]) ? $this->convertRecordToOdmObject($recordData[$propName],$bundle) : $this->arrayToObject($recordData[$propName],$bundle);
$methodName = 'set' . ucfirst( $propName );
if ( method_exists( $entityClass, $methodName ) ) {
$entityClass->{$methodName}( $value);
} elseif( property_exists( $entityClass, $propName ) ) {
$entityClass->{$key} = $value;
} else {
// skip not existent configuration params
}

}
}
if(method_exists($entityClass,'setRid'))
$entityClass->setRid($record->getRid());
return $entityClass;
}

private function arrayToObject($arrayObject,$bundle)
{

if(is_array($arrayObject))
foreach ($arrayObject as &$value) $value = $this->dataManipulator->checkisRecord($arrayObject) ? $this->convertRecordToOdmObject($value,$bundle) : (is_array($value) ? $this->arrayToObject($value,$bundle): $value);

return $arrayObject;
}
public function getDataManipulator()
{
return $this->dataManipulator;

0 comments on commit 5bf405a

Please sign in to comment.