Iterating and modifying a PHP object

I have the code below that is like code that creates an Object built from a Magento order and converted to JSON then sent to our Oracle DB for processing. The issue we have with this is that Oracle has an issue with the length of the JSON string.

What I'd like to do is to parse this out to where the ItemCol collection has a maximum of 3 items each. The GrandTotal will need to be recalculated for the number of items in each ItemCol collection.

So, essentially just the ItemCol is what is changing (except the GrandTotal) The main object will need to be cloned and the ItemCol be amended to a maximum of three items each.

class NATransportMsg {
	public $CartSession;
	public $ClientIP;
	public $ClientBrowser;
	public $CartTime;
	public $Version;
	public $OracleSession;
	public $Action;
	public $ObjectSent;
}

class NAOrder {
	public $ShippingAmount;
	public $ShippingMethod;
	public $BaseTaxAmount;
	public $HowDidYouHear;
	public $Payment;
	public $ItemCol;
	public $CouponCode;
	public $OrderID;
	public $CustomerID;
	public $AddressesCol;
}

class NAOrderItem {
	public $Price;
	public $DiscountAmount;
	public $QuantityOrdered;
	public $Sku;
	public $CEDeclined;
	public $CEFirstName;
	public $CELastName;
	public $CEEventCode;
	public $CELicense;
	public $CEState;
	public $CETypeID;
	public $SP;
}

class NAOrderBillAddress {
	public $AddressType;
	public $Address1;
	public $Address2;
	public $Address3;
	public $City;
	public $State;
	public $Zip;
	public $Country;
}

class NAOrderShipAddress {
	public $AddressType;
	public $Address1;
	public $Address2;
	public $Address3;
	public $City;
	public $State;
	public $Zip;
	public $Country;
}

class NAPayment {
	public $AuthID;
	public $CCLast4;
	public $CCType;
	public $TransID;
	public $Method;
	public $GrandTotal;
}

class NASP {
	public $Address1;
	public $Address2;
	public $Address3;
	public $City;
	public $State;
	public $Country;
	public $PID;
	public $Email;
	public $Zip;
	public $LastName;
	public $FirstName;
}


$message                = new NATransportMsg;
$message->CartSession   = "32 bit hash value";
$message->ClientIP      = "Client IP address";
$message->ClientBrowser = "Client Browser UserAgent";
$message->CartTime      = "01-01-2015";
$message->Version       = 2;
$message->OracleSession = "";
$message->Action        = "Order";

$Object = new NAOrder;

$Order->ShippingAmount = 0;
$Order->ShippingMethod = "Shipping Method";
$Order->BaseTaxAmount = 0;

$Payment = new NAPayment;

$Payment->AuthID = null;
$Payment->CCLast4 = 9999;
$Payment->CCType = "VI";
$Payment->TransID = "PayPal V#";
$Payment->Method = "paypal";
$Payment->GrandTotal = 2150;

$Order->Payment = $Payment;

for($i = 1; $i < 6; $i++) {
	// create itemcol and SP
	$item = new NAOrderItem;	
	$sp = new NASP;
	$item->Price = 430;
	$item->DiscountAmount = 0;
	$item->QuantityOrdered = 1;
	$item->Sku = "SKU FOR ITEM";
	$item->C_Declined = null;
	$item->C_TypeID = null;
	
	$sp->DOB="99-99-9999";
	$sp->Address1="P.O. Box 1490";
	$sp->Address2=null;
	$sp->Address3=null;
	$sp->City="Jackson";
	$sp->State="NV";
	$sp->Country="US";
	$sp->PID="9999999";
	$sp->Phone="1-888-5551212-";
	$sp->Email="user".$i."@email.com";
	$sp->Zip="99999";
	$sp->LastName="Last1".$i;
	$sp->FirstName="First".$i;

	$item->SP = $sp;
	$ItemCol[] = $item;
}

$Order->ItemCol = $ItemCol;
$Order->OrderID = 9999999999;
$Order->CustomerID = 9999999;
$Order->CouponCode = null;

$AddressCol = new NAOrderBillAddress;

$AddressCol->AddressType = "B";
$AddressCol->Address1 = "P.O. Box 1490";
$AddressCol->Address2 = null;
$AddressCol->Address3 = null;
$AddressCol->City = "Jackson";
$AddressCol->State = "NV";
$AddressCol->Country =  "US";
$Order->AddressesCol = $AddressCol;

$message->ObjectSent    = $Order;

if(count((array)$message->ObjectSent->ItemCol) > 3)) {
	foreach($message->ObjectSent->ItemCol) {
		// Not sure how to actually iterate this and create a new $message object
		// for each time it goes past 3 items and copy the rest of the $message object and
		// update the GrandTotal of each $message object		
	}
}

Open in new window


I will also have an issue with transmission of the order data at 2 minute intervals but that is another question for later.
LVL 26
Eddie ShipmanAll-around developerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Slick812Commented:
greetin to EddieShipman, , looked over your code for those classes, and If I understand the problem, you need to have a JSON string conversion of the $message (or several $message) with three or LESS $item in the $Order->ItemCol array.

you have this to try an Extract the ItemCol arrays -

if(count((array)$message->ObjectSent->ItemCol) > 3)) {
  foreach($message->ObjectSent->ItemCol) {

      }
}
and make new $message objects with 3 or less ItemCol in each.

If I were doing this I would NOT start with the $message After it's fully built, as the above code with
foreach($message

If I had full access to all code you show, I would do an Array count( ) right After the $ItemCol[] was filled as in-

{// code where $item is created
$ItemCol[] = $item;
}
      $aryNum = count($ItemCol);
      if($aryNum > 3) {
        $messAmount =  ceil ($aryNum / 3);
        for ($i = 0; $i < $messAmount; ++$i) {
          $orderAry[ ] = clone $Order;
          $messAry[ ] = clone $message;
          }

      $itemAry = array_chunk($ItemCol, 3);
        for ($i = 0; $i < count($itemAry); ++$i) {
           $orderAry[i ]->ItemCol = $itemAry[i];
           $messAry[i ]->ObjectSent    = $orderAry[i ];
          }
        }
$Order->ItemCol = $ItemCol;

Open in new window


I would check to see if the $ItemCol array was more than 3 count(), then clone the $message and $order objects enough times to have 3 items in each
 then chop up the $ItemCol array with array_chunk( ) into 3 array chunks then build several $message objects with cloned the $message and $order objects

you will need to MOVE all of the -
$Order->CouponCode = null;
$AddressCol = new NAOrderBillAddress;

up above where you clone the objects so you will get all the data needed in them, this is just a prototype IDEA, and my math and data inserts need to be checked an verified, but that my idea about this.
  by the by - I have had trouble with the -
$copy = clone $Original;
with Complex Classes, but none of your classes here seem complex, so I guess it may work?

although you might can just create one FULLY filled $message object with out the
$message->ObjectSent->ItemCol
ever being set, then do the -
$itemAry = array_chunk($ItemCol, 3);
and then just set the
$message->ObjectSent->ItemCol = $itemAry[j];
and JSON string it and then when you done wid JSON string load the complete items  as
$message->ObjectSent->ItemCol = $ItemCol;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Eddie ShipmanAll-around developerAuthor Commented:
I like the last option, I will give it a test. I had it up on phpfiddle but didn't save it.
0
Eddie ShipmanAll-around developerAuthor Commented:
Although we chose to do this in our Oracle code, I'm gonna accept it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.