function it_converts_metric_values_given_the_configured_base_unit_in_the_channel($converter, AbstractProductValue $weightValue, AbstractProductValue $surfaceValue, AbstractProductValue $nameValue, AbstractAttribute $weight, AbstractAttribute $surface, AbstractAttribute $name, AbstractMetric $weightMetric, AbstractMetric $surfaceMetric, ProductInterface $product, Channel $channel) { $weightValue->getAttribute()->willReturn($weight); $weightValue->getData()->willReturn($weightMetric); $weight->getCode()->willReturn('weight'); $weightMetric->getFamily()->willReturn('Weight'); $weightMetric->getUnit()->willReturn('KILOGRAM'); $weightMetric->getData()->willReturn(1); $surfaceValue->getAttribute()->willReturn($surface); $surfaceValue->getData()->willReturn($surfaceMetric); $surface->getCode()->willReturn('surface'); $surfaceMetric->getFamily()->willReturn('Surface'); $surfaceMetric->getUnit()->willReturn('METER_SQUARE'); $surfaceMetric->getData()->willReturn(10); $nameValue->getAttribute()->willReturn($name); $nameValue->getData()->willReturn('foobar'); $product->getValues()->willReturn(array($weightValue, $surfaceValue, $nameValue)); $channel->getConversionUnits()->willReturn(array('weight' => 'GRAM')); $converter->setFamily('Weight')->shouldBeCalled(); $converter->convert('KILOGRAM', 'GRAM', 1)->willReturn(0.001); $converter->setFamily('Surface')->shouldNotBeCalled(); $weightMetric->setData(0.001)->shouldBeCalled(); $weightMetric->setUnit('GRAM')->shouldBeCalled(); $this->convert($product, $channel); }
/** * Convert all the products metric values into the channel configured conversion units * * @param ProductInterface $product * @param Channel $channel */ public function convert(ProductInterface $product, Channel $channel) { $channelUnits = $channel->getConversionUnits(); foreach ($product->getValues() as $value) { $data = $value->getData(); $attribute = $value->getAttribute(); if ($data instanceof AbstractMetric && isset($channelUnits[$attribute->getCode()])) { $channelUnit = $channelUnits[$attribute->getCode()]; $this->converter->setFamily($data->getFamily()); $data->setData($this->converter->convert($data->getUnit(), $channelUnit, $data->getData())); $data->setUnit($channelUnit); } } }
/** * Returns conversion units * * @param Channel $channel * * @return array */ protected function normalizeConversionUnits(Channel $channel) { $result = array(); foreach ($channel->getConversionUnits() as $family => $unit) { $result[] = sprintf('%s: %s', $family, $unit); } return implode(', ', $result); }
/** * {@inheritDoc} */ public function getConversionUnits() { $this->__initializer__ && $this->__initializer__->__invoke($this, 'getConversionUnits', array()); return parent::getConversionUnits(); }