/libraries/lithium/tests/mocks/data/model/MockDocumentSource.php

https://github.com/kiranatama/sagalaya · PHP · 155 lines · 128 code · 21 blank · 6 comment · 17 complexity · d4c09e29f566d47f6566dd4c25791650 MD5 · raw file

  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\mocks\data\model;
  9. use MongoId;
  10. use MongoDate;
  11. class MockDocumentSource extends \lithium\data\Source {
  12. protected $_classes = array(
  13. 'entity' => 'lithium\data\entity\Document',
  14. 'array' => 'lithium\data\collection\DocumentArray',
  15. 'set' => 'lithium\data\collection\DocumentSet',
  16. 'relationship' => 'lithium\data\model\Relationship'
  17. );
  18. public function connect() {}
  19. public function disconnect() {}
  20. public function sources($class = null) {}
  21. public function describe($entity, array $meta = array()) {}
  22. public function create($query, array $options = array()) {}
  23. public function update($query, array $options = array()) {}
  24. public function delete($query, array $options = array()) {}
  25. protected $point = 0;
  26. protected $result = null;
  27. public function read($query = null, array $options = array()) {
  28. $this->point = 0;
  29. $this->result = array(
  30. array('id' => 1, 'name' => 'Joe'),
  31. array('id' => 2, 'name' => 'Moe'),
  32. array('id' => 3, 'name' => 'Roe')
  33. );
  34. }
  35. public function getNext() {
  36. return $this->result[$this->point++];
  37. }
  38. public function cast($entity, array $data, array $options = array()) {
  39. $defaults = array('schema' => null, 'first' => false, 'pathKey' => null, 'arrays' => true);
  40. $options += $defaults;
  41. $model = null;
  42. $exists = false;
  43. if (!$data) {
  44. return $data;
  45. }
  46. if ($entity && !$options['schema']) {
  47. $options['schema'] = $entity->schema() ?: array('_id' => array('type' => 'id'));
  48. }
  49. if ($entity) {
  50. if (!($entity instanceof $this->_classes['set'])) {
  51. $exists = $entity->exists();
  52. }
  53. $model = $entity->model();
  54. }
  55. $options['exists'] = $exists;
  56. $schema = $options['schema'];
  57. unset($options['schema']);
  58. $handlers = array(
  59. 'id' => function($v) {
  60. return is_string($v) && preg_match('/^[0-9a-f]{24}$/', $v) ? new MongoId($v) : $v;
  61. },
  62. 'date' => function($v) {
  63. $v = is_numeric($v) ? intval($v) : strtotime($v);
  64. return (time() == $v) ? new MongoDate() : new MongoDate($v);
  65. },
  66. 'regex' => function($v) { return new MongoRegex($v); },
  67. 'integer' => function($v) { return (integer) $v; },
  68. 'float' => function($v) { return (float) $v; },
  69. 'boolean' => function($v) { return (boolean) $v; },
  70. 'code' => function($v) { return new MongoCode($v); },
  71. 'binary' => function($v) { return new MongoBinData($v); }
  72. );
  73. $typeMap = array(
  74. 'MongoId' => 'id',
  75. 'MongoDate' => 'date',
  76. 'MongoCode' => 'code',
  77. 'MongoBinData' => 'binary',
  78. 'datetime' => 'date',
  79. 'timestamp' => 'date',
  80. 'int' => 'integer'
  81. );
  82. foreach ($data as $key => $value) {
  83. if (is_object($value)) {
  84. continue;
  85. }
  86. $path = is_int($key) ? null : $key;
  87. $path = $options['pathKey'] ? trim("{$options['pathKey']}.{$path}", '.') : $path;
  88. $field = (isset($schema[$path]) ? $schema[$path] : array());
  89. $field += array('type' => null, 'array' => null);
  90. $type = isset($typeMap[$field['type']]) ? $typeMap[$field['type']] : $field['type'];
  91. $isObject = ($type == 'object');
  92. $isArray = (is_array($value) && $field['array'] !== false && !$isObject);
  93. if (isset($handlers[$type])) {
  94. $handler = $handlers[$type];
  95. $value = $isArray ? array_map($handler, $value) : $handler($value);
  96. }
  97. if (!$options['arrays']) {
  98. $data[$key] = $value;
  99. continue;
  100. }
  101. $pathKey = $path;
  102. if (is_array($value)) {
  103. $arrayType = !$isObject && (array_keys($value) === range(0, count($value) - 1));
  104. $opts = $arrayType ? array('class' => 'array') + $options : $options;
  105. $value = $this->item($model, $value, compact('pathKey') + $opts);
  106. } elseif ($field['array']) {
  107. $opts = array('class' => 'array') + $options;
  108. $value = $this->item($model, array($value), compact('pathKey') + $opts);
  109. }
  110. $data[$key] = $value;
  111. }
  112. return $options['first'] ? reset($data) : $data;
  113. }
  114. public function result($type, $resource, $context) {
  115. switch ($type) {
  116. case 'next':
  117. $result = $resource->hasNext() ? $resource->getNext() : null;
  118. break;
  119. case 'close':
  120. unset($resource);
  121. $result = null;
  122. break;
  123. }
  124. return $result;
  125. }
  126. public function relationship($class, $type, $name, array $options = array()) {
  127. $key = Inflector::camelize($type == 'belongsTo' ? $name : $class::meta('name'));
  128. $options += compact('name', 'type', 'key');
  129. $options['from'] = $class;
  130. $relationship = $this->_classes['relationship'];
  131. return new $relationship($options);
  132. }
  133. }
  134. ?>