Beispiel #1
0
 /**
  * Sets up the default units
  * @throws \Exception
  */
 public function setupUnits()
 {
     $count = 0;
     $skipped = 0;
     $data = Setup::loadYAML(self::UNIT_DATA_FILE);
     $aUnits = array();
     foreach ($data as $unitName => $unitData) {
         if (UnitManager::getInstance()->unitExists($unitName)) {
             $skipped++;
             continue;
         }
         $unit = new Unit();
         $unit->setName($unitName);
         $unit->setSymbol($unitData["symbol"]);
         if (array_key_exists("prefixes", $unitData)) {
             if (!is_array($unitData["prefixes"])) {
                 throw new \Exception($unitName . " doesn't contain a prefix list, or the prefix list is not an array.");
             }
             foreach ($unitData["prefixes"] as $prefix) {
                 $siPrefix = SiPrefixManager::getInstance()->getSiPrefixBySymbol($prefix);
                 if ($siPrefix === false) {
                     throw new \Exception("Unable to find prefix " . $prefix);
                 }
                 $unit->getPrefixes()->add($siPrefix);
             }
         }
         PartKeepr::getEM()->persist($unit);
         $count++;
     }
     $this->entityManager->flush();
     $this->logMessage(sprintf("Imported %d Units, skipped %d because they already exist", $count, $skipped));
 }
Beispiel #2
0
 public function create()
 {
     $this->requireParameter("name");
     $unit = new Unit();
     $unit->deserialize($this->getParameters());
     PartKeepr::getEM()->persist($unit);
     PartKeepr::getEM()->flush();
     return array("data" => $unit->serialize());
 }
 /**
  * Deserializes the part parameter
  * @param array $parameters The array with the parameters to set
  */
 public function deserialize(array $parameters)
 {
     foreach ($parameters as $key => $value) {
         switch ($key) {
             case "name":
                 $this->setName($value);
                 break;
             case "description":
                 $this->setDescription($value);
                 break;
             case "value":
                 $this->setValue($value);
                 break;
             case "siprefix_id":
                 $prefix = SiPrefix::loadById($value);
                 $this->setSiPrefix($prefix);
                 break;
             case "unit_id":
                 $unit = Unit::loadById($value);
                 $this->setUnit($unit);
                 break;
         }
     }
 }
Beispiel #4
0
 private function processParameterChanges(Part $part, array $data)
 {
     if (array_key_exists("updates", $data)) {
         foreach ($data["updates"] as $record) {
             foreach ($part->getParameters() as $partParameter) {
                 if ($partParameter->getId() == $record["id"]) {
                     $partParameter->setName($record["name"]);
                     $partParameter->setDescription($record["description"]);
                     $partParameter->setValue($record["value"]);
                     $partParameter->setSiPrefix(SiPrefix::loadById($record["siprefix_id"]));
                     $partParameter->setUnit(Unit::loadById($record["unit_id"]));
                     break;
                 }
             }
         }
     }
     if (array_key_exists("removals", $data)) {
         foreach ($data["removals"] as $record) {
             foreach ($part->getParameters() as $partParameter) {
                 if ($partParameter->getId() == $record["id"]) {
                     PartKeepr::getEM()->remove($partParameter);
                     $part->getParameters()->removeElement($partParameter);
                     break;
                 }
             }
         }
     }
     if (array_key_exists("inserts", $data)) {
         foreach ($data["inserts"] as $record) {
             $partParameter = new PartParameter();
             $partParameter->setPart($part);
             $partParameter->setName($record["name"]);
             $partParameter->setDescription($record["description"]);
             $partParameter->setValue($record["value"]);
             $partParameter->setSiPrefix(SiPrefix::loadById($record["siprefix_id"]));
             $partParameter->setUnit(Unit::loadById($record["unit_id"]));
             $part->getParameters()->add($partParameter);
         }
     }
 }