MockSplFileInfo.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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 Symfony\Component\Finder\Tests\Iterator;
  11. class MockSplFileInfo extends \SplFileInfo
  12. {
  13. const TYPE_DIRECTORY = 1;
  14. const TYPE_FILE = 2;
  15. const TYPE_UNKNOWN = 3;
  16. private $contents = null;
  17. private $mode = null;
  18. private $type = null;
  19. private $relativePath = null;
  20. private $relativePathname = null;
  21. public function __construct($param)
  22. {
  23. if (is_string($param)) {
  24. parent::__construct($param);
  25. } elseif (is_array($param)) {
  26. $defaults = array(
  27. 'name' => 'file.txt',
  28. 'contents' => null,
  29. 'mode' => null,
  30. 'type' => null,
  31. 'relativePath' => null,
  32. 'relativePathname' => null,
  33. );
  34. $defaults = array_merge($defaults, $param);
  35. parent::__construct($defaults['name']);
  36. $this->setContents($defaults['contents']);
  37. $this->setMode($defaults['mode']);
  38. $this->setType($defaults['type']);
  39. $this->setRelativePath($defaults['relativePath']);
  40. $this->setRelativePathname($defaults['relativePathname']);
  41. } else {
  42. throw new \RuntimeException(sprintf('Incorrect parameter "%s"', $param));
  43. }
  44. }
  45. public function isFile()
  46. {
  47. if (null === $this->type) {
  48. return false !== strpos($this->getFilename(), 'file');
  49. }
  50. return self::TYPE_FILE === $this->type;
  51. }
  52. public function isDir()
  53. {
  54. if (null === $this->type) {
  55. return false !== strpos($this->getFilename(), 'directory');
  56. }
  57. return self::TYPE_DIRECTORY === $this->type;
  58. }
  59. public function isReadable()
  60. {
  61. if (null === $this->mode) {
  62. return preg_match('/r\+/', $this->getFilename());
  63. }
  64. return preg_match('/r\+/', $this->mode);
  65. }
  66. public function getContents()
  67. {
  68. return $this->contents;
  69. }
  70. public function setContents($contents)
  71. {
  72. $this->contents = $contents;
  73. }
  74. public function setMode($mode)
  75. {
  76. $this->mode = $mode;
  77. }
  78. public function setType($type)
  79. {
  80. if (is_string($type)) {
  81. switch ($type) {
  82. case 'directory':
  83. case 'd':
  84. $this->type = self::TYPE_DIRECTORY;
  85. break;
  86. case 'file':
  87. case 'f':
  88. $this->type = self::TYPE_FILE;
  89. break;
  90. default:
  91. $this->type = self::TYPE_UNKNOWN;
  92. }
  93. } else {
  94. $this->type = $type;
  95. }
  96. }
  97. public function setRelativePath($relativePath)
  98. {
  99. $this->relativePath = $relativePath;
  100. }
  101. public function setRelativePathname($relativePathname)
  102. {
  103. $this->relativePathname = $relativePathname;
  104. }
  105. public function getRelativePath()
  106. {
  107. return $this->relativePath;
  108. }
  109. public function getRelativePathname()
  110. {
  111. return $this->relativePathname;
  112. }
  113. }