Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting attributes and values from an element not working as expected #198

Open
tacman opened this issue Dec 21, 2020 · 0 comments
Open

Comments

@tacman
Copy link

tacman commented Dec 21, 2020

What an excellent library, thanks for sharing this.

It is mostly working for me, but I'm struggling how to get an element that has a #text value and attributes, e.g.

  <elementSets>
    <settings>
      <setting name="fieldWidth">70</setting>
      <setting name="fieldHeight">1</setting>
    </settings>
   ...

I've overwritten mapValueObject to get the attributes, but must be doing something wrong:

    // from the parent
    public function mapValueObject(string $elementName, string $className)
    {

        list($namespace) = self::parseClarkNotation($elementName);

        $this->elementMap[$elementName] = function (Reader $reader) use ($className, $namespace) {

            $properties = [];
// if I call this, then $o comes back with the #text and is what I want, but I need the attributes to figure out the context.  If I add this line, I can't figure out how to get the actual value.
//            $properties = $reader->parseAttributes(); 
            $o = \Sabre\Xml\Deserializer\valueObject($reader, $className, $namespace);

// hacking away here.
            if ($className == ProfileSetting::class) {
                if ($reader->name === '#text') {
                    $_value = $reader->value;
                }
                dump($o, $properties, $className, $reader->name, $_value);
            }


            if (count($properties)) {
                if (array_key_exists('fieldWidth', $properties)) {
                    dd($properties, $o);
                }
                $o->setAttributes($properties);
            }

//            foreach($properties as $key=>$value) {
//                $o->$key = $value;
//                if (isset($o->{$key})) {
//                    $o->$key = $value;
//                }
//            }

            return $o;

        };
        $this->classMap[$className] = function (Writer $writer, $valueObject) use ($namespace) {
            return \Sabre\Xml\Serializer\valueObject($writer, $valueObject, $namespace);
        };
        $this->valueObjectMap[$className] = $elementName;
    }

So I should be able to use in my class

  $this->mapValueObject($ca . 'setting', ProfileSetting::class);

I'd like to suggest that this is such a common situation that perhaps you could make that the default, or at least the example. You could put the attributes in a '@attributes' property (like SimpleXML), or some convention like $_, which is probably what I'll do when I figure this out.

Thanks!

Tac

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

No branches or pull requests

1 participant