PageRenderTime 22ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
PHP | 234 lines | 81 code | 16 blank | 137 comment | 3 complexity | 7a3cdaae591ae9c31aff7b051655c817 MD5 | raw file
  1. <?php
  2. /**
  3. * Type to represent DateTime
  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 DateTime
  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 DateTime 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::DATETIME;
  39. }
  40. /**
  41. * Checks this type 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::DATETIME);
  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. //1. The datetime value present in the $filter option should have
  65. // 'datetime' prefix.
  66. //2. Month and day should be two digit
  67. if (!preg_match("/^datetime\'(\d{4})-(\d{2})-(\d{2})((\s|T)([0-1][0-9]|2[0-4]):([0-5][0-9])(:([0-5][0-9])([Z])?)?)?\'$/", strval($value), $matches)
  68. ) {
  69. return false;
  70. }
  71. //stripoff prefix, and quotes from both ends
  72. $value = trim($value, 'datetime\'');
  73. //Validate the date using PHP DateTime class
  74. if (!self::validateWithoutPreFix($value)) {
  75. return false;
  76. }
  77. $outValue = "'" . $value . "'";
  78. return true;
  79. }
  80. /**
  81. * Gets full name of this type in EDM namespace
  82. * Note: implementation of IType::getFullTypeName
  83. *
  84. * @return string
  85. */
  86. public function getFullTypeName()
  87. {
  88. return 'Edm.DateTime';
  89. }
  90. /**
  91. * Converts the given string value to datetime type.
  92. * Note: This function will not perfrom any conversion.
  93. *
  94. * @param string $stringValue Value to convert.
  95. *
  96. * @return string
  97. */
  98. public function convert($stringValue)
  99. {
  100. return $stringValue;
  101. }
  102. /**
  103. * Convert the given value to a form that can be used in OData uri.
  104. * Note: The calling function should not pass null value, as this
  105. * function will not perform any check for nullability
  106. *
  107. * @param mixed $value Value to convert.
  108. *
  109. * @return string
  110. */
  111. public function convertToOData($value)
  112. {
  113. return 'datetime\'' . urlencode($value) . '\'';
  114. }
  115. /**
  116. * Checks a value is valid datetime
  117. *
  118. * @param string $dateTime value to validate.
  119. *
  120. * @return boolean
  121. */
  122. public static function validateWithoutPreFix($dateTime)
  123. {
  124. try {
  125. $dt = new \DateTime($dateTime, new \DateTimeZone('UTC'));
  126. } catch (\Exception $e) {
  127. return false;
  128. }
  129. return true;
  130. }
  131. /**
  132. * Gets year from datetime
  133. *
  134. * @param string $dateTime datetime to get the year from
  135. *
  136. * @return int
  137. */
  138. public static function year($dateTime)
  139. {
  140. $date = new \DateTime($dateTime);
  141. return $date->format("Y");
  142. }
  143. /**
  144. * Gets month from datetime
  145. *
  146. * @param string $dateTime datetime to get the month from
  147. *
  148. * @return int
  149. */
  150. public static function month($dateTime)
  151. {
  152. $date = new \DateTime($dateTime);
  153. return $date->format("m");
  154. }
  155. /**
  156. * Gets day from datetime
  157. *
  158. * @param string $dateTime datetime to get the day from
  159. *
  160. * @return int
  161. */
  162. public static function day($dateTime)
  163. {
  164. $date = new \DateTime($dateTime);
  165. return $date->format("d");
  166. }
  167. /**
  168. * Gets hour from datetime
  169. *
  170. * @param string $dateTime datetime to get the hour from
  171. *
  172. * @return int
  173. */
  174. public static function hour($dateTime)
  175. {
  176. $date = new \DateTime($dateTime);
  177. return $date->format("H");
  178. }
  179. /**
  180. * Gets minute from datetime
  181. *
  182. * @param string $dateTime datetime to get the minute from
  183. *
  184. * @return int
  185. */
  186. public static function minute($dateTime)
  187. {
  188. $date = new \DateTime($dateTime);
  189. return $date->format("i");
  190. }
  191. /**
  192. * Gets second from datetime
  193. *
  194. * @param string $dateTime datetime to get the second from
  195. *
  196. * @return int
  197. */
  198. public static function second($dateTime)
  199. {
  200. $date = new \DateTime($dateTime);
  201. return $date->format("s");
  202. }
  203. /**
  204. * Compare two dates. Note that this function will not perform any
  205. * validation on dates, one should use either validate or
  206. * validateWithoutPrefix to validate the date before calling this
  207. * function
  208. *
  209. * @param string $dateTime1 First date
  210. * @param string $dateTime2 Second date
  211. *
  212. * @return int
  213. */
  214. public static function dateTimeCmp($dateTime1, $dateTime2)
  215. {
  216. return strtotime($dateTime1) - strtotime($dateTime2);
  217. }
  218. }