Solved

Iterating and modifying a PHP object

Posted on 2014-09-05
3
149 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
  • 2
3 Comments
 
LVL 33

Accepted Solution

by:
Slick812 earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Although we chose to do this in our Oracle code, I'm gonna accept it.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now