/** * 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_correctly_handles_converter_priorities() { $this->app['config']->set('laravel-xml.converters.custom.models', ['priority' => 1000, 'value' => CustomConverter::class]); $this->app['config']->set('laravel-xml.converters.custom.objects', ['priority' => 1001, 'value' => CustomConverter::class]); $this->app['config']->set('laravel-xml.converters.custom.collections', ['priority' => 1001, 'value' => CustomConverter::class]); $this->app['config']->set('laravel-xml.converters.custom.Illuminate\\Database\\Eloquent\\Collection', ['priority' => 1001, 'value' => CustomCollectionConverter::class]); $this->app['config']->set('laravel-xml.converters.custom.TestObject', ['priority' => 1001, 'value' => CustomObjectConverter::class]); $this->assertEquals(CustomConverter::class, Xml::getConverterFor(new Collection())); $this->assertInstanceOf(CustomConverter::class, app(Xml::getConverterFor(new Collection()))); $this->assertEquals(CustomCollectionConverter::class, Xml::getConverterFor(new \Illuminate\Database\Eloquent\Collection())); $this->assertInstanceOf(CustomCollectionConverter::class, app(Xml::getConverterFor(new \Illuminate\Database\Eloquent\Collection()))); $this->assertEquals(CustomConverter::class, Xml::getConverterFor(new TestModel())); $this->assertInstanceOf(CustomConverter::class, app(Xml::getConverterFor(new TestModel()))); $this->assertEquals(CustomObjectConverter::class, Xml::getConverterFor(new TestObject())); $this->assertInstanceOf(CustomObjectConverter::class, app(Xml::getConverterFor(new TestObject()))); }
/** * Intelligent key technology... such a boring name * Only use if absolutely necessary!!! * * This is really quite intelligent *sarcasm* * * @param $value */ protected function intelligent_key($value) { return Xml::getType($value); }