pax_global_header00006660000000000000000000000064135764441250014525gustar00rootroot0000000000000052 comment=47f55860678a9e202206047bc02767556d298106 link-util-1.1.0/000077500000000000000000000000001357644412500134345ustar00rootroot00000000000000link-util-1.1.0/.editorconfig000066400000000000000000000004171357644412500161130ustar00rootroot00000000000000; This file is for unifying the coding style for different editors and IDEs. ; More information at http://editorconfig.org root = true [*] charset = utf-8 indent_size = 4 indent_style = space end_of_line = lf insert_final_newline = true trim_trailing_whitespace = true link-util-1.1.0/LICENSE.md000066400000000000000000000021511357644412500150370ustar00rootroot00000000000000# The MIT License (MIT) Copyright (c) 2016 :author_name <:author_email> > Permission is hereby granted, free of charge, to any person obtaining a copy > of this software and associated documentation files (the "Software"), to deal > in the Software without restriction, including without limitation the rights > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell > copies of the Software, and to permit persons to whom the Software is > furnished to do so, subject to the following conditions: > > The above copyright notice and this permission notice shall be included in > all copies or substantial portions of the Software. > > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN > THE SOFTWARE. link-util-1.1.0/README.md000066400000000000000000000004651357644412500147200ustar00rootroot00000000000000PSR Http Link Utilities ======================= This repository includes common utilities to assist with implementing [PSR-13](http://www.php-fig.org/psr/psr-13/). Note that it is not intended as a complete PSR-13 implementation, only a partial implementation to make writing other implementations easier. link-util-1.1.0/composer.json000066400000000000000000000015311357644412500161560ustar00rootroot00000000000000{ "name": "fig/link-util", "description": "Common utility implementations for HTTP links", "keywords": ["psr", "psr-13", "http", "http-link", "link", "rest"], "license": "MIT", "authors": [ { "name": "PHP-FIG", "homepage": "http://www.php-fig.org/" } ], "require": { "php": ">=5.5.0", "psr/link": "~1.0@dev" }, "require-dev": { "phpunit/phpunit": "^5.1", "squizlabs/php_codesniffer": "^2.3.1" }, "provide": { "psr/link-implementation": "1.0" }, "autoload": { "psr-4": { "Fig\\Link\\": "src/" } }, "autoload-dev": { "psr-4": { "Fig\\Link\\Test\\": "test/" } }, "extra": { "branch-alias": { "dev-master": "1.0.x-dev" } } } link-util-1.1.0/phpcs.xml000066400000000000000000000010621357644412500152720ustar00rootroot00000000000000 PSR-2 coding standards src test link-util-1.1.0/rebuild-rels.php000066400000000000000000000131321357644412500165360ustar00rootroot00000000000000compile($records(), $out); } class Records { /** * Loads the XML object from the remote source. * * IANA requires a valid User-Agent string for all requests, and non-curl stream options for PHP seem to all * fail at sending a User-Agent, even when told to. So we use the ugly way. */ protected function getXml(): \SimpleXMLElement { $ch = curl_init(REGISTRY_FILE); curl_setopt($ch, CURLOPT_USERAGENT, 'php-fig/fig-utils'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $xml = curl_exec($ch); return simplexml_load_string($xml); } /** * Fetches the records for the relation registry. * * @return iterable<\SimpleXMLElement> */ public function __invoke() { foreach ($this->getXml()->registry->children() as $element) { if ($element->getName() == 'record') { yield $element; } } } } /** * Compiles a list of SimpleXmlElements to a Relations interface. */ class RegistryCompiler { /** * Writes a Relations index class based on the provided records. * * @param iterable<\SimpleXMLElement> $records * An iterable of SimpleXml elements from the Link Relations XML file. * @param $stream * An open file stream to which to write the generated code. * @param string $class * The name of the interface to produce. * @param string $namespace * The namespace of the interface to generate. */ public function compile(iterable $records, $stream, string $class = 'Relations', string $namespace = 'Fig\\Link') : void { fwrite($stream, $this->createPreamble($class, $namespace)); foreach ($records as $record) { $item = $this->createEntry($record); fwrite($stream, $item); } fwrite($stream, $this->createClosing()); } /** * Processes a SimpleXml record into a constant definition. * * @param \SimpleXMLElement $record * The record to process. * @return string */ protected function createEntry(\SimpleXMLElement $record) : string { $value = (string)$record->value; $name = $this->buildName($value); $description = $this->rewrap((string)$record->description); $note = $this->rewrap((string)$record->note); $seeUri = $this->xrefLink($record->spec->xref); if ($note) { return <<links)) { $that->links[$splosh] = $link; } return $that; } /** * {@inheritdoc} */ public function withoutLink(LinkInterface $link) { $that = clone($this); $splosh = spl_object_hash($link); unset($that->links[$splosh]); return $that; } } link-util-1.1.0/src/EvolvableLinkTrait.php000066400000000000000000000031411357644412500204740ustar00rootroot00000000000000href = $href; $that->templated = ($this->hrefIsTemplated($href)); return $that; } /** * {@inheritdoc} * * @return EvolvableLinkInterface */ public function withRel($rel) { /** @var EvolvableLinkInterface $that */ $that = clone($this); $that->rel[$rel] = true; return $that; } /** * {@inheritdoc} * * @return EvolvableLinkInterface */ public function withoutRel($rel) { /** @var EvolvableLinkInterface $that */ $that = clone($this); unset($that->rel[$rel]); return $that; } /** * {@inheritdoc} * * @return EvolvableLinkInterface */ public function withAttribute($attribute, $value) { /** @var EvolvableLinkInterface $that */ $that = clone($this); $that->attributes[$attribute] = $value; return $that; } /** * {@inheritdoc} * * @return EvolvableLinkInterface */ public function withoutAttribute($attribute) { /** @var EvolvableLinkInterface $that */ $that = clone($this); unset($that->attributes[$attribute]); return $that; } } link-util-1.1.0/src/GenericLinkProvider.php000066400000000000000000000014461357644412500206460ustar00rootroot00000000000000links = array_combine($hashes, $links); } } link-util-1.1.0/src/Link.php000066400000000000000000000007611357644412500156350ustar00rootroot00000000000000rel[$rel] = true; } $this->href = $href; } } link-util-1.1.0/src/LinkProviderTrait.php000066400000000000000000000015001357644412500203440ustar00rootroot00000000000000links; } /** * {@inheritdoc} */ public function getLinksByRel($rel) { $filter = function (LinkInterface $link) use ($rel) { return in_array($rel, $link->getRels()); }; return array_filter($this->links, $filter); } } link-util-1.1.0/src/LinkTrait.php000066400000000000000000000022031357644412500166320ustar00rootroot00000000000000href; } /** * {@inheritdoc} */ public function isTemplated() { return $this->hrefIsTemplated($this->href); } /** * {@inheritdoc} */ public function getRels() { return array_keys($this->rel); } /** * {@inheritdoc} */ public function getAttributes() { return $this->attributes; } } link-util-1.1.0/src/Relations.php000066400000000000000000000761401357644412500167040ustar00rootroot00000000000000withHref('http://www.google.com') ->withRel('next') ->withAttribute('me', 'you') ; $provider = (new GenericLinkProvider()) ->withLink($link); $this->assertContains($link, $provider->getLinks()); } public function test_can_add_links_by_constructor() { $link = (new Link()) ->withHref('http://www.google.com') ->withRel('next') ->withAttribute('me', 'you') ; $provider = (new GenericLinkProvider()) ->withLink($link); $this->assertContains($link, $provider->getLinks()); } public function test_can_get_links_by_rel() { $link1 = (new Link()) ->withHref('http://www.google.com') ->withRel('next') ->withAttribute('me', 'you') ; $link2 = (new Link()) ->withHref('http://www.php-fig.org/') ->withRel('home') ->withAttribute('me', 'you') ; $provider = (new GenericLinkProvider()) ->withLink($link1) ->withLink($link2); $links = $provider->getLinksByRel('home'); $this->assertContains($link2, $links); $this->assertFalse(in_array($link1, $links)); } public function test_can_remove_links() { $link = (new Link()) ->withHref('http://www.google.com') ->withRel('next') ->withAttribute('me', 'you') ; $provider = (new GenericLinkProvider()) ->withLink($link) ->withoutLink($link); $this->assertFalse(in_array($link, $provider->getLinks())); } } link-util-1.1.0/test/LinkTest.php000066400000000000000000000032601357644412500166620ustar00rootroot00000000000000withHref('http://www.google.com') ->withRel('next') ->withAttribute('me', 'you') ; $this->assertEquals('http://www.google.com', $link->getHref()); $this->assertContains('next', $link->getRels()); $this->assertArrayHasKey('me', $link->getAttributes()); $this->assertEquals('you', $link->getAttributes()['me']); } public function test_can_remove_values() { $link = (new Link()) ->withHref('http://www.google.com') ->withRel('next') ->withAttribute('me', 'you') ; $link = $link->withoutAttribute('me') ->withoutRel('next'); $this->assertEquals('http://www.google.com', $link->getHref()); $this->assertFalse(in_array('next', $link->getRels())); $this->assertFalse(array_key_exists('me', $link->getAttributes())); } public function test_multiple_rels() { $link = (new Link()) ->withHref('http://www.google.com') ->withRel('next') ->withRel('reference'); $this->assertCount(2, $link->getRels()); $this->assertContains('next', $link->getRels()); $this->assertContains('reference', $link->getRels()); } public function test_constructor() { $link = new Link('next', 'http://www.google.com'); $this->assertEquals('http://www.google.com', $link->getHref()); $this->assertContains('next', $link->getRels()); } } link-util-1.1.0/test/TemplatedHrefTraitTest.php000066400000000000000000000020551357644412500215160ustar00rootroot00000000000000withHref($href); $this->assertTrue($link->isTemplated()); } /** * * @dataProvider notTemplatedHrefProvider * * @param string $href * The href to check. */ public function test_not_templated($href) { $link = (new Link()) ->withHref($href); $this->assertFalse($link->isTemplated()); } public function templatedHrefProvider() { return [ ['http://www.google.com/{param}/foo'], ['http://www.google.com/foo?q={param}'], ]; } public function notTemplatedHrefProvider() { return [ ['http://www.google.com/foo'], ['/foo/bar/baz'], ]; } }