<commit_before><?php

namespace Trellis\StaticLdp\Model;

use Silex\Application;
use Symfony\Component\HttpFoundation\Request;

abstract class Resource
{
    const LDP_NS = "http://www.w3.org/ns/ldp#";
    const RDF_NS = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
    const DCTERMS_NS = "http://purl.org/dc/terms/";

    /**
     * Create a LDP Resource
     * @param $path
     *    the file path to the resource
     * @param $formats array
     *    the supported RDF formats
     */
    protected function __construct($path, $formats)
    {
        $this->path = $path;
        $this->formats = $formats;
    }

    /**
     * Get a representation of the given resource
     *
     * @param \Silex\Application $app
     *   The Silex application.
     * @param \Symfony\Component\HttpFoundation\Request $request
     *   The incoming request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    abstract public function respond(Application $app, Request $request);

    /**
     * Compute the SHA1 checksum of a file
     * @return string
     *    The SHA1 checksum of the file
     */
    public function sha1()
    {
        if (is_file($this->path)) {
            return sha1_file($this->path);
        }
        return null;
    }

    /**
     * Apply a want-digest header, if applicable
     *
     * @param $wantDigestHeader
     *      The Want-Digest header
     * @return string
     *      The instance digest, if relevant
     */
    protected function wantDigest($wantDigestHeader)
    {
        if ($wantDigestHeader) {
            $maxQVal = 0.0;
            $bestAlgorithm = null;
            foreach (explode(",", $wantDigestHeader) as $algorithm) {
                $parts = explode(";", $algorithm);
                $qVal = 1.0;
                if (count($parts) > 1 && strpos($parts[1], "q=") == 0) {
                    $qVal = floatval(trim(substr($parts[1], 2)));
                }
                $alg = strtolower(trim($parts[0]));
                if ($qVal > $maxQVal && in_array($alg, ["md5", "sha1"])) {
                    $bestAlgorithm = $alg;
                    $maxQVal = $qVal;
                }
            }
            switch($bestAlgorithm) {
                case "md5":
                    return "md5=" . $this->md5();
                case "sha1":
                    return "sha1=" . $this->sha1();
            }
        }
        return null;
    }

    /**
     * Compute the MD5 checksum of a file
     * @return string
     *    The MD5 checksum of the file
     */
    public function md5()
    {
        if (is_file($this->path)) {
            return md5_file($this->path);
        }
        return null;<commit_msg>Fix strpos comparison<commit_after><?php

namespace Trellis\StaticLdp\Model;

use Silex\Application;
use Symfony\Component\HttpFoundation\Request;

abstract class Resource
{
    const LDP_NS = "http://www.w3.org/ns/ldp#";
    const RDF_NS = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
    const DCTERMS_NS = "http://purl.org/dc/terms/";

    /**
     * Create a LDP Resource
     * @param $path
     *    the file path to the resource
     * @param $formats array
     *    the supported RDF formats
     */
    protected function __construct($path, $formats)
    {
        $this->path = $path;
        $this->formats = $formats;
    }

    /**
     * Get a representation of the given resource
     *
     * @param \Silex\Application $app
     *   The Silex application.
     * @param \Symfony\Component\HttpFoundation\Request $request
     *   The incoming request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    abstract public function respond(Application $app, Request $request);

    /**
     * Compute the SHA1 checksum of a file
     * @return string
     *    The SHA1 checksum of the file
     */
    public function sha1()
    {
        if (is_file($this->path)) {
            return sha1_file($this->path);
        }
        return null;
    }

    /**
     * Apply a want-digest header, if applicable
     *
     * @param $wantDigestHeader
     *      The Want-Digest header
     * @return string
     *      The instance digest, if relevant
     */
    protected function wantDigest($wantDigestHeader)
    {
        if ($wantDigestHeader) {
            $maxQVal = 0.0;
            $bestAlgorithm = null;
            foreach (explode(",", $wantDigestHeader) as $algorithm) {
                $parts = explode(";", $algorithm);
                $qVal = 1.0;
                if (count($parts) > 1 && strpos($parts[1], "q=") === 0) {
                    $qVal = floatval(trim(substr($parts[1], 2)));
                }
                $alg = strtolower(trim($parts[0]));
                if ($qVal > $maxQVal && in_array($alg, ["md5", "sha1"])) {
                    $bestAlgorithm = $alg;
                    $maxQVal = $qVal;
                }
            }
            switch($bestAlgorithm) {
                case "md5":
                    return "md5=" . $this->md5();
                case "sha1":
                    return "sha1=" . $this->sha1();
            }
        }
        return null;
    }

    /**
     * Compute the MD5 checksum of a file
     * @return string
     *    The MD5 checksum of the file
     */
    public function md5()
    {
        if (is_file($this->path)) {
            return md5_file($this->path);
        }
        return null;
{'fraction_non_alphanumeric': 0.12752923705216263, 'fraction_numerical': 0.013365509560051976, 'mean_word_length': 1.6424717999019127, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '1965761', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1606, 'n_words': 387}