Exemple #1
0
 /**
  * Tests Address->toXML
  */
 public function testToXML()
 {
     $data = array('atHome' => array('product' => 'bpack 24h Pro', 'options' => array(array('common:infoNextDay' => array('@attributes' => array('language' => 'NL'), 'common:emailAddress' => '*****@*****.**'))), 'weight' => 2000, 'openingHours' => array('Monday' => '10:00-17:00'), 'desiredDeliveryPlace' => 1234, 'receiver' => array('name' => 'Tijs Verkoyen', 'company' => 'Sumo Coders', 'address' => array('streetName' => 'Afrikalaan', 'number' => '289', 'box' => '3', 'postalCode' => '9000', 'locality' => 'Gent', 'countryCode' => 'BE'), 'emailAddress' => '*****@*****.**', 'phoneNumber' => '+32 9 395 02 51')));
     $expectedDocument = self::createDomDocument();
     $nationalBox = $expectedDocument->createElement('nationalBox');
     $expectedDocument->appendChild($nationalBox);
     $atHome = $expectedDocument->createElement('atHome');
     $nationalBox->appendChild($atHome);
     $atHome->appendChild($expectedDocument->createElement('product', $data['atHome']['product']));
     $options = $expectedDocument->createElement('options');
     foreach ($data['atHome']['options'] as $value) {
         foreach ($value as $key2 => $value2) {
             $element = $expectedDocument->createElement($key2);
             foreach ($value2 as $key3 => $value3) {
                 if ($key3 == '@attributes') {
                     foreach ($value3 as $key4 => $value4) {
                         $element->setAttribute($key4, $value4);
                     }
                 } else {
                     $element->appendChild($expectedDocument->createElement($key3, $value3));
                 }
             }
             $options->appendChild($element);
         }
     }
     $atHome->appendChild($options);
     $atHome->appendChild($expectedDocument->createElement('weight', $data['atHome']['weight']));
     $openingHours = $expectedDocument->createElement('openingHours');
     foreach ($data['atHome']['openingHours'] as $key => $value) {
         $openingHours->appendChild($expectedDocument->createElement($key, $value));
     }
     $atHome->appendChild($openingHours);
     $atHome->appendChild($expectedDocument->createElement('desiredDeliveryPlace', $data['atHome']['desiredDeliveryPlace']));
     $receiver = $expectedDocument->createElement('receiver');
     $atHome->appendChild($receiver);
     foreach ($data['atHome']['receiver'] as $key => $value) {
         $key = 'common:' . $key;
         if ($key == 'common:address') {
             $address = $expectedDocument->createElement($key);
             foreach ($value as $key2 => $value2) {
                 $key2 = 'common:' . $key2;
                 $address->appendChild($expectedDocument->createElement($key2, $value2));
             }
             $receiver->appendChild($address);
         } else {
             $receiver->appendChild($expectedDocument->createElement($key, $value));
         }
     }
     $actualDocument = self::createDomDocument();
     $address = new Address($data['atHome']['receiver']['address']['streetName'], $data['atHome']['receiver']['address']['number'], $data['atHome']['receiver']['address']['box'], $data['atHome']['receiver']['address']['postalCode'], $data['atHome']['receiver']['address']['locality'], $data['atHome']['receiver']['address']['countryCode']);
     $receiver = new Receiver();
     $receiver->setName($data['atHome']['receiver']['name']);
     $receiver->setCompany($data['atHome']['receiver']['company']);
     $receiver->setAddress($address);
     $receiver->setEmailAddress($data['atHome']['receiver']['emailAddress']);
     $receiver->setPhoneNumber($data['atHome']['receiver']['phoneNumber']);
     $openingHourDay = new OpeninghourDay('Monday', $data['atHome']['openingHours']['Monday']);
     $messaging = new Messaging('infoNextDay', $data['atHome']['options'][0]['common:infoNextDay']['@attributes']['language'], $data['atHome']['options'][0]['common:infoNextDay']['common:emailAddress']);
     $atHome = new AtHome();
     $atHome->setProduct($data['atHome']['product']);
     $atHome->setWeight($data['atHome']['weight']);
     $atHome->setReceiver($receiver);
     $atHome->setDesiredDeliveryPlace($data['atHome']['desiredDeliveryPlace']);
     $atHome->addOpeningHour($openingHourDay);
     // I know, the line below is kinda bogus, but it will make sure all code is tested
     $atHome->setOpeningHours(array($openingHourDay));
     $atHome->addOption($messaging);
     // I know, the line below is kinda bogus, but it will make sure all code is tested
     $atHome->setOptions(array($messaging));
     $actualDocument->appendChild($atHome->toXML($actualDocument));
     $this->assertEquals($expectedDocument, $actualDocument);
 }
Exemple #2
0
 /**
  * @param  \SimpleXMLElement $xml
  * @return AtHome
  */
 public static function createFromXML(\SimpleXMLElement $xml)
 {
     $atHome = new AtHome();
     if (isset($xml->atHome->product) && $xml->atHome->product != '') {
         $atHome->setProduct((string) $xml->atHome->product);
     }
     if (isset($xml->atHome->options) and !empty($xml->atHome->options)) {
         foreach ($xml->atHome->options as $optionData) {
             $optionData = $optionData->children('http://schema.post.be/shm/deepintegration/v3/common');
             if (in_array($optionData->getName(), array('infoDistributed'))) {
                 $option = Messaging::createFromXML($optionData);
             } else {
                 $className = '\\TijsVerkoyen\\Bpost\\Bpost\\Order\\Box\\Option\\' . ucfirst($optionData->getName());
                 if (!method_exists($className, 'createFromXML')) {
                     throw new Exception('Not Implemented');
                 }
                 $option = call_user_func(array($className, 'createFromXML'), $optionData);
             }
             $atHome->addOption($option);
         }
     }
     if (isset($xml->atHome->weight) && $xml->atHome->weight != '') {
         $atHome->setWeight((int) $xml->atHome->weight);
     }
     if (isset($xml->atHome->openingHours) && $xml->atHome->openingHours != '') {
         throw new Exception('Not Implemented');
         $atHome->setProduct((string) $xml->atHome->openingHours);
     }
     if (isset($xml->atHome->desiredDeliveryPlace) && $xml->atHome->desiredDeliveryPlace != '') {
         $atHome->setDesiredDeliveryPlace((string) $xml->atHome->desiredDeliveryPlace);
     }
     if (isset($xml->atHome->receiver)) {
         $atHome->setReceiver(Receiver::createFromXML($xml->atHome->receiver->children('http://schema.post.be/shm/deepintegration/v3/common')));
     }
     return $atHome;
 }