<commit_before><?php

namespace Contentacle\Resources;

class Resource extends \Tonic\Resource
{
    protected $container;
    private $extension = '';

    function __construct($app, $request)
    {
        parent::__construct($app, $request);
        $this->params['embed'] = true;
    }

    function setContainer($container)
    {
        $this->container = $container;
    }

    protected function provides($mimetype)
    {
        if (count($this->request->accept) == 0) return 0;

        $altMimetype = null;
        if (substr($mimetype, -4) == 'yaml') {
            $altMimetype = 'text/yaml';
        } elseif (substr($mimetype, -4) == 'json') {
            $altMimetype = 'application/json';
        }

        $pos = array_search($mimetype, $this->request->accept);
        $altPos = array_search($altMimetype, $this->request->accept);
        if ($pos === FALSE && $altPos === FALSE) {
            if (in_array('*/*', $this->request->accept)) {
                return 0;
            } else {
                throw new \Tonic\NotAcceptableException('No matching method for response type "'.join(', ', $this->request->accept).'"');
            }
        } else {
            $this->after(function ($response) use ($mimetype) {
                $response->contentType = $mimetype;
            });
            return count($this->request->accept) - $pos;
        }
    }

    protected function fixPath($path, $username, $repoName, $branchName, $pathType = 'documents')
    {
        if ($path === true) {
            $path = '';
        } elseif (isset($_SERVER['REQUEST_URI'])) {
            return substr($_SERVER['REQUEST_URI'], strlen('/users/'.$username.'/repos/'.$repoName.'/branches/'.$branchName.'/'.$pathType.'/'));
        }
        return $path;
    }

    protected function getChildResource($resourceName, $parameters, $embedChildren = false)
    {
        $resource = new $resourceName($this->app, $this->request);
        $resource->setContainer($this->container);
        $resource->embed = $embedChildren;
        $response = call_user_func_array(array($resource, 'get'), $parameters);
        return $response->body;
    }

    protected function formatExtension($prefix = '.')
    {
        if (isset($this->request->accept[0])) {
            switch ($this->request->accept[0]) {
            case 'application/yaml':
            case 'text/yaml':<commit_msg>Be cleverer about custom provides method so as to handle partial format matches (+yaml etc)<commit_after><?php

namespace Contentacle\Resources;

class Resource extends \Tonic\Resource
{
    protected $container;
    private $extension = '';

    function __construct($app, $request)
    {
        parent::__construct($app, $request);
        $this->params['embed'] = true;
    }

    function setContainer($container)
    {
        $this->container = $container;
    }

    protected function provides($mimetype)
    {
        if (count($this->request->getAccept()) == 0) return 0;

        $match = null;
        $pos = 0;

        foreach ($this->request->getAccept() as $acceptMimetype) {
            if ($acceptMimetype == $mimetype) {
                $match = $pos;
                break;
            } else {
                $format = substr($acceptMimetype, strrpos($acceptMimetype, '/') + 1);
                if (substr($mimetype, -strlen($format)) == $format) {
                    $match = $pos;
                }
            }
            $pos++;
        }
        if ($match !== null) {
            $this->after(function ($response) use ($mimetype) {
                $response->contentType = $mimetype;
            });

            return count($this->request->getAccept()) - $match;
        }
    }

    protected function fixPath($path, $username, $repoName, $branchName, $pathType = 'documents')
    {
        if ($path === true) {
            $path = '';
        } elseif (isset($_SERVER['REQUEST_URI'])) {
            return substr($_SERVER['REQUEST_URI'], strlen('/users/'.$username.'/repos/'.$repoName.'/branches/'.$branchName.'/'.$pathType.'/'));
        }
        return $path;
    }

    protected function getChildResource($resourceName, $parameters, $embedChildren = false)
    {
        $resource = new $resourceName($this->app, $this->request);
        $resource->setContainer($this->container);
        $resource->embed = $embedChildren;
        $response = call_user_func_array(array($resource, 'get'), $parameters);
        return $response->body;
    }

    protected function formatExtension($prefix = '.')
    {
        if (isset($this->request->accept[0])) {
            switch ($this->request->accept[0]) {
            case 'application/yaml':
            case 'text/yaml':
{'fraction_non_alphanumeric': 0.16205281090289608, 'fraction_numerical': 0.002768313458262351, 'mean_word_length': 2.21492128678987, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, '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': '7135334', 'n_tokens_mistral': 1326, 'n_tokens_neox': 1302, 'n_words': 260}