Replies: 3 comments 5 replies
-
so to be specific: class IteratorAssembler implements AssemblerInterface
{
public function __construct(public ?string $propertyName) {
public function assemble(ContextInterface $context): void
{
$class = $context->getClass();
$propertyToUse = NULL;
$properties = $context->getType()->getProperties();
if ($this->propertyName) {
foreach ($properties as $property) {
if ($property->getName() === $this->propertyName) {
$propertyToUse = $property;
break;
}
}
}
if (!$propertyToUse) {
$propertyToUse = count($properties) ? current($properties) : null;
}
// the rest of the function, but using $propertyToUse instead of $firstProperty
}
} I already created my own assembler, I am only asking to add this simple feature to this package, because I can guess, |
Beta Was this translation helpful? Give feedback.
-
Hello, The idea behind that assembler is to use it on complex types that wrap a single element with minOccurs=0 and maxOccurs=unbounded. So basically to make the complex type behave like an array instead of an object. <complexType name="Categories" mixed="true">
<sequence>
<element minOccurs="0" maxOccurs="unbounded" name="Category" type="tns:Category"/>
</sequence>
</complexType> This will result in class Categories implements IteratorAggregate
{
/**
* @var Category[]
*/
private $Category;
public function getIterator()
{
return new \ArrayIterator(is_array($this->Category) ? $this->Category : []);
}
} If your object has multiple properties, it doesn't make sense to make it behave like an iterator. Another downside about hardcoding which property to use, is that you won't be able to apply that same assembler on multiple type matches, since the properties could vary. The idea behind the assembler is to generate code if a specific rule matches. A rule could be validated against one specific type, but as well on multiple types. Eg by regex: |
Beta Was this translation helpful? Give feedback.
-
arg.... i was so optimistic... that this might be a good idea 🙈 but sadly, I have to admit, you are right. i really appreciate you taking the time answering that detailed. at the end, I guess it´s really easier "not" using return-types. having them makes so many things very complicated 😕 |
Beta Was this translation helpful? Give feedback.
-
hi @veewee,
when using the "IteratorAssembler" i was wondering why the first property gets the iteration and why this cannot be set by the user (me).
soap-client/src/Phpro/SoapClient/CodeGenerator/Assembler/IteratorAssembler.php
Line 39 in 9c616b6
before I get the well known copy&paste answer 😏 I just want to ask, if you see a problem in choosing the user which property gets the iteration?
because right now it´s:
but why not:
so the assembler has an optional parameter which property should be used, because always using the first, is very
random and pure luck if that is the property that really needs to be an array.
thanks.
Beta Was this translation helpful? Give feedback.
All reactions