|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace CXml\Model; |
| 6 | + |
| 7 | +use Assert\Assertion; |
| 8 | +use CXml\Model\Trait\CommentsTrait; |
| 9 | +use JMS\Serializer\Annotation as Serializer; |
| 10 | +use ReflectionClass; |
| 11 | +use Stringable; |
| 12 | + |
| 13 | +#[Serializer\XmlRoot('Index')] |
| 14 | +#[Serializer\AccessorOrder(order: 'custom', custom: ['supplierId', 'comments', 'indexItems'])] |
| 15 | +class Index implements Stringable |
| 16 | +{ |
| 17 | + use CommentsTrait; |
| 18 | + |
| 19 | + public function __construct( |
| 20 | + #[Serializer\XmlAttribute] |
| 21 | + #[Serializer\SerializedName('loadmode')] |
| 22 | + public readonly string $loadmode, |
| 23 | + #[Serializer\SerializedName('SupplierID')] |
| 24 | + public readonly SupplierId $supplierId, |
| 25 | + /** |
| 26 | + * @var IndexItemWrapper[] |
| 27 | + */ |
| 28 | + #[Serializer\XmlList(entry: 'IndexItem', inline: true)] |
| 29 | + #[Serializer\Type('array<CXml\Model\IndexItemWrapper>')] |
| 30 | + private array $indexItems = [], |
| 31 | + #[Serializer\Exclude] |
| 32 | + public string $dtdUri = 'http://xml.cxml.org/schemas/cXML/1.2.063/cXML.dtd', |
| 33 | + ) { |
| 34 | + Assertion::inArray($this->loadmode, ['Full', 'Incremental']); |
| 35 | + } |
| 36 | + |
| 37 | + public static function create(string $loadmode, SupplierId $supplierId): self |
| 38 | + { |
| 39 | + return new self($loadmode, $supplierId); |
| 40 | + } |
| 41 | + |
| 42 | + public function addIndexItem(IndexItemInterface $indexItem): self |
| 43 | + { |
| 44 | + $shortClsName = (new ReflectionClass($indexItem))->getShortName(); |
| 45 | + |
| 46 | + if (!isset($this->indexItems[$shortClsName])) { |
| 47 | + $this->indexItems[$shortClsName] = new IndexItemWrapper(); |
| 48 | + } |
| 49 | + |
| 50 | + $this->indexItems[$shortClsName]->addIndexItem($indexItem); |
| 51 | + |
| 52 | + return $this; |
| 53 | + } |
| 54 | + |
| 55 | + public function __toString(): string |
| 56 | + { |
| 57 | + return 'Index'; |
| 58 | + } |
| 59 | +} |
0 commit comments