gtCodeSnippet.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Retrieves code snippets for adding to test cases
  4. *
  5. */
  6. class gtCodeSnippet
  7. {
  8. /**
  9. * get the code snippet and initialise an array with it
  10. *
  11. * @param string $name
  12. * @return array
  13. */
  14. public static function get($name) {
  15. $filename = dirname(__FILE__) . '/codeSnippets/' . $name . '.txt';
  16. if (!file_exists($filename)) {
  17. throw new LogicException('The code snippet ' . $name . ' does not exist');
  18. }
  19. $lines = file($filename);
  20. foreach($lines as $l) {
  21. $array[] = rtrim($l);
  22. }
  23. return $array;
  24. }
  25. /**
  26. * Append the code snippet on to an existing array
  27. *
  28. * @param string $name
  29. * @param array $array
  30. * @return array
  31. */
  32. public static function append($name, $array) {
  33. $filename = dirname(__FILE__) . '/codeSnippets/' . $name . '.txt';
  34. if (!file_exists($filename)) {
  35. throw new LogicException('The code snippet ' . $name . ' does not exist');
  36. }
  37. $text = file($filename);
  38. foreach ($text as $t) {
  39. $array[] = rtrim($t);
  40. }
  41. return $array;
  42. }
  43. /**
  44. * Appends blank entries on to an array
  45. *
  46. * @param int $numberOfLines
  47. * @param array $array
  48. * @return array
  49. */
  50. public static function appendBlankLines($numberOfLines, $array) {
  51. for ($i=0; $i< $numberOfLines; $i++) {
  52. $array[] = "";
  53. }
  54. return $array;
  55. }
  56. }
  57. ?>