Determine if we're working with a one or many relationship

@param  string $model
@return string
435294
private function getRelationshipType($model)
    {
        // Poke method to check the type of instance
        $class = get_class($this->{$model}());
        $reflection = new ReflectionClass($class);

        return (in_array($reflection->getShortName(), ['BelongsTo', 'HasOne', 'MorphOne', 'MorphTo'])) ? 'one' : 'many';
    }
651412
public function getCommonAncestor($model)
    {
        if (empty($this->parentAttribute) || !is_string($this->parentAttribute) ||
            empty($model) || !$model->hasParent()) {
            return null;
        }
        $ancestors = $this->getAncestorChain();
        if (in_array($model->parent->getRefId(), $ancestors)) {
            return $model->parent;
        }
        return $this->getCommonAncestor($model->parent);
    }
396267
protected function _inferRelation($model, $name)
    {
        if (in_array($name . '_id', $model->getFields(), true)) {
            $referenceName = $this->_inferClassName($model, $name);
            return $referenceName ? [$referenceName, Relation::TYPE_HAS_ONE] : false;
        }

        if (preg_match('#^(.+[a-z\d])Of([A-Z].*)$#', $name, $match)) {
            if (!$singular = $this->_pluralToSingular($match[1])) {
                return false;
            }

            if (!$referenceName = $this->_inferClassName($model, $singular)) {
                return false;
            }

            $valueField = lcfirst($match[2]) . '_id';
            if (in_array($valueField, $model->getForeignKeys(), true)) {
                /** @var \ManaPHP\Model $reference */
                $reference = new $referenceName;
                return [$referenceName, Relation::TYPE_HAS_MANY_TO_MANY, $reference->getPrimaryKey(), $valueField];
            } else {
                return false;
            }
        }

        if ($singular = $this->_pluralToSingular($name)) {
            if (!$referenceName = $this->_inferClassName($model, $singular)) {
                return false;
            }

            /** @var \ManaPHP\Model $reference */
            $reference = new $referenceName;

            $keys = $model->getForeignKeys();
            if (count($keys) === 2) {
                $foreignKey = $singular . '_id';
                if (in_array($foreignKey, $keys, true)) {
                    $keys = array_flip($keys);
                    unset($keys[$foreignKey]);
                    return [$referenceName, Relation::TYPE_HAS_MANY_TO_MANY, $reference->getPrimaryKey(), key($keys)];
                }
            }
            if (in_array($model->getPrimaryKey(), $reference->getFields(), true)) {
                return [$referenceName, Relation::TYPE_HAS_MANY];
            } else {
                $r1Name = substr($referenceName, strrpos($referenceName, '\\') + 1);

                $modelName = get_class($model);
                $pos = strrpos($modelName, '\\');
                $baseName = substr($modelName, 0, $pos + 1);
                $r2Name = substr($modelName, $pos + 1);

                $tryViaName = $baseName . $r1Name . $r2Name;
                if (class_exists($tryViaName)) {
                    return [$referenceName, Relation::TYPE_HAS_MANY_VIA, $tryViaName, $model->getPrimaryKey()];
                } else {
                    $tryViaName = $baseName . $r2Name . $r1Name;
                    if (!class_exists($tryViaName)) {
                        throw new RuntimeException(['infer `:relation` relation failed', 'relation' => $name]);
                    }

                    return [$referenceName, Relation::TYPE_HAS_MANY_VIA, $tryViaName, $model->getPrimaryKey()];
                }
            }
        }

        return false;
    }
793767
public function isOwner(Model $Model) {
		if (!$Model->exists()) {
			return false;
		}

		if (!$owner = Hash::get($Model->data, $Model->alias . '.' . $this->settings[$Model->alias]['owner']['path'])) {
			$requireAuth = $Model->requireAuth;
			$Model->requireAuth = false;
			$owner = $Model->field(
				$this->settings[$Model->alias]['owner']['path'],
				array($Model->alias . '.' . $Model->primaryKey => $Model->getID())
			);
			$Model->requireAuth = $requireAuth;
		}

		return $this->getCurrentUser($Model, $this->settings[$Model->alias]['owner']['auth']) == $owner;
	}
310550
protected function getModelTable($model)
    {
        if (!is_object($model)) {
            $model = new $model;
        }

        return DB::getTablePrefix().$model->getTable();
    }
445416
protected function getValueFromModel($model)
    {
        if ( ! ($model instanceof Model)) {
            return null;
        }

        return get_class($model)
             . static::CLASS_AND_KEY_SEPARATOR
             . $model->getKey();
    }
1111196
protected function getModelPrimaryKeyString($model)
    {
        return is_array($model->getPrimaryKey()) ? implode('-', $model->getPrimaryKey()) : $model->getPrimaryKey();
    }
595525
public function applyToModel(Model $model)
    {
        if (! $this->onlyScopeRelations && ! is_null($this->scope)) {
            $model->scope = $this->scope;
        }

        return $model;
    }
917982
protected function getDefaultFor(Model $model)
    {
        if (! $this->withDefault) {
            return;
        }

        $instance = $this->related->newInstance();

        $foreignKey = $this->getForeignKeyName();

        if (is_array($foreignKey)) { //Check for multi-columns relationship
            foreach ($foreignKey as $index => $key) {
                $instance->setAttribute($key, $model->getAttribute($this->localKey[$index]));
            }
        } else {
            $instance->setAttribute($foreignKey, $model->getAttribute($this->localKey));
        }

        if (is_callable($this->withDefault)) {
            return call_user_func($this->withDefault, $instance) ?: $instance;
        }

        if (is_array($this->withDefault)) {
            $instance->forceFill($this->withDefault);
        }

        return $instance;
    }
1157516
public static function getModelClass($model): string
    {
        if (!isset(self::$modelClassMap[$model])) {
            // try to detect
            $className = '\\Application\\' . $model . '\\Table';

            if (!class_exists($className)) {
                throw new RelationNotFoundException("Related class for model `$model` not found");
            }
            self::$modelClassMap[$model] = $className;
        }
        return self::$modelClassMap[$model];
    }
892650
public function matchRelation($model, $results) {
        $data = [];
        foreach ($results as $item) {
            if (!$this->isMatchRelation($model, $item)) {
                continue;
            }
            if ($this->type == self::TYPE_ONE) {
                return $item;
            }
            $data[] = $item;
        }
        return $data;
    }
1251511
public static function get(Model $model)
    {
        return Mention::where('model_type', get_class($model))
            ->where('model_id', $model->getKey())
            ->get();
    }
1147567
public function modelSlug($model)
    {
        if (is_object($model)) {
            $model = get_class($model);
        }

        return str_replace('\\', '-', strtolower($model));
    }
2330
public function is($model)
    {
        return ! is_null($model) &&
               $this->getKey() === $model->getKey() &&
               $this->getTable() === $model->getTable() &&
               $this->getConnectionName() === $model->getConnectionName();
    }
431149
protected function showDetail(Model $model)
    {
        $name = $this->manager->getClassName($model);

        $this->title("Summary of Model {$name} :");

        $summary = $this->manager->getModelSummary($name);
        $rows = [];

        foreach ($summary as $key => $value) {
            if (is_bool($value)) {
                $value = $value ? 'yes' : 'no';
            }

            $valueText = $this->paintString($value, 'brown');

            $rows[] = [$key, $valueText];
        }

        $this->table(['Key', 'Value'], $rows);

        $this->showDatabaseFields($model);
        $this->showRelatoins($model);
        $this->showModifier($model);
    }
308601
private function isRelationship($model, $conditions)
    {
        /**
         * A valid relationship must:
         * - Be a method
         * - Not appear in the 'except' list
         * - Appear in the 'only' list (if used)
         * - Extend Illuminate\Database\Eloquent\Relations\Relation
         */
        return (
            method_exists($this, $model) &&
            $this->filterRelationshipByConditions($model, $conditions) &&
            is_subclass_of($this->{Str::camel($model)}(), 'Illuminate\Database\Eloquent\Relations\Relation')
        );
    }
224381
protected function getReference(Model $model)
    {
        $reference = $this->getReferenceValue($model);

        return $this->wrapWithLink($reference, $this->getLinkForReferenceModel($model));
    }