<commit_before><?php

namespace mheinzerling\entity;


use mheinzerling\commons\FileUtils;
use mheinzerling\commons\StringUtils;

class PhpSnippets
{
    public static function repository($name, $namespace)
    {
        $result = self::header($namespace);
        $result .= "class " . $name . "Repository extends Base" . $name . "Repository\n";
        $result .= "{\n";
        $result .= "}";
        return $result;
    }

    public static function entity($name, $namespace)
    {
        $result = self::header($namespace);
        $result .= "class " . $name . " extends Base" . $name . "\n";
        $result .= "{\n";
        $result .= "}";
        return $result;
    }

    private static function header($namespace)
    {
        $result = "<?php\n";
        if (!empty($namespace)) $result .= "namespace $namespace;\n\n";
        return $result;
    }

    public static function baserepository($name, $properties)
    {
        $ns = isset($properties['namespace']) ? $properties['namespace'] : "";
        $fields = '';
        $auto = null;
        $pk = array();
        $unique = "";
        foreach ($properties as $field => $property) {
            if ($field == 'namespace') continue;
            if ($field == 'unique') {
                foreach ($property as $iname => $f) {
                    $unique .= "'" . $iname . "'=>array('" . implode("', '", $f) . "'),";
                }
                if (strlen($unique) > 0) $unique = substr($unique, 0, -1);
                continue;
            }
            if (isset($property['primary']) && $property['primary']) $pk[] = $field;
            if (isset($property['auto']) && $property['auto']) {
                $auto = $field;
            }
            $p = '';
            foreach ($property as $key => $value) {
                if (is_array($value)) {
                    $arr = "array('";
                    foreach ($value as $k => $v) {
                        $arr .= $k . "' => '" . $v . "', '";
                    }
                    $arr = substr($arr, 0, -4);
                    $arr .= "')";
                    $value = $arr;
                } elseif (is_bool($value)) {
                    $value = $value ? 'true' : 'false';
                } else if (is_string($value)) {
                    $value = "'" . $value . "'";
                }
                $p .= "'$key' => " . $value . ", ";
            }
            $p = substr($p, 0, -2);
            $fields .= "                '$field' => array(" . $p . "),\n";
        }
        $fields = substr($fields, 0, -2);

        $result = self::header($ns);
        $result .= "use mheinzerling\\entity\\EntityRepository;\n\n";
        $result .= "class Base" . $name . "Repository extends EntityRepository\n";
        $result .= "{\n";
        $result .= "    public function getMeta()\n";
        $result .= "    {\n";

        $result .= "        return array(\n";
        $result .= "            'name' => '" . $name . "',\n";
        $result .= "            'repoClass' => '" . $ns . "\\" . $name . "Repository',\n";
        $result .= "            'baseClass' => '" . $ns . "\\Base" . $name . "',\n";
        $result .= "            'entityClass' => '" . $ns . "\\" . $name . "',\n";
        $result .= "            'namespace' => '" . $ns . "',\n";
        $result .= "            'table' => '" . strtolower($name) . "',\n";
        $result .= "            'fields' => array(\n";
        $result .= "" . $fields . "),\n";
        $result .= "            'pk' => array('" . implode("', '", $pk) . "'),\n";
        $result .= "            'unique' => array(" . $unique . "),\n";
        $result .= "            'autoincrement' => " . ($auto == null ? "null" : "'$auto'") . "\n";
        $result .= "        );\n";
        $result .= "    }\n";

        if (count($pk) > 0) {
            $upk = array_map('mheinzerling\commons\StringUtils::firstCharToUpper', $pk);
            $binding = array_map(function ($n) {
                return "`$n`=:$n";
            }, $pk);
            $array = array_map(function ($n) {
                return "'$n'=>\$$n";
            }, $pk);
            $result .= "\n";
            $result .= "    /**\n";
            $result .= "     * @return $name\n";
            $result .= "     */\n";
            $result .= "    public function fetchBy" . implode("And", $upk) . "(\$" . implode(", \$", $pk) . ")\n";
            $result .= "    {\n";
            $result .= "        return \$this->fetchUnique(\"WHERE " . implode(" AND ", $binding) . "\", array(" . implode(", ", $array) . "));\n";
            $result .= "    }\n";
        }

        $result .= "}";
        return $result;
    }

    public static function base($name, $properties, $foreignKeys, $enums)
    {
        $qualifiedEnums = array();
        foreach ($enums as $key => $property) {<commit_msg>fix "no primary key"<commit_after><?php

namespace mheinzerling\entity;


use mheinzerling\commons\FileUtils;
use mheinzerling\commons\StringUtils;

class PhpSnippets
{
    public static function repository($name, $namespace)
    {
        $result = self::header($namespace);
        $result .= "class " . $name . "Repository extends Base" . $name . "Repository\n";
        $result .= "{\n";
        $result .= "}";
        return $result;
    }

    public static function entity($name, $namespace)
    {
        $result = self::header($namespace);
        $result .= "class " . $name . " extends Base" . $name . "\n";
        $result .= "{\n";
        $result .= "}";
        return $result;
    }

    private static function header($namespace)
    {
        $result = "<?php\n";
        if (!empty($namespace)) $result .= "namespace $namespace;\n\n";
        return $result;
    }

    public static function baserepository($name, $properties)
    {
        $ns = isset($properties['namespace']) ? $properties['namespace'] : "";
        $fields = '';
        $auto = null;
        $pk = array();
        $unique = "";
        foreach ($properties as $field => $property) {
            if ($field == 'namespace') continue;
            if ($field == 'unique') {
                foreach ($property as $iname => $f) {
                    $unique .= "'" . $iname . "'=>array('" . implode("', '", $f) . "'),";
                }
                if (strlen($unique) > 0) $unique = substr($unique, 0, -1);
                continue;
            }
            if (isset($property['primary']) && $property['primary']) $pk[] = $field;
            if (isset($property['auto']) && $property['auto']) {
                $auto = $field;
            }
            $p = '';
            foreach ($property as $key => $value) {
                if (is_array($value)) {
                    $arr = "array('";
                    foreach ($value as $k => $v) {
                        $arr .= $k . "' => '" . $v . "', '";
                    }
                    $arr = substr($arr, 0, -4);
                    $arr .= "')";
                    $value = $arr;
                } elseif (is_bool($value)) {
                    $value = $value ? 'true' : 'false';
                } else if (is_string($value)) {
                    $value = "'" . $value . "'";
                }
                $p .= "'$key' => " . $value . ", ";
            }
            $p = substr($p, 0, -2);
            $fields .= "                '$field' => array(" . $p . "),\n";
        }
        $fields = substr($fields, 0, -2);

        $result = self::header($ns);
        $result .= "use mheinzerling\\entity\\EntityRepository;\n\n";
        $result .= "class Base" . $name . "Repository extends EntityRepository\n";
        $result .= "{\n";
        $result .= "    public function getMeta()\n";
        $result .= "    {\n";

        $result .= "        return array(\n";
        $result .= "            'name' => '" . $name . "',\n";
        $result .= "            'repoClass' => '" . $ns . "\\" . $name . "Repository',\n";
        $result .= "            'baseClass' => '" . $ns . "\\Base" . $name . "',\n";
        $result .= "            'entityClass' => '" . $ns . "\\" . $name . "',\n";
        $result .= "            'namespace' => '" . $ns . "',\n";
        $result .= "            'table' => '" . strtolower($name) . "',\n";
        $result .= "            'fields' => array(\n";
        $result .= "" . $fields . "),\n";
		$pks="'" . implode("', '", $pk) . "'";
        $result .= "            'pk' => array(".(count($pk)>0?$pks:'')."),\n";
        $result .= "            'unique' => array(" . $unique . "),\n";
        $result .= "            'autoincrement' => " . ($auto == null ? "null" : "'$auto'") . "\n";
        $result .= "        );\n";
        $result .= "    }\n";

        if (count($pk) > 0) {
            $upk = array_map('mheinzerling\commons\StringUtils::firstCharToUpper', $pk);
            $binding = array_map(function ($n) {
                return "`$n`=:$n";
            }, $pk);
            $array = array_map(function ($n) {
                return "'$n'=>\$$n";
            }, $pk);
            $result .= "\n";
            $result .= "    /**\n";
            $result .= "     * @return $name\n";
            $result .= "     */\n";
            $result .= "    public function fetchBy" . implode("And", $upk) . "(\$" . implode(", \$", $pk) . ")\n";
            $result .= "    {\n";
            $result .= "        return \$this->fetchUnique(\"WHERE " . implode(" AND ", $binding) . "\", array(" . implode(", ", $array) . "));\n";
            $result .= "    }\n";
        }

        $result .= "}";
        return $result;
    }

    public static function base($name, $properties, $foreignKeys, $enums)
    {
        $qualifiedEnums = array();
        foreach ($enums as $key => $property) {
{'fraction_non_alphanumeric': 0.21425636811832374, 'fraction_numerical': 0.0021569433032046013, 'mean_word_length': 1.3941480206540446, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 50, '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': '5700920', 'n_tokens_mistral': 2974, 'n_tokens_neox': 2869, 'n_words': 629}