/** * Convert a value to XML. * * @param Collection $value * @param SimpleXMLElement $element * @return SimpleXMLElement * @throws CantConvertValueException */ public function convert($value, SimpleXMLElement $element) : SimpleXMLElement { if (!$value instanceof Collection) { throw new CantConvertValueException("Value is not a collection."); } // Hand off an array form of the collection to another converter. return Xml::convert($value->toArray(), $element); }
/** @test */ public function it_properly_converts_models() { $model = new TestModel(); $model->complexStructure = new Collection(); $model->complexStructure->put('test1', ['test' => ['whatever' => 'test', 'nested' => ['value' => 1, 50000, 'other' => 'testing123', 'array' => [], ['test' => 'testing']]]]); $model->otherProperty = 'testing'; /* @var SimpleXMLElement $result */ $result = Xml::convert($model); $this->assertCount(1, $result->xpath('complexStructure/test1')); }