Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. /**
  2. * @param \Illuminate\Database\Eloquent\Model $model
  3. */
  4. protected function getPropertiesFromMethods($model)
  5. {
  6. $methods = get_class_methods($model);
  7. if ($methods) {
  8. foreach ($methods as $method) {
  9. if (Str::startsWith($method, 'get') && Str::endsWith(
  10. $method,
  11. 'Attribute'
  12. ) && $method !== 'getAttribute'
  13. ) {
  14. //Magic get<name>Attribute
  15. $name = Str::snake(substr($method, 3, -9));
  16. if (!empty($name)) {
  17. $reflection = new \ReflectionMethod($model, $method);
  18. $type = $this->getReturnTypeFromDocBlock($reflection);
  19. $this->setProperty($name, $type, true, null);
  20. }
  21. } elseif (Str::startsWith($method, 'set') && Str::endsWith(
  22. $method,
  23. 'Attribute'
  24. ) && $method !== 'setAttribute'
  25. ) {
  26. //Magic set<name>Attribute
  27. $name = Str::snake(substr($method, 3, -9));
  28. if (!empty($name)) {
  29. $this->setProperty($name, null, null, true);
  30. }
  31. } elseif (Str::startsWith($method, 'scope') && $method !== 'scopeQuery') {
  32. //Magic set<name>Attribute
  33. $name = Str::camel(substr($method, 5));
  34. if (!empty($name)) {
  35. $reflection = new \ReflectionMethod($model, $method);
  36. $args = $this->getParameters($reflection);
  37. //Remove the first ($query) argument
  38. array_shift($args);
  39. $this->setMethod($name, '\Illuminate\Database\Query\Builder|\\' . $reflection->class, $args);
  40. }
  41. } elseif (!method_exists('Illuminate\Database\Eloquent\Model', $method)
  42. && !Str::startsWith($method, 'get')
  43. ) {
  44. //Use reflection to inspect the code, based on Illuminate/Support/SerializableClosure.php
  45. $reflection = new \ReflectionMethod($model, $method);
  46. $file = new \SplFileObject($reflection->getFileName());
  47. $file->seek($reflection->getStartLine() - 1);
  48. $code = '';
  49. while ($file->key() < $reflection->getEndLine()) {
  50. $code .= $file->current();
  51. $file->next();
  52. }
  53. $code = trim(preg_replace('/\s\s+/', '', $code));
  54. $begin = strpos($code, 'function(');
  55. $code = substr($code, $begin, strrpos($code, '}') - $begin + 1);
  56. foreach (array(
  57. 'hasMany',
  58. 'hasManyThrough',
  59. 'belongsToMany',
  60. 'hasOne',
  61. 'belongsTo',
  62. 'morphOne',
  63. 'morphTo',
  64. 'morphMany',
  65. 'morphToMany'
  66. ) as $relation) {
  67. $search = '$this->' . $relation . '(';
  68. if ($pos = stripos($code, $search)) {
  69. //Resolve the relation's model to a Relation object.
  70. $relationObj = $model->$method();
  71. if ($relationObj instanceof Relation) {
  72. $relatedModel = '\\' . get_class($relationObj->getRelated());
  73. $relations = ['hasManyThrough', 'belongsToMany', 'hasMany', 'morphMany', 'morphToMany'];
  74. if (in_array($relation, $relations)) {
  75. //Collection or array of models (because Collection is Arrayable)
  76. $this->setProperty(
  77. $method,
  78. $this->getCollectionClass($relatedModel) . '|' . $relatedModel . '[]',
  79. true,
  80. null
  81. );
  82. } elseif ($relation === "morphTo") {
  83. // Model isn't specified because relation is polymorphic
  84. $this->setProperty(
  85. $method,
  86. '\Illuminate\Database\Eloquent\Model|\Eloquent',
  87. true,
  88. null
  89. );
  90. } else {
  91. //Single model is returned
  92. $this->setProperty($method, $relatedModel, true, null);
  93. }
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement