PageRenderTime 4629ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/OData Producer for PHP/library/ODataProducer/Providers/Metadata/Type/String.php

#
PHP | 124 lines | 37 code | 7 blank | 80 comment | 3 complexity | 6946ddf9e914316dcf15c6761a463924 MD5 | raw file
  1. <?php
  2. /**
  3. * Type to represent String
  4. *
  5. * PHP version 5.3
  6. *
  7. * @category ODataProducer
  8. * @package ODataProducer_Providers_Metadata_Type
  9. * @author Anu T Chandy <odataphpproducer_alias@microsoft.com>
  10. * @copyright 2011 Microsoft Corp. (http://www.microsoft.com)
  11. * @license New BSD license, (http://www.opensource.org/licenses/bsd-license.php)
  12. * @version SVN: 1.0
  13. * @link http://odataphpproducer.codeplex.com
  14. *
  15. */
  16. namespace ODataProducer\Providers\Metadata\Type;
  17. /**
  18. * Type to represent String
  19. *
  20. * @category ODataProducer
  21. * @package ODataProducer_Providers_Metadata_Type
  22. * @author Anu T Chandy <odataphpproducer_alias@microsoft.com>
  23. * @copyright 2011 Microsoft Corp. (http://www.microsoft.com)
  24. * @license New BSD license, (http://www.opensource.org/licenses/bsd-license.php)
  25. * @version Release: 1.0
  26. * @link http://odataphpproducer.codeplex.com
  27. */
  28. class String implements IType
  29. {
  30. /**
  31. * Gets the type code
  32. * Note: implementation of IType::getTypeCode
  33. *
  34. * @return TypeCode
  35. */
  36. public function getTypeCode()
  37. {
  38. return TypeCode::STRING;
  39. }
  40. /**
  41. * Checks this type (String) is compactible with another type
  42. * Note: implementation of IType::isCompatibleWith
  43. *
  44. * @param IType $type Type to check compactibility
  45. *
  46. * @return boolean
  47. */
  48. public function isCompatibleWith(IType $type)
  49. {
  50. return ($type->getTypeCode() == TypeCode::STRING);
  51. }
  52. /**
  53. * Validate a value in Astoria uri is in a format for this type
  54. * Note: implementation of IType::validate
  55. *
  56. * @param string $value The value to validate
  57. * @param string &$outValue The stripped form of $value that can
  58. * be used in PHP expressions
  59. *
  60. * @return boolean
  61. */
  62. public function validate($value, &$outValue)
  63. {
  64. if (!is_string($value)) {
  65. return false;
  66. }
  67. $outValue = $value;
  68. return true;
  69. }
  70. /**
  71. * Gets full name of this type in EDM namespace
  72. * Note: implementation of IType::getFullTypeName
  73. *
  74. * @return string
  75. */
  76. public function getFullTypeName()
  77. {
  78. return 'Edm.String';
  79. }
  80. /**
  81. * Converts the given string value to string type.
  82. *
  83. * @param string $stringValue value to convert.
  84. *
  85. * @return string
  86. */
  87. public function convert($stringValue)
  88. {
  89. //Consider the odata url option
  90. //$filter=ShipName eq 'Antonio%20Moreno%20Taquer%C3%ADa'
  91. //WebOperationContext will do urldecode, so the clause become
  92. //$filter=ShipName eq 'Antonio Moreno Taquería', the lexer will
  93. //give the token as
  94. //Token {Text string(25):'Antonio Moreno Taquería', Id: String},
  95. //this function is used to remove the pre-post quotes from Token::Text
  96. //i.e. 'Antonio Moreno Taquería'
  97. //to Antonio Moreno Taquería
  98. $len = strlen($stringValue);
  99. if ($len < 2) {
  100. return $stringValue;
  101. }
  102. return substr($stringValue, 1, $len - 2);
  103. }
  104. /**
  105. * Convert the given value to a form that can be used in OData uri.
  106. * Note: The calling function should not pass null value, as this
  107. * function will not perform any check for nullability
  108. *
  109. * @param mixed $value value to convert.
  110. *
  111. * @return string
  112. */
  113. public function convertToOData($value)
  114. {
  115. return '\'' . str_replace('%27', "''", urlencode(utf8_encode($value))) . '\'';
  116. }
  117. }