<commit_before><?php

namespace Outspaced\GoogleChartMakerBundle\Chart\Renderer;

use Outspaced\GoogleChartMakerBundle\Chart\Charts\BaseChart;

/**
 * This library has been deprecated by Google, although the API is still available
 */
class Image
{
	/**
	 * @var string
	 */
	const BASE_URL = 'http://chart.googleapis.com/chart?';

    public function render(BaseChart $chart)
    {
        $url = self::BASE_URL;

        // Now it needs to run through the data returned by getData() and form it into a URL

        // TYPE
        $url .= 'cht='. $chart->getType() .'&';

        // SIZE
        $url .= 'chs='. implode('x', $chart->getSize()->getDimensions()) .'&';

        // MARGINS
        if ($chart->getMargin()) {
            $url .= 'chma='. implode(',', $chart->getMargin()->getDimensions()) .'&';
        }

        // LEGEND
        if ($chartLegend = $chart->getLegend()) {
            //chdlp=<opt_position>|<opt_label_order>
            $url .= 'chdls='.$chartLegend->getColor()->getColor().','.$chartLegend->getFontSize().'&';
        }

        // TITLE
        if ($title = $chart->getTitle()) {
            $url .= 'chtt='. urlencode($title->getTitle()) .'&';

            if ($title->getColor()) {
                $url .= 'chts='. $title->getColor()->getColor() .'&';
            }
        }

        // SOME ELEMENT
        foreach ($chart->getElements() as $chartElement) {
            $url .= $chartElement->getKey() . '=' . $chartElement->render() .'&';
        }

        // DATA SETS
        // So there's several elements that might rely on a dataset
        // Start with a loop then refactor
        $data = [];
        $lineColors = [];
        $legendLabels = [];

        foreach ($chart->getDataSets() as $dataSet) {

            $data[] = implode(',', $dataSet->getData());

            // OK so how to handle the chart-specific elements?
            if ($dataSet->getColor()) {
                $lineColors[] = $dataSet->getColor()->getColor();
            }

            if ($legend = $dataSet->getLegend()) {
                $legendLabels[] = urlencode($legend->getLabel());
            }
        }

        // Dataset data
        $url .= 'chd=t:'. implode('|', $data) .'&';

        // Dataset colors
        if ($lineColors) {
            $url .= 'chco='. implode(',', $lineColors) .'&';
        }

        // Legend stuff
        if ($legendLabels) {
            $url .= 'chdl='. implode('|', $legendLabels) .'&';
        }

        return $url;
    }
}<commit_msg>Work in progress refactor of Image renderer<commit_after><?php

namespace Outspaced\GoogleChartMakerBundle\Chart\Renderer;

use Outspaced\GoogleChartMakerBundle\Chart\Charts\BaseChart;

/**
 * This library has been deprecated by Google, although the API is still available
 */
class Image
{
    /**
     * @var string
     */
    const BASE_URL = 'http://chart.googleapis.com/chart?';

    protected function renderType($type=NULL)
    {
        if ($type === NULL) {
            return '';
        } else {
            return 'cht='. $type .'&';
        }
    }

    protected function renderSize(array $size=[])
    {
        if (empty($size)) {
            return '';
        } else {
            return 'chs='.implode('x', $size).'&';
        }
    }

    protected function renderMargins(array $margins=[])
    {
        if (empty($margins)) {
            return '';
        } else {
            return 'chma='.implode(',', $margins).'&';
        }
    }

    protected function renderLegendLabels(array $legendLabels=[])
    {
        if ($legendLabels) {
            return 'chdl='. implode('|', $legendLabels) .'&';
        }

        return '';
    }

    protected function renderLineColors(array $lineColors=[])
    {
        if ($lineColors) {
            return 'chco='. implode(',', $lineColors) .'&';
        }

        return '';
    }

    protected function renderTitle($title, $color='')
    {
        $url = 'chtt='. urlencode($title) .'&';

        if ( ! empty($color)) {
            $url .= 'chts='. $color .'&';
        }

        return $url;
    }

    // Still not sure of best way to actually handle this - to reliably type hint, I
    // need to be sure that getLegend() will always return a legend
    protected function renderChartLegend($chartLegend=NULL)
    {
        if ($chartLegend !== NULL) {
            //chdlp=<opt_position>|<opt_label_order>
            return 'chdls='.$chartLegend->getColor()->getColor().','.$chartLegend->getFontSize().'&';
        }

        return '';
    }

    public function render(BaseChart $chart)
    {
        $url = self::BASE_URL;

        $url .= $this->renderType($chart->getType());
        $url .= $this->renderSize($chart->getSize()->getDimensions());

        if ($chart->getMargin()) {
            $url .= $this->renderMargins($chart->getMargin()->getDimensions());
        }

        $url .= $this->renderChartLegend($chart->getLegend());

        // TITLE
        if ($title = $chart->getTitle()) {
            $url .= $this->renderTitle($title->getTitle(), $title->getColor()->getColor());
        }

        // SOME ELEMENT
        foreach ($chart->getElements() as $chartElement) {
            $url .= $chartElement->getKey() . '=' . $chartElement->render() .'&';
        }

        // DATA SETS
        // So there's several elements that might rely on a dataset
        // Start with a loop then refactor
        $data = [];
        $lineColors = [];
        $legendLabels = [];

        foreach ($chart->getDataSets() as $dataSet) {

            $data[] = implode(',', $dataSet->getData());

            // OK so how to handle the chart-specific elements?
            if ($dataSet->getColor()) {
                $lineColors[] = $dataSet->getColor()->getColor();
            }

            if ($legend = $dataSet->getLegend()) {
                $legendLabels[] = urlencode($legend->getLabel());
            }
        }

        // Dataset data
        $url .= 'chd=t:'. implode('|', $data) .'&';

        $url .= $this->renderLineColors($lineColors);
        $url .= $this->renderLegendLabels($legendLabels);

        return $url;
    }
}
{'fraction_non_alphanumeric': 0.1749551606065547, 'fraction_numerical': 0.0, 'mean_word_length': 1.877110694183865, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '737251', 'n_tokens_mistral': 1863, 'n_tokens_neox': 1810, 'n_words': 386}