Skip to content
This repository has been archived by the owner on Jan 7, 2023. It is now read-only.

Implement add-prefix option for convert:php #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ozmodiar
Copy link

Hello

When converting an XSD to PHP classes, you are using the addTo... notation to add multiple elements. I'm not a huge fan of this notation so I've made this configurable.

It is now possible to do this...

./bin/xsd2php convert:php example.xsd --ns-map='...' --ns-dest='...' --alias-map='...' --add-prefix='add'

...which will result in for example "addElement" instead of "addToElement".

If a developer does not provide the option, nothing will change and "addTo" will still be used.

@goetas
Copy link
Owner

goetas commented May 1, 2015

I like this PR.

Here you can find the reason behind the "addTo" prefix.

Is your idea still valid?

@ozmodiar
Copy link
Author

ozmodiar commented May 4, 2015

Hmm, I don't know. The example given there is correct in my opinion, but the generate code actually generates something else. I think it has something to do with the plural/singular form of the method.

The following example will make things more clear.

<xs:complexType name="accommodation">
    <xs:sequence>
        ...
    </xs:sequence>
</xs:complexType>

<xs:element name="accommodations">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="accommodation" type="accommodation" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

This produces the following classes:

class Accommodations
{
    /**
     * @property AccommodationType[] $accommodation
     */
    private $accommodation = null;

    /**
     * Adds as accommodation
     *
     * @return self
     * @param AccommodationType $accommodation
     */
    public function addToAccommodation(AccommodationType $accommodation)
    {
        $this->accommodation[] = $accommodation;
        return $this;
    }

    /**
     * isset accommodation
     *
     * @param scalar $index
     * @return boolean
     */
    public function issetAccommodation($index)
    {
        return isset($this->accommodation[$index]);
    }

    /**
     * unset accommodation
     *
     * @param scalar $index
     * @return void
     */
    public function unsetAccommodation($index)
    {
        unset($this->accommodation[$index]);
    }

    /**
     * Gets as accommodation
     *
     * @return AccommodationType[]
     */
    public function getAccommodation()
    {
        return $this->accommodation;
    }

    /**
     * Sets a new accommodation
     *
     * @param AccommodationType[] $accommodation
     * @return self
     */
    public function setAccommodation(array $accommodation)
    {
        $this->accommodation = $accommodation;
        return $this;
    }
}

class AccommodationType
{
    //...
}

I would expect one of the following add-methods:

  • addToAccommodations(AccommodationType $accommodation) -> notice the plural form
  • addAccommodation(AccommodationType $accommodation) -> notice the singular form

But the generator produces addToAccommodation which to me sound really strange...

Or am I missing something else here? :-)

@goetas
Copy link
Owner

goetas commented May 5, 2015

Hmm... most probably because of the xsd:element

If you consider this example:

<xs:element name="city">
    <xs:complexType>
      <xs:sequence>
            <xs:element name="name" type="xsd:string"/>
            <xs:element name="accommodations" type="accommodations"/>
       </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:complexType name="accommodations">
      <xs:sequence>
          <xs:element name="accommodation" type="accommodation" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
</xs:complexType>

<xs:complexType name="accommodation">
    <xs:sequence>
        ...
    </xs:sequence>
</xs:complexType>

This will generate only a Accommodation and a City class.

class Accommodation {
 // ...
}

class City {
   protected $name;
   protected $accomodations = array();
   public function addToAccomodations(Accomodation $accomodation) {
       $this->accomodations[] = $accomodation;
   }
   // ....
}

In this case, IMO, addTo make sense

@goetas
Copy link
Owner

goetas commented May 5, 2015

An "array" type is a different thing compared to an "array" element... most probably they should be handled differently...

@goetas
Copy link
Owner

goetas commented May 5, 2015

Of course, the case exposed by you ("array elements") having just add is more appropriate than addTo

@goetas
Copy link
Owner

goetas commented May 5, 2015

Can you check if this (https://github.com/goetas/xsd2php/tree/adder-name) branch works for you?

@ozmodiar
Copy link
Author

ozmodiar commented May 6, 2015

No, that branch still does not work for me.

In PhpConverter.php line 169, a ComplexType is passed to the isArrayElement method for accommodations which causes the isArray to be null since it is not an instance of ElementSimple.

I'll try to do some debugging as well in the next few days.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants