diff --git a/README.md b/README.md index 0778277..50760a9 100644 --- a/README.md +++ b/README.md @@ -102,7 +102,7 @@ $locations = $client->space()->locations()->cache()->send(); It is recommended to consult your Simple Cache client's documentation on how to best set up the cache to preserve entries between processes. ## Limitations -This client currently only implements the Spaces/Seats endpoints under `/space`. +This client currently only implements the Spaces/Seats endpoints under `/space` and and the Hours endpoint at `/hours`. ## Related Projects [An application that provides a public interface for space booking](https://github.com/bgsu-lits/book) using this library is also available from the BGSU University Libraries. diff --git a/src/Action/HoursAction.php b/src/Action/HoursAction.php new file mode 100644 index 0000000..c400b4d --- /dev/null +++ b/src/Action/HoursAction.php @@ -0,0 +1,115 @@ +from = self::buildDateString($from); + + return $this; + } + + /** + * Set the date to retrieve hours to. + * + * @param string|\DateTimeInterface|null $to Value to set. Strings should + * be in yyyy-mm-dd format + * @return self A reference to this object for method chaining. + * @throws ActionException If an invalid date is specified. + */ + final public function setTo($to): self + { + $this->to = self::buildDateString($to); + + return $this; + } + + /** + * @return HoursData[] + * @throws \Lits\LibCal\Exception\ClientException + */ + final public function send(): array + { + $uri = '/' . Client::VERSION . '/hours'; + $uri = $this->addId($uri); + $uri = self::addQuery($uri, 'from', $this->from); + $uri = self::addQuery($uri, 'to', $this->to); + + /** @var HoursData[] $result */ + $result = $this->memoize( + $uri, + fn (string $uri) => HoursData::fromJsonAsArray( + $this->client->get($uri) + ) + ); + + return $result; + } + + /** + * Return a date string in 'yyyy-mm-dd' format. + * + * @param string|\DateTimeInterface|null $date The date. + * @return string|null The date in 'yyyy-mm-dd' format. Null if null input. + * @throws ActionException If an invalid date is specified. + */ + private static function buildDateString($date): ?string + { + if (\is_null($date)) { + return null; + } + + if ($date instanceof \DateTimeInterface) { + return $date->format('Y-m-d'); + } + + if (!self::dateStringIsValid($date)) { + throw new ActionException('Invalid date specified'); + } + + return $date; + } + + /** + * Is string date in 'yyyy-mm-dd' format? + * + * @param string $date Value to set. + * @return bool true if the string is in 'yyyy-mm-dd' format, + * false if not + */ + private static function dateStringIsValid(string $date): bool + { + $date = \filter_var( + $date, + \FILTER_VALIDATE_REGEXP, + ['options' => ['regexp' => '/^\d{4}-\d{2}-\d{2}$/']] + ); + + return $date !== false; + } +} diff --git a/src/Client.php b/src/Client.php index 9cb7af8..73c9553 100644 --- a/src/Client.php +++ b/src/Client.php @@ -4,6 +4,7 @@ namespace Lits\LibCal; +use Lits\LibCal\Action\HoursAction; use Lits\LibCal\Action\SpaceAction; use Lits\LibCal\Data\TokenData; use Lits\LibCal\Exception\ClientException; @@ -182,6 +183,17 @@ public function space(): SpaceAction return new SpaceAction($this); } + /** + * Retrieve action for LibCal Hours API. + * + * @param int|int[] $id The id of the location to retrieve. + * @return HoursAction Hours lookup action. + */ + public function hours($id): HoursAction + { + return new HoursAction($this, $id); + } + /** * Get any memoized value from cache or memory. * diff --git a/src/Data/Hours/DateData.php b/src/Data/Hours/DateData.php new file mode 100644 index 0000000..007ed2a --- /dev/null +++ b/src/Data/Hours/DateData.php @@ -0,0 +1,17 @@ +