PageRenderTime 55ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/Hamcrest-1.1.0/Hamcrest/Core/IsSet.php

https://bitbucket.org/txava/codeigniter-cloudsearch
PHP | 106 lines | 70 code | 11 blank | 25 comment | 6 complexity | c6f617edd39923bd0321e1be297e6408 MD5 | raw file
  1. <?php
  2. /*
  3. Copyright (c) 2010 hamcrest.org
  4. */
  5. require_once 'Hamcrest/BaseMatcher.php';
  6. require_once 'Hamcrest/Description.php';
  7. /**
  8. * Tests if a value (class, object, or array) has a named property.
  9. *
  10. * For example:
  11. * <pre>
  12. * assertThat(array('a', 'b'), set('b'));
  13. * assertThat($foo, set('bar'));
  14. * assertThat('Server', notSet('defaultPort'));
  15. * </pre>
  16. *
  17. * @todo Replace $property with a matcher and iterate all property names.
  18. */
  19. class Hamcrest_Core_IsSet extends Hamcrest_BaseMatcher
  20. {
  21. private $_property;
  22. private $_not;
  23. public function __construct($property, $not = false)
  24. {
  25. $this->_property = $property;
  26. $this->_not = $not;
  27. }
  28. public function matches($item)
  29. {
  30. if ($item === null) {
  31. return false;
  32. }
  33. $property = $this->_property;
  34. if (is_array($item)) {
  35. $result = isset($item[$property]);
  36. }
  37. elseif (is_object($item)) {
  38. $result = isset($item->$property);
  39. }
  40. elseif (is_string($item)) {
  41. $result = isset($item::$$property);
  42. }
  43. else {
  44. throw new InvalidArgumentException(
  45. 'Must pass an object, array, or class name');
  46. }
  47. return $this->_not ? !$result : $result;
  48. }
  49. public function describeTo(Hamcrest_Description $description)
  50. {
  51. $description->appendText(
  52. $this->_not
  53. ? 'unset property '
  54. : 'set property '
  55. )->appendText($this->_property);
  56. }
  57. public function describeMismatch($item,
  58. Hamcrest_Description $description)
  59. {
  60. if (!$this->_not) {
  61. $description->appendText('was not set');
  62. }
  63. else {
  64. $property = $this->_property;
  65. if (is_array($item)) {
  66. $value = $item[$property];
  67. }
  68. elseif (is_object($item)) {
  69. $value = $item->$property;
  70. }
  71. elseif (is_string($item)) {
  72. $value = $item::$$property;
  73. }
  74. parent::describeMismatch($value, $description);
  75. }
  76. }
  77. /**
  78. * Matches if value (class, object, or array) has named $property.
  79. *
  80. * @factory
  81. */
  82. public static function set($property)
  83. {
  84. return new self($property);
  85. }
  86. /**
  87. * Matches if value (class, object, or array) does not have named $property.
  88. *
  89. * @factory
  90. */
  91. public static function notSet($property)
  92. {
  93. return new self($property, true);
  94. }
  95. }