partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

PartParameter.php (10369B)


      1 <?php
      2 
      3 namespace PartKeepr\PartBundle\Entity;
      4 
      5 use Doctrine\ORM\Mapping as ORM;
      6 use PartKeepr\CoreBundle\Entity\BaseEntity;
      7 use PartKeepr\SiPrefixBundle\Entity\SiPrefix;
      8 use PartKeepr\UnitBundle\Entity\Unit;
      9 use Symfony\Component\Serializer\Annotation\Groups;
     10 
     11 /**
     12  * This object represents a parameter. Each parameter can have an unit (defined by the class "Unit") associated with
     13  * a numeric value.
     14  *
     15  * @ORM\Entity @ORM\HasLifecycleCallbacks
     16  */
     17 class PartParameter extends BaseEntity
     18 {
     19     const VALUE_TYPE_STRING = 'string';
     20 
     21     const VALUE_TYPE_NUMERIC = 'numeric';
     22 
     23     const VALUE_TYPES = [self::VALUE_TYPE_STRING, self::VALUE_TYPE_NUMERIC];
     24 
     25     /**
     26      * @ORM\ManyToOne(targetEntity="PartKeepr\PartBundle\Entity\Part", inversedBy="parameters")
     27      * The part this parameter is bound to
     28      *
     29      * @var Part
     30      */
     31     private $part;
     32 
     33     /**
     34      * The name of the parameter (e.g. Resistance, Voltage).
     35      *
     36      * @ORM\Column(type="string")
     37      * @Groups({"default"})
     38      *
     39      * @var string
     40      */
     41     private $name;
     42 
     43     /**
     44      * A description for this parameter.
     45      *
     46      * @ORM\Column(type="string")
     47      * @Groups({"default"})
     48      *
     49      * @var string
     50      */
     51     private $description;
     52 
     53     /**
     54      * The unit for this type. May be null.
     55      *
     56      * @ORM\ManyToOne(targetEntity="PartKeepr\UnitBundle\Entity\Unit")
     57      * @Groups({"default"})
     58      *
     59      * @var \PartKeepr\UnitBundle\Entity\Unit
     60      */
     61     private $unit;
     62 
     63     /**
     64      * The value of the unit. Together with the prefix, it becomes the actual value.
     65      *
     66      * Example: If you have 10µ, the value field will contain "10", the prefix object is linked to the SiPrefix
     67      * representing "µ" and the rawValue field will contain 0.000001
     68      *
     69      * @ORM\Column(type="float",nullable=true)
     70      * @Groups({"default"})
     71      *
     72      * @var float
     73      */
     74     private $value;
     75 
     76     /**
     77      * The normalized value (the product of si prefix + value).
     78      *
     79      * @ORM\Column(type="float",nullable=true)
     80      *
     81      * @var
     82      */
     83     private $normalizedValue;
     84 
     85     /**
     86      * The maximum value of the parameter.
     87      *
     88      * @ORM\Column(type="float",name="maximumValue",nullable=true)
     89      * @Groups({"default"})
     90      *
     91      * @var float
     92      */
     93     private $maxValue;
     94 
     95     /**
     96      * The normalized maximum value (the product of si prefix + value).
     97      *
     98      * @ORM\Column(type="float",nullable=true)
     99      *
    100      * @var
    101      */
    102     private $normalizedMaxValue;
    103 
    104     /**
    105      * The minimum value of the parameter.
    106      *
    107      * @ORM\Column(type="float",name="minimumValue",nullable=true)
    108      * @Groups({"default"})
    109      *
    110      * @var float
    111      */
    112     private $minValue;
    113 
    114     /**
    115      * The normalized minimum value (the product of si prefix + value).
    116      *
    117      * @ORM\Column(type="float",nullable=true)
    118      *
    119      * @var int
    120      */
    121     private $normalizedMinValue;
    122 
    123     /**
    124      * The string value if the parameter is a string.
    125      *
    126      * @ORM\Column(type="string")
    127      * @Groups({"default"})
    128      *
    129      * @var string
    130      */
    131     private $stringValue;
    132 
    133     /**
    134      * The type of the value.
    135      *
    136      * @ORM\Column(type="string")
    137      * @Groups({"default"})
    138      *
    139      * @var string
    140      */
    141     private $valueType;
    142 
    143     /**
    144      * The SiPrefix of the unit.
    145      *
    146      * @ORM\ManyToOne(targetEntity="PartKeepr\SiPrefixBundle\Entity\SiPrefix")
    147      * @Groups({"default"})
    148      *
    149      * @var SiPrefix
    150      */
    151     private $siPrefix;
    152 
    153     /**
    154      * The SiPrefix of the min value.
    155      *
    156      * @ORM\ManyToOne(targetEntity="PartKeepr\SiPrefixBundle\Entity\SiPrefix")
    157      * @Groups({"default"})
    158      *
    159      * @var SiPrefix
    160      */
    161     private $minSiPrefix;
    162 
    163     /**
    164      * The SiPrefix of the min value.
    165      *
    166      * @ORM\ManyToOne(targetEntity="PartKeepr\SiPrefixBundle\Entity\SiPrefix")
    167      * @Groups({"default"})
    168      *
    169      * @var SiPrefix
    170      */
    171     private $maxSiPrefix;
    172 
    173     public function __construct()
    174     {
    175         $this->setValueType(self::VALUE_TYPE_STRING);
    176         $this->setDescription("");
    177         $this->setStringValue("");
    178     }
    179 
    180     /**
    181      * @return mixed
    182      */
    183     public function getNormalizedValue()
    184     {
    185         return $this->normalizedValue;
    186     }
    187 
    188     /**
    189      * @param mixed $normalizedValue
    190      */
    191     public function setNormalizedValue($normalizedValue)
    192     {
    193         $this->normalizedValue = $normalizedValue;
    194     }
    195 
    196     /**
    197      * @return mixed
    198      */
    199     public function getNormalizedMaxValue()
    200     {
    201         return $this->normalizedMaxValue;
    202     }
    203 
    204     /**
    205      * @param mixed $normalizedMaxValue
    206      */
    207     public function setNormalizedMaxValue($normalizedMaxValue)
    208     {
    209         $this->normalizedMaxValue = $normalizedMaxValue;
    210     }
    211 
    212     /**
    213      * @return mixed
    214      */
    215     public function getNormalizedMinValue()
    216     {
    217         return $this->normalizedMinValue;
    218     }
    219 
    220     /**
    221      * @param mixed $normalizedMinValue
    222      */
    223     public function setNormalizedMinValue($normalizedMinValue)
    224     {
    225         $this->normalizedMinValue = $normalizedMinValue;
    226     }
    227 
    228     /**
    229      * @return string
    230      */
    231     public function getStringValue()
    232     {
    233         return $this->stringValue;
    234     }
    235 
    236     /**
    237      * @param string $stringValue
    238      */
    239     public function setStringValue($stringValue)
    240     {
    241         $this->stringValue = $stringValue;
    242     }
    243 
    244     /**
    245      * @return string
    246      */
    247     public function getValueType()
    248     {
    249         // Fallback to numeric if legacy parameter
    250         if (!in_array($this->valueType, self::VALUE_TYPES)) {
    251             return self::VALUE_TYPE_NUMERIC;
    252         }
    253 
    254         return $this->valueType;
    255     }
    256 
    257     /**
    258      * @param string $valueType
    259      *
    260      * @throws \Exception
    261      */
    262     public function setValueType($valueType)
    263     {
    264         if (!in_array($valueType, self::VALUE_TYPES)) {
    265             throw new \Exception("Invalid value type given:".$valueType);
    266         }
    267 
    268         $this->valueType = $valueType;
    269     }
    270 
    271     /**
    272      * Returns the name for this parameter.
    273      *
    274      * @return string The name for this parameter
    275      */
    276     public function getName()
    277     {
    278         return $this->name;
    279     }
    280 
    281     /**
    282      * Sets the name for this parameter.
    283      *
    284      * @param string $name The name
    285      */
    286     public function setName($name)
    287     {
    288         $this->name = $name;
    289     }
    290 
    291     /**
    292      * Returns the description.
    293      *
    294      * @return string The description
    295      */
    296     public function getDescription()
    297     {
    298         return $this->description;
    299     }
    300 
    301     /**
    302      * Sets the description for this parameter.
    303      *
    304      * @param string $description The description
    305      */
    306     public function setDescription($description)
    307     {
    308         $this->description = $description;
    309     }
    310 
    311     /**
    312      * Returns the unit.
    313      *
    314      * @return \PartKeepr\UnitBundle\Entity\Unit the unit
    315      */
    316     public function getUnit()
    317     {
    318         return $this->unit;
    319     }
    320 
    321     /**
    322      * Sets the unit.
    323      *
    324      * @param \PartKeepr\UnitBundle\Entity\Unit $unit The unit to set
    325      */
    326     public function setUnit(Unit $unit = null)
    327     {
    328         $this->unit = $unit;
    329     }
    330 
    331     /**
    332      * Returns the part.
    333      *
    334      * @return \PartKeepr\PartBundle\Entity\Part the part
    335      */
    336     public function getPart()
    337     {
    338         return $this->part;
    339     }
    340 
    341     /**
    342      * Sets the part.
    343      *
    344      * @param Part $part The part to set
    345      */
    346     public function setPart(Part $part = null)
    347     {
    348         $this->part = $part;
    349     }
    350 
    351     protected function recalculateNormalizedValues()
    352     {
    353         if ($this->getSiPrefix() === null) {
    354             $this->setNormalizedValue($this->getValue());
    355         } else {
    356             $this->setNormalizedValue($this->getSiPrefix()->calculateProduct($this->getValue()));
    357         }
    358 
    359         if ($this->getMinSiPrefix() === null) {
    360             $this->setNormalizedMinValue($this->getMinValue());
    361         } else {
    362             $this->setNormalizedMinValue($this->getMinSiPrefix()->calculateProduct($this->getMinValue()));
    363         }
    364 
    365         if ($this->getMaxSiPrefix() === null) {
    366             $this->setNormalizedMaxValue($this->getMaxValue());
    367         } else {
    368             $this->setNormalizedMaxValue($this->getMaxSiPrefix()->calculateProduct($this->getMaxValue()));
    369         }
    370     }
    371 
    372     /**
    373      * Returns the si prefix for this parameter.
    374      *
    375      * @return \PartKeepr\SiPrefixBundle\Entity\SiPrefix the si prefix or null
    376      */
    377     public function getSiPrefix()
    378     {
    379         return $this->siPrefix;
    380     }
    381 
    382     /**
    383      * Sets the si prefix for this parameter.
    384      *
    385      * @param \PartKeepr\SiPrefixBundle\Entity\SiPrefix $prefix The prefix to set, or null
    386      */
    387     public function setSiPrefix(SiPrefix $prefix = null)
    388     {
    389         $this->siPrefix = $prefix;
    390         $this->recalculateNormalizedValues();
    391     }
    392 
    393     /**
    394      * Returns the value.
    395      *
    396      * @return float The value
    397      */
    398     public function getValue()
    399     {
    400         return $this->value;
    401     }
    402 
    403     /**
    404      * Sets the value.
    405      *
    406      * @param float $value The value to set
    407      */
    408     public function setValue($value)
    409     {
    410         $this->value = $value;
    411         $this->recalculateNormalizedValues();
    412     }
    413 
    414     /**
    415      * @return SiPrefix
    416      */
    417     public function getMinSiPrefix()
    418     {
    419         return $this->minSiPrefix;
    420     }
    421 
    422     /**
    423      * @param SiPrefix $minSiPrefix
    424      */
    425     public function setMinSiPrefix($minSiPrefix)
    426     {
    427         $this->minSiPrefix = $minSiPrefix;
    428         $this->recalculateNormalizedValues();
    429     }
    430 
    431     /**
    432      * @return float
    433      */
    434     public function getMinValue()
    435     {
    436         return $this->minValue;
    437     }
    438 
    439     /**
    440      * @param float $minValue
    441      */
    442     public function setMinValue($minValue)
    443     {
    444         $this->minValue = $minValue;
    445         $this->recalculateNormalizedValues();
    446     }
    447 
    448     /**
    449      * @return SiPrefix
    450      */
    451     public function getMaxSiPrefix()
    452     {
    453         return $this->maxSiPrefix;
    454     }
    455 
    456     /**
    457      * @param SiPrefix $maxSiPrefix
    458      */
    459     public function setMaxSiPrefix($maxSiPrefix)
    460     {
    461         $this->maxSiPrefix = $maxSiPrefix;
    462         $this->recalculateNormalizedValues();
    463     }
    464 
    465     /**
    466      * @return float
    467      */
    468     public function getMaxValue()
    469     {
    470         return $this->maxValue;
    471     }
    472 
    473     /**
    474      * @param float $maxValue
    475      */
    476     public function setMaxValue($maxValue)
    477     {
    478         $this->maxValue = $maxValue;
    479         $this->recalculateNormalizedValues();
    480     }
    481 }