ArgumentFormatter.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /**
  3. * This file is part of Collision.
  4. *
  5. * (c) Nuno Maduro <enunomaduro@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace NunoMaduro\Collision;
  11. use NunoMaduro\Collision\Contracts\ArgumentFormatter as ArgumentFormatterContract;
  12. /**
  13. * This is an Collision Argument Formatter implementation.
  14. *
  15. * @author Nuno Maduro <enunomaduro@gmail.com>
  16. */
  17. class ArgumentFormatter implements ArgumentFormatterContract
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function format(array $arguments, bool $recursive = true): string
  23. {
  24. $result = [];
  25. foreach ($arguments as $argument) {
  26. switch (true) {
  27. case is_string($argument):
  28. $result[] = '"'.$argument.'"';
  29. break;
  30. case is_array($argument):
  31. $associative = array_keys($argument) !== range(0, count($argument) - 1);
  32. if ($recursive && $associative && count($argument) <= 5) {
  33. $result[] = '['.$this->format($argument, false).']';
  34. }
  35. break;
  36. case is_object($argument):
  37. $class = get_class($argument);
  38. $result[] = "Object($class)";
  39. break;
  40. }
  41. }
  42. return implode(', ', $result);
  43. }
  44. }