<commit_before><?php
namespace Hook\Database;

use Hook\Model\App as App;
use Hook\Database\Schema\Cache as Cache;

/**
 * Relationship
 * @author <NAME> <<EMAIL>>
 */
class Relationship
{

    public static function getRelation($model, $relation_name) {
        $schema = Cache::get($model->getTable());

        if (isset($schema['relationships'])) {
            // TODO: refactoring.
            // change the way to store relationships to prevent excessive loops
            foreach($schema['relationships'] as $relation => $fields) {
                foreach($fields as $field => $collection) {
                    if ($field == $relation_name || $collection == $relation_name) {
                        // TODO: '$collection' should be always a string
                        $related_collection_name = (is_array($collection)) ? $collection[0] : $collection;
                        $related_collection = App::collection($related_collection_name);
                        return static::getRelationInstance($model, $related_collection, $relation, $field);
                    }
                }
            }
        }

        return null;
    }

    /**
     * get
     *
     * @param mixed $model
     * @param string $relation_name
     *
     * @return \Illuminate\Database\Eloquent\Relations\Relation
     */
    public static function getRelationInstance($model, $related_collection, $relation_type, $field) {
        $model_table = str_singular($model->getTable());

        $related_model = $related_collection->getModel();
        $related_table = $related_model->getTable();
        $foreign_key = $field . '_id';

        // define relation model
        $related_klass = "Related" . ucfirst( str_singular( camel_case( $related_collection->getTable() ) ) );

        // TODO: refactoring
        // eval is evil. But it's necessary here since Eloquent\Model
        // will try to instantiate the 'related class' without constructor params.
        if (!class_exists($related_klass)) {
            eval("class {$related_klass} extends Hook\Model\Collection { protected \$table = '{$related_table}'; }");
        }

        switch ($relation_type) {
        case "belongs_to":<commit_msg>fix retrieving relationships from built-in models. closes #107<commit_after><?php
namespace Hook\Database;

use Hook\Model\App as App;
use Hook\Database\Schema\Cache as Cache;

/**
 * Relationship
 * @author <NAME> <<EMAIL>>
 */
class Relationship
{

    public static function getRelation($model, $relation_name) {
        $schema = Cache::get($model->getTable());

        if (isset($schema['relationships'])) {
            // TODO: refactoring.
            // change the way to store relationships to prevent excessive loops
            foreach($schema['relationships'] as $relation => $fields) {
                foreach($fields as $field => $collection) {
                    if ($field == $relation_name || $collection == $relation_name) {
                        // TODO: '$collection' should be always a string
                        $related_collection_name = (is_array($collection)) ? $collection[0] : $collection;
                        $related_collection = App::collection($related_collection_name);
                        return static::getRelationInstance($model, $related_collection, $relation, $field);
                    }
                }
            }
        }

        return null;
    }

    /**
     * get
     *
     * @param mixed $model
     * @param string $relation_name
     *
     * @return \Illuminate\Database\Eloquent\Relations\Relation
     */
    public static function getRelationInstance($model, $related_collection, $relation_type, $field) {
        $model_table = str_singular($model->getTable());

        $related_model = $related_collection->getModel();
        $related_table = $related_model->getTable();
        $foreign_key = $field . '_id';

        // define relation model
        $related_klass = "Related" . ucfirst( str_singular( camel_case( $related_collection->getTable() ) ) );

        // TODO: refactoring
        // eval is evil. But it's necessary here since Eloquent\Model
        // will try to instantiate the 'related class' without constructor params.
        if (!class_exists($related_klass)) {
            $related_model_class = get_class($related_model);
            eval("class {$related_klass} extends {$related_model_class} { protected \$table = '{$related_table}'; }");
        }

        switch ($relation_type) {
        case "belongs_to":
{'fraction_non_alphanumeric': 0.123342175066313, 'fraction_numerical': 0.0011052166224580018, 'mean_word_length': 2.339483394833948, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 23, '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': '4414344', 'n_tokens_mistral': 1217, 'n_tokens_neox': 1162, 'n_words': 286}