@@ -52,109 +52,109 @@ |
||
| 52 | 52 | |
| 53 | 53 | class HolidayIteratorFactory |
| 54 | 54 | { |
| 55 | - /** @var ItemFromDomElementCreator[] */ |
|
| 56 | - private $factories; |
|
| 57 | - |
|
| 58 | - /** @var DecorateFromDomElement[] */ |
|
| 59 | - private $decorators; |
|
| 60 | - |
|
| 61 | - public function __construct() |
|
| 62 | - { |
|
| 63 | - $this->factories = [ |
|
| 64 | - new EasterFactory(), |
|
| 65 | - new EasterOrthodoxFactory(), |
|
| 66 | - new DateFactory(), |
|
| 67 | - new DateFollowupFactory(), |
|
| 68 | - new RelativeFactory(), |
|
| 69 | - ]; |
|
| 70 | - |
|
| 71 | - $this->decorators = [ |
|
| 72 | - new ObservanceDecoratorFactory(), |
|
| 73 | - new SwapDecoratorFactory(), |
|
| 74 | - ]; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Create a HolidayIterator from an ISO 3166-code. |
|
| 79 | - * |
|
| 80 | - * @param string $isoCode |
|
| 81 | - * |
|
| 82 | - * @return HolidayIterator |
|
| 83 | - */ |
|
| 84 | - public function createIteratorFromIso3166(string $isoCode): HolidayIterator |
|
| 85 | - { |
|
| 86 | - $file = __DIR__ . '/../share/%s.xml'; |
|
| 87 | - $file1 = sprintf($file, $isoCode); |
|
| 88 | - |
|
| 89 | - if (!is_readable($file1)) { |
|
| 90 | - throw new UnexpectedValueException(sprintf( |
|
| 91 | - 'There is no holiday-file for %s', |
|
| 92 | - $isoCode |
|
| 93 | - )); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - return $this->createIteratorFromXmlFile($file1); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Create a HolidayIterator from an XML-File |
|
| 101 | - * |
|
| 102 | - * The provided XML-File has to validate against the holiday.xsd-file you |
|
| 103 | - * can find in this projects "share" folder. |
|
| 104 | - * |
|
| 105 | - * @param string $file |
|
| 106 | - * |
|
| 107 | - * @return HolidayIterator |
|
| 108 | - */ |
|
| 109 | - public function createIteratorFromXmlFile(string $file): HolidayIterator |
|
| 110 | - { |
|
| 111 | - $iterator = new HolidayIterator(); |
|
| 112 | - |
|
| 113 | - $dom = new DOMDocument('1.0', 'UTF-8'); |
|
| 114 | - $dom->load($file); |
|
| 115 | - $dom->xinclude(); |
|
| 116 | - |
|
| 117 | - if (!@$dom->schemaValidate(__DIR__ . '/../share/holidays.xsd')) { |
|
| 118 | - throw new Exception('XML-File does not validate agains schema'); |
|
| 119 | - } |
|
| 120 | - foreach ($dom->documentElement->childNodes as $child) { |
|
| 121 | - if (!$child instanceof DOMElement) { |
|
| 122 | - continue; |
|
| 123 | - } |
|
| 124 | - if ($child->nodeName === 'resources') { |
|
| 125 | - continue; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - try { |
|
| 129 | - $element = $this->getElement($child); |
|
| 130 | - $element = $this->decorateElement($element, $child); |
|
| 131 | - $iterator->append($element); |
|
| 132 | - } catch (Throwable $e) { |
|
| 133 | - // Do nothing on purpose |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - return $iterator; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - private function getElement(DOMElement $child): HolidayIteratorItemInterface |
|
| 141 | - { |
|
| 142 | - foreach ($this->factories as $factory) { |
|
| 143 | - $element = $factory->itemFromDomElement($child); |
|
| 144 | - if ($element instanceof HolidayIteratorItemInterface) { |
|
| 145 | - return $element; |
|
| 146 | - } |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - throw new RuntimeException('Unknown element encountered'); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - private function decorateElement(HolidayIteratorItemInterface $element, DOMElement $child): HolidayIteratorItemInterface |
|
| 153 | - { |
|
| 154 | - foreach ($this->decorators as $decorator) { |
|
| 155 | - $element = $decorator->decorate($element, $child); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - return $element; |
|
| 159 | - } |
|
| 55 | + /** @var ItemFromDomElementCreator[] */ |
|
| 56 | + private $factories; |
|
| 57 | + |
|
| 58 | + /** @var DecorateFromDomElement[] */ |
|
| 59 | + private $decorators; |
|
| 60 | + |
|
| 61 | + public function __construct() |
|
| 62 | + { |
|
| 63 | + $this->factories = [ |
|
| 64 | + new EasterFactory(), |
|
| 65 | + new EasterOrthodoxFactory(), |
|
| 66 | + new DateFactory(), |
|
| 67 | + new DateFollowupFactory(), |
|
| 68 | + new RelativeFactory(), |
|
| 69 | + ]; |
|
| 70 | + |
|
| 71 | + $this->decorators = [ |
|
| 72 | + new ObservanceDecoratorFactory(), |
|
| 73 | + new SwapDecoratorFactory(), |
|
| 74 | + ]; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Create a HolidayIterator from an ISO 3166-code. |
|
| 79 | + * |
|
| 80 | + * @param string $isoCode |
|
| 81 | + * |
|
| 82 | + * @return HolidayIterator |
|
| 83 | + */ |
|
| 84 | + public function createIteratorFromIso3166(string $isoCode): HolidayIterator |
|
| 85 | + { |
|
| 86 | + $file = __DIR__ . '/../share/%s.xml'; |
|
| 87 | + $file1 = sprintf($file, $isoCode); |
|
| 88 | + |
|
| 89 | + if (!is_readable($file1)) { |
|
| 90 | + throw new UnexpectedValueException(sprintf( |
|
| 91 | + 'There is no holiday-file for %s', |
|
| 92 | + $isoCode |
|
| 93 | + )); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + return $this->createIteratorFromXmlFile($file1); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Create a HolidayIterator from an XML-File |
|
| 101 | + * |
|
| 102 | + * The provided XML-File has to validate against the holiday.xsd-file you |
|
| 103 | + * can find in this projects "share" folder. |
|
| 104 | + * |
|
| 105 | + * @param string $file |
|
| 106 | + * |
|
| 107 | + * @return HolidayIterator |
|
| 108 | + */ |
|
| 109 | + public function createIteratorFromXmlFile(string $file): HolidayIterator |
|
| 110 | + { |
|
| 111 | + $iterator = new HolidayIterator(); |
|
| 112 | + |
|
| 113 | + $dom = new DOMDocument('1.0', 'UTF-8'); |
|
| 114 | + $dom->load($file); |
|
| 115 | + $dom->xinclude(); |
|
| 116 | + |
|
| 117 | + if (!@$dom->schemaValidate(__DIR__ . '/../share/holidays.xsd')) { |
|
| 118 | + throw new Exception('XML-File does not validate agains schema'); |
|
| 119 | + } |
|
| 120 | + foreach ($dom->documentElement->childNodes as $child) { |
|
| 121 | + if (!$child instanceof DOMElement) { |
|
| 122 | + continue; |
|
| 123 | + } |
|
| 124 | + if ($child->nodeName === 'resources') { |
|
| 125 | + continue; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + try { |
|
| 129 | + $element = $this->getElement($child); |
|
| 130 | + $element = $this->decorateElement($element, $child); |
|
| 131 | + $iterator->append($element); |
|
| 132 | + } catch (Throwable $e) { |
|
| 133 | + // Do nothing on purpose |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + return $iterator; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + private function getElement(DOMElement $child): HolidayIteratorItemInterface |
|
| 141 | + { |
|
| 142 | + foreach ($this->factories as $factory) { |
|
| 143 | + $element = $factory->itemFromDomElement($child); |
|
| 144 | + if ($element instanceof HolidayIteratorItemInterface) { |
|
| 145 | + return $element; |
|
| 146 | + } |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + throw new RuntimeException('Unknown element encountered'); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + private function decorateElement(HolidayIteratorItemInterface $element, DOMElement $child): HolidayIteratorItemInterface |
|
| 153 | + { |
|
| 154 | + foreach ($this->decorators as $decorator) { |
|
| 155 | + $element = $decorator->decorate($element, $child); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + return $element; |
|
| 159 | + } |
|
| 160 | 160 | } |