function testPackages() { $p = new ShippingPackage(25, array('depth' => 4, 'height' => 23, 'width' => 12)); $this->assertEquals($p->height(), 23); $this->assertEquals($p->width(), 12); $this->assertEquals($p->depth(), 4); $this->assertEquals($p->weight(), 25); $p = new ShippingPackage(25.3, array('h' => 23.7, 'd' => 4, 'w' => 12.344)); $this->assertEquals($p->height(), 23.7); $this->assertEquals($p->width(), 12.344); $this->assertEquals($p->depth(), 4); $this->assertEquals($p->weight(), 25.3); $p = new ShippingPackage(1, array(3, 4, 5)); $this->assertEquals($p->height(), 3); $this->assertEquals($p->width(), 4); $this->assertEquals($p->depth(), 5); $this->assertEquals($p->volume(), 60); $p = new ShippingPackage(13, array(1, 1, 2.5), array('shape' => 'cylinder')); $this->assertEquals($p->height(), 1); $this->assertEquals($p->depth(), 2.5); $this->assertEquals($p->weight(), 13); $this->assertEquals($p->volume(), 2.5); }
/** * all_add used to add all Items to one package * * @author John Dillick * @since 1.2 * * @param ShippingPackageItem $Item Item to add * @param ShippingPackager $pkgr the calling packager object * @param string $type expect dimensions 'dims', or just mass * @return void **/ public function all_add($Item, $pkgr, $type = 'dims') { if ($pkgr->module != $this->module) { return; } // not mine $defaults = array('wtl' => -1, 'wl' => -1, 'hl' => -1, 'll' => -1); $limits = array_merge($defaults, isset($this->options['limits']) ? $this->options['limits'] : array()); // Base Case Test: The item will never fit $package = new ShippingPackage('dims' == $type, $limits); $fit = $package->limits($Item); // No fit possible with these limits; force separate packaging on item. if (!$fit) { unset($package); $this->piece_add($Item, $this); // package alone return; } // Package Initialization if (empty($this->packages)) { $this->packages[] = $package; } // General Case Test: We have a package that the Item will fit in. foreach ($this->packages as $i => $pkg) { $Result = $pkg->add($Item); if (!$Result) { continue; // try next } if (true === $Result) { return; // found full fit } if (is_object($Result)) { // recurse to place remainder $this->all_add($Result, $this, $type); return; } } // Default Case: All packages too full; Create new and recurse. $this->packages[] = new ShippingPackage('dims' == $type, $limits); $this->all_add($Item, $this, $type); }