Skip to content

Commit 93de0c9

Browse files
committed
Removed the alias check. Not adding anything, but complicating serialization use cases
1 parent 21c18ea commit 93de0c9

File tree

4 files changed

+97
-76
lines changed

4 files changed

+97
-76
lines changed

src/Mapping/Mapper.php

+16-13
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,8 @@ public function __construct(array $mappings = null)
3535
foreach ($mappings as $mappedClass) {
3636
$mapping = $this->buildMapping($mappedClass);
3737

38-
if (false === empty($this->aliasMap[$mapping->getClassAlias()])) {
39-
40-
throw new MappingException(
41-
sprintf(
42-
'Class with name \'%s\' already present, used by \'%s\'. Please add an alias for \'%s\' or change an existing one.',
43-
$mapping->getClassAlias(),
44-
$this->aliasMap[$mapping->getClassAlias()],
45-
$mapping->getClassName()
46-
)
47-
);
48-
}
49-
5038
$this->classMap[ltrim($mapping->getClassName(), '\\')] = $mapping;
51-
$this->aliasMap[ltrim($mapping->getClassAlias(), '\\')] = $mapping->getClassName();
39+
$this->aliasMap[ltrim($mapping->getClassAlias(), '\\')][] = $mapping->getClassName();
5240
}
5341
}
5442
}
@@ -80,4 +68,19 @@ public function setClassMap(array $array)
8068
{
8169
$this->classMap = $array;
8270
}
71+
72+
/**
73+
* @param string $firstClass
74+
* @param string $secondClass
75+
*
76+
* @return bool
77+
*/
78+
private function isSubclass($firstClass, $secondClass)
79+
{
80+
if ($firstClass === $secondClass) {
81+
return false;
82+
}
83+
84+
return is_subclass_of($firstClass, $secondClass, true) || is_subclass_of($secondClass, $firstClass, true);
85+
}
8386
}

src/Mapping/MappingFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class MappingFactory
3939
* @param string $className
4040
*
4141
* @throws MappingException
42+
*
4243
* @return Mapping
4344
*
4445
* @since 2.0.0
@@ -80,7 +81,6 @@ public static function fromClass($className)
8081
return static::fromArray($mappedClass);
8182
}
8283

83-
8484
/**
8585
* @param array $mappedClass
8686
*
+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/**
4+
* Author: Nil Portugués Calderó <[email protected]>
5+
* Date: 7/18/15
6+
* Time: 10:42 AM.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
namespace NilPortugues\Tests\Api\Dummy\ComplexObject;
12+
13+
use NilPortugues\Tests\Api\Dummy\ComplexObject\ValueObject\PostId;
14+
use NilPortugues\Tests\Api\Dummy\ComplexObject\ValueObject\UserId;
15+
16+
class PostLite
17+
{
18+
/**
19+
* @var PostId
20+
*/
21+
private $postId;
22+
/**
23+
* @var
24+
*/
25+
private $title;
26+
/**
27+
* @var
28+
*/
29+
private $content;
30+
/**
31+
* @var User
32+
*/
33+
private $author;
34+
35+
/**
36+
* @param PostId $id
37+
* @param $title
38+
* @param $content
39+
* @param User $user
40+
*/
41+
public function __construct(PostId $id, $title, $content, User $user)
42+
{
43+
$this->postId = $id;
44+
$this->title = $title;
45+
$this->content = $content;
46+
$this->author = $user;
47+
}
48+
49+
/**
50+
* @return mixed
51+
*/
52+
public function getContent()
53+
{
54+
return $this->content;
55+
}
56+
57+
/**
58+
* @return PostId
59+
*/
60+
public function getPostId()
61+
{
62+
return $this->postId;
63+
}
64+
65+
/**
66+
* @return mixed
67+
*/
68+
public function getTitle()
69+
{
70+
return $this->title;
71+
}
72+
73+
/**
74+
* @return UserId
75+
*/
76+
public function getUserId()
77+
{
78+
return $this->author;
79+
}
80+
}

tests/Mapping/MapperTest.php

-62
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
namespace NilPortugues\Tests\Api\Mapping;
1212

1313
use NilPortugues\Api\Mapping\Mapper;
14-
use NilPortugues\Api\Mapping\MappingException;
1514
use NilPortugues\Tests\Api\Dummy\ComplexObject\Post;
1615
use NilPortugues\Tests\Api\Dummy\PostApiMapping;
1716

@@ -62,67 +61,6 @@ public function testItCanConstructWithArray()
6261
$this->assertNotEmpty($mapper->getClassMap());
6362
}
6463

65-
public function testItCanThrowException()
66-
{
67-
$this->setExpectedException(MappingException::class);
68-
69-
$mapping = [
70-
[
71-
'class' => Post::class,
72-
'alias' => 'Message',
73-
'aliased_properties' => [
74-
'author' => 'author',
75-
'title' => 'headline',
76-
'content' => 'body',
77-
],
78-
'hide_properties' => [
79-
80-
],
81-
'id_properties' => [
82-
'postId',
83-
],
84-
'urls' => [
85-
// Mandatory
86-
'self' => 'http://example.com/posts/{postId}',
87-
// Optional
88-
'comments' => 'http://example.com/posts/{postId}/comments',
89-
],
90-
// (Optional) Used by HAL+JSON
91-
'curies' => [
92-
'name' => 'example',
93-
'href' => 'http://example.com/docs/rels/{rel}',
94-
],
95-
],
96-
[
97-
'class' => Post::class,
98-
'alias' => 'Message',
99-
'aliased_properties' => [
100-
'author' => 'author',
101-
'title' => 'headline',
102-
'content' => 'body',
103-
],
104-
'hide_properties' => [
105-
106-
],
107-
'id_properties' => [
108-
'postId',
109-
],
110-
'urls' => [
111-
// Mandatory
112-
'self' => 'http://example.com/posts/{postId}',
113-
// Optional
114-
'comments' => 'http://example.com/posts/{postId}/comments',
115-
],
116-
// (Optional) Used by HAL+JSON
117-
'curies' => [
118-
'name' => 'example',
119-
'href' => 'http://example.com/docs/rels/{rel}',
120-
],
121-
],
122-
];
123-
new Mapper($mapping);
124-
}
125-
12664
public function testItCanSetClassMap()
12765
{
12866
$mapper = new Mapper();

0 commit comments

Comments
 (0)