Skip to content

Commit

Permalink
Avoid using named constructors (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
markuspoerschke authored Sep 27, 2020
1 parent c881fd6 commit 3e4130c
Show file tree
Hide file tree
Showing 27 changed files with 149 additions and 155 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ $event = (new Eluceo\iCal\Domain\Entity\Event())
->setSummary('Christmas Eve')
->setDescription('Lorem Ipsum Dolor...')
->setOccurrence(
Eluceo\iCal\Domain\ValueObject\SingleDay::fromDate(
Eluceo\iCal\Domain\ValueObject\Date::fromDateTimeInterface(
new Eluceo\iCal\Domain\ValueObject\SingleDay(
new Eluceo\iCal\Domain\ValueObject\Date(
\DateTimeImmutable::createFromFormat('Y-m-d', '2030-12-24')
)
)
);

// 2. Create Calendar domain entity
$calendar = Eluceo\iCal\Domain\Entity\Calendar::create([$event]);
$calendar = new Eluceo\iCal\Domain\Entity\Calendar([$event]);

// 3. Transform domain entity into an iCalendar component
$componentFactory = new Eluceo\iCal\Presentation\Factory\CalendarFactory();
Expand Down
26 changes: 13 additions & 13 deletions docs/components/event.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use Eluceo\iCal\Domain\ValueObject\SingleDay;
$event = (new Event())
->setSummary('Lunch Meeting')
->setDescription('Lorem Ipsum...')
->setOccurrence(SingleDay::fromDate(Date::fromCurrentDay()));
->setOccurrence(new SingleDay(new Date()));
```

## Properties
Expand Down Expand Up @@ -74,7 +74,7 @@ use Eluceo\iCal\Domain\ValueObject\Timestamp;
use Eluceo\iCal\Domain\Entity\Event;

$event = new Event();
$event->touch(Timestamp::fromCurrentTime());
$event->touch(new Timestamp());
```

A timestamp object can be also created from an object that implements `\DateTimeInterface` like this:
Expand All @@ -85,7 +85,7 @@ use Eluceo\iCal\Domain\ValueObject\Timestamp;

$event = new Event();
$dateTime = DateTimeImmutable::createFromFormat('Y-m-d', '2019-12-24');
$timestamp = Timestamp::fromDateTimeInterface($dateTime);
$timestamp = new Timestamp($dateTime);
$event->touch($timestamp);
```

Expand Down Expand Up @@ -131,8 +131,8 @@ use Eluceo\iCal\Domain\ValueObject\SingleDay;
use Eluceo\iCal\Domain\ValueObject\Date;
use Eluceo\iCal\Domain\Entity\Event;

$date = Date::fromDateTimeInterface(DateTimeImmutable::createFromFormat('Y-m-d', '2019-12-24'));
$occurrence = SingleDay::fromDate($date);
$date = new Date(DateTimeImmutable::createFromFormat('Y-m-d', '2019-12-24'));
$occurrence = new SingleDay($date);

$event = new Event();
$event->setOccurrence($occurrence);
Expand All @@ -143,7 +143,7 @@ $event->setOccurrence($occurrence);
A multi day event will take place on more than one consecutive day.
The multi day occurrence defines a span of days.

The named constructor `MultiDay::fromDates()` accepts two dates:
The constructor `MultiDay($firstDay, $lastDay)` accepts two dates:

- The `$firstDay` attribute defines the first inclusive day, the event will take place.
- The `$lastDay` attribute defines the last inclusive day, the event will take place.
Expand All @@ -155,9 +155,9 @@ use Eluceo\iCal\Domain\ValueObject\MultiDay;
use Eluceo\iCal\Domain\ValueObject\Date;
use Eluceo\iCal\Domain\Entity\Event;

$firstDay = Date::fromDateTimeInterface(DateTimeImmutable::createFromFormat('Y-m-d', '2019-12-24'));
$lastDay = Date::fromDateTimeInterface(DateTimeImmutable::createFromFormat('Y-m-d', '2019-12-26'));
$occurrence = MultiDay::fromDates($firstDay, $lastDay);
$firstDay = new Date(DateTimeImmutable::createFromFormat('Y-m-d', '2019-12-24'));
$lastDay = new Date(DateTimeImmutable::createFromFormat('Y-m-d', '2019-12-26'));
$occurrence = new MultiDay($firstDay, $lastDay);

$event = new Event();
$event->setOccurrence($occurrence);
Expand All @@ -178,9 +178,9 @@ use Eluceo\iCal\Domain\ValueObject\TimeSpan;
use Eluceo\iCal\Domain\ValueObject\DateTime;
use Eluceo\iCal\Domain\Entity\Event;

$start = DateTime::fromDateTimeInterface(DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-03 13:00:00'));
$end = DateTime::fromDateTimeInterface(DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-03 14:00:00'));
$occurrence = TimeSpan::create($start, $end);
$start = new DateTime(DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-03 13:00:00'));
$end = new DateTime(DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2020-01-03 14:00:00'));
$occurrence = new TimeSpan($start, $end);

$event = new Event();
$event->setOccurrence($occurrence);
Expand All @@ -202,7 +202,7 @@ use Eluceo\iCal\Domain\ValueObject\GeographicPosition;
$location = new Location('North Pole');

// optionally a location with a geographical position can be created
$location = $location->withGeographicPosition(GeographicPosition::fromLatitudeAndLongitude(64.751111,147.349444));
$location = $location->withGeographicPosition(new GeographicPosition(64.751111, 147.349444));

$event = new Event();
```
6 changes: 2 additions & 4 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ $event = new Event();
$event
->setSummary('Christmas Eve')
->setOccurrence(
SingleDay::fromDate(
Date::fromDateTimeInterface(
DateTimeImmutable::createFromFormat('Y-m-d', '2030-12-24')
)
new SingleDay(
new Date(DateTimeImmutable::createFromFormat('Y-m-d', '2030-12-24'))
)
);

Expand Down
10 changes: 3 additions & 7 deletions examples/example1.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,9 @@
->setSummary('Christmas Eve')
->setDescription('Lorem Ipsum Dolor...')
->setOccurrence(
TimeSpan::create(
DateTime::fromDateTimeInterface(
DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2030-12-24 13:30:00')
),
DateTime::fromDateTimeInterface(
DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2030-12-24 14:30:00')
)
new TimeSpan(
new DateTime(DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2030-12-24 13:30:00')),
new DateTime(DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2030-12-24 14:30:00'))
)
)
;
Expand Down
2 changes: 1 addition & 1 deletion examples/example3.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
for ($i = 0; $i < 10; ++$i) {
yield (new Event())
->setSummary('Event ' . $i)
->setOccurrence(SingleDay::fromDate(Date::fromDateTimeInterface($day)))
->setOccurrence(new SingleDay(new Date($day)))
;
$day = $day->add($dayInterval);
}
Expand Down
8 changes: 2 additions & 6 deletions src/Domain/Entity/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Event
public function __construct(?UniqueIdentifier $uniqueIdentifier = null)
{
$this->uniqueIdentifier = $uniqueIdentifier ?? UniqueIdentifier::createRandom();
$this->touchedAt = Timestamp::fromCurrentTime();
$this->touchedAt = new Timestamp();
}

public function getUniqueIdentifier(): ?UniqueIdentifier
Expand All @@ -43,11 +43,7 @@ public function getTouchedAt(): Timestamp

public function touch(?Timestamp $dateTime = null): self
{
if ($dateTime === null) {
$dateTime = Timestamp::fromCurrentTime();
}

$this->touchedAt = $dateTime;
$this->touchedAt = $dateTime ?? new Timestamp();

return $this;
}
Expand Down
22 changes: 0 additions & 22 deletions src/Domain/ValueObject/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,10 @@
namespace Eluceo\iCal\Domain\ValueObject;

use DateInterval;
use DateTimeImmutable as PhpDateTimeImmutable;
use DateTimeInterface as PhpDateTimeInterface;
use InvalidArgumentException;
use RuntimeException;

final class Date extends PointInTime
{
public static function fromDateTimeInterface(PhpDateTimeInterface $dateTime): self
{
$dateTime = PhpDateTimeImmutable::createFromFormat(
PhpDateTimeInterface::ATOM,
$dateTime->format(PhpDateTimeInterface::ATOM), $dateTime->getTimezone()
);

if ($dateTime === false) {
throw new RuntimeException('Unexpected date time value.');
}

return new self($dateTime);
}

public static function fromCurrentDay(): self
{
return self::fromDateTimeInterface(new PhpDateTimeImmutable());
}

public function add(DateInterval $interval): self
{
if (
Expand Down
7 changes: 1 addition & 6 deletions src/Domain/ValueObject/GeographicPosition.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class GeographicPosition
private float $latitude;
private float $longitude;

private function __construct(float $latitude, float $longitude)
public function __construct(float $latitude, float $longitude)
{
$this->latitude = $latitude;
$this->longitude = $longitude;
Expand All @@ -32,11 +32,6 @@ private function __construct(float $latitude, float $longitude)
}
}

public static function fromLatitudeAndLongitude(float $latitude, float $longitude): self
{
return new static($latitude, $longitude);
}

public function getLatitude(): float
{
return $this->latitude;
Expand Down
5 changes: 0 additions & 5 deletions src/Domain/ValueObject/MultiDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ public function __construct(Date $firstDay, Date $lastDay)
$this->lastDay = $lastDay;
}

public static function fromDates(Date $firstDay, Date $lastDay): self
{
return new static($firstDay, $lastDay);
}

public function getFirstDay(): Date
{
return $this->firstDay;
Expand Down
10 changes: 8 additions & 2 deletions src/Domain/ValueObject/PointInTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use DateInterval;
use DateTimeImmutable as PhpDateTimeImmutable;
use DateTimeInterface as PhpDateTimeInterface;
use Eluceo\iCal\Util\DateTimeImmutableFactory;

/**
* @internal
Expand All @@ -21,9 +23,13 @@ abstract class PointInTime
{
private PhpDateTimeImmutable $dateTime;

final protected function __construct(PhpDateTimeImmutable $dateTime)
public function __construct(PhpDateTimeInterface $dateTime = null)
{
$this->dateTime = $dateTime;
if ($dateTime === null) {
$dateTime = new PhpDateTimeImmutable();
}

$this->dateTime = DateTimeImmutableFactory::createFromInterface($dateTime);
}

public function getDateTime(): PhpDateTimeImmutable
Expand Down
7 changes: 1 addition & 6 deletions src/Domain/ValueObject/SingleDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class SingleDay extends Occurrence
{
private Date $date;

private function __construct(Date $date)
public function __construct(Date $date)
{
$this->date = $date;
}
Expand All @@ -24,9 +24,4 @@ public function getDate(): Date
{
return $this->date;
}

public static function fromDate(Date $date): self
{
return new static($date);
}
}
2 changes: 1 addition & 1 deletion src/Domain/ValueObject/TimeSpan.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class TimeSpan extends Occurrence
private DateTime $begin;
private DateTime $end;

private function __construct(DateTime $begin, DateTime $end)
public function __construct(DateTime $begin, DateTime $end)
{
$this->begin = $begin;
$this->end = $end;
Expand Down
22 changes: 0 additions & 22 deletions src/Domain/ValueObject/Timestamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,6 @@

namespace Eluceo\iCal\Domain\ValueObject;

use DateTimeImmutable as PhpDateTimeImmutable;
use DateTimeInterface as PhpDateTimeInterface;
use RuntimeException;

class Timestamp extends PointInTime
{
public static function fromDateTimeInterface(PhpDateTimeInterface $dateTime): self
{
$dateTime = PhpDateTimeImmutable::createFromFormat(
PhpDateTimeInterface::ATOM,
$dateTime->format(PhpDateTimeInterface::ATOM), $dateTime->getTimezone()
);

if ($dateTime === false) {
throw new RuntimeException('Unexpected date time value.');
}

return new static($dateTime);
}

public static function fromCurrentTime(): self
{
return static::fromDateTimeInterface(new PhpDateTimeImmutable());
}
}
5 changes: 0 additions & 5 deletions src/Domain/ValueObject/UniqueIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ public function __construct(string $uid)
$this->uid = $uid;
}

public static function fromString(string $uid): self
{
return new static($uid);
}

public static function createRandom(): self
{
if (function_exists('uuid_create') && defined('UUID_TYPE_RANDOM')) {
Expand Down
6 changes: 3 additions & 3 deletions src/Presentation/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ public function getIterator()

protected function getContentLines(): Generator
{
yield ContentLine::fromString('BEGIN:' . $this->componentName);
yield new ContentLine('BEGIN:' . $this->componentName);
yield from $this->getContentLinesGenerator();
yield ContentLine::fromString('END:' . $this->componentName);
yield new ContentLine('END:' . $this->componentName);
}

protected function getContentLinesGenerator(): Generator
{
yield from array_map(
[ContentLine::class, 'fromString'],
fn (string $string) => new ContentLine($string),
array_map('strval', $this->properties)
);
}
Expand Down
Loading

0 comments on commit 3e4130c

Please sign in to comment.