Skip to content

Commit

Permalink
Merge branch 'pu/pm/TbAvoidToArrayCloneRecursions' into '2024.11'
Browse files Browse the repository at this point in the history
tweak(TB Record) avoid recursion in toArray and clone

See merge request tine20/tine20!4999
  • Loading branch information
paulmhh committed Feb 22, 2024
2 parents acdf8f0 + 89724d5 commit 25f17d3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 16 deletions.
9 changes: 9 additions & 0 deletions tests/tine20/Sales/Document/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,19 @@ public function testOfferToOrderToInvoiceTransition()
$savedDocument = $this->_instance->saveDocument_Offer($document->toArray(true));
$this->assertSame(Sales_Config::DOCUMENT_FOLLOWUP_STATUS_NONE, $savedDocument[SMDOffer::FLD_FOLLOWUP_ORDER_CREATED_STATUS]);
$this->assertSame(Sales_Config::DOCUMENT_FOLLOWUP_STATUS_NONE, $savedDocument[SMDOffer::FLD_FOLLOWUP_ORDER_BOOKED_STATUS]);
$savedDocument[SMDOffer::FLD_POSITIONS][] = [
Sales_Model_DocumentPosition_Offer::FLD_TITLE => 'ipsum sub',
Sales_Model_DocumentPosition_Offer::FLD_PARENT_ID => $savedDocument[SMDOffer::FLD_POSITIONS][0]['id'],
Sales_Model_DocumentPosition_Offer::FLD_PRODUCT_ID => $subProduct->toArray(),
Sales_Model_DocumentPosition_Offer::FLD_SALES_TAX_RATE => 19,
Sales_Model_DocumentPosition_Offer::FLD_SALES_TAX => 100 * 19 / 100,
Sales_Model_DocumentPosition_Offer::FLD_NET_PRICE => 100,
];
$savedDocument[SMDOffer::FLD_OFFER_STATUS] = SMDOffer::STATUS_RELEASED;
$savedDocument = $this->_instance->saveDocument_Offer($savedDocument);
$this->assertNotSame($customer->getId(), $savedDocument[SMDOffer::FLD_CUSTOMER_ID]['id']);
$this->assertSame($customer->getId(), $savedDocument[SMDOffer::FLD_CUSTOMER_ID]['original_id']);
$this->assertCount(2, $savedDocument[SMDOffer::FLD_POSITIONS]);

Tinebase_Record_Expander_DataRequest::clearCache();

Expand Down
58 changes: 42 additions & 16 deletions tine20/Tinebase/Record/NewAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,17 +213,28 @@ public static function resetConfiguration()
*/
public function __clone()
{
foreach ($this->_data as $name => &$value)
{
if (is_object($value)) {
$this->_data[$name] = clone $value;
} else if (is_array($value)) {
foreach ($value as $arrKey => $arrValue) {
if (is_object($arrValue)) {
$value[$arrKey] = clone $arrValue;
static $idMap = [];
if ($id = $this->getId()) {
if (isset($idMap[$id])) {
return;
}
$idMap[$id] = true;
}

try {
foreach ($this->_data as $name => &$value) {
if (is_object($value)) {
$this->_data[$name] = clone $value;
} else if (is_array($value)) {
foreach ($value as $arrKey => $arrValue) {
if (is_object($arrValue)) {
$value[$arrKey] = clone $arrValue;
}
}
}
}
} finally {
unset($idMap[$id]);
}
}

Expand Down Expand Up @@ -497,18 +508,33 @@ public function getValidationErrors()
*/
public function toArray($_recursive = TRUE)
{
$recordArray = $this->_data;
$this->_convertDateTimeToString($recordArray, Tinebase_Record_Abstract::ISO8601LONG);
static $splObjSet = null;
if (null === $splObjSet) {
$splObjSet = new SplObjectStorage();
}
if ($splObjSet->contains($this)) {
$_recursive = false;
} elseif ($_recursive) {
$splObjSet->attach($this);
}

if ($_recursive) {
/** @var Tinebase_Record_Interface $value */
foreach ($recordArray as $property => $value) {
if (is_object($value) && method_exists($value, 'toArray')) {
$recordArray[$property] = $value->toArray();
try {
$recordArray = $this->_data;
$this->_convertDateTimeToString($recordArray, Tinebase_Record_Abstract::ISO8601LONG);

if ($_recursive) {
/** @var Tinebase_Record_Interface $value */
foreach ($recordArray as $property => $value) {
if (is_object($value) && method_exists($value, 'toArray')) {
$recordArray[$property] = $value->toArray();
}
}
}
} finally {
if ($_recursive) {
$splObjSet->detach($this);
}
}

return $recordArray;
}

Expand Down

0 comments on commit 25f17d3

Please sign in to comment.