I have the following code example which defines a Contact class that can hold a name and a list of phone numbers. What I want to be able to do is set an attribute of "primary" on all the numbers, where the value for it is '1' for one of them arbitrarily, and '0' for the rest.
I'd really like to do this using a class method but couldn't find a way to do this using a setter - is it possible somehow?
Here's my code. In it I:
- Create my objects and set their properies
- print_r the Contact object to see how it is stored
- Deserialize the xml that I'd like to be able to generate into the same Contact object
- print_r the Contact object to see how it is stored
- Add another number using setter
- print_r the Contact object to see how it is stored
- Serialize to xml and see how the mixture of objects and arrays are treated
- print the xml
<?php
// Symfony Serializer experiment
require $_SERVER['DOCUMENT_ROOT'].'/libraries/symfony/vendor/autoload.php';
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
Class Number {
private $Number;
private $Type;
// Getters
public function getNumber()
{
return $this->Number;
}
public function getType()
{
return $this->Type;
}
// Setters
public function setNumber($number)
{
$this->Number = $number;
return $this;
}
public function setType($type)
{
$this->Type = $type;
return $this;
}
}
class Contact
{
private $Name;
private $Numbers = array();
function __construct() {
${@Primary} = 0;
}
// Getters
public function getName()
{
return $this->Name;
}
public function getNumbers()
{
return $this->Numbers;
}
// Setters
public function setName($name)
{
$this->Name = $name;
return $this;
}
public function setNumbers($numbers)
{
$this->Numbers = $numbers;
return $this;
}
public function addNumber($number) {
$this->Numbers['Number'][] = $number;
}
}
// Serializer setup
$xmlEncoder = new XmlEncoder();
$xmlEncoder->setRootNodeName('Contact');
$encoders = array($xmlEncoder, new JsonEncoder());
$normalizers = array(new ObjectNormalizer());
$serializer = new Serializer($normalizers, $encoders);
// Create my object
$contact = new Contact();
$contact->setName('baa');
$number = new Number();
$number->setNumber('07878386123')->setType('Mobile');
$contact->addNumber($number);
// take a look at how it's stored
echo "<pre lang=xml>";
print_r($contact);
echo "</pre><br><br>";
// deserialize my desired xml into the same object
$data = <<<EOF
<Contact>
<Name>foo</Name>
<Numbers>
<Number primary='1'>
<Number>07878386459</Number>
<Type>Mobile</Type>
</Number>
<Number primary='0'>
<Number>02380860835</Number>
<Type>Landline</Type>
</Number>
</Numbers>
</Contact>
EOF;
$contact = $serializer->deserialize($data, Contact::class, 'xml', array('object_to_populate' => $contact));
// take a look at how it's stored
echo "<pre lang=xml>";
print_r($contact);
echo "</pre><br><br>";
// Add another number
$number = new Number();
$number->setNumber('02387345123')->setType('Landline');
$contact->addNumber($number);
// take a look at how it's stored
echo "<pre lang=xml>";
print_r($contact);
echo "</pre><br><br>";
// serialize to xml and see how the mixture of objects and arrays are treated
$xmlContent = $serializer->serialize($contact, 'xml');
$dom = new domDocument( '1.0', 'utf-8' );
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$result = $dom->loadXML($xmlContent);
$xmlContentFormatted=htmlentities($dom->saveXML());
?>
<pre lang=xml><?= $xmlContentFormatted ?></pre>
What I see in the output is:
2)
Contact Object
(
[Name:Contact:private] => baa
[Numbers:Contact:private] => Array
(
[Number] => Array
(
[0] => Number Object
(
[Number:Number:private] => 07878386123
[Type:Number:private] => Mobile
)
)
)
)
4)
Contact Object
(
[Name:Contact:private] => foo
[Numbers:Contact:private] => Array
(
[Number] => Array
(
[0] => Array
(
[@primary] => 1
[Number] => 07878386459
[Type] => Mobile
)
[1] => Array
(
[@primary] => 0
[Number] => 02380860835
[Type] => Landline
)
)
)
6) Mixture of objects and arrays. Could I code my Number class so that it uses an array rather than properties and have another method for setting attribute? (eg
$number->setNumber('07878386123')->setType('Mobile')->addAttr(array('primary' => '1'));
Contact Object
(
[Name:Contact:private] => foo
[Numbers:Contact:private] => Array
(
[Number] => Array
(
[0] => Array
(
[@primary] => 1
[Number] => 07878386459
[Type] => Mobile
)
[1] => Array
(
[@primary] => 0
[Number] => 02380860835
[Type] => Landline
)
[2] => Number Object
(
[Number:Number:private] => 02387345123
[Type:Number:private] => Landline
)
)
)
)
8)
<?xml version="1.0"?>
<Contact>
<Name>foo</Name>
<Numbers>
<Number primary="1">
<Number>07878386459</Number>
<Type>Mobile</Type>
</Number>
<Number primary="0">
<Number>02380860835</Number>
<Type>Landline</Type>
</Number>
<Number>
<Number>02387345123</Number>
<Type>Landline</Type>
</Number>
</Numbers>
</Contact>
When deserializing from XML that has tags that use attributes, the php objects use arrays, so it would appear that if you want to start out with a php representation that can be given attributes you need to use arrays.