Skip to content

Instantly share code, notes, and snippets.

@merk
Created December 2, 2011 10:37
Show Gist options
  • Save merk/1422751 to your computer and use it in GitHub Desktop.
Save merk/1422751 to your computer and use it in GitHub Desktop.
<?php
/**
* Infinite Networks Pty Ltd
*/
namespace Infinite\BookingBundle\Entity;
use Doctrine\ORM\Mapping AS ORM;
/**
* Indicates a single availability period.
*
* The period can be specified as just a single day of the
* week where the record will be considered for every occurrence
* of that day.
*
* If day of week is not specified, the availability will set
* based on the start and end dates.
*
* @ORM\Entity
* @ORM\Table(name="booking_availability")
*/
class Availability
{
const DAY_SUNDAY = 0;
const DAY_MONDAY = 1;
const DAY_TUESDAY = 2;
const DAY_WEDNESDAY = 3;
const DAY_THURSDAY = 4;
const DAY_FRIDAY = 5;
const DAY_SATURDAY = 6;
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
* @var int
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Resource", inversedBy="availabilities")
* @ORM\JoinColumn(nullable=false)
* @var Resource
*/
protected $resource;
/**
* Indicates that the resource is available depending
* on the conditions met from the below variables.
*
* @ORM\Column(type="boolean")
* @var bool
*/
protected $available = false;
/**
* If this variable is set, the algorithm will only
* include the specific day of the week for this
* record.
*
* If start date and end date are set, along with this
* field being set to Monday, only Mondays inside the
* date range will be considered to be covered.
*
* Base 0 with 0 being Sunday, 6 being Saturday.
*
* @ORM\Column(type="integer", nullable=true)
* @var int
*/
protected $dayOfWeek;
/**
* Represents the start date for this availability
* record.
*
* Must be set if dayOfWeek is unset.
*
* @ORM\Column(type="datetime", nullable=true)
* @var \DateTime
*/
protected $startDate;
/**
* Represents the end date for this availability
* record.
*
* Must be set if dayOfWeek is unset.
*
* @ORM\Column(type="datetime", nullable=true)
* @var \DateTime
*/
protected $endDate;
/**
* Represents the starting time for each day of
* this record.
*
* Only persists the H:i:s component of the DateTime
* object.
*
* @ORM\Column(type="time", nullable=true)
* @var \DateTime
*/
protected $startTime;
/**
* Represents the ending time for each day of this
* record.
*
* Only persists the H:i:s component of the DateTime
* object.
*
* @ORM\Column(type="time", nullable=true)
* @var \DateTime
*/
protected $endTime;
public function __toString()
{
$available = $this->available ? 'Yes' : 'No';
$startDate = $this->getStartDate() ? $this->getStartDate()->format('Y-m-d') : 'NULL';
$endDate = $this->getEndDate() ? $this->getEndDate()->format('Y-m-d') : 'NULL';
$dateRange = sprintf('%s to %s', $startDate, $endDate);
$startTime = $this->getStartTime() ? $this->getStartTime()->format('H:i:s') : 'NULL';
$endTime = $this->getEndTime() ? $this->getEndTime()->format('H:i:s') : 'NULL';
$timeRange = sprintf('%s to %s', $startTime, $endTime);
return sprintf('%s: (%s / %s)', $available, $dateRange, $timeRange);
}
/**
* @param \DateTime $endTime
*/
public function setEndTime(\DateTime $endTime = null)
{
$this->endTime = $endTime;
}
/**
* @return \DateTime
*/
public function getEndTime()
{
return $this->endTime;
}
/**
* @param \DateTime $startTime
*/
public function setStartTime(\DateTime $startTime = null)
{
$this->startTime = $startTime;
}
/**
* @return \DateTime
*/
public function getStartTime()
{
return $this->startTime;
}
/**
* @param \DateTime $endDate
*/
public function setEndDate(\DateTime $endDate = null)
{
$this->endDate = $endDate;
}
/**
* @return \DateTime
*/
public function getEndDate()
{
return $this->endDate;
}
/**
* @param \DateTime $startDate
*/
public function setStartDate(\DateTime $startDate = null)
{
$this->startDate = $startDate;
}
/**
* @return \DateTime
*/
public function getStartDate()
{
return $this->startDate;
}
/**
* @return int
*/
public function getDayOfWeek()
{
return $this->dayOfWeek;
}
/**
* @param int $dayOfWeek
*/
public function setDayOfWeek($dayOfWeek)
{
if (null !== $dayOfWeek && !in_array($dayOfWeek, array_keys(static::getDaysOfWeek()))) {
throw new \InvalidArgumentException(sprintf(
'%i is an invalid day of the week.', $dayOfWeek
));
}
$this->dayOfWeek = $dayOfWeek;
}
/**
* Returns an array of days that are suitable for the
* $dayOfWeek variable.
*
* @static
* @return array
*/
public static function getDaysOfWeek()
{
return array(
static::DAY_SUNDAY => 'Sunday',
static::DAY_MONDAY => 'Monday',
static::DAY_TUESDAY => 'Tuesday',
static::DAY_WEDNESDAY => 'Wednesday',
static::DAY_THURSDAY => 'Thursday',
static::DAY_FRIDAY => 'Friday',
static::DAY_SATURDAY => 'Saturday',
);
}
/**
* Indicates if this record is indicating availability
* or unavailability.
*
* @return bool
*/
public function isAvailable()
{
return $this->available;
}
/**
* Sets if this record will indicate availability or
* unavailability.
*
* @param bool $available
*/
public function setAvailable($available)
{
$this->available = (bool) $available;
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param \Infinite\BookingBundle\Entity\Resource $resource
*/
public function setResource(Resource $resource)
{
$this->resource = $resource;
}
/**
* @return \Infinite\BookingBundle\Entity\Resource
*/
public function getResource()
{
return $this->resource;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment