Skip to content

Commit 7a40df3

Browse files
authored
Merge pull request #2 from tacxticx88/feature/marking-various-tests
Feature/marking various tests
2 parents 0cd4653 + 41997e6 commit 7a40df3

File tree

16 files changed

+748
-445
lines changed

16 files changed

+748
-445
lines changed

composer.lock

Lines changed: 305 additions & 393 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Mvc/Collection.php

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Phalcon\Incubator\MongoDB\Mvc;
1515

16+
use ArrayIterator;
1617
use JsonSerializable;
1718
use MongoDB\BSON\ObjectId;
1819
use MongoDB\BSON\Serializable as BsonSerializable;
@@ -31,7 +32,10 @@
3132
use Phalcon\Messages\MessageInterface;
3233
use Phalcon\Mvc\EntityInterface;
3334
use Phalcon\Validation\ValidationInterface;
35+
use ReflectionClass;
36+
use ReflectionException;
3437
use Serializable;
38+
use Traversable;
3539

3640
/**
3741
* Class Collection
@@ -166,28 +170,37 @@ public function appendMessage(MessageInterface $message): CollectionInterface
166170
*
167171
* @param array|null $parameters
168172
* @param array|null $options
169-
* @return array
173+
* @return Cursor|ArrayIterator
170174
* @throws Exception
171175
*/
172-
public static function aggregate(?array $parameters = [], ?array $options = []): array
176+
public static function aggregate(array $parameters = [], array $options = []): Traversable
173177
{
174178
$className = static::class;
175-
/** @var CollectionInterface $collection */
176-
$collection = new $className();
177179

178-
$source = $collection->getSource();
180+
/** @var CollectionInterface $base */
181+
$base = new $className();
182+
$source = $base->getSource();
183+
179184
if (empty($source)) {
180185
throw new Exception("Method getSource() returns empty string");
181186
}
182187

183-
$connection = $collection->getConnection();
184-
$cursorOrArrayIterator = $connection->selectCollection($source)->aggregate($parameters, $options);
185-
186-
if ($cursorOrArrayIterator instanceof Cursor) {
187-
return $cursorOrArrayIterator->toArray();
188+
/**
189+
* Check if a "typeMap" clause was defined or force default
190+
*/
191+
if (isset($options["typeMap"])) {
192+
$options['typeMap'] = array_merge(
193+
self::getTypeMap('array'),
194+
$options["typeMap"]
195+
);
196+
} else {
197+
$options['typeMap'] = self::getTypeMap('array');
188198
}
189199

190-
return (array)$cursorOrArrayIterator;
200+
$connection = $base->getConnection();
201+
202+
// Driver now return a Cursor class by default for more performances.
203+
return $connection->selectCollection($source)->aggregate($parameters, $options);
191204
}
192205

193206
/**
@@ -761,10 +774,10 @@ public static function count(array $parameters = []): int
761774
* ```
762775
*
763776
* @param array $parameters
764-
* @return iterable
777+
* @return Cursor|Traversable
765778
* @throws Exception
766779
*/
767-
public static function find(array $parameters = []): iterable
780+
public static function find(array $parameters = []): Traversable
768781
{
769782
$className = static::class;
770783
/** @var CollectionInterface $collection */
@@ -1210,7 +1223,8 @@ public static function getTypeMap($base = null): array
12101223
"document" => 'array'
12111224
];
12121225

1213-
if (is_array($base::$typeMap)) {
1226+
/** @noinspection NotOptimalIfConditionsInspection */
1227+
if (class_exists($base) && is_array($base::$typeMap)) {
12141228
$typeMap = array_merge($typeMap, $base::$typeMap);
12151229
}
12161230

@@ -1342,7 +1356,7 @@ protected function exists($collection): bool
13421356
* @param CollectionInterface $collection
13431357
* @param mixed|Database $connection
13441358
* @param bool $unique
1345-
* @return array|object|null
1359+
* @return Cursor|object|array
13461360
* @throws Exception
13471361
*/
13481362
protected static function getResultset(
@@ -1415,12 +1429,8 @@ protected static function getResultset(
14151429
return $document;
14161430
}
14171431

1418-
/**
1419-
* Requesting a complete resultset
1420-
*/
1421-
$documentsCursor = $mongoCollection->find($conditions, $parameters);
1422-
1423-
return $documentsCursor->toArray();
1432+
// Driver now return a Cursor class by default for more performances.
1433+
return $mongoCollection->find($conditions, $parameters);
14241434
}
14251435

14261436
/**
@@ -1453,7 +1463,7 @@ protected static function getGroupResultset(array $parameters, CollectionInterfa
14531463

14541464
final protected function possibleSetter(string $property, $value): bool
14551465
{
1456-
$possibleSetter = "set" . Str::camelize($property);
1466+
$possibleSetter = "set" . ucfirst(Str::camelize($property));
14571467

14581468
if (!method_exists($this, $possibleSetter)) {
14591469
return false;
@@ -1472,7 +1482,7 @@ final protected function possibleSetter(string $property, $value): bool
14721482
*/
14731483
final protected function possibleGetter(string $property)
14741484
{
1475-
$possibleGetter = "get" . Str::camelize($property);
1485+
$possibleGetter = "get" . ucfirst(Str::camelize($property));
14761486

14771487
if (!method_exists($this, $possibleGetter)) {
14781488
return $this->$property;
@@ -1505,14 +1515,29 @@ public function assign(array $data, $dataColumnMap = null, $whiteList = null): C
15051515
return $this;
15061516
}
15071517

1508-
foreach (get_object_vars($this) as $key => $value) {
1518+
// Use reflection to list uninitialized properties
1519+
try {
1520+
$reflection = new ReflectionClass($this);
1521+
$reflectionProperties = $reflection->getProperties();
1522+
} catch (ReflectionException $e) {
1523+
$reflectionProperties = [];
1524+
}
1525+
$reserved = $this->getReservedAttributes();
1526+
1527+
foreach ($reflectionProperties as $reflectionMethod) {
1528+
$key = $reflectionMethod->getName();
1529+
1530+
if (isset($reserved[$key])) {
1531+
continue;
1532+
}
1533+
15091534
if (isset($dataMapped[$key])) {
15101535
if (is_array($whiteList) && !in_array($key, $whiteList, true)) {
15111536
continue;
15121537
}
15131538

1514-
if (!$this->possibleSetter($key, $value)) {
1515-
$this->$key = $value;
1539+
if (!$this->possibleSetter($key, $dataMapped[$key])) {
1540+
$this->$key = $dataMapped[$key];
15161541
}
15171542
}
15181543
}

src/Mvc/Collection/Document.php

Lines changed: 84 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
/** @noinspection PhpUndefinedClassInspection */
4-
53
/**
64
* This file is part of the Phalcon Framework.
75
*
@@ -20,7 +18,10 @@
2018
use MongoDB\BSON\Serializable;
2119
use MongoDB\BSON\Unserializable;
2220
use Phalcon\Helper\Str;
21+
use Phalcon\Incubator\MongoDB\Mvc\CollectionInterface;
2322
use Phalcon\Mvc\EntityInterface;
23+
use ReflectionClass;
24+
use ReflectionException;
2425

2526
/**
2627
* Class Document
@@ -42,19 +43,68 @@ class Document implements
4243
*
4344
* @param array $data
4445
*/
45-
final public function __construct(array $data = [])
46+
final public function __construct($data = null)
4647
{
47-
foreach ($data as $key => $value) {
48-
$this->offsetSet($key, $value);
49-
}
50-
5148
/**
5249
* This allows the developer to execute initialization stuff every time
5350
* an instance is created
5451
*/
5552
if (method_exists($this, 'onConstruct')) {
56-
$this->onConstruct();
53+
$this->onConstruct($data);
54+
}
55+
56+
if (is_array($data)) {
57+
$this->assign($data);
58+
}
59+
}
60+
61+
/**
62+
* @param array $data
63+
* @param null $dataColumnMap
64+
* @param null $whiteList
65+
* @return $this|CollectionInterface
66+
*/
67+
public function assign(array $data, $dataColumnMap = null, $whiteList = null): self
68+
{
69+
if (is_array($dataColumnMap)) {
70+
$dataMapped = [];
71+
72+
foreach ($data as $key => $value) {
73+
if (isset($dataColumnMap[$key])) {
74+
$dataMapped[$dataColumnMap[$key]] = $value;
75+
}
76+
}
77+
} else {
78+
$dataMapped = $data;
79+
}
80+
81+
if (count($dataMapped) === 0) {
82+
return $this;
83+
}
84+
85+
// Use reflection to list uninitialized properties
86+
try {
87+
$reflection = new ReflectionClass($this);
88+
$reflectionProperties = $reflection->getProperties();
89+
} catch (ReflectionException $e) {
90+
$reflectionProperties = [];
91+
}
92+
93+
foreach ($reflectionProperties as $reflectionMethod) {
94+
$key = $reflectionMethod->getName();
95+
96+
if (isset($dataMapped[$key])) {
97+
if (is_array($whiteList) && !in_array($key, $whiteList, true)) {
98+
continue;
99+
}
100+
101+
if (!$this->possibleSetter($key, $dataMapped[$key])) {
102+
$this->$key = $dataMapped[$key];
103+
}
104+
}
57105
}
106+
107+
return $this;
58108
}
59109

60110
/**
@@ -146,7 +196,7 @@ public function toArray(): array
146196
*
147197
* @return array
148198
*/
149-
public function jsonSerialize()
199+
public function jsonSerialize(): array
150200
{
151201
$data = [];
152202

@@ -157,9 +207,13 @@ public function jsonSerialize()
157207
return $data;
158208
}
159209

210+
/**
211+
* @param string $property
212+
* @return mixed
213+
*/
160214
final protected function possibleGetter(string $property)
161215
{
162-
$possibleGetter = "get" . Str::camelize($property);
216+
$possibleGetter = "get" . ucfirst(Str::camelize($property));
163217

164218
if (!method_exists($this, $possibleGetter)) {
165219
return $this->$property;
@@ -171,15 +225,33 @@ final protected function possibleGetter(string $property)
171225
/**
172226
* @return array
173227
*/
174-
public function bsonSerialize()
228+
public function bsonSerialize(): array
175229
{
176230
return $this->toArray();
177231
}
178232

233+
/**
234+
* @param string $property
235+
* @param $value
236+
* @return bool
237+
*/
238+
final protected function possibleSetter(string $property, $value): bool
239+
{
240+
$possibleSetter = "set" . ucfirst(Str::camelize($property));
241+
242+
if (!method_exists($this, $possibleSetter)) {
243+
return false;
244+
}
245+
246+
$this->$possibleSetter($value);
247+
248+
return true;
249+
}
250+
179251
/**
180252
* @param array $data
181253
*/
182-
public function bsonUnserialize(array $data)
254+
public function bsonUnserialize(array $data): void
183255
{
184256
foreach ($data as $key => $value) {
185257
$this->offsetSet($key, $value);

tests/_data/fixtures/Mvc/Collections/Documents/RobotPart.php

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515

1616
use DateTime;
1717
use DateTimeInterface;
18+
use MongoDB\BSON\ObjectId;
1819
use MongoDB\BSON\UTCDateTime;
20+
use Phalcon\Incubator\MongoDB\Helper\Mongo;
1921
use Phalcon\Incubator\MongoDB\Mvc\Collection\Document;
22+
use function GuzzleHttp\Psr7\str;
2023

2124
class RobotPart extends Document
2225
{
23-
public $id;
26+
protected $id;
2427

2528
public $common_name;
2629

@@ -44,8 +47,40 @@ public function setDate(DateTimeInterface $date)
4447
*/
4548
public function getDate()
4649
{
47-
return $this->date
48-
->toDateTime()
49-
->format(DateTime::ISO8601);
50+
if (null !== $this->date) {
51+
return $this->date
52+
->toDateTime()
53+
->format(DateTime::ATOM);
54+
}
55+
56+
return null;
57+
}
58+
59+
/**
60+
* @param string $type
61+
* @return mixed
62+
*/
63+
public function getId($type = 'string')
64+
{
65+
switch ($type) {
66+
case 'string':
67+
return (string) $this->id;
68+
69+
case 'object':
70+
return $this->id;
71+
72+
default:
73+
return null;
74+
}
75+
}
76+
77+
/**
78+
* @param mixed $id
79+
*/
80+
public function setId($id): void
81+
{
82+
$this->id = Mongo::isValidObjectId($id)
83+
? new ObjectId((string)$id)
84+
: null;
5085
}
5186
}

0 commit comments

Comments
 (0)