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
cac0db1e
Commit
cac0db1e
authored
Aug 27, 2018
by
Guillaume Perréal
Browse files
Utilise directement un type plutôt qu'un nom dans les References.
Signed-off-by:
Guillaume Perréal
<
guillaume.perreal@irstea.fr
>
parent
b23449d8
Changes
4
Hide whitespace changes
Inline
Side-by-side
src/Models/Types/Reference.php
View file @
cac0db1e
...
...
@@ -19,28 +19,42 @@
namespace
Irstea\NgModelGeneratorBundle\Models\Types
;
use
Irstea\NgModelGeneratorBundle\Models\NamedTrait
;
/**
* Class Ref.
*/
final
class
Reference
extends
AbstractType
{
use
NamedTrait
;
/** @var ReferenceResolver */
private
$resolver
;
/** @var Type */
private
$target
;
/**
* Reference constructor.
*
* @param string $name
* @param ReferenceResolver $resolver
* @param Type|null $target
*/
public
function
__construct
(
Type
$target
=
null
)
{
$this
->
target
=
$target
?:
BuiltinType
::
get
(
'never'
);
}
/**
* Get target.
*
* @return Type
*/
public
function
getTarget
():
Type
{
return
$this
->
target
;
}
/**
* Set target.
*
* @param Type $target
*/
public
function
__construct
(
string
$name
,
ReferenceResolver
$resolver
)
public
function
setTarget
(
Type
$target
)
{
$this
->
name
=
$name
;
$this
->
resolver
=
$resolver
;
$this
->
target
=
$target
;
}
/**
...
...
@@ -48,7 +62,7 @@ final class Reference extends AbstractType
*/
public
function
dereference
():
Type
{
return
$this
->
resolver
->
resolveReference
(
$this
->
name
);
return
$this
->
target
->
dereference
(
);
}
/**
...
...
@@ -56,6 +70,14 @@ final class Reference extends AbstractType
*/
public
function
getUsage
():
string
{
return
$this
->
name
;
return
$this
->
dereference
()
->
getUsage
();
}
/**
* {@inheritdoc}
*/
public
function
getIterator
()
{
yield
$this
->
target
;
}
}
src/Models/Types/ReferenceResolver.php
deleted
100644 → 0
View file @
b23449d8
<?php
declare
(
strict_types
=
1
);
/*
* irstea/ng-model-generator-bundle generates Typescript interfaces for Angular using api-platform metadata.
* Copyright (C) 2018 IRSTEA
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License and the GNU
* Lesser General Public License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/
namespace
Irstea\NgModelGeneratorBundle\Models\Types
;
/**
* Interface ReferenceResolver.
*/
interface
ReferenceResolver
{
/**
* @param string $name
*
* @return Type
*/
public
function
resolveReference
(
string
$name
):
Type
;
}
src/Models/Types/TypeFactoryInterface.php
View file @
cac0db1e
...
...
@@ -24,11 +24,6 @@ namespace Irstea\NgModelGeneratorBundle\Models\Types;
*/
interface
TypeFactoryInterface
{
/**
* @return Type[]
*/
public
function
all
():
array
;
/**
* @param string $name
*
...
...
src/TypeFactory.php
View file @
cac0db1e
...
...
@@ -21,31 +21,25 @@ namespace Irstea\NgModelGeneratorBundle;
use
Irstea\NgModelGeneratorBundle\Models\Types\BuiltinType
;
use
Irstea\NgModelGeneratorBundle\Models\Types\Reference
;
use
Irstea\NgModelGeneratorBundle\Models\Types\ReferenceResolver
;
use
Irstea\NgModelGeneratorBundle\Models\Types\Type
;
use
Irstea\NgModelGeneratorBundle\Models\Types\TypeFactoryInterface
;
/**
* Class TypeFactory.
*/
final
class
TypeFactory
implements
TypeFactoryInterface
,
ReferenceResolver
final
class
TypeFactory
implements
TypeFactoryInterface
,
\
IteratorAggregate
{
/**
* @var array<string,Type>
*/
private
$references
=
[];
/**
* @var array<string,Type>
*/
private
$declarations
=
[];
private
$types
=
[];
/**
* {@inheritdoc}
*/
public
function
all
():
array
public
function
getIterator
()
{
return
$this
->
declaration
s
;
yield
from
$this
->
type
s
;
}
/**
...
...
@@ -55,7 +49,7 @@ final class TypeFactory implements TypeFactoryInterface, ReferenceResolver
*/
public
function
has
(
string
$name
):
bool
{
return
isset
(
$this
->
referenc
es
[
$name
]);
return
isset
(
$this
->
typ
es
[
$name
]);
}
/**
...
...
@@ -65,11 +59,11 @@ final class TypeFactory implements TypeFactoryInterface, ReferenceResolver
*/
public
function
get
(
string
$name
):
Type
{
if
(
!
isset
(
$this
->
referenc
es
[
$name
]))
{
if
(
!
isset
(
$this
->
typ
es
[
$name
]))
{
throw
new
\
InvalidArgumentException
(
"unknown type ${name}"
);
}
return
$this
->
referenc
es
[
$name
];
return
$this
->
typ
es
[
$name
];
}
/**
...
...
@@ -77,12 +71,12 @@ final class TypeFactory implements TypeFactoryInterface, ReferenceResolver
*/
public
function
getOrCreate
(
string
$name
,
callable
$builder
,
...
$args
):
Type
{
if
(
isset
(
$this
->
referenc
es
[
$name
]))
{
return
$this
->
referenc
es
[
$name
];
if
(
isset
(
$this
->
typ
es
[
$name
]))
{
return
$this
->
typ
es
[
$name
];
}
$this
->
referenc
es
[
$name
]
=
$ref
=
new
Reference
(
$name
,
$this
);
$
this
->
declarations
[
$name
]
=
$builder
(
...
$args
);
$this
->
typ
es
[
$name
]
=
$ref
=
new
Reference
();
$
ref
->
setTarget
(
$builder
(
...
$args
)
)
;
return
$ref
;
}
...
...
@@ -92,11 +86,11 @@ final class TypeFactory implements TypeFactoryInterface, ReferenceResolver
*/
public
function
addBuiltin
(
string
$name
):
void
{
if
(
isset
(
$this
->
referenc
es
[
$name
]))
{
if
(
isset
(
$this
->
typ
es
[
$name
]))
{
throw
new
\
InvalidArgumentException
(
"builtin already exists: ${name}"
);
}
$this
->
referenc
es
[
$name
]
=
BuiltinType
::
get
(
$name
);
$this
->
typ
es
[
$name
]
=
BuiltinType
::
get
(
$name
);
}
/**
...
...
@@ -104,25 +98,13 @@ final class TypeFactory implements TypeFactoryInterface, ReferenceResolver
*/
public
function
addAlias
(
string
$alias
,
string
$target
):
void
{
if
(
isset
(
$this
->
referenc
es
[
$alias
]))
{
if
(
isset
(
$this
->
typ
es
[
$alias
]))
{
return
;
}
if
(
!
isset
(
$this
->
referenc
es
[
$target
]))
{
if
(
!
isset
(
$this
->
typ
es
[
$target
]))
{
throw
new
\
InvalidArgumentException
(
"alias target does not exist: ${target}"
);
}
$this
->
references
[
$alias
]
=
$this
->
references
[
$target
];
}
/**
* {@inheritdoc}
*/
public
function
resolveReference
(
string
$name
):
Type
{
if
(
!
isset
(
$this
->
declarations
[
$name
]))
{
throw
new
\
InvalidArgumentException
(
"unresolved reference ${name}"
);
}
return
$this
->
declarations
[
$name
];
$this
->
types
[
$alias
]
=
$this
->
types
[
$target
];
}
}
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