PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/PHPSpec/Specification/Interceptor/Object.php

http://github.com/phpspec/phpspec
PHP | 149 lines | 82 code | 11 blank | 56 comment | 10 complexity | 72f306a23dd29a3f0921afa91ea9c93c MD5 | raw file
  1. <?php
  2. /**
  3. * PHPSpec
  4. *
  5. * LICENSE
  6. *
  7. * This file is subject to the GNU Lesser General Public License Version 3
  8. * that is bundled with this package in the file LICENSE.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.gnu.org/licenses/lgpl-3.0.txt
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@phpspec.net so we can send you a copy immediately.
  14. *
  15. * @category PHPSpec
  16. * @package PHPSpec
  17. * @copyright Copyright (c) 2007-2009 P??draic Brady, Travis Swicegood
  18. * @copyright Copyright (c) 2010-2012 P??draic Brady, Travis Swicegood,
  19. * Marcello Duarte
  20. * @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU Lesser General Public Licence Version 3
  21. */
  22. namespace PHPSpec\Specification\Interceptor;
  23. use \PHPSpec\Specification\Interceptor;
  24. /**
  25. * @category PHPSpec
  26. * @package PHPSpec
  27. * @copyright Copyright (c) 2007-2009 P??draic Brady, Travis Swicegood
  28. * @copyright Copyright (c) 2010-2012 P??draic Brady, Travis Swicegood,
  29. * Marcello Duarte
  30. * @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU Lesser General Public Licence Version 3
  31. */
  32. class Object extends Interceptor
  33. {
  34. /**
  35. * Accepted predicantes
  36. *
  37. * @var array
  38. */
  39. protected $_predicate = array('be' => 'is', 'have' => 'has');
  40. /**
  41. * Proxies call to specification and if method is a dsl call than it calls
  42. * the interceptor factory for the returned value
  43. *
  44. * @param string $method
  45. * @param array $args
  46. * @return \PHPSpec\Specification\Interceptor|boolean
  47. */
  48. public function __call($method, $args)
  49. {
  50. $dslResult = parent::__call($method, $args);
  51. if (!is_null($dslResult)) {
  52. return $dslResult;
  53. }
  54. if ($this->isPredicate('have', $method, $args) ||
  55. $this->isPredicate('be', $method, $args)) {
  56. $this->performMatching();
  57. return true;
  58. }
  59. $object = $this->getActualValue();
  60. if (method_exists($object, $method)) {
  61. return InterceptorFactory::create(
  62. call_user_func_array(array($object, $method), $args)
  63. );
  64. }
  65. $class = get_class($object);
  66. throw new \BadMethodCallException(
  67. "Call to undefined method {$class}::{$method}"
  68. );
  69. }
  70. /**
  71. * Checks whether a method is a predicate
  72. *
  73. * @param string $type
  74. * @param string $method
  75. * @param array $args
  76. * @return boolean
  77. */
  78. protected function isPredicate($type, $method, $args)
  79. {
  80. if (!in_array($type, array_keys($this->_predicate))) {
  81. return false;
  82. }
  83. if (strpos($method, $type) !== 0) {
  84. return false;
  85. }
  86. $plain = $this->_predicate[$type] . substr($method, strlen($type));
  87. $a = $this->_predicate[$type] . substr($method, strlen($type) + 1);
  88. $an = $this->_predicate[$type] . substr($method, strlen($type) + 2);
  89. switch (true) {
  90. case method_exists($this->_actualValue, $plain) :
  91. $predicate = $plain;
  92. break;
  93. case method_exists($this->_actualValue, $a) :
  94. if (strtolower(substr($method, strlen($type), 1)) !== 'a') {
  95. return false;
  96. }
  97. $predicate = $a;
  98. break;
  99. case method_exists($this->_actualValue, $an) :
  100. if (strtolower(substr($method, strlen($type), 2)) !== 'an') {
  101. return false;
  102. }
  103. $predicate = $an;
  104. break;
  105. default:
  106. return false;
  107. }
  108. $this->setExpectedValue($args);
  109. $this->createMatcher('beTrue');
  110. $this->setActualValue(
  111. call_user_func_array(array($this->_actualValue, $predicate), $args)
  112. );
  113. return true;
  114. }
  115. /**
  116. * Proxies call to specification and if argument is a dsl call than it calls
  117. * the interceptor factory for the returned value
  118. *
  119. * @param string $attribute
  120. * @return mixed
  121. */
  122. public function __get($attribute)
  123. {
  124. $dslResult = parent::__get($attribute);
  125. if (!is_null($dslResult)) {
  126. return $dslResult;
  127. }
  128. if (isset($this->getActualValue()->$attribute)) {
  129. return InterceptorFactory::create(
  130. $this->getActualValue()->$attribute
  131. );
  132. }
  133. trigger_error(
  134. "Undefined property: " . get_class($this->getActualValue()) .
  135. "::$attribute", E_USER_NOTICE
  136. );
  137. }
  138. }