Skip to content

Commit

Permalink
Merge pull request #2 from tacxticx88/feature/marking-various-tests
Browse files Browse the repository at this point in the history
Feature/marking various tests
  • Loading branch information
Jeckerson authored Aug 25, 2020
2 parents 0cd4653 + 41997e6 commit 7a40df3
Show file tree
Hide file tree
Showing 16 changed files with 748 additions and 445 deletions.
698 changes: 305 additions & 393 deletions composer.lock

Large diffs are not rendered by default.

77 changes: 51 additions & 26 deletions src/Mvc/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Phalcon\Incubator\MongoDB\Mvc;

use ArrayIterator;
use JsonSerializable;
use MongoDB\BSON\ObjectId;
use MongoDB\BSON\Serializable as BsonSerializable;
Expand All @@ -31,7 +32,10 @@
use Phalcon\Messages\MessageInterface;
use Phalcon\Mvc\EntityInterface;
use Phalcon\Validation\ValidationInterface;
use ReflectionClass;
use ReflectionException;
use Serializable;
use Traversable;

/**
* Class Collection
Expand Down Expand Up @@ -166,28 +170,37 @@ public function appendMessage(MessageInterface $message): CollectionInterface
*
* @param array|null $parameters
* @param array|null $options
* @return array
* @return Cursor|ArrayIterator
* @throws Exception
*/
public static function aggregate(?array $parameters = [], ?array $options = []): array
public static function aggregate(array $parameters = [], array $options = []): Traversable
{
$className = static::class;
/** @var CollectionInterface $collection */
$collection = new $className();

$source = $collection->getSource();
/** @var CollectionInterface $base */
$base = new $className();
$source = $base->getSource();

if (empty($source)) {
throw new Exception("Method getSource() returns empty string");
}

$connection = $collection->getConnection();
$cursorOrArrayIterator = $connection->selectCollection($source)->aggregate($parameters, $options);

if ($cursorOrArrayIterator instanceof Cursor) {
return $cursorOrArrayIterator->toArray();
/**
* Check if a "typeMap" clause was defined or force default
*/
if (isset($options["typeMap"])) {
$options['typeMap'] = array_merge(
self::getTypeMap('array'),
$options["typeMap"]
);
} else {
$options['typeMap'] = self::getTypeMap('array');
}

return (array)$cursorOrArrayIterator;
$connection = $base->getConnection();

// Driver now return a Cursor class by default for more performances.
return $connection->selectCollection($source)->aggregate($parameters, $options);
}

/**
Expand Down Expand Up @@ -761,10 +774,10 @@ public static function count(array $parameters = []): int
* ```
*
* @param array $parameters
* @return iterable
* @return Cursor|Traversable
* @throws Exception
*/
public static function find(array $parameters = []): iterable
public static function find(array $parameters = []): Traversable
{
$className = static::class;
/** @var CollectionInterface $collection */
Expand Down Expand Up @@ -1210,7 +1223,8 @@ public static function getTypeMap($base = null): array
"document" => 'array'
];

if (is_array($base::$typeMap)) {
/** @noinspection NotOptimalIfConditionsInspection */
if (class_exists($base) && is_array($base::$typeMap)) {
$typeMap = array_merge($typeMap, $base::$typeMap);
}

Expand Down Expand Up @@ -1342,7 +1356,7 @@ protected function exists($collection): bool
* @param CollectionInterface $collection
* @param mixed|Database $connection
* @param bool $unique
* @return array|object|null
* @return Cursor|object|array
* @throws Exception
*/
protected static function getResultset(
Expand Down Expand Up @@ -1415,12 +1429,8 @@ protected static function getResultset(
return $document;
}

/**
* Requesting a complete resultset
*/
$documentsCursor = $mongoCollection->find($conditions, $parameters);

return $documentsCursor->toArray();
// Driver now return a Cursor class by default for more performances.
return $mongoCollection->find($conditions, $parameters);
}

/**
Expand Down Expand Up @@ -1453,7 +1463,7 @@ protected static function getGroupResultset(array $parameters, CollectionInterfa

final protected function possibleSetter(string $property, $value): bool
{
$possibleSetter = "set" . Str::camelize($property);
$possibleSetter = "set" . ucfirst(Str::camelize($property));

if (!method_exists($this, $possibleSetter)) {
return false;
Expand All @@ -1472,7 +1482,7 @@ final protected function possibleSetter(string $property, $value): bool
*/
final protected function possibleGetter(string $property)
{
$possibleGetter = "get" . Str::camelize($property);
$possibleGetter = "get" . ucfirst(Str::camelize($property));

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

foreach (get_object_vars($this) as $key => $value) {
// Use reflection to list uninitialized properties
try {
$reflection = new ReflectionClass($this);
$reflectionProperties = $reflection->getProperties();
} catch (ReflectionException $e) {
$reflectionProperties = [];
}
$reserved = $this->getReservedAttributes();

foreach ($reflectionProperties as $reflectionMethod) {
$key = $reflectionMethod->getName();

if (isset($reserved[$key])) {
continue;
}

if (isset($dataMapped[$key])) {
if (is_array($whiteList) && !in_array($key, $whiteList, true)) {
continue;
}

if (!$this->possibleSetter($key, $value)) {
$this->$key = $value;
if (!$this->possibleSetter($key, $dataMapped[$key])) {
$this->$key = $dataMapped[$key];
}
}
}
Expand Down
96 changes: 84 additions & 12 deletions src/Mvc/Collection/Document.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

/** @noinspection PhpUndefinedClassInspection */

/**
* This file is part of the Phalcon Framework.
*
Expand All @@ -20,7 +18,10 @@
use MongoDB\BSON\Serializable;
use MongoDB\BSON\Unserializable;
use Phalcon\Helper\Str;
use Phalcon\Incubator\MongoDB\Mvc\CollectionInterface;
use Phalcon\Mvc\EntityInterface;
use ReflectionClass;
use ReflectionException;

/**
* Class Document
Expand All @@ -42,19 +43,68 @@ class Document implements
*
* @param array $data
*/
final public function __construct(array $data = [])
final public function __construct($data = null)
{
foreach ($data as $key => $value) {
$this->offsetSet($key, $value);
}

/**
* This allows the developer to execute initialization stuff every time
* an instance is created
*/
if (method_exists($this, 'onConstruct')) {
$this->onConstruct();
$this->onConstruct($data);
}

if (is_array($data)) {
$this->assign($data);
}
}

/**
* @param array $data
* @param null $dataColumnMap
* @param null $whiteList
* @return $this|CollectionInterface
*/
public function assign(array $data, $dataColumnMap = null, $whiteList = null): self
{
if (is_array($dataColumnMap)) {
$dataMapped = [];

foreach ($data as $key => $value) {
if (isset($dataColumnMap[$key])) {
$dataMapped[$dataColumnMap[$key]] = $value;
}
}
} else {
$dataMapped = $data;
}

if (count($dataMapped) === 0) {
return $this;
}

// Use reflection to list uninitialized properties
try {
$reflection = new ReflectionClass($this);
$reflectionProperties = $reflection->getProperties();
} catch (ReflectionException $e) {
$reflectionProperties = [];
}

foreach ($reflectionProperties as $reflectionMethod) {
$key = $reflectionMethod->getName();

if (isset($dataMapped[$key])) {
if (is_array($whiteList) && !in_array($key, $whiteList, true)) {
continue;
}

if (!$this->possibleSetter($key, $dataMapped[$key])) {
$this->$key = $dataMapped[$key];
}
}
}

return $this;
}

/**
Expand Down Expand Up @@ -146,7 +196,7 @@ public function toArray(): array
*
* @return array
*/
public function jsonSerialize()
public function jsonSerialize(): array
{
$data = [];

Expand All @@ -157,9 +207,13 @@ public function jsonSerialize()
return $data;
}

/**
* @param string $property
* @return mixed
*/
final protected function possibleGetter(string $property)
{
$possibleGetter = "get" . Str::camelize($property);
$possibleGetter = "get" . ucfirst(Str::camelize($property));

if (!method_exists($this, $possibleGetter)) {
return $this->$property;
Expand All @@ -171,15 +225,33 @@ final protected function possibleGetter(string $property)
/**
* @return array
*/
public function bsonSerialize()
public function bsonSerialize(): array
{
return $this->toArray();
}

/**
* @param string $property
* @param $value
* @return bool
*/
final protected function possibleSetter(string $property, $value): bool
{
$possibleSetter = "set" . ucfirst(Str::camelize($property));

if (!method_exists($this, $possibleSetter)) {
return false;
}

$this->$possibleSetter($value);

return true;
}

/**
* @param array $data
*/
public function bsonUnserialize(array $data)
public function bsonUnserialize(array $data): void
{
foreach ($data as $key => $value) {
$this->offsetSet($key, $value);
Expand Down
43 changes: 39 additions & 4 deletions tests/_data/fixtures/Mvc/Collections/Documents/RobotPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@

use DateTime;
use DateTimeInterface;
use MongoDB\BSON\ObjectId;
use MongoDB\BSON\UTCDateTime;
use Phalcon\Incubator\MongoDB\Helper\Mongo;
use Phalcon\Incubator\MongoDB\Mvc\Collection\Document;
use function GuzzleHttp\Psr7\str;

class RobotPart extends Document
{
public $id;
protected $id;

public $common_name;

Expand All @@ -44,8 +47,40 @@ public function setDate(DateTimeInterface $date)
*/
public function getDate()
{
return $this->date
->toDateTime()
->format(DateTime::ISO8601);
if (null !== $this->date) {
return $this->date
->toDateTime()
->format(DateTime::ATOM);
}

return null;
}

/**
* @param string $type
* @return mixed
*/
public function getId($type = 'string')
{
switch ($type) {
case 'string':
return (string) $this->id;

case 'object':
return $this->id;

default:
return null;
}
}

/**
* @param mixed $id
*/
public function setId($id): void
{
$this->id = Mongo::isValidObjectId($id)
? new ObjectId((string)$id)
: null;
}
}
Loading

0 comments on commit 7a40df3

Please sign in to comment.