<commit_before><?php
class Vpc_Advanced_Amazon_Product_Component extends Vpc_Abstract
{
    public static function getSettings()
    {
        $ret = parent::getSettings();
        $ret['componentName'] = trlVps('Amazon.Product');
        $ret['ownModel'] = 'Vps_Component_FieldModel';

        $ret['associateTag'] = 'vps-21';
        return $ret;
    }

    public function getTemplateVars()
    {
        $ret = parent::getTemplateVars();

        static $productsByPage = array();
        $products = array();

        if (!isset($productsByPage[$this->getData()->parent->componentId])) {
            $all = $this->getData()->parent->getChildComponents(array(
                'componentClass' => $this->getData()->componentClass,
                'ignoreVisible' => true
            ));
            $asins = array();
            foreach ($all as $c) {
                $asins[] = $c->getComponent()->getRow()->asin;
            }
            $asinsChunks = array_chunk($asins, 10);
            foreach($asinsChunks as $chunk) {
                $amazon = new Vps_Service_Amazon();
                try {
                    $resultSet = $amazon->itemLookup(
                        implode($chunk,','), 
                        array(
                            'AssociateTag' => $this->_getSetting('associateTag'),
                            'ResponseGroup' => 'Small,ItemAttributes,Images'
                        )
                    );
                } catch (Zend_Service_Exception $e) {
                    $e = new Vps_Exception_Other($e);
                    $e->logOrThrow();
                    $resultSet = array();
                }
                foreach ($resultSet as $i) {
                    if(!is_null($i) && isset($i->ASIN)) { 
                        $products[$i->ASIN] = (object)array(
                            'item' => $i,
                            'title' => isset($i->Title) ? $i->Title : null,
                            'author' => isset($i->Author) ? (is_array($i->Author) ? implode($i->Author, ', ') : $i->Author) : null,
                            'asin' => $i->ASIN, 
                            'detailPageURL' => isset($i->DetailPageURL) ? $i->DetailPageURL : null,
                            'currencyCode' => isset($i->CurrencyCode) ? $i->CurrencyCode : null, 
                            'amount' => isset($i->Amount) ? $i->Amount : null, 
                            'formattedPrice' => isset($i->FormattedPrice) ? $i->FormattedPrice : null,
                            'salesRank' => isset($i->SalesRank) ? $i->SalesRank : null, 
                            'averageRating' => isset($i->AverageRating) ? $i->AverageRating : null
                        );
                    }
                }
            }

            $productsByPage[$this->getData()->parent->componentId] = $products;
        }

        $ret['product'] = null;
        if ($this->getRow()->asin) {
            if(isset($productsByPage[$this->getData()->parent->componentId][strtoupper($this->getRow()->asin)])) {
                $ret['product'] = $productsByPage[$this->getData()->parent->componentId][strtoupper($this->getRow()->asin)];
            }
        }

        return $ret;
    }<commit_msg>Fix when only one item is found where item is returned directly<commit_after><?php
class Vpc_Advanced_Amazon_Product_Component extends Vpc_Abstract
{
    public static function getSettings()
    {
        $ret = parent::getSettings();
        $ret['componentName'] = trlVps('Amazon.Product');
        $ret['ownModel'] = 'Vps_Component_FieldModel';

        $ret['associateTag'] = 'vps-21';
        return $ret;
    }

    public function getTemplateVars()
    {
        $ret = parent::getTemplateVars();

        static $productsByPage = array();
        $products = array();

        if (!isset($productsByPage[$this->getData()->parent->componentId])) {
            $all = $this->getData()->parent->getChildComponents(array(
                'componentClass' => $this->getData()->componentClass,
                'ignoreVisible' => true
            ));
            $asins = array();
            foreach ($all as $c) {
                $asins[] = $c->getComponent()->getRow()->asin;
            }
            $asinsChunks = array_chunk($asins, 10);
            foreach($asinsChunks as $chunk) {
                $amazon = new Vps_Service_Amazon();
                try {
                    $resultSet = $amazon->itemLookup(
                        implode($chunk,','), 
                        array(
                            'AssociateTag' => $this->_getSetting('associateTag'),
                            'ResponseGroup' => 'Small,ItemAttributes,Images'
                        )
                    );
                } catch (Zend_Service_Exception $e) {
                    $e = new Vps_Exception_Other($e);
                    $e->logOrThrow();
                    $resultSet = array();
                }
                if ($resultSet instanceof Vps_Service_Amazon_Item) {
                    $resultSet = array($resultSet);
                }
                foreach ($resultSet as $i) {
                    if(!is_null($i) && isset($i->ASIN)) { 
                        $products[$i->ASIN] = (object)array(
                            'item' => $i,
                            'title' => isset($i->Title) ? $i->Title : null,
                            'author' => isset($i->Author) ? (is_array($i->Author) ? implode($i->Author, ', ') : $i->Author) : null,
                            'asin' => $i->ASIN, 
                            'detailPageURL' => isset($i->DetailPageURL) ? $i->DetailPageURL : null,
                            'currencyCode' => isset($i->CurrencyCode) ? $i->CurrencyCode : null, 
                            'amount' => isset($i->Amount) ? $i->Amount : null, 
                            'formattedPrice' => isset($i->FormattedPrice) ? $i->FormattedPrice : null,
                            'salesRank' => isset($i->SalesRank) ? $i->SalesRank : null, 
                            'averageRating' => isset($i->AverageRating) ? $i->AverageRating : null
                        );
                    }
                }
            }

            $productsByPage[$this->getData()->parent->componentId] = $products;
        }

        $ret['product'] = null;
        if ($this->getRow()->asin) {
            if(isset($productsByPage[$this->getData()->parent->componentId][strtoupper($this->getRow()->asin)])) {
                $ret['product'] = $productsByPage[$this->getData()->parent->componentId][strtoupper($this->getRow()->asin)];
            }
        }

        return $ret;
    }
{'fraction_non_alphanumeric': 0.15429775703748305, 'fraction_numerical': 0.0012042751768779167, 'mean_word_length': 1.4893218433870363, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 131, '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': '7046413', 'n_tokens_mistral': 1825, 'n_tokens_neox': 1757, 'n_words': 275}