/** * Return a label for the given ref * @param Ref $ref * @return string */ public function createLabel(Ref $ref) { $timestamp = $ref->getScheduledAt(); if (null !== $timestamp) { $date = new DateTime(); $date->setTimestamp($timestamp); $timestamp = sprintf("(Scheduled for release on %s)", $date->format('jS F Y')); } return trim(sprintf("%s %s", $ref->getLabel(), $timestamp)); }
/** * @dataProvider getRefs */ public function testGetScheduledDate($json) { $ref = Ref::parse($json); if (!is_null($ref->getScheduledAtTimestamp())) { $date = $ref->getScheduledDate(); $this->assertInstanceOf('DateTime', $date); $this->assertSame($ref->getScheduledAtTimestamp(), $date->getTimestamp()); $this->assertNotSame($date, $ref->getScheduledDate(), 'Returned date should be a new instance every time'); } else { $this->assertNull($ref->getScheduledDate()); } }
/** * This is the endpoint to build your API, and is a static method. * If your API is set to "public" or "open", you can instantiate your Api object just like this: * Api::get('http://idofyourrepository.prismic.io/api') * * @api * * @param string $action the URL of your repository API's endpoint * @param string $accessToken a permanent access token to use to access your content, for instance if your repository API is set to private * @param HttpAdapterInterface $httpAdapter by default, the HTTP adapter uses CURL with a certain configuration, but you can override it here * @param CacheInterface $cache Cache implementation * @throws \RuntimeException * @return Api the Api object, useable to perform queries */ public static function get($action, $accessToken = null, HttpAdapterInterface $httpAdapter = null, CacheInterface $cache = null) { $cache = is_null($cache) ? self::defaultCache() : $cache; $cacheKey = $action . (is_null($accessToken) ? "" : "#" . $accessToken); $apiData = $cache->get($cacheKey); $api = $apiData ? new Api(unserialize($apiData), $accessToken, $httpAdapter, $cache) : null; if ($api) { return $api; } else { $url = $action . ($accessToken ? '?access_token=' . $accessToken : ''); $httpAdapter = is_null($httpAdapter) ? self::defaultHttpAdapter() : $httpAdapter; $response = $httpAdapter->get($url); $response = json_decode($response->getBody(true)); $experiments = isset($response->experiments) ? Experiments::parse($response->experiments) : new Experiments(array(), array()); if (!$response) { throw new \RuntimeException('Unable to decode the json response'); } $apiData = new ApiData(array_map(function ($ref) { return Ref::parse($ref); }, $response->refs), (array) $response->bookmarks, (array) $response->types, $response->tags, (array) $response->forms, $experiments, $response->oauth_initiate, $response->oauth_token); $api = new Api($apiData, $accessToken, $httpAdapter, $cache); $cache->set($cacheKey, serialize($apiData), 5); return $api; } }
/** * Set the Prismic Ref * @param Ref $ref * @return void */ public function setRef(Ref $ref) { $this->ref = $ref; $this->refString = $ref->getRef(); }