Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Pôle IS
Bundles Symfony 2
ng-model-generator-bundle
Commits
57213904
Commit
57213904
authored
Sep 18, 2018
by
Guillaume Perréal
Browse files
Remplace Reference::resolveAs par Type->getAsType.
parent
5ed75b27
Changes
7
Hide whitespace changes
Inline
Side-by-side
src/Metadata/MetadataFactory.php
View file @
57213904
...
...
@@ -132,20 +132,18 @@ final class MetadataFactory implements MetadataFactoryInterface
$classMeta
=
new
\
ReflectionClass
(
$className
);
$parentClass
=
$classMeta
->
getParentClass
();
$defaultNormalization
=
$this
->
getOperationSerialization
(
null
,
$class
,
true
,
[]
// $metadata->getAttribute('normalization_context', [])['groups'] ?? []
);
return
new
ResourceMetadata
(
$class
,
$parentClass
?
PHPClass
::
get
(
$parentClass
->
getName
())
:
null
,
$metadata
->
getShortName
(),
$metadata
->
getDescription
(),
$classMeta
->
isAbstract
(),
$defaultNormalization
,
$this
->
getOperationSerialization
(
null
,
$class
,
true
,
$metadata
->
getAttribute
(
'normalization_context'
,
[])[
'groups'
]
??
[]
),
$this
->
getOperations
(
$class
)
);
}
...
...
src/Models/Types/AbstractCollection.php
View file @
57213904
...
...
@@ -81,4 +81,12 @@ abstract class AbstractCollection extends AbstractType
* @return string
*/
abstract
protected
function
getGenericUsage
():
string
;
/**
* {@inheritdoc}
*/
public
function
findType
(
string
$typeClass
):
?Type
{
return
parent
::
findType
(
$typeClass
)
?:
$this
->
valueType
->
findType
(
$typeClass
);
}
}
src/Models/Types/AbstractType.php
View file @
57213904
...
...
@@ -20,7 +20,6 @@
namespace
Irstea\NgModelGeneratorBundle\Models\Types
;
use
Irstea\NgModelGeneratorBundle\Exceptions\DomainException
;
use
Irstea\NgModelGeneratorBundle\FQCN
;
/**
* Class AbstractType.
...
...
@@ -32,7 +31,7 @@ abstract class AbstractType implements Type
*/
public
function
__toString
()
{
return
sprintf
(
'%s(#%x)'
,
FQCN
::
baseName
(
get_class
(
$this
)
)
,
spl_object_hash
(
$this
));
return
sprintf
(
'%s(#%x)'
,
get_class
(
$this
),
spl_object_hash
(
$this
));
}
/**
...
...
@@ -66,4 +65,29 @@ abstract class AbstractType implements Type
{
return
$this
->
getUsage
();
}
/**
* {@inheritdoc}
*/
public
function
getAsType
(
string
$typeClass
):
Type
{
$type
=
$this
->
findType
(
$typeClass
);
if
(
!
$type
)
{
throw
new
\
TypeError
(
sprintf
(
'Cannot get type %s from %s'
,
$typeClass
,
$this
));
}
return
$type
;
}
/**
* {@inheritdoc}
*/
public
function
findType
(
string
$typeClass
):
?Type
{
if
(
is_a
(
$this
,
$typeClass
,
true
))
{
return
$this
;
}
return
null
;
}
}
src/Models/Types/Alias.php
View file @
57213904
...
...
@@ -76,4 +76,12 @@ final class Alias extends AbstractType implements Declaration
{
yield
$this
->
target
;
}
/**
* {@inheritdoc}
*/
public
function
findType
(
string
$typeClass
):
?Type
{
return
parent
::
findType
(
$typeClass
)
?:
$this
->
target
->
findType
(
$typeClass
);
}
}
src/Models/Types/Reference.php
View file @
57213904
...
...
@@ -120,20 +120,10 @@ class Reference extends AbstractType
}
/**
* @param Type $type
* @param string|null $expectedClass
*
* @return Type Type
* {@inheritdoc}
*/
public
static
function
resolveAs
(
Type
$type
,
string
$expectedClass
=
null
):
Type
public
function
findType
(
string
$typeClass
):
?
Type
{
if
(
$type
instanceof
Reference
)
{
return
self
::
resolveAs
(
$type
->
dereference
(),
$expectedClass
);
}
if
(
$expectedClass
&&
!
is_a
(
$type
,
$expectedClass
,
true
))
{
throw
new
\
TypeError
(
"Cannot resolve as
$expectedClass
"
);
}
return
$type
;
return
parent
::
findType
(
$typeClass
)
?:
$this
->
target
->
findType
(
$typeClass
);
}
}
src/Models/Types/Type.php
View file @
57213904
...
...
@@ -43,4 +43,18 @@ interface Type extends \IteratorAggregate, \JsonSerializable
* @return string
*/
public
function
checkType
(
string
$expr
,
bool
$explicit
=
false
):
string
;
/**
* @param string $typeClass
*
* @return Type
*/
public
function
getAsType
(
string
$typeClass
):
Type
;
/**
* @param string $typeClass
*
* @return Type|null
*/
public
function
findType
(
string
$typeClass
):
?Type
;
}
src/Models/Types/Unresolved.php
View file @
57213904
...
...
@@ -116,4 +116,20 @@ final class Unresolved implements Type
return
$instance
;
}
/**
* {@inheritdoc}
*/
public
function
getAsType
(
string
$typeClass
):
Type
{
throw
new
\
TypeError
(
__CLASS__
.
" cannot be cast as
$typeClass
"
);
}
/**
* {@inheritdoc}
*/
public
function
findType
(
string
$typeClass
):
?Type
{
return
null
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment