public function createFromArray(array $config) { $calendarName = $this->isCalendarNameProvided($config) ? $config[self::FIELD_CALENDAR_NAME] : null; if ($this->isSeasonAwareConfig($config)) { $builder = new SeasonAwareCalendarBuilder($calendarName); $seasons = $config[self::FIELD_SEASONS]; foreach ($seasons as $seasonData) { $name = $seasonData['name']; $months = []; foreach ($seasonData[self::FIELD_MONTHS] as $monthData) { $months[] = new Month($monthData['name'], $monthData['numberOfDays']); } $builder->addSeason($name, $months); } if (isset($config['firstMonth'])) { $monthName = $config['firstMonth']; $builder->setFirstMonthByName($monthName); } } elseif ($this->isMonthAwareConfig($config)) { $builder = new MonthAwareCalendarBuilder($calendarName); $months = $config[self::FIELD_MONTHS]; foreach ($months as $name => $numberOfDays) { $builder->addMonth($name, $numberOfDays); } } else { throw new InvalidConfigurationException('Configuration must provide seasons or months units. '); } return $builder->buildCalandar(); }
public function testSetFirstMonthByNameWithInvalidValue() { $firstMonth = $this->getMock(MonthInterface::class); $firstMonth->method('getName')->willReturn('first'); $months = [$firstMonth]; $builder = new SeasonAwareCalendarBuilder(); $builder->addSeason('Foo', $months); $builder->setFirstMonthByName('zero'); $this->setExpectedException(InvalidConfigurationException::class); $builder->buildCalandar(); }