Нема описа

TinkerCaster.php 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace Laravel\Tinker;
  3. use Exception;
  4. use Symfony\Component\VarDumper\Caster\Caster;
  5. class TinkerCaster
  6. {
  7. /**
  8. * Application methods to include in the presenter.
  9. *
  10. * @var array
  11. */
  12. private static $appProperties = [
  13. 'configurationIsCached',
  14. 'environment',
  15. 'environmentFile',
  16. 'isLocal',
  17. 'routesAreCached',
  18. 'runningUnitTests',
  19. 'version',
  20. 'path',
  21. 'basePath',
  22. 'configPath',
  23. 'databasePath',
  24. 'langPath',
  25. 'publicPath',
  26. 'storagePath',
  27. 'bootstrapPath',
  28. ];
  29. /**
  30. * Get an array representing the properties of an application.
  31. *
  32. * @param \Illuminate\Foundation\Application $app
  33. * @return array
  34. */
  35. public static function castApplication($app)
  36. {
  37. $results = [];
  38. foreach (self::$appProperties as $property) {
  39. try {
  40. $val = $app->$property();
  41. if (! is_null($val)) {
  42. $results[Caster::PREFIX_VIRTUAL.$property] = $val;
  43. }
  44. } catch (Exception $e) {
  45. //
  46. }
  47. }
  48. return $results;
  49. }
  50. /**
  51. * Get an array representing the properties of a collection.
  52. *
  53. * @param \Illuminate\Support\Collection $collection
  54. * @return array
  55. */
  56. public static function castCollection($collection)
  57. {
  58. return [
  59. Caster::PREFIX_VIRTUAL.'all' => $collection->all(),
  60. ];
  61. }
  62. /**
  63. * Get an array representing the properties of a model.
  64. *
  65. * @param \Illuminate\Database\Eloquent\Model $model
  66. * @return array
  67. */
  68. public static function castModel($model)
  69. {
  70. $attributes = array_merge(
  71. $model->getAttributes(), $model->getRelations()
  72. );
  73. $visible = array_flip(
  74. $model->getVisible() ?: array_diff(array_keys($attributes), $model->getHidden())
  75. );
  76. $results = [];
  77. foreach (array_intersect_key($attributes, $visible) as $key => $value) {
  78. $results[(isset($visible[$key]) ? Caster::PREFIX_VIRTUAL : Caster::PREFIX_PROTECTED).$key] = $value;
  79. }
  80. return $results;
  81. }
  82. }