PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

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