?
Solved

Iterating and modifying a PHP object

Posted on 2014-09-05
3
Medium Priority
?
157 Views
Last Modified: 2016-03-02
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.
0
Comment
Question by:EddieShipman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 34

Accepted Solution

by:
Slick812 earned 2000 total points
ID: 40307680
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
 
LVL 26

Author Comment

by:EddieShipman
ID: 40310947
I like the last option, I will give it a test. I had it up on phpfiddle but didn't save it.
0
 
LVL 26

Author Closing Comment

by:EddieShipman
ID: 40313238
Although we chose to do this in our Oracle code, I'm gonna accept it.
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question