Skip to content

Commit

Permalink
Removed the alias check. Not adding anything, but complicating serial…
Browse files Browse the repository at this point in the history
…ization use cases
  • Loading branch information
nilportugues committed Oct 20, 2015
1 parent 21c18ea commit 93de0c9
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 76 deletions.
29 changes: 16 additions & 13 deletions src/Mapping/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,8 @@ public function __construct(array $mappings = null)
foreach ($mappings as $mappedClass) {
$mapping = $this->buildMapping($mappedClass);

if (false === empty($this->aliasMap[$mapping->getClassAlias()])) {

throw new MappingException(
sprintf(
'Class with name \'%s\' already present, used by \'%s\'. Please add an alias for \'%s\' or change an existing one.',
$mapping->getClassAlias(),
$this->aliasMap[$mapping->getClassAlias()],
$mapping->getClassName()
)
);
}

$this->classMap[ltrim($mapping->getClassName(), '\\')] = $mapping;
$this->aliasMap[ltrim($mapping->getClassAlias(), '\\')] = $mapping->getClassName();
$this->aliasMap[ltrim($mapping->getClassAlias(), '\\')][] = $mapping->getClassName();
}
}
}
Expand Down Expand Up @@ -80,4 +68,19 @@ public function setClassMap(array $array)
{
$this->classMap = $array;
}

/**
* @param string $firstClass
* @param string $secondClass
*
* @return bool
*/
private function isSubclass($firstClass, $secondClass)
{
if ($firstClass === $secondClass) {
return false;
}

return is_subclass_of($firstClass, $secondClass, true) || is_subclass_of($secondClass, $firstClass, true);
}
}
2 changes: 1 addition & 1 deletion src/Mapping/MappingFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class MappingFactory
* @param string $className
*
* @throws MappingException
*
* @return Mapping
*
* @since 2.0.0
Expand Down Expand Up @@ -80,7 +81,6 @@ public static function fromClass($className)
return static::fromArray($mappedClass);
}


/**
* @param array $mappedClass
*
Expand Down
80 changes: 80 additions & 0 deletions tests/Dummy/ComplexObject/PostLite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/**
* Author: Nil Portugués Calderó <[email protected]>
* Date: 7/18/15
* Time: 10:42 AM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Tests\Api\Dummy\ComplexObject;

use NilPortugues\Tests\Api\Dummy\ComplexObject\ValueObject\PostId;
use NilPortugues\Tests\Api\Dummy\ComplexObject\ValueObject\UserId;

class PostLite
{
/**
* @var PostId
*/
private $postId;
/**
* @var
*/
private $title;
/**
* @var
*/
private $content;
/**
* @var User
*/
private $author;

/**
* @param PostId $id
* @param $title
* @param $content
* @param User $user
*/
public function __construct(PostId $id, $title, $content, User $user)
{
$this->postId = $id;
$this->title = $title;
$this->content = $content;
$this->author = $user;
}

/**
* @return mixed
*/
public function getContent()
{
return $this->content;
}

/**
* @return PostId
*/
public function getPostId()
{
return $this->postId;
}

/**
* @return mixed
*/
public function getTitle()
{
return $this->title;
}

/**
* @return UserId
*/
public function getUserId()
{
return $this->author;
}
}
62 changes: 0 additions & 62 deletions tests/Mapping/MapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
namespace NilPortugues\Tests\Api\Mapping;

use NilPortugues\Api\Mapping\Mapper;
use NilPortugues\Api\Mapping\MappingException;
use NilPortugues\Tests\Api\Dummy\ComplexObject\Post;
use NilPortugues\Tests\Api\Dummy\PostApiMapping;

Expand Down Expand Up @@ -62,67 +61,6 @@ public function testItCanConstructWithArray()
$this->assertNotEmpty($mapper->getClassMap());
}

public function testItCanThrowException()
{
$this->setExpectedException(MappingException::class);

$mapping = [
[
'class' => Post::class,
'alias' => 'Message',
'aliased_properties' => [
'author' => 'author',
'title' => 'headline',
'content' => 'body',
],
'hide_properties' => [

],
'id_properties' => [
'postId',
],
'urls' => [
// Mandatory
'self' => 'http://example.com/posts/{postId}',
// Optional
'comments' => 'http://example.com/posts/{postId}/comments',
],
// (Optional) Used by HAL+JSON
'curies' => [
'name' => 'example',
'href' => 'http://example.com/docs/rels/{rel}',
],
],
[
'class' => Post::class,
'alias' => 'Message',
'aliased_properties' => [
'author' => 'author',
'title' => 'headline',
'content' => 'body',
],
'hide_properties' => [

],
'id_properties' => [
'postId',
],
'urls' => [
// Mandatory
'self' => 'http://example.com/posts/{postId}',
// Optional
'comments' => 'http://example.com/posts/{postId}/comments',
],
// (Optional) Used by HAL+JSON
'curies' => [
'name' => 'example',
'href' => 'http://example.com/docs/rels/{rel}',
],
],
];
new Mapper($mapping);
}

public function testItCanSetClassMap()
{
$mapper = new Mapper();
Expand Down

0 comments on commit 93de0c9

Please sign in to comment.