<commit_before><?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/zf2 for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 * @package   Zend_Mvc
 */

namespace Zend\Mvc\Controller\Plugin;

use Zend\Mvc\Controller\Plugin\AbstractPlugin;
use Zend\Mvc\Exception\RuntimeException;
use Zend\View\Model\JsonModel;
use Zend\View\Model\FeedModel;
use Zend\View\Model\ViewModel;
use Zend\View\Model\ModelInterface;
use Zend\Http\Request;
use Zend\Mvc\Exception;
use Zend\Mvc\InjectApplicationEventInterface;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\RouteStackInterface;
use Zend\Http\Header\Accept\FieldValuePart\AcceptFieldValuePart;
/**
 * @category   Zend
 * @package    Zend_Mvc
 * @subpackage Controller
 */
class AcceptedModel extends AbstractPlugin
{
    protected $event;
    protected $request;

    /**
     * Grabs a param from route match by default.
     *
     * @param string $param
     * @param mixed $default
     * @return ModelInterface
     */
    public function __invoke($param = null, $default = null)
    {
        $request = $this->getRequest();
        $headers = $request->getHeaders();

        if (!$headers->has('accept')) {
            return new ViewModel();
        }
        
        $accept = $headers->get('Accept');
        foreach ($accept->getPrioritized() as $acceptPart) {
            if ($acceptPart->getTypeString() == 'application/xhtml+xml' || $acceptPart->getTypeString() == 'text/html') {
                return new ViewModel();
            }

            if ($acceptPart->getTypeString() == 'application/json' || $acceptPart->getTypeString() == 'application/javascript') {
                return new JsonModel();
            }

            if ($acceptPart->getTypeString('application/rss+xml' || $acceptPart->getTypeString() == 'application/atom+xml')) {
                return new FeedModel();
            }
        }
        
        return new ViewModel();
    }

    /**
     * Get the request
     *
     * @return Request
     * @throws Exception\DomainException if unable to find request
     */
    protected function getRequest()
    {
        if ($this->request) {
            return $this->request;
        }

        $event = $this->getEvent();
        $request = $event->getRequest();
        if (!$request instanceof Request) {
            throw new Exception\DomainException('AcceptedModel plugin requires event compose a request');
        }
        $this->request = $request;
        return $this->request;
    }

    /**
     * Get the event
     *
     * @return MvcEvent
     * @throws Exception\DomainException if unable to find event
     */
    protected function getEvent()
    {
        if ($this->event) {
            return $this->event;
        }

        $controller = $this->getController();
        if (!$controller instanceof InjectApplicationEventInterface) {
            throw new Exception\DomainException('Redirect plugin requires a controller that implements InjectApplicationEventInterface');
        }

        $event = $controller->getEvent();
        if (!$event instanceof MvcEvent) {
            $params = $event->getParams();
            $event = new MvcEvent();
            $event->setParams($params);
        }
        $this->event = $event;

        return $this->event;
    }

}<commit_msg>cs + api doc + tweak<commit_after><?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/zf2 for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 * @package   Zend_Mvc
 */

namespace Zend\Mvc\Controller\Plugin;

use Zend\Http\Request;
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
use Zend\View\Model\JsonModel;
use Zend\View\Model\FeedModel;
use Zend\View\Model\ModelInterface;
use Zend\View\Model\ViewModel;
use Zend\Mvc\InjectApplicationEventInterface;
use Zend\Mvc\MvcEvent;

/**
 * @category   Zend
 * @package    Zend_Mvc
 * @subpackage Controller
 */
class AcceptedModel extends AbstractPlugin
{
    protected $event;
    protected $request;

    /**
     * Detects an appropriate model for view.
     *
     * @return ModelInterface
     */
    public function __invoke()
    {
        $request = $this->getRequest();
        $headers = $request->getHeaders();

        if (!$headers->has('accept')) {
            return new ViewModel();
        }

        $accept = $headers->get('Accept');
        foreach ($accept->getPrioritized() as $acceptPart) {
            if (in_array($acceptPart->getTypeString(), array('application/xhtml+xml', 'text/html', 'application/xml'))) {
                return new ViewModel();
            }

            if (in_array($acceptPart->getTypeString(), array('application/json', 'application/javascript'))) {
                return new JsonModel();
            }

            if (in_array($acceptPart->getTypeString(), array('application/rss+xml', 'application/atom+xml'))) {
                return new FeedModel();
            }
        }

        return new ViewModel();
    }

    /**
     * Get the request
     *
     * @return Request
     * @throws Exception\DomainException if unable to find request
     */
    protected function getRequest()
    {
        if ($this->request) {
            return $this->request;
        }

        $event = $this->getEvent();
        $request = $event->getRequest();
        if (!$request instanceof Request) {
            throw new Exception\DomainException('AcceptedModel plugin requires event compose a request');
        }
        $this->request = $request;

        return $this->request;
    }

    /**
     * Get the event
     *
     * @return MvcEvent
     * @throws Exception\DomainException if unable to find event
     */
    protected function getEvent()
    {
        if ($this->event) {
            return $this->event;
        }

        $controller = $this->getController();
        if (!$controller instanceof InjectApplicationEventInterface) {
            throw new Exception\DomainException('AcceptedModel plugin requires a controller that implements InjectApplicationEventInterface');
        }

        $event = $controller->getEvent();
        if (!$event instanceof MvcEvent) {
            $params = $event->getParams();
            $event = new MvcEvent();
            $event->setParams($params);
        }
        $this->event = $event;

        return $this->event;
    }

}
{'fraction_non_alphanumeric': 0.12811655151697207, 'fraction_numerical': 0.0027035145689396217, 'mean_word_length': 2.3580433686333837, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 7}, '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': '4655490', 'n_tokens_mistral': 1845, 'n_tokens_neox': 1795, 'n_words': 425}