Schema.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\data\source\mongo_db;
  9. use MongoId;
  10. use MongoCode;
  11. use MongoDate;
  12. use MongoRegex;
  13. use MongoBinData;
  14. class Schema extends \lithium\data\DocumentSchema {
  15. protected $_handlers = array();
  16. protected $_types = array(
  17. 'MongoId' => 'id',
  18. 'MongoDate' => 'date',
  19. 'MongoCode' => 'code',
  20. 'MongoBinData' => 'binary',
  21. 'datetime' => 'date',
  22. 'timestamp' => 'date',
  23. 'int' => 'integer'
  24. );
  25. public function __construct(array $config = array()) {
  26. $defaults = array('fields' => array('_id' => array('type' => 'id')));
  27. parent::__construct(array_filter($config) + $defaults);
  28. }
  29. protected function _init() {
  30. $this->_autoConfig[] = 'handlers';
  31. parent::_init();
  32. $this->_handlers += array(
  33. 'id' => function($v) {
  34. return is_string($v) && preg_match('/^[0-9a-f]{24}$/', $v) ? new MongoId($v) : $v;
  35. },
  36. 'date' => function($v) {
  37. $v = is_numeric($v) ? intval($v) : strtotime($v);
  38. return !$v ? new MongoDate() : new MongoDate($v);
  39. },
  40. 'regex' => function($v) { return new MongoRegex($v); },
  41. 'integer' => function($v) { return (integer) $v; },
  42. 'float' => function($v) { return (float) $v; },
  43. 'boolean' => function($v) { return (boolean) $v; },
  44. 'code' => function($v) { return new MongoCode($v); },
  45. 'binary' => function($v) { return new MongoBinData($v); }
  46. );
  47. }
  48. }
  49. ?>