Skip to content

Commit

Permalink
Merge pull request #7 from mrsuh/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
mrsuh authored Jul 19, 2017
2 parents 2512879 + 73c1c46 commit c039b14
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 46 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Simple Mongo ODM library.
Add package to your require section in the composer.json file.

```bash
composer require mrsuh/mongo-odm:1.*
composer require mrsuh/mongo-odm:2.*
```

## Usage ##
Expand All @@ -24,20 +24,20 @@ require 'vendor/autoload.php';

use ODM\DBAL;
use ODM\Document\Document;
use ODM\DocumentMapper\DocumentManagerFactory;
use ODM\DocumentManager\DocumentManagerFactory;

/**
* @ODM\Mapping\Annotator\Collection(name="alphabet")
* @Collection(name="alphabet")
*/
class Alphabet extends Document {

/**
* @ODM\Mapping\Annotator\Field(name="language", type="string")
* @Field(name="language", type="string")
*/
private $language;

/**
* @ODM\Mapping\Annotator\Field(name="words", type="Tests\Word[]")
* @Field(name="words", type="Tests\Word[]")
*/
private $words;

Expand Down Expand Up @@ -91,7 +91,7 @@ class Alphabet extends Document {
class Word {

/**
* @ODM\Mapping\Annotator\Field(name="name", type="string")
* @Field(name="name", type="string")
*/
private $name;

Expand Down
4 changes: 2 additions & 2 deletions src/Document/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
class Document
{
/**
* @ODM\Mapping\Annotator\Field(name="_id", type="string")
* @Field(name="_id", type="string")
*/
private $id;
protected $id;

/**
* @return mixed
Expand Down
8 changes: 4 additions & 4 deletions src/DocumentManager/DocumentManager.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace ODM\DocumentMapper;
namespace ODM\DocumentManager;

use ODM\Document\Document;
use ODM\Exception\MapperException;
use ODM\Exception\MappingException;
use ODM\Mapping\DataMapper;
use ODM\DBAL;
use ODM\Mapping\DataMapperFactory;
Expand All @@ -30,7 +30,7 @@ class DocumentManager
* DocumentManager constructor.
* @param DBAL $dbal
* @param string $class_name
* @throws MapperException
* @throws MappingException
*/
public function __construct(DBAL $dbal, string $class_name)
{
Expand All @@ -41,7 +41,7 @@ public function __construct(DBAL $dbal, string $class_name)

if (!$this->mapper->getAnnotator()->isCollection()) {

throw new MapperException('There is no collection name for class ' . $class_name);
throw new MappingException('There is no collection name for class ' . $class_name);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/DocumentManager/DocumentManagerFactory.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace ODM\DocumentMapper;
namespace ODM\DocumentManager;

use ODM\DBAL;

Expand Down
8 changes: 0 additions & 8 deletions src/Exception/MapperException.php

This file was deleted.

8 changes: 8 additions & 0 deletions src/Exception/MappingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace ODM\Exception;

class MappingException extends \Exception
{

}
8 changes: 4 additions & 4 deletions src/Mapping/Annotator/Annotator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(Metadata $metadata)
$this->parseCollection($metadata->getReflectionClass());

foreach($metadata->getReflectionProperties() as $property) {
$this->parseFields($property);
$this->parseField($property);
}
}

Expand All @@ -48,7 +48,7 @@ public function __construct(Metadata $metadata)
*/
private function parseCollection(\ReflectionClass $reflection_class)
{
$pattern = '/@' . preg_quote(Collection::class) . '\((.*)\)/';
$pattern = '/@Collection\((.*)\)/';
preg_match($pattern, $reflection_class->getDocComment(), $matches);

$doc = array_key_exists(1, $matches) ? $matches[1] : '';
Expand All @@ -64,9 +64,9 @@ private function parseCollection(\ReflectionClass $reflection_class)
* @param \ReflectionProperty $reflection_property
* @return bool
*/
private function parseFields(\ReflectionProperty $reflection_property)
private function parseField(\ReflectionProperty $reflection_property)
{
$pattern = '/@' . preg_quote(Field::class) . '\((.*)\)/';
$pattern = '/@Field\((.*)\)/';
preg_match($pattern, $reflection_property->getDocComment(), $matches);

$doc = array_key_exists(1, $matches) ? $matches[1] : '';
Expand Down
8 changes: 0 additions & 8 deletions src/Mapping/Annotator/Collection.php

This file was deleted.

8 changes: 0 additions & 8 deletions src/Mapping/Annotator/Field.php

This file was deleted.

13 changes: 8 additions & 5 deletions src/Mapping/DataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace ODM\Mapping;

use ODM\Exception\MapperException;
use ODM\Exception\MappingException;
use ODM\Mapping\Annotator\Annotator;
use ODM\Mapping\Annotator\AnnotatorFactory;
use ODM\Mapping\Instantiator\Instantiator;
Expand Down Expand Up @@ -132,11 +132,14 @@ public function objectToArray($obj)
* @param $value
* @param int $to
* @return array|float|int|string|mixed
* @throws MapperException
* @throws MappingException
*/
private function typeCast(string $type, $value, int $to)
{
switch ($type) {
case 'bool':
$value = (bool)$value;
break;
case 'integer':
$value = (integer)$value;
break;
Expand Down Expand Up @@ -180,7 +183,7 @@ private function typeCast(string $type, $value, int $to)

if (!class_exists($class_name)) {

throw new MapperException('Class not exists ' . $class_name);
throw new MappingException('Class not exists ' . $class_name);
}

if (!is_array($value)) {
Expand All @@ -207,7 +210,7 @@ private function typeCast(string $type, $value, int $to)

if (!class_exists($class_name)) {

throw new MapperException('Class not exists ' . $class_name);
throw new MappingException('Class not exists ' . $class_name);
}

$mapper = DataMapperFactory::getInstance()->get($class_name);
Expand All @@ -221,7 +224,7 @@ private function typeCast(string $type, $value, int $to)
break;
}

throw new MapperException('Invalid type ' . $type);
throw new MappingException('Invalid type ' . $type);
}

return $value;
Expand Down

0 comments on commit c039b14

Please sign in to comment.