Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @param \Illuminate\Database\Eloquent\Model $model
- */
- protected function getPropertiesFromMethods($model)
- {
- $methods = get_class_methods($model);
- if ($methods) {
- foreach ($methods as $method) {
- if (Str::startsWith($method, 'get') && Str::endsWith(
- $method,
- 'Attribute'
- ) && $method !== 'getAttribute'
- ) {
- //Magic get<name>Attribute
- $name = Str::snake(substr($method, 3, -9));
- if (!empty($name)) {
- $reflection = new \ReflectionMethod($model, $method);
- $type = $this->getReturnTypeFromDocBlock($reflection);
- $this->setProperty($name, $type, true, null);
- }
- } elseif (Str::startsWith($method, 'set') && Str::endsWith(
- $method,
- 'Attribute'
- ) && $method !== 'setAttribute'
- ) {
- //Magic set<name>Attribute
- $name = Str::snake(substr($method, 3, -9));
- if (!empty($name)) {
- $this->setProperty($name, null, null, true);
- }
- } elseif (Str::startsWith($method, 'scope') && $method !== 'scopeQuery') {
- //Magic set<name>Attribute
- $name = Str::camel(substr($method, 5));
- if (!empty($name)) {
- $reflection = new \ReflectionMethod($model, $method);
- $args = $this->getParameters($reflection);
- //Remove the first ($query) argument
- array_shift($args);
- $this->setMethod($name, '\Illuminate\Database\Query\Builder|\\' . $reflection->class, $args);
- }
- } elseif (!method_exists('Illuminate\Database\Eloquent\Model', $method)
- && !Str::startsWith($method, 'get')
- ) {
- //Use reflection to inspect the code, based on Illuminate/Support/SerializableClosure.php
- $reflection = new \ReflectionMethod($model, $method);
- $file = new \SplFileObject($reflection->getFileName());
- $file->seek($reflection->getStartLine() - 1);
- $code = '';
- while ($file->key() < $reflection->getEndLine()) {
- $code .= $file->current();
- $file->next();
- }
- $code = trim(preg_replace('/\s\s+/', '', $code));
- $begin = strpos($code, 'function(');
- $code = substr($code, $begin, strrpos($code, '}') - $begin + 1);
- foreach (array(
- 'hasMany',
- 'hasManyThrough',
- 'belongsToMany',
- 'hasOne',
- 'belongsTo',
- 'morphOne',
- 'morphTo',
- 'morphMany',
- 'morphToMany'
- ) as $relation) {
- $search = '$this->' . $relation . '(';
- if ($pos = stripos($code, $search)) {
- //Resolve the relation's model to a Relation object.
- $relationObj = $model->$method();
- if ($relationObj instanceof Relation) {
- $relatedModel = '\\' . get_class($relationObj->getRelated());
- $relations = ['hasManyThrough', 'belongsToMany', 'hasMany', 'morphMany', 'morphToMany'];
- if (in_array($relation, $relations)) {
- //Collection or array of models (because Collection is Arrayable)
- $this->setProperty(
- $method,
- $this->getCollectionClass($relatedModel) . '|' . $relatedModel . '[]',
- true,
- null
- );
- } elseif ($relation === "morphTo") {
- // Model isn't specified because relation is polymorphic
- $this->setProperty(
- $method,
- '\Illuminate\Database\Eloquent\Model|\Eloquent',
- true,
- null
- );
- } else {
- //Single model is returned
- $this->setProperty($method, $relatedModel, true, null);
- }
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement