em = $entityManager; $this->dateStart = $dateStart; $this->dateEnd = $dateEnd; $this->interval = $interval; } public function addProperty(string $propertyName, array $options) { $resolver = new OptionsResolver(); $this->configurePropertyOptions($resolver); $this->properties[$propertyName] = $resolver->resolve($options); $this->resultsSort[$propertyName] = $propertyName; } public function addAverageProperty(string $propertyName, array $options) { $resolver = new OptionsResolver(); $this->configurePropertyOptions($resolver); $this->averageProperties[$propertyName] = $resolver->resolve($options); $this->resultsSort[$propertyName] = $propertyName; } public function setAverageData(string $propertyName, string $key, $value) { if (isset($this->averageProperties[$propertyName])) { $this->averageProperties[$propertyName]['data'][$key] += number_format($value, 2); } else { throw new \Exception('La proprieté "' . $propertyName . '" n\'existe pas '); } } public function setData(string $propertyName, string $key, $value) { if (isset($this->properties[$propertyName])) { if(isset($this->properties[$propertyName]['data'][$key])) { $this->properties[$propertyName]['data'][$key] += $value; } $this->properties[$propertyName]['total_period'] += $value; } else { throw new \Exception('La proprieté "' . $propertyName . '" n\'existe pas '); } } public function setAveragePropertiesData() { foreach ($this->getLabels() as $key => $label) { foreach ($this->getAverageProperties() as $averagePropertyName => $averageProperty) { if ($this->getData($averageProperty['divider'], $key)) { $this->setAverageData( $averagePropertyName, $key, $this->getData($averageProperty['dividend'], $key) / $this->getData( $averageProperty['divider'], $key ) ); } if ($this->getTotalPeriod($averageProperty['divider'])) { $this->averageProperties[$averagePropertyName]['total_period'] = $this->getTotalPeriod( $averageProperty['dividend'] ) / $this->getTotalPeriod($averageProperty['divider']); } } } foreach ($this->getProperties() as $propertyName => $property) { $this->properties[$propertyName]['average_period'] = number_format( $this->properties[$propertyName]['total_period'] / count($this->getLabels()), 2 ); } foreach ($this->getAverageProperties() as $averagePropertyName => $averageProperty) { $this->averageProperties[$averagePropertyName]['average_period'] = number_format( $this->averageProperties[$averagePropertyName]['total_period'] / count($this->getLabels()), 2 ); } } public function getLabels() { return $this->labels; } public function getProperties() { return $this->properties; } public function getData($propertyName, $key) { if (!isset($this->properties[$propertyName])) { throw new \Exception('La proprieté "' . $propertyName . '" n\'existe pas '); } return $this->properties[$propertyName]['data'][$key]; } public function getTotalPeriod($propertyName) { if (!isset($this->properties[$propertyName])) { throw new \Exception('La proprieté "' . $propertyName . '" n\'existe pas '); } return $this->properties[$propertyName]['total_period']; } public function getAverageProperties() { return $this->averageProperties; } public function getResults() { $results = array_replace($this->resultsSort, $this->properties, $this->averageProperties); return $results; } public function getAsArray() { return array( 'label' => $this->getLabels(), 'data' => $this->getResults(), ); } public function getDateRange() { return new \DatePeriod( $this->getDateStart(), new \DateInterval('P1' . $this->getInterval()), $this->getDateEnd() ); } public function getDateStart() { return $this->dateStart; } public function getDateEnd() { return $this->dateEnd; } public function getInterval() { return $this->interval; } public function configurePropertyOptions(OptionsResolver $resolver) { $resolver->setDefaults( [ 'unit' => '', 'label' => 'Chiffre affaire produit', 'label_short' => 'CA produit', 'data' => array(), 'total_period' => 0, 'average_period' => 0, 'dividend' => null, 'divider' => null, ] ); } }