Beispiel #1
0
 public function testGet()
 {
     $this->assertSame('1', $this->object->get('foo'));
     $this->assertSame(['3', '4'], $this->object->get('baz'));
     $nested = $this->object->get('nested');
     $this->assertInstanceOf('\\StdClass', $nested);
     $this->assertObjectHasAttribute('one', $nested);
     $this->assertEquals('5', $nested->one);
     $this->assertEquals('5', $this->object->get('nested->one'));
     $this->assertObjectHasAttribute('two', $nested);
     $this->assertEquals('6', $nested->two);
     $this->assertEquals('6', $this->object->get('nested->two'));
     $this->assertEquals(['magento/theme-adminhtml-backend' => 7, 'magento/theme-frontend-luma' => 8], (array) $this->object->get('nested', '/^magento\\/theme/'));
 }
<?php

/**
 * Registry
 */
class Package
{
    protected static $data = array();
    public static function set($key, $value)
    {
        self::$data[$key] = $value;
    }
    public static function get($key)
    {
        return isset(self::$data[$key]) ? self::$data[$key] : null;
    }
    public static function removeObject($key)
    {
        if (array_key_exists($key, self::$data)) {
            unset(self::$data[$key]);
        }
    }
}
Package::set('ACL', 'Assess Control List');
Package::set('DISP', 'Dispatcher');
var_dump(Package::get('ACL'));
var_dump(Package::get('DISP'));
Package::removeObject('ACL');
var_dump(Package::get('ACL'));
Beispiel #3
0
 /**
  * [getVolumePackagesByGross description]
  * @return [type] [description]
  */
 public function getVolumePackagesByGross()
 {
     /**
      * Storage
      */
     $itemVolumeTotal = 0;
     $itemWeightTotal = 0;
     $final_packages = array();
     $items = $this->getItems();
     $packageOptions = $this->getPackageOptions();
     /**
      * Total Items
      */
     for ($i = 0; $i < count($items); $i++) {
         $itemVolumeTotal += $items[$i]->get('volume');
         $itemWeightTotal += $items[$i]->get('weight');
     }
     /**
      * Calculate based on Package Dimensions
      */
     if ($packageOptions && ($itemVolumeTotal || $itemWeightTotal)) {
         $itemVolumeRemain = $itemVolumeTotal;
         $itemWeightRemain = $itemWeightTotal;
         $itemWeightSizeRatio = $itemVolumeRemain / $itemWeightRemain;
         do {
             // Prepare
             $package_found = false;
             // Fit into New Package
             if (!$package_found) {
                 foreach ($packageOptions as $key => $package) {
                     if ($package->get('volume') >= $itemVolumeRemain && $package->get('weight') >= $itemWeightRemain) {
                         $new_package = new Package($package);
                         $new_package->set('volume_used', $itemVolumeRemain);
                         $new_package->set('weight_used', $itemWeightRemain);
                         $final_packages[] = $new_package;
                         $itemVolumeRemain = 0;
                         $itemWeightRemain = 0;
                         $package_found = true;
                         break;
                     }
                 }
             }
             // Split and place into largest package
             if (!$package_found) {
                 $new_package = new Package(end($packageOptions));
                 $package_maxVolume = $new_package->get('volume');
                 $package_maxWeight = $new_package->get('weight');
                 $package_ratio = $package_maxVolume / $package_maxWeight;
                 $newVolumeUsed = null;
                 $newWeightUsed = null;
                 $isOverVolume = $itemVolumeRemain > $package_maxVolume;
                 $isOverWeight = $itemWeightRemain > $package_maxWeight;
                 if ($isOverVolume || $isOverWeight) {
                     if ($itemWeightSizeRatio > $package_ratio) {
                         $newVolumeUsed = $isOverVolume ? $package_maxVolume : $itemVolumeRemain;
                         $newWeightUsed = round($newVolumeUsed / $itemWeightSizeRatio, 2, PHP_ROUND_HALF_DOWN);
                         if ($newWeightUsed > $package_maxWeight) {
                             $newWeightUsed = $package_maxWeight;
                             $newVolumeUsed = round($newWeightUsed * $itemWeightSizeRatio, 2, PHP_ROUND_HALF_DOWN);
                         }
                     } else {
                         $newWeightUsed = $isOverWeight ? $package_maxWeight : $itemWeightRemain;
                         $newVolumeUsed = round($newWeightUsed * $itemWeightSizeRatio, 2, PHP_ROUND_HALF_DOWN);
                         if ($newVolumeUsed > $package_maxVolume) {
                             $newVolumeUsed = $package_maxVolume;
                             $newWeightUsed = round($newVolumeUsed / $itemWeightSizeRatio, 2, PHP_ROUND_HALF_DOWN);
                         }
                     }
                 }
                 if (is_null($netVolumeUsed) || $netVolumeUsed > $itemVolumeRemain) {
                     $netVolumeUsed = $itemVolumeRemain;
                 }
                 if (is_null($newWeightUsed) || $newWeightUsed > $itemWeightRemain) {
                     $newWeightUsed = $itemWeightRemain;
                 }
                 $new_package->set('volume_used', $newVolumeUsed > 0 ? $newVolumeUsed : 1);
                 $new_package->set('weight_used', $newWeightUsed > 0 ? $newWeightUsed : 1);
                 $final_packages[] = $new_package;
                 $itemVolumeRemain -= $newVolumeUsed;
                 $itemWeightRemain -= $newWeightUsed;
                 $package_found = true;
             }
         } while ($package_found && ($itemVolumeRemain || $itemWeightRemain));
     }
     /**
      * Complete
      */
     return $final_packages;
 }