├── .gitignore ├── .styleci.yml ├── .travis.yml ├── CHANGELOG.md ├── LICENSE ├── README.md ├── composer.json ├── phpstan.neon ├── phpunit.xml.dist ├── src ├── Analysis │ ├── ResponseAnalysis.php │ ├── ResponseAnalysisInterface.php │ ├── ResponseAnalyzer.php │ └── ResponseAnalyzerInterface.php ├── Asset.php ├── AssetFile.php ├── AssetInterface.php ├── Cache │ ├── NullCacheItem.php │ └── NullCacheItemPool.php ├── CanDecorateAssetsInterface.php ├── CanResolveResourcesInterface.php ├── ContentType.php ├── ContentTypeField.php ├── ContentTypeFieldInterface.php ├── ContentTypeInterface.php ├── Contentful.php ├── Decorator │ ├── AbstractDecoratedAsset.php │ ├── AssetDecoratorInterface.php │ ├── CompositeAssetDecorator.php │ └── NullAssetDecorator.php ├── DisallowArrayAccessMutationTrait.php ├── DynamicEntry.php ├── Entry.php ├── EntryInterface.php ├── EntryUnknownMethodTrait.php ├── Exception │ ├── ExceptionInterface.php │ ├── InvalidIdException.php │ ├── LinkUnresolvableException.php │ └── ResourceUnavailableException.php ├── Filter │ ├── AbstractComparisonFilter.php │ ├── AfterFilter.php │ ├── BeforeFilter.php │ ├── ClassBasedNameTrait.php │ ├── ContentTypeFilter.php │ ├── ContentTypeFilterProvider.php │ ├── ContentTypeNameFilter.php │ ├── DecidesCacheKeyInterface.php │ ├── EqualFilter.php │ ├── ExcludeFilter.php │ ├── ExistsFilter.php │ ├── GreaterThanFilter.php │ ├── GreaterThanOrEqualFilter.php │ ├── IncludeFilter.php │ ├── IncompleteTrait.php │ ├── LessThanFilter.php │ ├── LessThanOrEqualFilter.php │ ├── LinksToAssetFilter.php │ ├── LocaleFilter.php │ ├── MimeTypeGroupFilter.php │ ├── NearFilter.php │ ├── NotEqualFilter.php │ ├── PropertyFilter.php │ ├── RelativeTimeFilter.php │ ├── SearchFilter.php │ ├── SimpleValueTrait.php │ ├── WithinCircleFilter.php │ └── WithinRectangleFilter.php ├── FilterInterface.php ├── GuzzleAbstractionTrait.php ├── GuzzleOptions.php ├── ImageApiOptions.php ├── IncompleteParameterInterface.php ├── Link.php ├── LinkInterface.php ├── Locale.php ├── Location.php ├── Log │ ├── LinkResolveCounter.php │ ├── LinkResolveCounterInterface.php │ ├── Log.php │ ├── LogInterface.php │ ├── LoggerInterface.php │ ├── NullLinkResolveCounter.php │ ├── NullLogger.php │ ├── NullTimer.php │ ├── StandardLogger.php │ ├── StandardTimer.php │ └── TimerInterface.php ├── MemoizedResourceEnvelope.php ├── Metadata.php ├── MetadataAccessTrait.php ├── MetadataInterface.php ├── Parameter │ ├── Limit.php │ ├── OrderBy.php │ ├── Query.php │ └── Skip.php ├── ParameterInterface.php ├── Promise │ ├── AssetPromise.php │ ├── ContentTypePromise.php │ ├── DelegatingMetadataPropertyAccessTrait.php │ ├── DelegatingPromiseTrait.php │ ├── EntryPromise.php │ ├── ResolvedResourceTrait.php │ ├── ResourceArrayPromise.php │ ├── ResourcePromise.php │ └── SpacePromise.php ├── Property │ ├── FieldProperty.php │ ├── Locale.php │ ├── MimeTypeGroup.php │ └── SystemProperty.php ├── PropertyInterface.php ├── ResourceArray.php ├── ResourceArrayInterface.php ├── ResourceBuilder.php ├── ResourceEnvelopeInterface.php ├── ResourceEnvelopePool.php ├── ResourceInsertDelegationTrait.php ├── ResourceInterface.php ├── Space.php ├── SpaceInterface.php ├── Webhook.php └── WebhookInterface.php └── tests ├── Analysis ├── ResponseAnalysisTest.php └── ResponseAnalyzerTest.php ├── AssetTest.php ├── ContentTypeFieldTest.php ├── ContentTypeTest.php ├── ContentfulTest.php ├── Decorator ├── CompositeAssetDecoratorTest.php ├── ConcreteDecoratedAsset.php ├── DecoratedAssetTest.php └── NullAssetDecoratorTest.php ├── DynamicEntryTest.php ├── EntryTest.php ├── Filter ├── AfterFilterTest.php ├── BeforeFilterTest.php ├── ContentTypeFilterProviderTest.php ├── ContentTypeFilterTest.php ├── EqualFilterTest.php ├── ExcludeFilterTest.php ├── ExistsFilterTest.php ├── IncludeFilterTest.php ├── LessThanFilterTest.php ├── LessThanOrEqualFilterTest.php ├── LocaleFilterTest.php ├── MimeTypeGroupFilterTest.php ├── NearFilterTest.php ├── NotEqualFilterTest.php ├── SearchFilterTest.php ├── WithinCircleFilterTest.php └── WithinRectangleFilterTest.php ├── ImageApiOptionsTest.php ├── LinkTest.php ├── Log ├── LinkResolveCounterTest.php ├── LogTest.php ├── StandardLoggerTest.php └── StandardTimerTest.php ├── MemoizedResourceEnvelopeTest.php ├── Parameter ├── LimitTest.php ├── OrderByTest.php └── SkipTest.php ├── Promise ├── AssetPromiseTest.php ├── ContentTypePromiseTest.php ├── EntryPromiseTest.php ├── ResourceArrayPromiseTest.php └── SpacePromiseTest.php ├── Property ├── FieldPropertyTest.php ├── LocaleTest.php ├── MimeTypeGroupTest.php └── SystemPropertyTest.php ├── ResourceArrayTest.php ├── ResourceBuilderTest.php ├── ResourceEnvelopePoolTest.php └── WebhookTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | composer.lock 2 | vendor/ 3 | -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: psr2 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | sudo: false 3 | dist: xenial 4 | group: edge 5 | php: 6 | - '7.1' 7 | - '7.2' 8 | - '7.3' 9 | 10 | matrix: 11 | include: 12 | - php: '7.1' 13 | env: deps=low 14 | 15 | env: 16 | global: 17 | - deps=no 18 | 19 | before_install: 20 | - composer self-update 21 | 22 | install: 23 | - if [ "$deps" = "no" ]; then composer --prefer-source install; fi; 24 | - if [ "$deps" = "low" ]; then composer --prefer-source --prefer-lowest --prefer-stable update; fi 25 | 26 | script: 27 | - vendor/bin/phpstan analyse -c phpstan.neon --level 7 . 28 | - vendor/bin/phpunit 29 | 30 | notifications: 31 | email: "douglas@usemarkup.com" 32 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | CHANGELOG FOR 0.3.0 2 | =================== 3 | 4 | This version removes the `exclude_archived` option as this was a patch to filter out archived entries that were previously buggily leaking out from the Contentful CDA. 5 | 6 | The `IsArchivedFilter` class is removed - using this filter will now break queries. 7 | 8 | If any users of this library were not specifying `exclude_archived` as an option set to true, this release does not alter behaviour as this was strictly opt-in. 9 | 10 | 11 | CHANGELOG FOR 0.2.0 12 | =================== 13 | 14 | This version moves to using the final (Recommendation) version of [PSR-6](http://www.php-fig.org/psr/psr-6/), the PHP-FIG standard for caches in PHP. Previous versions made use of the draft version of this PSR, which is not the same as the final version. 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Markup 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | contentful 2 | ========== 3 | 4 | Provides integrations with Contentful APIs (Delivery and Preview) for PHP 7.1+. 5 | 6 | There is an official Contentful PHP SDK now ([contentful/contentful](https://packagist.org/packages/contentful/contentful)) - so use what makes sense for you. This package has been in large-scale production for over two years now, and I am focusing on making it the fastest client available for fetching Contentful data. 7 | 8 | Previous aspirations to integrate with Contentful's Content Management API are now dropped - please look to Contentful to support that. 9 | 10 | Documentation 11 | ------------- 12 | 13 | I know this is sorely missing - _mea culpa_. Docs will be forthcoming. 14 | 15 | [![Build Status](https://api.travis-ci.org/usemarkup/contentful.png?branch=master)](http://travis-ci.org/usemarkup/contentful) 16 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "markup/contentful", 3 | "description": "A client library to integrate with the Contentful APIs (Content Delivery and Content Management) for PHP 5.6+.", 4 | "keywords": ["contentful", "cms"], 5 | "type": "library", 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Douglas Greenshields", 10 | "email": "douglas@usemarkup.com" 11 | }, 12 | { 13 | "name": "Markup", 14 | "homepage": "http://www.usemarkup.com/" 15 | } 16 | ], 17 | "require": { 18 | "php": ">=7.1", 19 | "guzzlehttp/guzzle": "^6", 20 | "guzzlehttp/promises": "^1.1", 21 | "psr/cache": "~1.0.0", 22 | "psr/http-message": "^1.0.1" 23 | }, 24 | "require-dev": { 25 | "phpunit/phpunit": "^7.5", 26 | "mockery/mockery": "^1", 27 | "symfony/phpunit-bridge": "^4.2", 28 | "phpstan/phpstan-shim": "0.11.5" 29 | }, 30 | "conflict": { 31 | "hhvm": "*" 32 | }, 33 | "autoload": { 34 | "psr-4": { 35 | "Markup\\Contentful\\Tests\\": "tests/", 36 | "Markup\\Contentful\\": "src/" 37 | } 38 | }, 39 | "extra": { 40 | "branch-alias": { 41 | "dev-master": "0.8.x-dev" 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /phpstan.neon: -------------------------------------------------------------------------------- 1 | parameters: 2 | excludes_analyse: 3 | - %currentWorkingDirectory%/tests/* 4 | - %currentWorkingDirectory%/vendor/* 5 | ignoreErrors: 6 | - '#is not subtype of Throwable#' 7 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | ./tests 8 | 9 | 10 | 11 | 12 | 13 | ./src 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/Analysis/ResponseAnalysis.php: -------------------------------------------------------------------------------- 1 | indicatedResponseCount = $indicatedResponseCount; 21 | $this->isError = $isError; 22 | } 23 | 24 | public function getIndicatedResponseCount(): ?int 25 | { 26 | return $this->indicatedResponseCount; 27 | } 28 | 29 | public function indicatesError(): bool 30 | { 31 | return $this->isError; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Analysis/ResponseAnalysisInterface.php: -------------------------------------------------------------------------------- 1 | checkForType($response, 'Array')) { 12 | preg_match('/total.{3}(\d+),/', $response, $matches); 13 | 14 | return new ResponseAnalysis((isset($matches[1])) ? intval($matches[1]) : null); 15 | } 16 | if ($this->checkForType($response,'Error')) { 17 | return $this->createErrorAnalysis(); 18 | } 19 | if ($this->checkForType($response, 'Entry') || $this->checkForType($response, 'Asset')) { 20 | return $this->createGoodAnalysisForSingleResource(); 21 | } 22 | 23 | return $this->createEmptyAnalysis(); 24 | } 25 | 26 | private function checkForType(string $response, string $type): bool 27 | { 28 | return (bool) preg_match(sprintf('/type.{4}%s/', preg_quote($type)), $response); 29 | } 30 | 31 | private function createGoodAnalysisForSingleResource() 32 | { 33 | return new ResponseAnalysis(1); 34 | } 35 | 36 | private function createErrorAnalysis() 37 | { 38 | return new ResponseAnalysis(null, true); 39 | } 40 | 41 | private function createEmptyAnalysis() 42 | { 43 | return new ResponseAnalysis(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Analysis/ResponseAnalyzerInterface.php: -------------------------------------------------------------------------------- 1 | filename = $filename; 36 | $this->contentType = $contentType; 37 | $this->details = $details; 38 | $this->url = $url; 39 | } 40 | 41 | /** 42 | * @return string 43 | */ 44 | public function getContentType() 45 | { 46 | return $this->contentType; 47 | } 48 | 49 | /** 50 | * @return array 51 | */ 52 | public function getDetails() 53 | { 54 | return $this->details; 55 | } 56 | 57 | /** 58 | * @return string 59 | */ 60 | public function getFilename() 61 | { 62 | return $this->filename; 63 | } 64 | 65 | /** 66 | * @return int|null 67 | */ 68 | public function getFileSizeInBytes() 69 | { 70 | $details = $this->getDetails(); 71 | if (!isset($details['size'])) { 72 | return null; 73 | } 74 | 75 | return intval($details['size']); 76 | } 77 | 78 | /** 79 | * @return int|null 80 | */ 81 | public function getWidth() 82 | { 83 | $details = $this->getDetails(); 84 | 85 | if (!isset($details['image'])) { 86 | return null; 87 | } 88 | 89 | $image = $details['image']; 90 | 91 | if (!isset($image['width'])) { 92 | return null; 93 | } 94 | 95 | return intval($image['width']); 96 | } 97 | 98 | /** 99 | * @return int|null 100 | */ 101 | public function getHeight() 102 | { 103 | $details = $this->getDetails(); 104 | 105 | if (!isset($details['image'])) { 106 | return null; 107 | } 108 | 109 | $image = $details['image']; 110 | 111 | if (!isset($image['height'])) { 112 | return null; 113 | } 114 | 115 | return intval($image['height']); 116 | } 117 | 118 | /** 119 | * @return float|null 120 | */ 121 | public function getRatio() 122 | { 123 | $width = $this->getWidth(); 124 | $height = $this->getHeight(); 125 | if (!$width || !$height) { 126 | return null; 127 | } 128 | 129 | return (float) $width/$height; 130 | } 131 | 132 | /** 133 | * @return string 134 | */ 135 | public function getUrl() 136 | { 137 | return $this->url; 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /src/AssetInterface.php: -------------------------------------------------------------------------------- 1 | key = $key; 23 | } 24 | 25 | /** 26 | * Returns the key for the current cache item. 27 | * 28 | * The key is loaded by the Implementing Library, but should be available to 29 | * the higher level callers when needed. 30 | * 31 | * @return string 32 | * The key string for this cache item. 33 | */ 34 | public function getKey() 35 | { 36 | return $this->key; 37 | } 38 | 39 | /** 40 | * Retrieves the value of the item from the cache associated with this objects key. 41 | * 42 | * The value returned must be identical to the value original stored by set(). 43 | * 44 | * if isHit() returns false, this method MUST return null. Note that null 45 | * is a legitimate cached value, so the isHit() method SHOULD be used to 46 | * differentiate between "null value was found" and "no value was found." 47 | * 48 | * @return mixed 49 | * The value corresponding to this cache item's key, or null if not found. 50 | */ 51 | public function get() 52 | { 53 | return null; 54 | } 55 | 56 | /** 57 | * Confirms if the cache item lookup resulted in a cache hit. 58 | * 59 | * Note: This method MUST NOT have a race condition between calling isHit() 60 | * and calling get(). 61 | * 62 | * @return bool 63 | * True if the request resulted in a cache hit. False otherwise. 64 | */ 65 | public function isHit() 66 | { 67 | return false; 68 | } 69 | 70 | /** 71 | * Sets the value represented by this cache item. 72 | * 73 | * The $value argument may be any item that can be serialized by PHP, 74 | * although the method of serialization is left up to the Implementing 75 | * Library. 76 | * 77 | * @param mixed $value 78 | * The serializable value to be stored. 79 | * 80 | * @return static 81 | * The invoked object. 82 | */ 83 | public function set($value) 84 | { 85 | // do nothing 86 | return $this; 87 | } 88 | 89 | /** 90 | * Sets the expiration time for this cache item. 91 | * 92 | * @param \DateTimeInterface|null $expiration 93 | * The point in time after which the item MUST be considered expired. 94 | * If null is passed explicitly, a default value MAY be used. If none is set, 95 | * the value should be stored permanently or for as long as the 96 | * implementation allows. 97 | * 98 | * @return static 99 | * The called object. 100 | */ 101 | public function expiresAt($expiration) 102 | { 103 | // do nothing 104 | return $this; 105 | } 106 | 107 | /** 108 | * Sets the expiration time for this cache item. 109 | * 110 | * @param int|\DateInterval|null $time 111 | * The period of time from the present after which the item MUST be considered 112 | * expired. An integer parameter is understood to be the time in seconds until 113 | * expiration. If null is passed explicitly, a default value MAY be used. 114 | * If none is set, the value should be stored permanently or for as long as the 115 | * implementation allows. 116 | * 117 | * @return static 118 | * The called object. 119 | */ 120 | public function expiresAfter($time) 121 | { 122 | // do nothing 123 | return $this; 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /src/Cache/NullCacheItemPool.php: -------------------------------------------------------------------------------- 1 | name = $name; 41 | $this->metadata = $metadata; 42 | $this->description = $description; 43 | $this->fields = []; 44 | foreach ($fields as $field) { 45 | $this->fields[$field->getId()] = $field; 46 | } 47 | $this->displayField = $displayField; 48 | } 49 | 50 | /** 51 | * @return string 52 | */ 53 | public function getName() 54 | { 55 | return $this->name; 56 | } 57 | 58 | /** 59 | * @return string 60 | */ 61 | public function getDescription() 62 | { 63 | return $this->description; 64 | } 65 | 66 | /** 67 | * @return ContentTypeFieldInterface[] 68 | */ 69 | public function getFields() 70 | { 71 | return $this->fields; 72 | } 73 | 74 | /** 75 | * @param string $fieldId 76 | * @return ContentTypeFieldInterface|null 77 | */ 78 | public function getField($fieldId) 79 | { 80 | if (!isset($this->fields[$fieldId])) { 81 | return null; 82 | } 83 | 84 | return $this->fields[$fieldId]; 85 | } 86 | 87 | /** 88 | * @return ContentTypeFieldInterface|null 89 | */ 90 | public function getDisplayField() 91 | { 92 | if (null === $this->displayField) { 93 | return null; 94 | } 95 | 96 | return $this->getField($this->displayField); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/ContentTypeField.php: -------------------------------------------------------------------------------- 1 | id = $id; 50 | $this->name = $name; 51 | $this->type = $type; 52 | $this->items = $items; 53 | $defaultOptions = [ 54 | 'required' => true, 55 | 'localized' => false, 56 | ]; 57 | $options = array_merge($defaultOptions, $options); 58 | $this->isRequired = $options['required']; 59 | $this->isLocalized = $options['localized']; 60 | } 61 | 62 | /** 63 | * @return string 64 | */ 65 | public function getId(): string 66 | { 67 | return $this->id; 68 | } 69 | 70 | /** 71 | * @return string 72 | */ 73 | public function getName(): string 74 | { 75 | return $this->name; 76 | } 77 | 78 | /** 79 | * @return string 80 | */ 81 | public function getType(): string 82 | { 83 | return $this->type; 84 | } 85 | 86 | /** 87 | * @return array 88 | */ 89 | public function getItems(): array 90 | { 91 | return $this->items; 92 | } 93 | 94 | /** 95 | * @return bool 96 | */ 97 | public function isLocalized(): bool 98 | { 99 | return $this->isLocalized; 100 | } 101 | 102 | /** 103 | * @return bool 104 | */ 105 | public function isRequired(): bool 106 | { 107 | return $this->isRequired; 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /src/ContentTypeFieldInterface.php: -------------------------------------------------------------------------------- 1 | 11 | */ 12 | private $decorators; 13 | 14 | public function __construct() 15 | { 16 | $this->decorators = new \SplQueue(); 17 | } 18 | 19 | /** 20 | * Decorates an asset. 21 | * 22 | * @param AssetInterface $asset 23 | * @return AssetInterface 24 | */ 25 | public function decorate(AssetInterface $asset) 26 | { 27 | foreach ($this->decorators as $decorator) { 28 | /** 29 | * @var AssetDecoratorInterface $decorator 30 | */ 31 | $asset = $decorator->decorate($asset); 32 | } 33 | 34 | return $asset; 35 | } 36 | 37 | /** 38 | * @param AssetDecoratorInterface $assetDecorator 39 | */ 40 | public function addDecorator(AssetDecoratorInterface $assetDecorator) 41 | { 42 | $this->decorators[] = $assetDecorator; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Decorator/NullAssetDecorator.php: -------------------------------------------------------------------------------- 1 | getField($method); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/Exception/ExceptionInterface.php: -------------------------------------------------------------------------------- 1 | id = $id; 16 | parent::__construct( 17 | $message ?: sprintf('The provided Contentful ID "%s" is illegal - see https://www.contentful.com/developers/docs/references/content-management-api/#/introduction/resource-ids', $id), 18 | ...$args 19 | ); 20 | } 21 | 22 | /** 23 | * @return string 24 | */ 25 | public function getId(): string 26 | { 27 | return $this->id; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Exception/LinkUnresolvableException.php: -------------------------------------------------------------------------------- 1 | link = $link; 26 | parent::__construct($message ?: sprintf('The link to the %s resource with ID %s could not be resolved.', $link->getLinkType(), $link->getId()), $code, $previous); 27 | } 28 | 29 | public function getLink() 30 | { 31 | return $this->link; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Exception/ResourceUnavailableException.php: -------------------------------------------------------------------------------- 1 | response = $response; 17 | parent::__construct(...$args); 18 | } 19 | 20 | /** 21 | * @return Response|null 22 | */ 23 | public function getResponse() 24 | { 25 | return $this->response; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Filter/AbstractComparisonFilter.php: -------------------------------------------------------------------------------- 1 | value = $value; 19 | } 20 | 21 | public function getKey() 22 | { 23 | return sprintf('%s[%s]', $this->getProperty()->getKey(), $this->getOperator()); 24 | } 25 | 26 | abstract protected function getOperator(); 27 | } 28 | -------------------------------------------------------------------------------- /src/Filter/AfterFilter.php: -------------------------------------------------------------------------------- 1 | createGreaterThanFilter()->getKey(); 15 | } 16 | 17 | /** 18 | * The value in a query string on an API request. 19 | * 20 | * @return string 21 | */ 22 | public function getValue() 23 | { 24 | return $this->createGreaterThanFilter()->getValue(); 25 | } 26 | 27 | private function createGreaterThanFilter() 28 | { 29 | return new GreaterThanFilter( 30 | $this->getProperty(), 31 | new \DateTime($this->getRelativeTime()) 32 | ); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Filter/BeforeFilter.php: -------------------------------------------------------------------------------- 1 | createLessThanFilter()->getKey(); 15 | } 16 | 17 | /** 18 | * The value in a query string on an API request. 19 | * 20 | * @return string 21 | */ 22 | public function getValue() 23 | { 24 | return $this->createLessThanFilter()->getValue(); 25 | } 26 | 27 | private function createLessThanFilter() 28 | { 29 | return new LessThanFilter( 30 | $this->getProperty(), 31 | new \DateTime($this->getRelativeTime()) 32 | ); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Filter/ClassBasedNameTrait.php: -------------------------------------------------------------------------------- 1 | value = $value; 24 | } 25 | 26 | /** 27 | * The key in a query string on an API request. 28 | * 29 | * @return string 30 | */ 31 | public function getKey() 32 | { 33 | return self::KEY; 34 | } 35 | 36 | /** 37 | * The value in a query string on an API request. 38 | * 39 | * @return string 40 | */ 41 | public function getValue() 42 | { 43 | return $this->value; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Filter/ContentTypeFilterProvider.php: -------------------------------------------------------------------------------- 1 | contentful = $contentful; 22 | } 23 | 24 | /** 25 | * @param string $contentTypeName 26 | * @param string $spaceName 27 | * @return ContentTypeFilter|null 28 | */ 29 | public function createForContentTypeName($contentTypeName, $spaceName) 30 | { 31 | /** @var ContentTypeInterface|null $contentType */ 32 | $contentType = $this->contentful->getContentTypeByName($contentTypeName, $spaceName); 33 | if (!$contentType) { 34 | return null; 35 | } 36 | 37 | return new ContentTypeFilter($contentType->getId()); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/Filter/ContentTypeNameFilter.php: -------------------------------------------------------------------------------- 1 | contentTypeName = $contentTypeName; 25 | } 26 | 27 | /** 28 | * The value in a query string on an API request. 29 | * 30 | * @return string 31 | */ 32 | public function getValue() 33 | { 34 | return $this->contentTypeName; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/Filter/DecidesCacheKeyInterface.php: -------------------------------------------------------------------------------- 1 | value = $value; 22 | } 23 | 24 | /** 25 | * The key in a query string on an API request. 26 | * 27 | * @return string 28 | */ 29 | public function getKey() 30 | { 31 | return $this->getProperty()->getKey(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Filter/ExcludeFilter.php: -------------------------------------------------------------------------------- 1 | values = $values; 24 | } 25 | 26 | /** 27 | * The key in a query string on an API request. 28 | * 29 | * @return string 30 | */ 31 | public function getKey() 32 | { 33 | return $this->getProperty()->getKey() . '[nin]'; 34 | } 35 | 36 | /** 37 | * The value in a query string on an API request. 38 | * 39 | * @return string 40 | */ 41 | public function getValue() 42 | { 43 | return implode(',', $this->values); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Filter/ExistsFilter.php: -------------------------------------------------------------------------------- 1 | value = $value; 24 | } 25 | 26 | /** 27 | * The key in a query string on an API request. 28 | * 29 | * @return string 30 | */ 31 | public function getKey() 32 | { 33 | return sprintf('%s[exists]', $this->getProperty()->getKey()); 34 | } 35 | 36 | /** 37 | * The value in a query string on an API request. 38 | * 39 | * @return string 40 | */ 41 | public function getValue() 42 | { 43 | return ($this->value) ? 'true' : 'false'; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Filter/GreaterThanFilter.php: -------------------------------------------------------------------------------- 1 | values = $values; 27 | } 28 | 29 | /** 30 | * The key in a query string on an API request. 31 | * 32 | * @return string 33 | */ 34 | public function getKey() 35 | { 36 | return $this->getProperty()->getKey() . '[in]'; 37 | } 38 | 39 | /** 40 | * The value in a query string on an API request. 41 | * 42 | * @return string 43 | */ 44 | public function getValue() 45 | { 46 | return implode(',', $this->values); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/Filter/IncompleteTrait.php: -------------------------------------------------------------------------------- 1 | getName())); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/Filter/LessThanFilter.php: -------------------------------------------------------------------------------- 1 | value = $value; 24 | } 25 | 26 | /** 27 | * The key in a query string on an API request. 28 | * 29 | * @return string 30 | */ 31 | public function getKey() 32 | { 33 | return self::KEY; 34 | } 35 | 36 | /** 37 | * The value in a query string on an API request. 38 | * 39 | * @return string 40 | */ 41 | public function getValue() 42 | { 43 | return $this->value; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Filter/LocaleFilter.php: -------------------------------------------------------------------------------- 1 | getConstants()); 28 | if (!in_array($mimeTypeGroup, $constants)) { 29 | throw new \InvalidArgumentException(sprintf( 30 | '"%s" is not a known MIME type group. Known types: %s.', 31 | $mimeTypeGroup, 32 | implode(', ', $constants) 33 | )); 34 | } 35 | parent::__construct(new MimeTypeGroup(), $mimeTypeGroup); 36 | } 37 | 38 | public function getName() 39 | { 40 | return 'mimetype_group'; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Filter/NearFilter.php: -------------------------------------------------------------------------------- 1 | property = $property; 33 | $this->location = $location; 34 | } 35 | 36 | /** 37 | * The key in a query string on an API request. 38 | * 39 | * @return string 40 | */ 41 | public function getKey() 42 | { 43 | return $this->property->getKey() . '[near]'; 44 | } 45 | 46 | /** 47 | * The value in a query string on an API request. 48 | * 49 | * @return string 50 | */ 51 | public function getValue() 52 | { 53 | return implode(',', [$this->location->getLatitude(), $this->location->getLongitude()]); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/Filter/NotEqualFilter.php: -------------------------------------------------------------------------------- 1 | value = $value; 19 | } 20 | 21 | /** 22 | * @return string 23 | */ 24 | public function getKey() 25 | { 26 | return $this->getProperty()->getKey() . '[ne]'; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Filter/PropertyFilter.php: -------------------------------------------------------------------------------- 1 | property = $property; 24 | } 25 | 26 | /** 27 | * @return PropertyInterface 28 | */ 29 | public function getProperty() 30 | { 31 | return $this->property; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Filter/RelativeTimeFilter.php: -------------------------------------------------------------------------------- 1 | relativeTime = $relativeTime; 24 | } 25 | 26 | /** 27 | * @return string 28 | */ 29 | public function getCacheKey() 30 | { 31 | return sprintf( 32 | '|%s|%s↦%s', 33 | $this->getName(), 34 | $this->getProperty()->getKey(), 35 | str_replace(' ', '_', $this->relativeTime) 36 | ); 37 | } 38 | 39 | protected function getRelativeTime() 40 | { 41 | return $this->relativeTime; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Filter/SearchFilter.php: -------------------------------------------------------------------------------- 1 | property = $property; 29 | $this->value = $query; 30 | } 31 | 32 | /** 33 | * The key in a query string on an API request. 34 | * 35 | * @return string 36 | */ 37 | public function getKey() 38 | { 39 | if (null === $this->property) { 40 | return self::OPERATOR_ALL_PROPERTIES; 41 | } 42 | 43 | return $this->property->getKey() . '[match]'; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Filter/SimpleValueTrait.php: -------------------------------------------------------------------------------- 1 | value instanceof \DateTime) { 20 | $this->value->setTimezone(new \DateTimeZone('UTC')); 21 | 22 | return $this->value->format('Y-m-d\TH:i:s\Z'); 23 | } 24 | 25 | return $this->value; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Filter/WithinCircleFilter.php: -------------------------------------------------------------------------------- 1 | property = $property; 36 | $this->center = $center; 37 | $this->radiusInKm = $radiusInKm; 38 | } 39 | 40 | /** 41 | * The key in a query string on an API request. 42 | * 43 | * @return string 44 | */ 45 | public function getKey() 46 | { 47 | return $this->property->getKey() . '[within]'; 48 | } 49 | 50 | /** 51 | * The value in a query string on an API request. 52 | * 53 | * @return string 54 | */ 55 | public function getValue() 56 | { 57 | return implode( 58 | ',', 59 | [ 60 | $this->center->getLatitude(), 61 | $this->center->getLongitude(), 62 | $this->radiusInKm, 63 | ] 64 | ); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/Filter/WithinRectangleFilter.php: -------------------------------------------------------------------------------- 1 | property = $property; 36 | $this->bottomLeft = $bottomLeft; 37 | $this->topRight = $topRight; 38 | } 39 | 40 | /** 41 | * The key in a query string on an API request. 42 | * 43 | * @return string 44 | */ 45 | public function getKey() 46 | { 47 | return $this->property->getKey() . '[within]'; 48 | } 49 | 50 | /** 51 | * The value in a query string on an API request. 52 | * 53 | * @return string 54 | */ 55 | public function getValue() 56 | { 57 | return implode( 58 | ',', 59 | [ 60 | $this->bottomLeft->getLatitude(), 61 | $this->bottomLeft->getLongitude(), 62 | $this->topRight->getLatitude(), 63 | $this->topRight->getLongitude(), 64 | ] 65 | ); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/FilterInterface.php: -------------------------------------------------------------------------------- 1 | guzzle->sendAsync($request, [RequestOptions::QUERY => $queryParams]); 29 | } 30 | 31 | /** 32 | * @param string $uri 33 | * @param string $method 34 | * @return Request 35 | */ 36 | private function createRequest($uri, $method) 37 | { 38 | return new Request($method, $uri); 39 | } 40 | 41 | /** 42 | * @param Request $request 43 | * @return string 44 | */ 45 | private function getUriForRequest(Request $request, array $queryParams = []) 46 | { 47 | $uri = strval($request->getUri()); 48 | if (count($queryParams) > 0) { 49 | $uri .= '?'.http_build_query($queryParams); 50 | } 51 | 52 | return $uri; 53 | } 54 | 55 | /** 56 | * @param Request $request 57 | * @param string $header 58 | * @param string $value 59 | * @return Request 60 | */ 61 | private function setHeaderOnRequest(Request $request, $header, $value) 62 | { 63 | return $request->withHeader($header, $value); 64 | } 65 | 66 | /** 67 | * @param ResponseInterface $response 68 | * @return array 69 | */ 70 | private function responseAsArrayFromJson($response) 71 | { 72 | return json_decode(strval($response->getBody()), true); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/GuzzleOptions.php: -------------------------------------------------------------------------------- 1 | defaults = []; 19 | } 20 | 21 | /** 22 | * @param array $options 23 | * @return GuzzleOptions 24 | */ 25 | public static function createForEnvironment(array $options) 26 | { 27 | $instance = new self(); 28 | 29 | if ((isset($options['guzzle_handler'])) && (is_callable($options['guzzle_handler']))) { 30 | $instance->setHandler($options['guzzle_handler']); 31 | } 32 | 33 | if (isset($options['guzzle_timeout']) && (intval($options['guzzle_timeout']) > 0)) { 34 | $instance->defaults['timeout'] = intval($options['guzzle_timeout']); 35 | } 36 | 37 | if (isset($options['guzzle_connection_timeout']) && (intval($options['guzzle_connection_timeout']) > 0)) { 38 | $instance->defaults['connect_timeout'] = intval($options['guzzle_connection_timeout']); 39 | } 40 | 41 | if (!empty($options['guzzle_proxy'])) { 42 | $instance->defaults['proxy'] = $options['guzzle_proxy']; 43 | } 44 | 45 | return $instance; 46 | } 47 | 48 | /** 49 | * @return array 50 | */ 51 | public function toArray() 52 | { 53 | return [ 54 | 'defaults' => $this->getDefaults(), 55 | 'handler' => $this->getHandler(), 56 | ]; 57 | } 58 | 59 | /** 60 | * @param callable $handler 61 | */ 62 | public function setHandler(callable $handler) 63 | { 64 | $this->handler = $handler; 65 | } 66 | 67 | /** 68 | * @return callable 69 | */ 70 | public function getHandler() 71 | { 72 | return $this->handler; 73 | } 74 | 75 | /** 76 | * @param array $defaults 77 | */ 78 | public function setDefaults($defaults) 79 | { 80 | $this->defaults = $defaults; 81 | } 82 | 83 | /** 84 | * @return array 85 | */ 86 | public function getDefaults() 87 | { 88 | return $this->defaults; 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/IncompleteParameterInterface.php: -------------------------------------------------------------------------------- 1 | metadata = $metadata; 19 | $this->spaceName = $spaceName; 20 | } 21 | 22 | /** 23 | * Gets a space name (not intrinsic, but how the space is referred to in configuration) that this link is associated with, if available. 24 | */ 25 | public function getSpaceName(): string 26 | { 27 | return $this->spaceName; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/LinkInterface.php: -------------------------------------------------------------------------------- 1 | code = $code; 27 | $this->name = $name; 28 | } 29 | 30 | /** 31 | * @return string 32 | */ 33 | public function getCode() 34 | { 35 | return $this->code; 36 | } 37 | 38 | /** 39 | * @return string 40 | */ 41 | public function getName() 42 | { 43 | return $this->name; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Location.php: -------------------------------------------------------------------------------- 1 | latitude = $latitude; 27 | $this->longitude = $longitude; 28 | } 29 | 30 | /** 31 | * @return float 32 | */ 33 | public function getLatitude() 34 | { 35 | return $this->latitude; 36 | } 37 | 38 | /** 39 | * @return float 40 | */ 41 | public function getLongitude() 42 | { 43 | return $this->longitude; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Log/LinkResolveCounter.php: -------------------------------------------------------------------------------- 1 | loggedLinks = []; 18 | } 19 | 20 | public function count(): int 21 | { 22 | return count($this->loggedLinks); 23 | } 24 | 25 | public function logLink(LinkInterface $link): void 26 | { 27 | $key = $this->calculateLinkKey($link); 28 | if (!in_array($key, $this->loggedLinks)) { 29 | $this->loggedLinks[] = $key; 30 | } 31 | } 32 | 33 | private function calculateLinkKey(LinkInterface $link): string 34 | { 35 | return sprintf('%s:%s', $link->getId(), $link->getSpaceName()); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Log/LinkResolveCounterInterface.php: -------------------------------------------------------------------------------- 1 | description = $description; 58 | $this->durationInSeconds = $durationInSeconds; 59 | $this->startTime = $startTime; 60 | $this->stopTime = $stopTime; 61 | $this->resourceType = $resourceType; 62 | $this->api = $api; 63 | $this->responseCount = $responseCount ?? 0; 64 | $this->wasError = $wasError; 65 | } 66 | 67 | /** 68 | * A description of what happened, containing pertinent information. 69 | * 70 | * @return string 71 | */ 72 | public function getDescription() 73 | { 74 | return $this->description; 75 | } 76 | 77 | /** 78 | * The duration of the lookup, in seconds. Returns null if none available. 79 | * 80 | * @return float|null 81 | */ 82 | public function getDurationInSeconds() 83 | { 84 | return $this->durationInSeconds; 85 | } 86 | 87 | public function getStartTime(): ?\DateTimeInterface 88 | { 89 | return $this->startTime; 90 | } 91 | 92 | public function getStopTime(): ?\DateTimeInterface 93 | { 94 | return $this->stopTime; 95 | } 96 | 97 | /** 98 | * The resource type. Possible values: RESOURCE_* interface constants. 99 | */ 100 | public function getResourceType() 101 | { 102 | return $this->resourceType; 103 | } 104 | 105 | /** 106 | * Gets the name of the API being used. 107 | * 108 | * @return string A value corresponding to one of the Contentful::*_API class constants 109 | */ 110 | public function getApi() 111 | { 112 | return $this->api; 113 | } 114 | 115 | public function getResponseCount(): int 116 | { 117 | return $this->responseCount; 118 | } 119 | 120 | public function wasError(): bool 121 | { 122 | return $this->wasError; 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /src/Log/LogInterface.php: -------------------------------------------------------------------------------- 1 | 12 | */ 13 | private $logs; 14 | 15 | public function __construct() 16 | { 17 | $this->logs = []; 18 | } 19 | 20 | /** 21 | * Gets a new timer that has already been started. 22 | * 23 | * @return TimerInterface 24 | */ 25 | public function getStartedTimer() 26 | { 27 | $timer = new StandardTimer(); 28 | $timer->start(); 29 | 30 | return $timer; 31 | } 32 | 33 | /** 34 | * Logs a lookup. 35 | * 36 | * @param string $description A description of what this lookup was, including pertinent information such as URLs and cache keys. 37 | * @param TimerInterface $timer A timer. If it is started but not stopped, it will be stopped and a reading taken. If not started, no reading. 38 | * @param string $resourceType 39 | * @param string $api 40 | * @param int|null $responseCount 41 | * @param bool $wasError 42 | */ 43 | public function log($description, TimerInterface $timer, $resourceType, $api, ?int $responseCount, bool $wasError = false) 44 | { 45 | if ($timer->isStarted()) { 46 | $timer->stop();//will have no effect if already stopped 47 | $duration = $timer->getDurationInSeconds(); 48 | } else { 49 | $duration = null; 50 | } 51 | $this->logs[] = new Log( 52 | $description, 53 | $duration, 54 | $timer->getStartTime(), 55 | $timer->getStopTime(), 56 | $resourceType, 57 | $api, 58 | $responseCount, 59 | $wasError 60 | ); 61 | } 62 | 63 | /** 64 | * Gets the collected logs. 65 | * 66 | * @return LogInterface[] 67 | */ 68 | public function getLogs() 69 | { 70 | return $this->logs; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/Log/StandardTimer.php: -------------------------------------------------------------------------------- 1 | wasStarted = false; 43 | $this->wasStopped = false; 44 | } 45 | 46 | public function start() 47 | { 48 | if ($this->isStarted()) { 49 | return; 50 | } 51 | $this->initialTimestamp = $this->getCurrentTimestamp(); 52 | $this->initialTime = \DateTimeImmutable::createFromFormat('U.u', strval($this->initialTimestamp)) ?: null; 53 | $this->wasStarted = true; 54 | } 55 | 56 | public function stop() 57 | { 58 | if ($this->isStopped()) { 59 | return; 60 | } 61 | $this->finalTimestamp = $this->getCurrentTimestamp(); 62 | $this->finalTime = \DateTimeImmutable::createFromFormat('U.u', strval($this->finalTimestamp)) ?: null; 63 | $this->wasStopped = true; 64 | } 65 | 66 | /** 67 | * @return bool 68 | */ 69 | public function isStarted() 70 | { 71 | return $this->wasStarted; 72 | } 73 | 74 | /** 75 | * @return bool 76 | */ 77 | public function isStopped() 78 | { 79 | return $this->wasStopped; 80 | } 81 | 82 | /** 83 | * @return float|null 84 | */ 85 | public function getDurationInSeconds() 86 | { 87 | if (!$this->isStarted()) { 88 | return null; 89 | } 90 | 91 | return (($this->isStopped()) ? $this->finalTimestamp : $this->getCurrentTimestamp()) - $this->initialTimestamp; 92 | } 93 | 94 | public function getStartTime(): ?\DateTimeInterface 95 | { 96 | return $this->initialTime; 97 | } 98 | 99 | public function getStopTime(): ?\DateTimeInterface 100 | { 101 | return $this->finalTime; 102 | } 103 | 104 | /** 105 | * @return float 106 | */ 107 | private function getCurrentTimestamp() 108 | { 109 | return microtime(true); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /src/Log/TimerInterface.php: -------------------------------------------------------------------------------- 1 | id = $id; 58 | } 59 | 60 | /** 61 | * @return string 62 | */ 63 | public function getId() 64 | { 65 | return $this->id; 66 | } 67 | 68 | /** 69 | * @param string $type 70 | */ 71 | public function setType($type) 72 | { 73 | $this->type = $type; 74 | } 75 | 76 | /** 77 | * @return string 78 | */ 79 | public function getType() 80 | { 81 | return $this->type; 82 | } 83 | 84 | /** 85 | * @param \Markup\Contentful\SpaceInterface $space 86 | */ 87 | public function setSpace($space) 88 | { 89 | $this->space = $space; 90 | } 91 | 92 | /** 93 | * @return \Markup\Contentful\SpaceInterface 94 | */ 95 | public function getSpace() 96 | { 97 | return $this->space; 98 | } 99 | 100 | public function getSpaceName(): string 101 | { 102 | $space = $this->space; 103 | if ($this->space instanceof Link) { 104 | return $space->getSpaceName(); 105 | } 106 | 107 | return $space->getName(); 108 | } 109 | 110 | /** 111 | * @param \Markup\Contentful\ContentTypeInterface $contentType 112 | */ 113 | public function setContentType($contentType) 114 | { 115 | $this->contentType = $contentType; 116 | } 117 | 118 | /** 119 | * @return \Markup\Contentful\ContentTypeInterface 120 | */ 121 | public function getContentType() 122 | { 123 | return $this->contentType; 124 | } 125 | 126 | /** 127 | * @param string $linkType 128 | * @return self 129 | */ 130 | public function setLinkType($linkType) 131 | { 132 | $this->linkType = $linkType; 133 | 134 | return $this; 135 | } 136 | 137 | /** 138 | * @return string 139 | */ 140 | public function getLinkType() 141 | { 142 | return $this->linkType; 143 | } 144 | 145 | /** 146 | * @param int $revision 147 | */ 148 | public function setRevision($revision) 149 | { 150 | $this->revision = $revision; 151 | } 152 | 153 | /** 154 | * @return int 155 | */ 156 | public function getRevision() 157 | { 158 | return $this->revision; 159 | } 160 | 161 | /** 162 | * @param \DateTimeInterface $createdAt 163 | */ 164 | public function setCreatedAt($createdAt) 165 | { 166 | $this->createdAt = $createdAt; 167 | } 168 | 169 | /** 170 | * @return \DateTimeInterface|null 171 | */ 172 | public function getCreatedAt() 173 | { 174 | return $this->createdAt; 175 | } 176 | 177 | /** 178 | * @param \DateTimeInterface $updatedAt 179 | */ 180 | public function setUpdatedAt($updatedAt) 181 | { 182 | $this->updatedAt = $updatedAt; 183 | } 184 | 185 | /** 186 | * @return \DateTimeInterface|null 187 | */ 188 | public function getUpdatedAt() 189 | { 190 | return $this->updatedAt; 191 | } 192 | 193 | /** 194 | * @param string $locale 195 | */ 196 | public function setLocale($locale) 197 | { 198 | $this->locale = $locale; 199 | } 200 | 201 | /** 202 | * @return string|null 203 | */ 204 | public function getLocale() 205 | { 206 | return $this->locale; 207 | } 208 | } 209 | -------------------------------------------------------------------------------- /src/MetadataAccessTrait.php: -------------------------------------------------------------------------------- 1 | metadata; 18 | } 19 | 20 | /** 21 | * @return string 22 | */ 23 | public function getType() 24 | { 25 | return $this->getMetadata()->getType(); 26 | } 27 | 28 | /** 29 | * @return string 30 | */ 31 | public function getId() 32 | { 33 | return $this->getMetadata()->getId(); 34 | } 35 | 36 | /** 37 | * @return SpaceInterface 38 | */ 39 | public function getSpace() 40 | { 41 | return $this->getMetadata()->getSpace(); 42 | } 43 | 44 | public function getSpaceName(): string 45 | { 46 | return $this->getMetadata()->getSpaceName(); 47 | } 48 | 49 | /** 50 | * @return ContentTypeInterface|null 51 | */ 52 | public function getContentType() 53 | { 54 | return $this->getMetadata()->getContentType(); 55 | } 56 | 57 | /** 58 | * @return string|null 59 | */ 60 | public function getLinkType() 61 | { 62 | return $this->getMetadata()->getLinkType(); 63 | } 64 | 65 | /** 66 | * @return int 67 | */ 68 | public function getRevision() 69 | { 70 | return $this->getMetadata()->getRevision(); 71 | } 72 | 73 | /** 74 | * @return \DateTimeInterface|null 75 | */ 76 | public function getCreatedAt() 77 | { 78 | return $this->getMetadata()->getCreatedAt(); 79 | } 80 | 81 | /** 82 | * @return \DateTimeInterface|null 83 | */ 84 | public function getUpdatedAt() 85 | { 86 | return $this->getMetadata()->getUpdatedAt(); 87 | } 88 | 89 | /** 90 | * @return string|null 91 | */ 92 | public function getLocale() 93 | { 94 | return $this->getMetadata()->getLocale(); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/MetadataInterface.php: -------------------------------------------------------------------------------- 1 | limit = intval($limit); 25 | } 26 | 27 | /** 28 | * The key in a query string on an API request. 29 | * 30 | * @return string 31 | */ 32 | public function getKey() 33 | { 34 | return self::KEY; 35 | } 36 | 37 | /** 38 | * The value in a query string on an API request. 39 | * 40 | * @return string 41 | */ 42 | public function getValue() 43 | { 44 | return (string) $this->limit; 45 | } 46 | 47 | /** 48 | * The name for the parameter (e.g. an EqualFilter would be called 'equal', etc) 49 | * 50 | * @return string 51 | */ 52 | public function getName() 53 | { 54 | return self::KEY; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/Parameter/OrderBy.php: -------------------------------------------------------------------------------- 1 | property = $property; 32 | $this->direction = ($direction === SORT_DESC) ? SORT_DESC : SORT_ASC; 33 | } 34 | 35 | /** 36 | * The key in a query string on an API request. 37 | * 38 | * @return string 39 | */ 40 | public function getKey() 41 | { 42 | return self::KEY; 43 | } 44 | 45 | /** 46 | * The value in a query string on an API request. 47 | * 48 | * @return string 49 | */ 50 | public function getValue() 51 | { 52 | $prefix = ($this->isDescending()) ? '-' : ''; 53 | 54 | return $prefix . $this->property->getKey(); 55 | } 56 | 57 | /** 58 | * The name for the parameter (e.g. an EqualFilter would be called 'equal', etc) 59 | * 60 | * @return string 61 | */ 62 | public function getName() 63 | { 64 | return self::KEY; 65 | } 66 | 67 | private function isDescending() 68 | { 69 | return $this->direction === SORT_DESC; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/Parameter/Query.php: -------------------------------------------------------------------------------- 1 | query = $query; 25 | } 26 | 27 | /** 28 | * The key in a query string on an API request. 29 | * 30 | * @return string 31 | */ 32 | public function getKey() 33 | { 34 | return self::KEY; 35 | } 36 | 37 | /** 38 | * The value in a query string on an API request. 39 | * 40 | * @return string 41 | */ 42 | public function getValue() 43 | { 44 | return $this->query; 45 | } 46 | 47 | /** 48 | * The name for the parameter (e.g. an EqualFilter would be called 'equal', etc) 49 | * 50 | * @return string 51 | */ 52 | public function getName() 53 | { 54 | return self::KEY; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/Parameter/Skip.php: -------------------------------------------------------------------------------- 1 | skip = intval($skip); 25 | } 26 | 27 | /** 28 | * The key in a query string on an API request. 29 | * 30 | * @return string 31 | */ 32 | public function getKey() 33 | { 34 | return self::KEY; 35 | } 36 | 37 | /** 38 | * The value in a query string on an API request. 39 | * 40 | * @return string 41 | */ 42 | public function getValue() 43 | { 44 | return (string) $this->skip; 45 | } 46 | 47 | /** 48 | * The name for the parameter (e.g. an EqualFilter would be called 'equal', etc) 49 | * 50 | * @return string 51 | */ 52 | public function getName() 53 | { 54 | return self::KEY; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/ParameterInterface.php: -------------------------------------------------------------------------------- 1 | getResolved(); 22 | if (!$resolved instanceof AssetInterface) { 23 | return ''; 24 | } 25 | 26 | return $resolved->getTitle(); 27 | } 28 | 29 | /** 30 | * @return string 31 | */ 32 | public function getDescription() 33 | { 34 | $resolved = $this->getResolved(); 35 | if (!$resolved instanceof AssetInterface) { 36 | return ''; 37 | } 38 | 39 | return $resolved->getDescription(); 40 | } 41 | 42 | /** 43 | * @return string 44 | */ 45 | public function getFilename() 46 | { 47 | $resolved = $this->getResolved(); 48 | if (!$resolved instanceof AssetInterface) { 49 | return ''; 50 | } 51 | 52 | return $resolved->getFilename(); 53 | } 54 | 55 | public function getMimeType() 56 | { 57 | $resolved = $this->getResolved(); 58 | if (!$resolved instanceof AssetInterface) { 59 | return ''; 60 | } 61 | 62 | return $resolved->getMimeType(); 63 | } 64 | 65 | /** 66 | * @param array|ImageApiOptions $imageApiOptions Options for rendering the image using the Image API @see http://docs.contentfulimagesapi.apiary.io/ 67 | * @return string 68 | */ 69 | public function getUrl($imageApiOptions = null) 70 | { 71 | $resolved = $this->getResolved(); 72 | if (!$resolved instanceof AssetInterface) { 73 | return ''; 74 | } 75 | 76 | return $resolved->getUrl($imageApiOptions); 77 | } 78 | 79 | /** 80 | * @return array 81 | */ 82 | public function getDetails() 83 | { 84 | $resolved = $this->getResolved(); 85 | if (!$resolved instanceof AssetInterface) { 86 | return []; 87 | } 88 | 89 | return $resolved->getDetails(); 90 | } 91 | 92 | /** 93 | * @return int 94 | */ 95 | public function getFileSizeInBytes() 96 | { 97 | $resolved = $this->getResolved(); 98 | if (!$resolved instanceof AssetInterface) { 99 | return 0; 100 | } 101 | 102 | return $resolved->getFileSizeInBytes(); 103 | } 104 | 105 | /** 106 | * @param PromiseInterface $promise 107 | * @return PromiseInterface 108 | */ 109 | protected function addRejectionHandlerToPromise(PromiseInterface $promise) 110 | { 111 | return $promise 112 | ->otherwise(function ($reason) { 113 | return new Asset('', '', null, new Metadata()); 114 | }); 115 | } 116 | 117 | /** 118 | * @return int|null 119 | */ 120 | public function getWidth() 121 | { 122 | $resolved = $this->getResolved(); 123 | if (!$resolved instanceof AssetInterface) { 124 | return null; 125 | } 126 | 127 | return $resolved->getWidth(); 128 | } 129 | 130 | /** 131 | * @return int|null 132 | */ 133 | public function getHeight() 134 | { 135 | $resolved = $this->getResolved(); 136 | if (!$resolved instanceof AssetInterface) { 137 | return null; 138 | } 139 | 140 | return $resolved->getHeight(); 141 | } 142 | 143 | /** 144 | * @return float|null 145 | */ 146 | public function getRatio() 147 | { 148 | $resolved = $this->getResolved(); 149 | if (!$resolved instanceof AssetInterface) { 150 | return null; 151 | } 152 | 153 | return $resolved->getRatio(); 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /src/Promise/ContentTypePromise.php: -------------------------------------------------------------------------------- 1 | getResolved(); 19 | if (!$resolved instanceof ContentTypeInterface) { 20 | return ''; 21 | } 22 | 23 | return $resolved->getName(); 24 | } 25 | 26 | /** 27 | * @return string 28 | */ 29 | public function getDescription() 30 | { 31 | $resolved = $this->getResolved(); 32 | if (!$resolved instanceof ContentTypeInterface) { 33 | return ''; 34 | } 35 | 36 | return $resolved->getDescription(); 37 | } 38 | 39 | /** 40 | * Returns the content type fields, keyed by ID. 41 | * 42 | * @return ContentTypeFieldInterface[] 43 | */ 44 | public function getFields() 45 | { 46 | $resolved = $this->getResolved(); 47 | if (!$resolved instanceof ContentTypeInterface) { 48 | return []; 49 | } 50 | 51 | return $resolved->getFields(); 52 | } 53 | 54 | /** 55 | * Returns the content type field matching the passed ID, or null if field does not exist. 56 | * 57 | * @param string $fieldId 58 | * @return ContentTypeFieldInterface|null 59 | */ 60 | public function getField($fieldId) 61 | { 62 | $resolved = $this->getResolved(); 63 | if (!$resolved instanceof ContentTypeInterface) { 64 | return null; 65 | } 66 | 67 | return $resolved->getField($fieldId); 68 | } 69 | 70 | /** 71 | * @return ContentTypeFieldInterface|null 72 | */ 73 | public function getDisplayField() 74 | { 75 | $resolved = $this->getResolved(); 76 | if (!$resolved instanceof ContentTypeInterface) { 77 | return null; 78 | } 79 | 80 | return $resolved->getDisplayField(); 81 | } 82 | 83 | /** 84 | * @param PromiseInterface $promise 85 | * @return PromiseInterface 86 | */ 87 | protected function addRejectionHandlerToPromise(PromiseInterface $promise) 88 | { 89 | return $promise 90 | ->otherwise(function ($reason) { 91 | return new ContentType('', '', [], new Metadata()); 92 | }); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/Promise/DelegatingMetadataPropertyAccessTrait.php: -------------------------------------------------------------------------------- 1 | getResolved(); 28 | if (!$resolved instanceof ResourceArrayInterface) { 29 | return false; 30 | } 31 | 32 | return $resolved->offsetExists($offset); 33 | } 34 | 35 | /** 36 | * @param mixed $offset 37 | * @return mixed 38 | */ 39 | public function offsetGet($offset) 40 | { 41 | $resolved = $this->getResolved(); 42 | if (!$resolved instanceof ResourceArrayInterface) { 43 | return null; 44 | } 45 | 46 | return $resolved->offsetGet($offset); 47 | } 48 | 49 | /** 50 | * Gets the type of resource. 51 | * 52 | * @return string 53 | */ 54 | public function getType() 55 | { 56 | return $this->getResolved()->getType(); 57 | } 58 | 59 | /** 60 | * Gets the unique ID of the resource. 61 | * 62 | * @return string 63 | */ 64 | public function getId() 65 | { 66 | return $this->getResolved()->getId(); 67 | } 68 | 69 | /** 70 | * Gets the space this resource is associated with. 71 | * 72 | * @return SpaceInterface 73 | */ 74 | public function getSpace() 75 | { 76 | return $this->getResolved()->getSpace(); 77 | } 78 | 79 | public function getSpaceName(): string 80 | { 81 | return $this->getResolved()->getSpaceName(); 82 | } 83 | 84 | /** 85 | * Gets the content type for an entry. (Only applicable for Entry resources.) 86 | * 87 | * @return ContentTypeInterface|null 88 | */ 89 | public function getContentType() 90 | { 91 | return $this->getResolved()->getContentType(); 92 | } 93 | 94 | /** 95 | * Gets the link type. (Only applicable for Link resources.) 96 | * 97 | * @return string|null 98 | */ 99 | public function getLinkType() 100 | { 101 | return $this->getResolved()->getLinkType(); 102 | } 103 | 104 | /** 105 | * Gets the revision number of this resource. 106 | * 107 | * @return int 108 | */ 109 | public function getRevision() 110 | { 111 | return $this->getResolved()->getRevision(); 112 | } 113 | 114 | /** 115 | * The time this resource was created. 116 | * 117 | * @return \DateTimeInterface|null 118 | */ 119 | public function getCreatedAt() 120 | { 121 | return $this->getResolved()->getCreatedAt(); 122 | } 123 | 124 | /** 125 | * The time this resource was last updated. 126 | * 127 | * @return \DateTimeInterface|null 128 | */ 129 | public function getUpdatedAt() 130 | { 131 | return $this->getResolved()->getUpdatedAt(); 132 | } 133 | 134 | /** 135 | * Gets the single locale for this resource, if there is one. 136 | * 137 | * @return null|string 138 | */ 139 | public function getLocale() 140 | { 141 | return $this->getResolved()->getLocale(); 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /src/Promise/DelegatingPromiseTrait.php: -------------------------------------------------------------------------------- 1 | promise = $promise; 17 | } 18 | 19 | protected function getPromise() 20 | { 21 | return $this->promise; 22 | } 23 | 24 | /** 25 | * Appends fulfillment and rejection handlers to the promise, and returns 26 | * a new promise resolving to the return value of the called handler. 27 | * 28 | * @param callable $onFulfilled Invoked when the promise fulfills. 29 | * @param callable $onRejected Invoked when the promise is rejected. 30 | * 31 | * @return PromiseInterface 32 | */ 33 | public function then( 34 | callable $onFulfilled = null, 35 | callable $onRejected = null 36 | ) { 37 | return $this->promise->then($onFulfilled, $onRejected); 38 | } 39 | 40 | /** 41 | * Appends a rejection handler callback to the promise, and returns a new 42 | * promise resolving to the return value of the callback if it is called, 43 | * or to its original fulfillment value if the promise is instead 44 | * fulfilled. 45 | * 46 | * @param callable $onRejected Invoked when the promise is rejected. 47 | * 48 | * @return PromiseInterface 49 | */ 50 | public function otherwise(callable $onRejected) 51 | { 52 | return $this->promise->otherwise($onRejected); 53 | } 54 | 55 | /** 56 | * Get the state of the promise ("pending", "rejected", or "fulfilled"). 57 | * 58 | * The three states can be checked against the constants defined on 59 | * PromiseInterface: PENDING, FULFILLED, and REJECTED. 60 | * 61 | * @return string 62 | */ 63 | public function getState() 64 | { 65 | return $this->promise->getState(); 66 | } 67 | 68 | /** 69 | * Resolve the promise with the given value. 70 | * 71 | * @param mixed $value 72 | * @throws \RuntimeException if the promise is already resolved. 73 | */ 74 | public function resolve($value) 75 | { 76 | $this->promise->resolve($value); 77 | } 78 | 79 | /** 80 | * Reject the promise with the given reason. 81 | * 82 | * @param mixed $reason 83 | * @throws \RuntimeException if the promise is already resolved. 84 | */ 85 | public function reject($reason) 86 | { 87 | $this->promise->reject($reason); 88 | } 89 | 90 | /** 91 | * Cancels the promise if possible. 92 | * 93 | * @link https://github.com/promises-aplus/cancellation-spec/issues/7 94 | */ 95 | public function cancel() 96 | { 97 | $this->promise->cancel(); 98 | } 99 | 100 | /** 101 | * Waits until the promise completes if possible. 102 | * 103 | * Pass $unwrap as true to unwrap the result of the promise, either 104 | * returning the resolved value or throwing the rejected exception. 105 | * 106 | * If the promise cannot be waited on, then the promise will be rejected. 107 | * 108 | * @param bool $unwrap 109 | * 110 | * @return mixed 111 | * @throws \LogicException if the promise has no wait function or if the 112 | * promise does not settle after waiting. 113 | */ 114 | public function wait($unwrap = true) 115 | { 116 | return $this->promise->wait($unwrap); 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /src/Promise/EntryPromise.php: -------------------------------------------------------------------------------- 1 | getResolved(); 23 | if (!$resolved instanceof EntryInterface) { 24 | return []; 25 | } 26 | 27 | return $resolved->getFields(); 28 | } 29 | 30 | /** 31 | * Gets an individual field value, or null if the field is not defined. 32 | * 33 | * @return mixed 34 | */ 35 | public function getField($key) 36 | { 37 | $resolved = $this->getResolved(); 38 | if (!$resolved instanceof EntryInterface) { 39 | return null; 40 | } 41 | 42 | return $resolved->getField($key); 43 | } 44 | 45 | /** 46 | * @param PromiseInterface $promise 47 | * @return PromiseInterface 48 | */ 49 | protected function addRejectionHandlerToPromise(PromiseInterface $promise) 50 | { 51 | return $promise 52 | ->otherwise(function ($reason) { 53 | return new Entry([], new Metadata()); 54 | }); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/Promise/ResolvedResourceTrait.php: -------------------------------------------------------------------------------- 1 | getPromise(); 22 | if (is_fulfilled($promise) && null !== $this->resolvedResource) { 23 | return; 24 | } 25 | $this->doResolve($promise); 26 | } 27 | 28 | protected function doResolve(PromiseInterface $promise) 29 | { 30 | $this->setResolvedResource($promise->wait()); 31 | } 32 | 33 | protected function setResolvedResource($resource) 34 | { 35 | $this->resolvedResource = $resource; 36 | } 37 | 38 | /** 39 | * @return ResourceInterface|ResourceArray 40 | */ 41 | protected function getResolved() 42 | { 43 | $this->ensureResolved(); 44 | 45 | return $this->resolvedResource; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Promise/ResourceArrayPromise.php: -------------------------------------------------------------------------------- 1 | getResolved(); 21 | if (!$resolved instanceof ResourceArrayInterface) { 22 | return new \ArrayIterator(); 23 | } 24 | 25 | return $resolved->getIterator(); 26 | } 27 | 28 | /** 29 | * Gets the total number of results in this array (i.e. not limited or offset). 30 | * 31 | * @return int 32 | */ 33 | public function getTotal() 34 | { 35 | $resolved = $this->getResolved(); 36 | if (!$resolved instanceof ResourceArrayInterface) { 37 | return 0; 38 | } 39 | 40 | return $resolved->getTotal(); 41 | } 42 | 43 | /** 44 | * @return int 45 | */ 46 | public function getSkip() 47 | { 48 | $resolved = $this->getResolved(); 49 | if (!$resolved instanceof ResourceArrayInterface) { 50 | return 0; 51 | } 52 | 53 | return $resolved->getSkip(); 54 | } 55 | 56 | /** 57 | * @return int 58 | */ 59 | public function getLimit() 60 | { 61 | $resolved = $this->getResolved(); 62 | if (!$resolved instanceof ResourceArrayInterface) { 63 | return 0; 64 | } 65 | 66 | return $resolved->getLimit(); 67 | } 68 | 69 | /** 70 | * @return ResourceEnvelopeInterface 71 | */ 72 | public function getEnvelope() 73 | { 74 | $resolved = $this->getResolved(); 75 | if (!$resolved instanceof ResourceArrayInterface) { 76 | return new MemoizedResourceEnvelope(); 77 | } 78 | 79 | return $resolved->getEnvelope(); 80 | } 81 | 82 | /** 83 | * Gets the count of items in this array. This does not represent the total count of a result set, but the possibly offset/limited count of items in this array. 84 | * 85 | * @return int 86 | */ 87 | public function count() 88 | { 89 | $resolved = $this->getResolved(); 90 | if (!$resolved instanceof ResourceArrayInterface) { 91 | return 0; 92 | } 93 | 94 | return count($resolved); 95 | } 96 | 97 | /** 98 | * Gets the first item in this array, or null if array is empty. 99 | * 100 | * @return ResourceInterface|null 101 | */ 102 | public function first() 103 | { 104 | $resolved = $this->getResolved(); 105 | if (!$resolved instanceof ResourceArrayInterface) { 106 | return null; 107 | } 108 | 109 | return $resolved->first(); 110 | } 111 | 112 | /** 113 | * Gets the last item in this array, or null if array is empty. 114 | * 115 | * @return ResourceInterface|null 116 | */ 117 | public function last() 118 | { 119 | $resolved = $this->getResolved(); 120 | if (!$resolved instanceof ResourceArrayInterface) { 121 | return null; 122 | } 123 | 124 | return $resolved->last(); 125 | } 126 | 127 | /** 128 | * @param PromiseInterface $promise 129 | * @return PromiseInterface 130 | */ 131 | protected function addRejectionHandlerToPromise(PromiseInterface $promise) 132 | { 133 | return $promise 134 | ->otherwise(function ($reason) { 135 | return new ResourceArray([], 0, 0, 0); 136 | }); 137 | } 138 | 139 | protected function doResolve(PromiseInterface $promise) 140 | { 141 | $resolved = $promise->wait(); 142 | //temporarily set resolved resource with array that may contain nulls 143 | $this->setResolvedResource($resolved); 144 | //now set it again but with access to skip/limit parameters etc - using a resource array will auto-filter nulls 145 | $this->setResolvedResource(new ResourceArray( 146 | $resolved, 147 | $this->getTotal(), 148 | $this->getSkip(), 149 | $this->getLimit(), 150 | $this->getEnvelope() 151 | )); 152 | } 153 | } 154 | -------------------------------------------------------------------------------- /src/Promise/ResourcePromise.php: -------------------------------------------------------------------------------- 1 | setPromise($this->addRejectionHandlerToPromise($promise)); 16 | } 17 | 18 | /** 19 | * @param PromiseInterface $promise 20 | * @return PromiseInterface 21 | */ 22 | abstract protected function addRejectionHandlerToPromise(PromiseInterface $promise); 23 | } 24 | -------------------------------------------------------------------------------- /src/Promise/SpacePromise.php: -------------------------------------------------------------------------------- 1 | getResolved(); 21 | if (!$resolved instanceof SpaceInterface) { 22 | return ''; 23 | } 24 | 25 | return $resolved->getName(); 26 | } 27 | 28 | /** 29 | * Gets the locales associated with the space. 30 | * 31 | * @return Locale[] 32 | */ 33 | public function getLocales() 34 | { 35 | $resolved = $this->getResolved(); 36 | if (!$resolved instanceof SpaceInterface) { 37 | return []; 38 | } 39 | 40 | return $resolved->getLocales(); 41 | } 42 | 43 | /** 44 | * Gets the default locale. 45 | * 46 | * @return Locale 47 | */ 48 | public function getDefaultLocale() 49 | { 50 | $resolved = $this->getResolved(); 51 | if (!$resolved instanceof SpaceInterface) { 52 | return new Locale('en', 'English'); 53 | } 54 | 55 | return $resolved->getDefaultLocale(); 56 | } 57 | 58 | protected function addRejectionHandlerToPromise(PromiseInterface $promise) 59 | { 60 | return $promise 61 | ->otherwise(function ($reason) { 62 | return new Space(null, new Metadata(), []); 63 | }); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/Property/FieldProperty.php: -------------------------------------------------------------------------------- 1 | name = $name; 20 | } 21 | 22 | /** 23 | * Gets the key to use against a Contentful API. 24 | * 25 | * @return string 26 | */ 27 | public function getKey() 28 | { 29 | return sprintf('fields.%s', $this->name); 30 | } 31 | 32 | /** 33 | * Cast to string, using the key to use against a Contentful API. 34 | * 35 | * @return string 36 | */ 37 | public function __toString() 38 | { 39 | return $this->getKey(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Property/Locale.php: -------------------------------------------------------------------------------- 1 | getKey(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Property/MimeTypeGroup.php: -------------------------------------------------------------------------------- 1 | getKey(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Property/SystemProperty.php: -------------------------------------------------------------------------------- 1 | name = $name; 20 | } 21 | 22 | /** 23 | * @return string 24 | */ 25 | public function getKey() 26 | { 27 | return sprintf('sys.%s', $this->name); 28 | } 29 | 30 | public function __toString() 31 | { 32 | return $this->getKey(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/PropertyInterface.php: -------------------------------------------------------------------------------- 1 | items = array_values(array_filter(array_values(iterator_to_array($items)), $filterNonResource)); 49 | } elseif (is_array($items)) { 50 | $this->items = array_values(array_filter(array_values($items), $filterNonResource)); 51 | } else { 52 | throw new \InvalidArgumentException('Items parameter should be an array or a traversable object.'); 53 | } 54 | $this->total = $total; 55 | $this->skip = $skip; 56 | $this->limit = $limit; 57 | $this->envelope = $envelope ?: new MemoizedResourceEnvelope(); 58 | } 59 | 60 | /** 61 | * Gets the total number of results in this array (i.e. not limited or offset). 62 | * 63 | * @return int 64 | */ 65 | public function getTotal() 66 | { 67 | return $this->total; 68 | } 69 | 70 | /** 71 | * @return int 72 | */ 73 | public function getSkip() 74 | { 75 | return $this->skip; 76 | } 77 | 78 | /** 79 | * @return int 80 | */ 81 | public function getLimit() 82 | { 83 | return $this->limit; 84 | } 85 | 86 | /** 87 | * @return \Traversable 88 | */ 89 | public function getIterator() 90 | { 91 | return new \ArrayIterator($this->items); 92 | } 93 | 94 | /** 95 | * @return ResourceEnvelopeInterface 96 | */ 97 | public function getEnvelope() 98 | { 99 | return $this->envelope; 100 | } 101 | 102 | /** 103 | * Gets the count of items in this array. This does not represent the total count of a result set, but the possibly offset/limited count of items in this array. 104 | * 105 | * @return int 106 | */ 107 | public function count() 108 | { 109 | return count($this->items); 110 | } 111 | 112 | /** 113 | * Gets the first item in this array, or null if array is empty. 114 | * 115 | * @return ResourceInterface|null 116 | */ 117 | public function first() 118 | { 119 | if (count($this->items) === 0) { 120 | return null; 121 | } 122 | 123 | return array_values($this->items)[0]; 124 | } 125 | 126 | /** 127 | * Gets the last item in this array, or null if array is empty. 128 | * 129 | * @return ResourceInterface|null 130 | */ 131 | public function last() 132 | { 133 | if (count($this->items) === 0) { 134 | return null; 135 | } 136 | 137 | return array_slice($this->items, -1)[0]; 138 | } 139 | 140 | /** 141 | * @return bool 142 | */ 143 | public function offsetExists($offset) 144 | { 145 | return isset($this->items[$offset]); 146 | } 147 | 148 | /** 149 | * @param mixed $offset 150 | * @return ResourceInterface|null 151 | */ 152 | public function offsetGet($offset) 153 | { 154 | if (!isset($this->items[$offset])) { 155 | return null; 156 | } 157 | 158 | return $this->items[$offset]; 159 | } 160 | 161 | /** 162 | * Ensures that this resource array strips out missing resources if they don't exist, so that counts and members 163 | * always reflect resources only. 164 | * 165 | * This will resolve a resource array if it is a promise/ future. 166 | * 167 | * @return void 168 | */ 169 | public function ensureNoMissing() 170 | { 171 | // constructor already ensures no missing, so no action necessary here 172 | } 173 | } 174 | -------------------------------------------------------------------------------- /src/ResourceArrayInterface.php: -------------------------------------------------------------------------------- 1 | envelopes = []; 19 | } 20 | 21 | public function getEnvelopeForSpace(string $space): ResourceEnvelopeInterface 22 | { 23 | if (!array_key_exists($space, $this->envelopes)) { 24 | throw new \RuntimeException(sprintf('No resource envelope exists for the space "%s".', $space)); 25 | } 26 | 27 | return $this->envelopes[$space]; 28 | } 29 | 30 | public function registerEnvelopeForSpace(ResourceEnvelopeInterface $envelope, string $space) 31 | { 32 | $this->envelopes[$space] = $envelope; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/ResourceInsertDelegationTrait.php: -------------------------------------------------------------------------------- 1 | insert($resourceItem); 17 | } 18 | } 19 | if ($resource instanceof EntryInterface) { 20 | return $this->insertEntry($resource); 21 | } 22 | if ($resource instanceof AssetInterface) { 23 | return $this->insertAsset($resource); 24 | } 25 | if ($resource instanceof ContentTypeInterface) { 26 | return $this->insertContentType($resource); 27 | } 28 | 29 | return $this; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/ResourceInterface.php: -------------------------------------------------------------------------------- 1 | name = $name; 33 | $this->metadata = $metadata; 34 | $this->locales = $locales; 35 | $this->defaultLocale = $defaultLocale ?: array_values($locales)[0]; 36 | $this->metadata->setSpace($this); 37 | } 38 | 39 | /** 40 | * Gets the name of the space. 41 | * 42 | * @return string|null 43 | */ 44 | public function getName() 45 | { 46 | return $this->name; 47 | } 48 | 49 | /** 50 | * Gets the locales associated with the space. 51 | * 52 | * @return Locale[] 53 | */ 54 | public function getLocales() 55 | { 56 | return $this->locales; 57 | } 58 | 59 | /** 60 | * Gets the default locale. 61 | * 62 | * @return Locale 63 | */ 64 | public function getDefaultLocale() 65 | { 66 | return $this->defaultLocale; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/SpaceInterface.php: -------------------------------------------------------------------------------- 1 | url = $url; 33 | $this->metadata = $metadata; 34 | $this->httpBasicUsername = $httpBasicUsername; 35 | $this->httpBasicPassword = $httpBasicPassword; 36 | } 37 | 38 | /** 39 | * Returns the URL that the webhook will hit. 40 | * 41 | * @return string 42 | */ 43 | public function getUrl() 44 | { 45 | return $this->url; 46 | } 47 | 48 | /** 49 | * Gets the HTTP Basic username being used to hit the webhook, if this is being used. 50 | * 51 | * @return string|null 52 | */ 53 | public function getHttpBasicUsername() 54 | { 55 | return $this->httpBasicUsername; 56 | } 57 | 58 | /** 59 | * Gets the HTTP Basic password being used to hit the webhook, if this is being used. 60 | * 61 | * @return string|null 62 | */ 63 | public function getHttpBasicPassword() 64 | { 65 | return $this->httpBasicPassword; 66 | } 67 | 68 | /** 69 | * Gets whether HTTP Basic is used for this webhook. 70 | * 71 | * @return bool 72 | */ 73 | public function usesHttpBasic() 74 | { 75 | return $this->httpBasicUsername && $this->httpBasicPassword; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/WebhookInterface.php: -------------------------------------------------------------------------------- 1 | responseCount = 42; 30 | $this->wasError = true; 31 | $this->analysis = new ResponseAnalysis( 32 | $this->responseCount, 33 | $this->wasError 34 | ); 35 | } 36 | 37 | public function testIsAnalysis() 38 | { 39 | $this->assertInstanceOf(ResponseAnalysisInterface::class, $this->analysis); 40 | } 41 | 42 | public function testGetters() 43 | { 44 | $this->assertEquals($this->responseCount, $this->analysis->getIndicatedResponseCount()); 45 | $this->assertEquals($this->wasError, $this->analysis->indicatesError()); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /tests/AssetTest.php: -------------------------------------------------------------------------------- 1 | assertTrue($refl->implementsInterface(AssetInterface::class)); 19 | } 20 | 21 | public function testCreateAssetWithNoFile() 22 | { 23 | $title = 'No File'; 24 | $description = 'This asset has no file, and so will be in draft'; 25 | $metadata = m::mock(MetadataInterface::class); 26 | $asset = new Asset($title, $description, null, $metadata); 27 | $this->assertEquals($title, $asset->getTitle()); 28 | $this->assertEquals($description, $asset->getDescription()); 29 | $this->assertNull($asset->getFilename()); 30 | $this->assertNull($asset->getMimeType()); 31 | $this->assertNull($asset->getUrl()); 32 | $this->assertEquals([], $asset->getDetails()); 33 | $this->assertEquals(0, $asset->getFileSizeInBytes()); 34 | } 35 | 36 | public function testGetUrlUsingImageApiOptionsArray() 37 | { 38 | $assetFile = m::mock(AssetFile::class); 39 | $baseUrl = 'http://domain.com/image'; 40 | $assetFile 41 | ->shouldReceive('getUrl') 42 | ->andReturn($baseUrl); 43 | $asset = new Asset('', '', $assetFile, m::mock(MetadataInterface::class)); 44 | $apiOptions = [ 45 | 'width' => 300, 46 | 'height' => 400, 47 | 'progressive' => true, 48 | ]; 49 | $expectedUrl = $baseUrl . '?fl=progressive&w=300&h=400'; 50 | $this->assertEquals($expectedUrl, $asset->getUrl($apiOptions)); 51 | } 52 | 53 | public function testGetUrlUsingImageApiOptionsObject() 54 | { 55 | $assetFile = m::mock(AssetFile::class); 56 | $baseUrl = 'http://domain.com/image'; 57 | $assetFile 58 | ->shouldReceive('getUrl') 59 | ->andReturn($baseUrl); 60 | $asset = new Asset('', '', $assetFile, m::mock(MetadataInterface::class)); 61 | $apiOptions = new ImageApiOptions(); 62 | $apiOptions->setProgressive(true); 63 | $apiOptions->setWidth(300); 64 | $apiOptions->setHeight(400); 65 | $expectedUrl = $baseUrl . '?fl=progressive&w=300&h=400'; 66 | $this->assertEquals($expectedUrl, $asset->getUrl($apiOptions)); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /tests/ContentTypeFieldTest.php: -------------------------------------------------------------------------------- 1 | assertTrue( 15 | (new \ReflectionClass(ContentTypeField::class)) 16 | ->implementsInterface(ContentTypeFieldInterface::class) 17 | ); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tests/ContentTypeTest.php: -------------------------------------------------------------------------------- 1 | assertTrue($refl->implementsInterface(ContentTypeInterface::class)); 17 | } 18 | 19 | public function testGetDisplayFieldWhenDefined() 20 | { 21 | $field1 = m::mock(ContentTypeField::class); 22 | $field2 = m::mock(ContentTypeField::class); 23 | $id1 = 'id1'; 24 | $id2 = 'id2'; 25 | $field1 26 | ->shouldReceive('getId') 27 | ->andReturn($id1); 28 | $field2 29 | ->shouldReceive('getId') 30 | ->andReturn($id2); 31 | $contentType = new ContentType('name', 'description', [$field1, $field2], m::mock('Markup\Contentful\MetadataInterface'), $id2); 32 | $this->assertSame($field2, $contentType->getDisplayField()); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/Decorator/CompositeAssetDecoratorTest.php: -------------------------------------------------------------------------------- 1 | composite = new CompositeAssetDecorator(); 21 | } 22 | 23 | public function testIsDecorator() 24 | { 25 | $this->assertInstanceOf(AssetDecoratorInterface::class, $this->composite); 26 | } 27 | 28 | public function testCompositeDoesNullDecorationByDefault() 29 | { 30 | $asset = $this->getMockAsset(); 31 | $id = 42; 32 | $asset 33 | ->shouldReceive('getId') 34 | ->andReturn($id); 35 | $this->assertEquals($id, $this->composite->decorate($asset)->getId()); 36 | } 37 | 38 | public function testCompositeWithTwoDecoratorsDecoratesInLifoOrder() 39 | { 40 | $initialAsset = $this->getMockAsset(); 41 | $asset1 = $this->getMockAsset(); 42 | $asset2 = $this->getMockAsset(); 43 | $decorator1 = $this->getMockDecorator(); 44 | $decorator2 = $this->getMockDecorator(); 45 | $decorator1 46 | ->shouldReceive('decorate') 47 | ->with($initialAsset) 48 | ->andReturn($asset2); 49 | $decorator2 50 | ->shouldReceive('decorate') 51 | ->with($asset2) 52 | ->andReturn($asset1); 53 | $this->composite->addDecorator($decorator1); 54 | $this->composite->addDecorator($decorator2); 55 | $this->assertSame($asset1, $this->composite->decorate($initialAsset)); 56 | } 57 | 58 | private function getMockAsset() 59 | { 60 | return m::mock(AssetInterface::class)->shouldIgnoreMissing(); 61 | } 62 | 63 | private function getMockDecorator() 64 | { 65 | return m::mock(AssetDecoratorInterface::class)->shouldIgnoreMissing(); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /tests/Decorator/ConcreteDecoratedAsset.php: -------------------------------------------------------------------------------- 1 | asset = m::mock(AssetInterface::class); 14 | $this->decorated = new ConcreteDecoratedAsset($this->asset); 15 | } 16 | 17 | public function testGetUrlPassesApiOptions() 18 | { 19 | $options = ['width' => 100, 'height' => 100]; 20 | $url = 'constrained_image.jpg'; 21 | $this->asset 22 | ->shouldReceive('getUrl') 23 | ->with($options) 24 | ->once() 25 | ->andReturn($url); 26 | $this->assertEquals($url, $this->decorated->getUrl($options)); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tests/Decorator/NullAssetDecoratorTest.php: -------------------------------------------------------------------------------- 1 | decorator = new NullAssetDecorator(); 15 | } 16 | 17 | public function testIsDecorator() 18 | { 19 | $this->assertInstanceOf(AssetDecoratorInterface::class, $this->decorator); 20 | } 21 | 22 | public function testDecorationReturnsSameAsset() 23 | { 24 | $asset = $this->createMock(AssetInterface::class); 25 | $this->assertSame($asset, $this->decorator->decorate($asset)); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tests/DynamicEntryTest.php: -------------------------------------------------------------------------------- 1 | entry = m::mock(Entry::class); 19 | $this->contentType = m::mock(ContentType::class)->shouldIgnoreMissing(); 20 | $this->entry 21 | ->shouldReceive('getContentType') 22 | ->andReturn($this->contentType); 23 | $this->dynamicEntry = new DynamicEntry($this->entry, $this->contentType); 24 | } 25 | 26 | public function testIsEntry() 27 | { 28 | $this->assertInstanceOf(EntryInterface::class, $this->dynamicEntry); 29 | } 30 | 31 | public function testCoercesToDate() 32 | { 33 | $key = 'date'; 34 | $contentTypeField = m::mock(ContentTypeField::class); 35 | $contentTypeField 36 | ->shouldReceive('getType') 37 | ->andReturn('Date'); 38 | $this->contentType 39 | ->shouldReceive('getField') 40 | ->with($key) 41 | ->andReturn($contentTypeField); 42 | $this->entry 43 | ->shouldReceive('getField') 44 | ->with($key) 45 | ->andReturn('2014-07-27T08:00:00Z'); 46 | $date = $this->dynamicEntry->getField($key); 47 | $this->assertInstanceOf(\DateTimeInterface::class, $date); 48 | $this->assertEquals('2014-07-27 08:00:00', $date->format('Y-m-d H:i:s')); 49 | } 50 | 51 | public function testGetLocation() 52 | { 53 | $key = 'location'; 54 | $contentTypeField = m::mock(ContentTypeField::class); 55 | $contentTypeField 56 | ->shouldReceive('getType') 57 | ->andReturn('Location'); 58 | $this->contentType 59 | ->shouldReceive('getField') 60 | ->with($key) 61 | ->andReturn($contentTypeField); 62 | $this->entry 63 | ->shouldReceive('getField') 64 | ->with($key) 65 | ->andReturn('23,42'); 66 | $location = $this->dynamicEntry->getField($key); 67 | $this->assertInstanceOf(Location::class, $location); 68 | $this->assertEquals(42, $location->getLongitude()); 69 | } 70 | 71 | public function testUnknownMethodCallsOnField() 72 | { 73 | $method = 'foo'; 74 | $value = 'baz'; 75 | $this->entry 76 | ->shouldReceive('getField') 77 | ->with($method) 78 | ->andReturn($value); 79 | $this->assertEquals($value, $this->dynamicEntry->$method()); 80 | } 81 | 82 | public function testExistenceCheckChecksAgainstContentType() 83 | { 84 | $contentTypeFieldIds = ['yes', 'ja']; 85 | $contentTypeFields = array_map(function ($id) { 86 | $field = m::mock(ContentTypeField::class); 87 | $field 88 | ->shouldReceive('getId') 89 | ->andReturn($id); 90 | 91 | return $field; 92 | }, $contentTypeFieldIds); 93 | $keyedFields = []; 94 | foreach ($contentTypeFields as $field) { 95 | $keyedFields[$field->getId()] = $field; 96 | } 97 | $this->contentType 98 | ->shouldReceive('getFields') 99 | ->andReturn($keyedFields); 100 | $this->assertArrayHasKey('ja', $this->dynamicEntry); 101 | $this->assertArrayNotHasKey('nein', $this->dynamicEntry); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /tests/Filter/AfterFilterTest.php: -------------------------------------------------------------------------------- 1 | property = m::mock(PropertyInterface::class); 33 | $this->relativeTime = 'now'; 34 | $this->filter = new AfterFilter($this->property, $this->relativeTime); 35 | } 36 | 37 | public function testIsFilter() 38 | { 39 | $this->assertInstanceOf(FilterInterface::class, $this->filter); 40 | } 41 | 42 | public function testIsPropertyFilter() 43 | { 44 | $this->assertInstanceOf(PropertyFilter::class, $this->filter); 45 | } 46 | 47 | public function testGetKeyUsesLessThan() 48 | { 49 | $propertyKey = 'created'; 50 | $this->property 51 | ->shouldReceive('getKey') 52 | ->andReturn($propertyKey); 53 | $this->assertEquals('created[gt]', $this->filter->getKey()); 54 | } 55 | 56 | /** 57 | * @group time-sensitive 58 | */ 59 | public function testGetDateTimeValue() 60 | { 61 | $nowValue = \DateTime::createFromFormat('U', time())->format('Y-m-d\TH:i:s\Z'); 62 | $this->assertEquals($nowValue, $this->filter->getValue()); 63 | } 64 | 65 | public function testDecidesCacheKey() 66 | { 67 | $this->assertInstanceOf(DecidesCacheKeyInterface::class, $this->filter); 68 | $propertyName = 'published'; 69 | $this->property 70 | ->shouldReceive('getKey') 71 | ->andReturn($propertyName); 72 | $this->assertEquals('|after|published↦now', $this->filter->getCacheKey()); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /tests/Filter/BeforeFilterTest.php: -------------------------------------------------------------------------------- 1 | property = m::mock(PropertyInterface::class); 33 | $this->relativeTime = 'now'; 34 | $this->filter = new BeforeFilter($this->property, $this->relativeTime); 35 | } 36 | 37 | public function testIsFilter() 38 | { 39 | $this->assertInstanceOf(FilterInterface::class, $this->filter); 40 | } 41 | 42 | public function testIsPropertyFilter() 43 | { 44 | $this->assertInstanceOf(PropertyFilter::class, $this->filter); 45 | } 46 | 47 | public function testGetKeyUsesLessThan() 48 | { 49 | $propertyKey = 'created'; 50 | $this->property 51 | ->shouldReceive('getKey') 52 | ->andReturn($propertyKey); 53 | $this->assertEquals('created[lt]', $this->filter->getKey()); 54 | } 55 | 56 | /** 57 | * @group time-sensitive 58 | */ 59 | public function testGetDateTimeValue() 60 | { 61 | $nowValue = \DateTime::createFromFormat('U', time())->format('Y-m-d\TH:i:s\Z'); 62 | $this->assertEquals($nowValue, $this->filter->getValue()); 63 | } 64 | 65 | public function testDecidesCacheKey() 66 | { 67 | $this->assertInstanceOf(DecidesCacheKeyInterface::class, $this->filter); 68 | $propertyName = 'published'; 69 | $this->property 70 | ->shouldReceive('getKey') 71 | ->andReturn($propertyName); 72 | $this->assertEquals('|before|published↦now', $this->filter->getCacheKey()); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /tests/Filter/ContentTypeFilterProviderTest.php: -------------------------------------------------------------------------------- 1 | contentful = m::mock(Contentful::class); 17 | $this->provider = new ContentTypeFilterProvider($this->contentful); 18 | } 19 | 20 | public function testCreateForExistingContentType() 21 | { 22 | $contentType = m::mock(ContentTypeInterface::class); 23 | $id = 42; 24 | $contentType 25 | ->shouldReceive('getId') 26 | ->andReturn($id); 27 | $name = 'unique_type'; 28 | $contentType 29 | ->shouldReceive('getName') 30 | ->andReturn($name); 31 | $this->contentful 32 | ->shouldReceive('getContentTypeByName') 33 | ->with($name, m::any()) 34 | ->andReturn($contentType); 35 | $filter = $this->provider->createForContentTypeName($name, 'test'); 36 | $this->assertInstanceOf(ContentTypeFilter::class, $filter); 37 | $this->assertEquals($id, $filter->getValue()); 38 | } 39 | 40 | public function testCreateForNotExistingContentType() 41 | { 42 | $this->contentful 43 | ->shouldReceive('getContentTypeByName') 44 | ->andReturn(null); 45 | $this->assertNull($this->provider->createForContentTypeName('unknown', 'test')); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /tests/Filter/ContentTypeFilterTest.php: -------------------------------------------------------------------------------- 1 | value = 'page'; 14 | $this->filter = new ContentTypeFilter($this->value); 15 | } 16 | 17 | public function testIsFilter() 18 | { 19 | $this->assertInstanceOf(FilterInterface::class, $this->filter); 20 | } 21 | 22 | public function testGetKey() 23 | { 24 | $this->assertEquals('content_type', $this->filter->getKey()); 25 | } 26 | 27 | public function testGetValue() 28 | { 29 | $this->assertEquals($this->value, $this->filter->getValue()); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/Filter/EqualFilterTest.php: -------------------------------------------------------------------------------- 1 | property = m::mock(PropertyInterface::class); 32 | $this->value = 'value'; 33 | $this->filter = new EqualFilter($this->property, $this->value); 34 | } 35 | 36 | public function testIsFilter() 37 | { 38 | $this->assertInstanceOf(FilterInterface::class, $this->filter); 39 | } 40 | 41 | public function testIsPropertyFilter() 42 | { 43 | $this->assertInstanceOf(PropertyFilter::class, $this->filter); 44 | } 45 | 46 | public function testGetKey() 47 | { 48 | $propertyKey = 'sys.id'; 49 | $this->property 50 | ->shouldReceive('getKey') 51 | ->andReturn($propertyKey); 52 | $this->assertEquals($propertyKey, $this->filter->getKey()); 53 | } 54 | 55 | public function testGetValue() 56 | { 57 | $this->assertEquals($this->value, $this->filter->getValue()); 58 | } 59 | 60 | public function testGetName() 61 | { 62 | $this->assertEquals('equal', $this->filter->getName()); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /tests/Filter/ExcludeFilterTest.php: -------------------------------------------------------------------------------- 1 | property = m::mock(PropertyInterface::class); 32 | $this->values = ['foo', 'bar']; 33 | $this->filter = new ExcludeFilter($this->property, $this->values); 34 | } 35 | 36 | public function testIsFilter() 37 | { 38 | $this->assertInstanceOf(FilterInterface::class, $this->filter); 39 | } 40 | 41 | public function testIsPropertyFilter() 42 | { 43 | $this->assertInstanceOf(PropertyFilter::class, $this->filter); 44 | } 45 | 46 | public function testGetKey() 47 | { 48 | $propertyKey = 'sys.id'; 49 | $this->property 50 | ->shouldReceive('getKey') 51 | ->andReturn($propertyKey); 52 | $this->assertEquals('sys.id[nin]', $this->filter->getKey()); 53 | } 54 | 55 | public function testGetValue() 56 | { 57 | $this->assertEquals('foo,bar', $this->filter->getValue()); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /tests/Filter/ExistsFilterTest.php: -------------------------------------------------------------------------------- 1 | property = m::mock(PropertyInterface::class); 31 | $this->propertyKey = 'prop'; 32 | $this->property 33 | ->shouldReceive('getKey') 34 | ->andReturn($this->propertyKey); 35 | $this->value = true; 36 | $this->filter = new ExistsFilter($this->property, $this->value); 37 | } 38 | 39 | public function testIsFilter() 40 | { 41 | $this->assertInstanceOf(FilterInterface::class, $this->filter); 42 | } 43 | 44 | public function testGetKey() 45 | { 46 | $this->assertEquals('prop[exists]', $this->filter->getKey()); 47 | } 48 | 49 | /** 50 | * @dataProvider values 51 | */ 52 | public function testGetValueReturnsBoolean($original, $boolean) 53 | { 54 | $filter = new ExistsFilter($this->property, $original); 55 | $this->assertSame($boolean, $filter->getValue()); 56 | } 57 | 58 | public function values() 59 | { 60 | return [ 61 | ['truthiness', 'true'], 62 | [0, 'false'], 63 | ]; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /tests/Filter/IncludeFilterTest.php: -------------------------------------------------------------------------------- 1 | property = m::mock(PropertyInterface::class); 17 | $this->values = ['foo', 'bar']; 18 | $this->filter = new IncludeFilter($this->property, $this->values); 19 | } 20 | 21 | public function testIsFilter() 22 | { 23 | $this->assertInstanceOf(FilterInterface::class, $this->filter); 24 | } 25 | 26 | public function testIsPropertyFilter() 27 | { 28 | $this->assertInstanceOf(PropertyFilter::class, $this->filter); 29 | } 30 | 31 | public function testGetKey() 32 | { 33 | $propertyKey = 'sys.id'; 34 | $this->property 35 | ->shouldReceive('getKey') 36 | ->andReturn($propertyKey); 37 | $this->assertEquals('sys.id[in]', $this->filter->getKey()); 38 | } 39 | 40 | public function testGetValue() 41 | { 42 | $this->assertEquals('foo,bar', $this->filter->getValue()); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /tests/Filter/LessThanFilterTest.php: -------------------------------------------------------------------------------- 1 | property = m::mock(PropertyInterface::class); 17 | $this->value = 3; 18 | $this->filter = new LessThanFilter($this->property, $this->value); 19 | } 20 | 21 | public function testIsFilter() 22 | { 23 | $this->assertInstanceOf(FilterInterface::class, $this->filter); 24 | } 25 | 26 | public function testIsPropertyFilter() 27 | { 28 | $this->assertInstanceOf(PropertyFilter::class, $this->filter); 29 | } 30 | 31 | public function testGetDateTimeValue() 32 | { 33 | $value = new \DateTime('2014-07-07 19:03:00', new \DateTimeZone('UTC')); 34 | $filter = new LessThanFilter($this->property, $value); 35 | $this->assertEquals('2014-07-07T19:03:00Z', $filter->getValue()); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /tests/Filter/LessThanOrEqualFilterTest.php: -------------------------------------------------------------------------------- 1 | property = m::mock(PropertyInterface::class); 16 | $this->value = 3; 17 | $this->filter = new LessThanOrEqualFilter($this->property, $this->value); 18 | } 19 | 20 | public function testIsFilter() 21 | { 22 | $this->assertInstanceOf(FilterInterface::class, $this->filter); 23 | } 24 | 25 | public function testGetName() 26 | { 27 | $this->assertEquals('less_than_or_equal', $this->filter->getName()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/Filter/LocaleFilterTest.php: -------------------------------------------------------------------------------- 1 | localeString = 'de_DE'; 25 | $this->filter = new LocaleFilter($this->localeString); 26 | } 27 | 28 | public function testIsFilter() 29 | { 30 | $this->assertInstanceOf(FilterInterface::class, $this->filter); 31 | } 32 | 33 | public function testIsPropertyFilter() 34 | { 35 | $this->assertInstanceOf(PropertyFilter::class, $this->filter); 36 | } 37 | 38 | public function testGetKey() 39 | { 40 | $this->assertEquals('locale', $this->filter->getKey()); 41 | } 42 | 43 | public function testGetValue() 44 | { 45 | $this->assertEquals($this->localeString, $this->filter->getValue()); 46 | } 47 | 48 | public function testGetName() 49 | { 50 | $this->assertEquals('locale', $this->filter->getName()); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /tests/Filter/MimeTypeGroupFilterTest.php: -------------------------------------------------------------------------------- 1 | mimeTypeGroup = 'plaintext'; 25 | $this->filter = new MimeTypeGroupFilter($this->mimeTypeGroup); 26 | } 27 | 28 | public function testIsFilter() 29 | { 30 | $this->assertInstanceOf(FilterInterface::class, $this->filter); 31 | } 32 | 33 | public function testIsPropertyFilter() 34 | { 35 | $this->assertInstanceOf(PropertyFilter::class, $this->filter); 36 | } 37 | 38 | public function testGetKey() 39 | { 40 | $this->assertEquals('mimetype_group', $this->filter->getKey()); 41 | } 42 | 43 | public function testGetValue() 44 | { 45 | $this->assertEquals($this->mimeTypeGroup, $this->filter->getValue()); 46 | } 47 | 48 | public function testGetName() 49 | { 50 | $this->assertEquals('mimetype_group', $this->filter->getName()); 51 | } 52 | 53 | public function testCannotCreateWithUnknownValue() 54 | { 55 | $this->expectException(\InvalidArgumentException::class); 56 | $this->expectExceptionMessage('"unknown" is not a known MIME type group. Known types: attachment, plaintext, image, audio, video, richtext, presentation, spreadsheet, pdf_document, archive, code, markup.'); 57 | new MimeTypeGroupFilter('unknown'); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /tests/Filter/NearFilterTest.php: -------------------------------------------------------------------------------- 1 | property = m::mock(PropertyInterface::class); 17 | $this->location = new Location(10, 20); 18 | $this->filter = new NearFilter($this->property, $this->location); 19 | } 20 | 21 | public function testIsFilter() 22 | { 23 | $this->assertInstanceOf(FilterInterface::class, $this->filter); 24 | } 25 | 26 | public function testGetKey() 27 | { 28 | $propertyKey = 'fields.location'; 29 | $this->property 30 | ->shouldReceive('getKey') 31 | ->andReturn($propertyKey); 32 | $this->assertEquals('fields.location[near]', $this->filter->getKey()); 33 | } 34 | 35 | public function testGetValue() 36 | { 37 | $this->assertEquals('10,20', $this->filter->getValue()); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /tests/Filter/NotEqualFilterTest.php: -------------------------------------------------------------------------------- 1 | property = m::mock(PropertyInterface::class); 17 | $this->value = 'value'; 18 | $this->filter = new NotEqualFilter($this->property, $this->value); 19 | } 20 | 21 | public function testIsFilter() 22 | { 23 | $this->assertInstanceOf(FilterInterface::class, $this->filter); 24 | } 25 | 26 | public function testIsPropertyFilter() 27 | { 28 | $this->assertInstanceOf(PropertyFilter::class, $this->filter); 29 | } 30 | 31 | public function testGetKey() 32 | { 33 | $propertyKey = 'sys.id'; 34 | $this->property 35 | ->shouldReceive('getKey') 36 | ->andReturn($propertyKey); 37 | $this->assertEquals('sys.id[ne]', $this->filter->getKey()); 38 | } 39 | 40 | public function testGetValue() 41 | { 42 | $this->assertEquals($this->value, $this->filter->getValue()); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /tests/Filter/SearchFilterTest.php: -------------------------------------------------------------------------------- 1 | query = 'look_for_me'; 16 | $this->property = m::mock(PropertyInterface::class); 17 | $this->filter = new SearchFilter($this->query, $this->property); 18 | } 19 | 20 | public function testIsFilter() 21 | { 22 | $this->assertInstanceOf(FilterInterface::class, $this->filter); 23 | } 24 | 25 | public function testGetKeyForQueryWithProperty() 26 | { 27 | $propertyKey = 'fields.description'; 28 | $this->property 29 | ->shouldReceive('getKey') 30 | ->andReturn($propertyKey); 31 | $this->assertEquals('fields.description[match]', $this->filter->getKey()); 32 | } 33 | 34 | public function testGetKeyForQueryWithoutProperty() 35 | { 36 | $filter = new SearchFilter($this->query); 37 | $this->assertEquals('query', $filter->getKey()); 38 | } 39 | 40 | public function testGetValue() 41 | { 42 | $this->assertEquals($this->query, $this->filter->getValue()); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /tests/Filter/WithinCircleFilterTest.php: -------------------------------------------------------------------------------- 1 | property = m::mock(PropertyInterface::class); 17 | $this->center = new Location(15, 40); 18 | $this->radiusInKm = 42; 19 | $this->filter = new WithinCircleFilter($this->property, $this->center, $this->radiusInKm); 20 | } 21 | 22 | public function testIsFilter() 23 | { 24 | $this->assertInstanceOf(FilterInterface::class, $this->filter); 25 | } 26 | 27 | public function testGetKey() 28 | { 29 | $propertyKey = 'fields.location'; 30 | $this->property 31 | ->shouldReceive('getKey') 32 | ->andReturn($propertyKey); 33 | $this->assertEquals('fields.location[within]', $this->filter->getKey()); 34 | } 35 | 36 | public function testGetValue() 37 | { 38 | $this->assertEquals('15,40,42', $this->filter->getValue()); 39 | } 40 | 41 | public function testGetName() 42 | { 43 | $this->assertEquals('within_circle', $this->filter->getName()); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /tests/Filter/WithinRectangleFilterTest.php: -------------------------------------------------------------------------------- 1 | property = m::mock(PropertyInterface::class); 40 | $this->bottomLeftLocation = new Location(35, 55); 41 | $this->topRightLocation = new Location(20, 70); 42 | $this->filter = new WithinRectangleFilter($this->property, $this->bottomLeftLocation, $this->topRightLocation); 43 | } 44 | 45 | public function testIsFilter() 46 | { 47 | $this->assertInstanceOf(FilterInterface::class, $this->filter); 48 | } 49 | 50 | public function testGetKey() 51 | { 52 | $propertyKey = 'fields.location'; 53 | $this->property 54 | ->shouldReceive('getKey') 55 | ->andReturn($propertyKey); 56 | $this->assertEquals('fields.location[within]', $this->filter->getKey()); 57 | } 58 | 59 | public function testGetValue() 60 | { 61 | $this->assertEquals('35,55,20,70', $this->filter->getValue()); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /tests/ImageApiOptionsTest.php: -------------------------------------------------------------------------------- 1 | ImageApiOptions::FORMAT_JPEG, 14 | 'quality' => 90, 15 | 'progressive' => true, 16 | 'width' => 200, 17 | 'height' => 250, 18 | 'fit' => ImageApiOptions::FIT_THUMB, 19 | 'focus' => ImageApiOptions::FOCUS_FACES, 20 | 'radius' => 20, 21 | 'background_color' => 'AAAAAA', 22 | ]; 23 | $options = ImageApiOptions::createFromHumanOptions($optionsArr); 24 | $this->assertInstanceOf(ImageApiOptions::class, $options); 25 | $expected = [ 26 | 'bg' => 'rgb:aaaaaa', 27 | 'f' => 'faces', 28 | 'fit' => 'thumb', 29 | 'fl' => 'progressive', 30 | 'fm' => 'jpg', 31 | 'h' => 250, 32 | 'q' => 90, 33 | 'r' => 20, 34 | 'w' => 200, 35 | ]; 36 | $apiOptionsArray = $options->toArray(); 37 | ksort($apiOptionsArray); 38 | $this->assertEquals($expected, $apiOptionsArray); 39 | } 40 | 41 | public function testUsingSetters() 42 | { 43 | $options = new ImageApiOptions(); 44 | $options->setImageFormat(ImageApiOptions::FORMAT_JPEG); 45 | $options->setQuality(90); 46 | $options->setProgressive(true); 47 | $options->setWidth(200); 48 | $options->setHeight(250); 49 | $options->setFit(ImageApiOptions::FIT_THUMB, ImageApiOptions::FOCUS_FACES); 50 | $options->setRadius(20); 51 | $options->setBackgroundColor('AAAAAA'); 52 | $expected = [ 53 | 'bg' => 'rgb:aaaaaa', 54 | 'f' => 'faces', 55 | 'fit' => 'thumb', 56 | 'fl' => 'progressive', 57 | 'fm' => 'jpg', 58 | 'h' => 250, 59 | 'q' => 90, 60 | 'r' => 20, 61 | 'w' => 200, 62 | ]; 63 | $apiOptionsArray = $options->toArray(); 64 | ksort($apiOptionsArray); 65 | $this->assertEquals($expected, $apiOptionsArray); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /tests/LinkTest.php: -------------------------------------------------------------------------------- 1 | getMockMetadata(), $name); 17 | $this->assertEquals($name, $link->getSpaceName()); 18 | } 19 | 20 | public function testIsLink() 21 | { 22 | $link = new Link($this->getMockMetadata(), 'name'); 23 | $this->assertInstanceOf(LinkInterface::class, $link); 24 | } 25 | 26 | private function getMockMetadata() 27 | { 28 | return m::mock(MetadataInterface::class); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tests/Log/LinkResolveCounterTest.php: -------------------------------------------------------------------------------- 1 | counter = new LinkResolveCounter(); 22 | } 23 | 24 | public function testIsCounter() 25 | { 26 | $this->assertInstanceOf(LinkResolveCounterInterface::class, $this->counter); 27 | } 28 | 29 | public function testAddLinks() 30 | { 31 | $link1 = $this->getLinkForIdAndSpaceName('jsdhfdjksh', 'space1'); 32 | $link2 = $this->getLinkForIdAndSpaceName('kjsdhfdksj', 'space2'); 33 | $this->assertCount(0, $this->counter); 34 | $this->counter->logLink($link1); 35 | $this->assertCount(1, $this->counter); 36 | $this->counter->logLink($link2); 37 | $this->assertCount(2, $this->counter); 38 | $this->counter->logLink($link2); 39 | $this->assertCount(2, $this->counter, 'check count in counter is still the same after a duplicate link is logged'); 40 | } 41 | 42 | private function getLinkForIdAndSpaceName(string $id, string $spaceName) 43 | { 44 | return m::mock(LinkInterface::class) 45 | ->shouldReceive('getId') 46 | ->andReturn($id) 47 | ->getMock() 48 | ->shouldReceive('getSpaceName') 49 | ->andReturn($spaceName) 50 | ->getMock(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /tests/Log/LogTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(LogInterface::class, $log); 34 | $this->assertEquals($description, $log->getDescription()); 35 | $this->assertEquals($duration, $log->getDurationInSeconds()); 36 | $this->assertSame($startTime, $log->getStartTime()); 37 | $this->assertSame($stopTime, $log->getStopTime()); 38 | $this->assertEquals($resourceType, $log->getResourceType()); 39 | $this->assertEquals($api, $log->getApi()); 40 | $this->assertEquals($responseCount, $log->getResponseCount()); 41 | $this->assertEquals($wasError, $log->wasError()); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /tests/Log/StandardLoggerTest.php: -------------------------------------------------------------------------------- 1 | logger = new StandardLogger(); 22 | } 23 | 24 | public function testIsLogger() 25 | { 26 | $this->assertInstanceOf(LoggerInterface::class, $this->logger); 27 | } 28 | 29 | public function testLogOneItem() 30 | { 31 | $initialLogs = $this->logger->getLogs(); 32 | $this->assertCount(0, $initialLogs); 33 | $timer = $this->logger->getStartedTimer(); 34 | $this->assertInstanceOf(TimerInterface::class, $timer); 35 | $this->assertTrue($timer->isStarted()); 36 | $description = 'description goes here'; 37 | $resourceType = LogInterface::RESOURCE_ASSET; 38 | $api = Contentful::CONTENT_DELIVERY_API; 39 | $responseCount = 43; 40 | $wasError = false; 41 | $this->logger->log($description, $timer, $resourceType, $api, $responseCount, $wasError); 42 | $finalLogs = $this->logger->getLogs(); 43 | $this->assertCount(1, $finalLogs); 44 | $log = reset($finalLogs); 45 | $this->assertInstanceOf(LogInterface::class, $log); 46 | $this->assertEquals($description, $log->getDescription()); 47 | $this->assertIsFloat($log->getDurationInSeconds()); 48 | $this->assertLessThan(1, $log->getDurationInSeconds()); 49 | $this->assertEquals($api, $log->getApi()); 50 | $this->assertEquals($responseCount, $log->getResponseCount()); 51 | $this->assertEquals($wasError, $log->wasError()); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /tests/Log/StandardTimerTest.php: -------------------------------------------------------------------------------- 1 | timer = new StandardTimer(); 19 | } 20 | 21 | public function testIsTimer() 22 | { 23 | $this->assertInstanceOf(TimerInterface::class, $this->timer); 24 | } 25 | 26 | public function testStandardCycle() 27 | { 28 | $this->assertFalse($this->timer->isStarted()); 29 | $this->timer->start(); 30 | $this->assertTrue($this->timer->isStarted()); 31 | $this->assertFalse($this->timer->isStopped()); 32 | $runningDuration = $this->timer->getDurationInSeconds(); 33 | $this->assertIsFloat($runningDuration); 34 | $this->assertLessThan(2, $runningDuration);//seems reasonable to suppose that this will have executed in less than 2s 35 | $this->timer->stop(); 36 | $this->assertTrue($this->timer->isStopped()); 37 | $finalDuration = $this->timer->getDurationInSeconds(); 38 | $secondFinalDuration = $this->timer->getDurationInSeconds(); 39 | $this->assertSame($finalDuration, $secondFinalDuration); 40 | $this->assertIsFloat($finalDuration); 41 | $this->assertGreaterThan($runningDuration, $finalDuration); 42 | $this->assertLessThan(2, $finalDuration);//same applies to final duration 43 | $this->timer->start(); 44 | $this->timer->stop(); 45 | $this->assertEquals($finalDuration, $this->timer->getDurationInSeconds(), 'the timer cannot be reused'); 46 | $this->assertInstanceOf(\DateTimeInterface::class, $this->timer->getStartTime()); 47 | $this->assertInstanceOf(\DateTimeInterface::class, $this->timer->getStopTime()); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /tests/Parameter/LimitTest.php: -------------------------------------------------------------------------------- 1 | count = 3; 14 | $this->limit = new Limit($this->count); 15 | } 16 | 17 | public function testIsParameter() 18 | { 19 | $this->assertInstanceOf(ParameterInterface::class, $this->limit); 20 | } 21 | 22 | public function testGetKey() 23 | { 24 | $this->assertEquals('limit', $this->limit->getKey()); 25 | } 26 | 27 | public function testGetValue() 28 | { 29 | $this->assertEquals($this->count, $this->limit->getValue()); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/Parameter/OrderByTest.php: -------------------------------------------------------------------------------- 1 | property = m::mock(PropertyInterface::class); 16 | $this->orderBy = new OrderBy($this->property); 17 | } 18 | 19 | public function testIsParameter() 20 | { 21 | $this->assertInstanceOf(ParameterInterface::class, $this->orderBy); 22 | } 23 | 24 | public function testGetKey() 25 | { 26 | $this->assertEquals('order', $this->orderBy->getKey()); 27 | } 28 | 29 | public function testGetValue() 30 | { 31 | $propertyKey = 'fields.birthday'; 32 | $this->property 33 | ->shouldReceive('getKey') 34 | ->andReturn($propertyKey); 35 | $this->assertEquals($propertyKey, $this->orderBy->getValue()); 36 | } 37 | 38 | public function testDescending() 39 | { 40 | $orderBy = new OrderBy($this->property, SORT_DESC); 41 | $propertyKey = 'fields.birthday'; 42 | $this->property 43 | ->shouldReceive('getKey') 44 | ->andReturn($propertyKey); 45 | $this->assertEquals('-' . $propertyKey, $orderBy->getValue()); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /tests/Parameter/SkipTest.php: -------------------------------------------------------------------------------- 1 | count = 42; 14 | $this->skip = new Skip($this->count); 15 | } 16 | 17 | public function testIsParameter() 18 | { 19 | $this->assertInstanceOf(ParameterInterface::class, $this->skip); 20 | } 21 | 22 | public function testGetKey() 23 | { 24 | $this->assertEquals('skip', $this->skip->getKey()); 25 | } 26 | 27 | public function testGetValue() 28 | { 29 | $this->assertEquals($this->count, $this->skip->getValue()); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/Promise/AssetPromiseTest.php: -------------------------------------------------------------------------------- 1 | asset = new AssetPromise(promise_for(m::mock(AssetInterface::class))); 22 | } 23 | 24 | public function testIsAsset() 25 | { 26 | $this->assertInstanceOf(AssetInterface::class, $this->asset); 27 | } 28 | 29 | public function testIsPromise() 30 | { 31 | $this->assertInstanceOf(PromiseInterface::class, $this->asset); 32 | } 33 | 34 | public function testGetFilename() 35 | { 36 | $inner = m::mock(AssetInterface::class); 37 | $filename = 'file.gif'; 38 | $inner 39 | ->shouldReceive('getFilename') 40 | ->andReturn($filename); 41 | $asset = new AssetPromise(promise_for($inner)); 42 | $this->assertEquals($filename, $asset->getFilename()); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /tests/Promise/ContentTypePromiseTest.php: -------------------------------------------------------------------------------- 1 | contentType = new ContentTypePromise(promise_for(m::mock(ContentTypeInterface::class))); 22 | } 23 | 24 | public function testIsContentType() 25 | { 26 | $this->assertInstanceOf(ContentTypeInterface::class, $this->contentType); 27 | } 28 | 29 | public function testIsPromise() 30 | { 31 | $this->assertInstanceOf(PromiseInterface::class, $this->contentType); 32 | } 33 | 34 | public function testGetDescription() 35 | { 36 | $description = 'Mock!'; 37 | $inner = m::mock(ContentTypeInterface::class) 38 | ->shouldReceive('getDescription') 39 | ->andReturn($description) 40 | ->getMock(); 41 | $contentType = new ContentTypePromise(promise_for($inner)); 42 | $this->assertEquals($description, $contentType->getDescription()); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /tests/Promise/EntryPromiseTest.php: -------------------------------------------------------------------------------- 1 | entry = new EntryPromise(promise_for(m::mock(EntryInterface::class))); 22 | } 23 | 24 | public function testIsEntry() 25 | { 26 | $this->assertInstanceOf(EntryInterface::class, $this->entry); 27 | } 28 | 29 | public function testIsPromise() 30 | { 31 | $this->assertInstanceOf(PromiseInterface::class, $this->entry); 32 | } 33 | 34 | public function testGetField() 35 | { 36 | $inner = m::mock(EntryInterface::class); 37 | $inner 38 | ->shouldReceive('getField') 39 | ->with(m::type('string')) 40 | ->andReturnUsing(function ($fieldName) { 41 | return $fieldName; 42 | }); 43 | $entry = new EntryPromise(promise_for($inner)); 44 | $this->assertEquals('yayayay', $entry->getField('yayayay')); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /tests/Promise/ResourceArrayPromiseTest.php: -------------------------------------------------------------------------------- 1 | resourceArray = new ResourceArrayPromise(promise_for(m::mock(ResourceArrayInterface::class))); 26 | } 27 | 28 | public function testIsResourceArray() 29 | { 30 | $this->assertInstanceOf(ResourceArrayInterface::class, $this->resourceArray); 31 | } 32 | 33 | public function testIsPromise() 34 | { 35 | $this->assertInstanceOf(PromiseInterface::class, $this->resourceArray); 36 | } 37 | 38 | public function testGetEnvelope() 39 | { 40 | $envelope = m::mock(ResourceEnvelopeInterface::class); 41 | $inner = m::spy(ResourceArrayInterface::class) 42 | ->shouldReceive('getEnvelope') 43 | ->andReturn($envelope) 44 | ->shouldReceive('getIterator') 45 | ->andReturn(new \ArrayIterator()) 46 | ->getMock(); 47 | $resourceArray = new ResourceArrayPromise(promise_for($inner)); 48 | $this->assertSame($envelope, $resourceArray->getEnvelope()); 49 | } 50 | 51 | public function testGettersForArray() 52 | { 53 | $resourceArray = new ResourceArrayPromise(promise_for([ 54 | m::mock(EntryInterface::class), 55 | m::mock(AssetInterface::class), 56 | ])); 57 | $this->assertInstanceOf(ResourceEnvelopeInterface::class, $resourceArray->getEnvelope()); 58 | $this->assertCount(2, $resourceArray); 59 | $this->assertInstanceOf(EntryInterface::class, $resourceArray->first()); 60 | $this->assertInstanceOf(AssetInterface::class, $resourceArray->last()); 61 | } 62 | 63 | public function testFiltersEmpty() 64 | { 65 | $resourceArray = new ResourceArrayPromise(promise_for([ 66 | m::mock(EntryInterface::class), 67 | null, 68 | m::mock(AssetInterface::class), 69 | ])); 70 | $this->assertCount(2, $resourceArray); 71 | $this->assertInstanceOf(EntryInterface::class, $resourceArray->first()); 72 | $this->assertInstanceOf(AssetInterface::class, $resourceArray->last()); 73 | $this->assertInstanceOf(AssetInterface::class, $resourceArray[1]); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /tests/Promise/SpacePromiseTest.php: -------------------------------------------------------------------------------- 1 | space = new SpacePromise(promise_for(SpaceInterface::class)); 23 | } 24 | 25 | public function testIsSpace() 26 | { 27 | $this->assertInstanceOf(SpaceInterface::class, $this->space); 28 | } 29 | 30 | public function testIsPromise() 31 | { 32 | $this->assertInstanceOf(PromiseInterface::class, $this->space); 33 | } 34 | 35 | public function testGetName() 36 | { 37 | $name = 'outer space'; 38 | $inner = m::mock(SpaceInterface::class) 39 | ->shouldReceive('getName') 40 | ->andReturn($name) 41 | ->getMock(); 42 | $space = new SpacePromise(promise_for($inner)); 43 | $this->assertEquals($name, $space->getName()); 44 | } 45 | 46 | public function testGetLocales() 47 | { 48 | $locales = [m::mock(Locale::class)]; 49 | $inner = m::mock(SpaceInterface::class) 50 | ->shouldReceive('getLocales') 51 | ->andReturn($locales) 52 | ->getMock(); 53 | $space = new SpacePromise(promise_for($inner)); 54 | $this->assertSame($locales, $space->getLocales()); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /tests/Property/FieldPropertyTest.php: -------------------------------------------------------------------------------- 1 | propertyName = 'likes'; 24 | $this->property = new FieldProperty($this->propertyName); 25 | } 26 | 27 | public function testIsProperty() 28 | { 29 | $this->assertInstanceOf(PropertyInterface::class, $this->property); 30 | } 31 | 32 | public function testGetKey() 33 | { 34 | $this->assertEquals(sprintf('fields.%s', $this->propertyName), $this->property->getKey()); 35 | } 36 | 37 | public function testToString() 38 | { 39 | $this->assertEquals(sprintf('fields.%s', $this->propertyName), strval($this->property)); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /tests/Property/LocaleTest.php: -------------------------------------------------------------------------------- 1 | localeProp = new Locale(); 14 | } 15 | 16 | public function testIsProperty() 17 | { 18 | $this->assertInstanceOf(PropertyInterface::class, $this->localeProp); 19 | } 20 | 21 | public function testGetKey() 22 | { 23 | $this->assertEquals('locale', $this->localeProp->getKey()); 24 | } 25 | 26 | public function testToString() 27 | { 28 | $this->assertEquals('locale', strval($this->localeProp)); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tests/Property/MimeTypeGroupTest.php: -------------------------------------------------------------------------------- 1 | mimeTypeProp = new MimeTypeGroup(); 19 | } 20 | 21 | public function testIsProperty() 22 | { 23 | $this->assertInstanceOf(PropertyInterface::class, $this->mimeTypeProp); 24 | } 25 | 26 | public function testGetKey() 27 | { 28 | $this->assertEquals('mimetype_group', $this->mimeTypeProp->getKey()); 29 | } 30 | 31 | public function testToString() 32 | { 33 | $this->assertEquals('mimetype_group', strval($this->mimeTypeProp)); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tests/Property/SystemPropertyTest.php: -------------------------------------------------------------------------------- 1 | propertyName = 'id'; 24 | $this->property = new SystemProperty($this->propertyName); 25 | } 26 | 27 | public function testIsProperty() 28 | { 29 | $this->assertInstanceOf(PropertyInterface::class, $this->property); 30 | } 31 | 32 | public function testGetKey() 33 | { 34 | $this->assertEquals(sprintf('sys.%s', $this->propertyName), $this->property->getKey()); 35 | } 36 | 37 | public function testToString() 38 | { 39 | $this->assertEquals(sprintf('sys.%s', $this->propertyName), strval($this->property)); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /tests/ResourceArrayTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(ResourceArrayInterface::class, new ResourceArray([], 0, 0, 1)); 16 | } 17 | 18 | public function testArray() 19 | { 20 | $item1 = $this->getMockEntry(); 21 | $item2 = $this->getMockEntry(); 22 | $items = new \ArrayIterator(['item1' => $item1, 'item2' => $item2]); 23 | $total = 22; 24 | $skip = 20; 25 | $limit = 10; 26 | $array = new ResourceArray($items, $total, $skip, $limit); 27 | $this->assertEquals(22, $array->getTotal()); 28 | $this->assertEquals(20, $array->getSkip()); 29 | $this->assertEquals(10, $array->getLimit()); 30 | $this->assertEquals([$item1, $item2], iterator_to_array($array)); 31 | //array access 32 | $this->assertSame($item1, $array[0]); 33 | } 34 | 35 | public function testIterationExcludesNull() 36 | { 37 | $item1 = $this->getMockEntry(); 38 | $item2 = $this->getMockEntry(); 39 | $items = ['item1' => $item1, 'item2' => $item2, 'item3' => null]; 40 | $total = 22; 41 | $skip = 20; 42 | $limit = 10; 43 | $array = new ResourceArray($items, $total, $skip, $limit); 44 | $this->assertEquals([$item1, $item2], iterator_to_array($array)); 45 | } 46 | 47 | public function testFirstWhenArrayNotEmpty() 48 | { 49 | $item1 = $this->getMockEntry(); 50 | $item2 = $this->getMockEntry(); 51 | $items = ['item1' => $item1, 'item2' => $item2]; 52 | $total = 22; 53 | $skip = 20; 54 | $limit = 10; 55 | $array = new ResourceArray($items, $total, $skip, $limit); 56 | $this->assertSame($item1, $array->first()); 57 | } 58 | 59 | public function testFirstWhenArrayEmptyReturnsNull() 60 | { 61 | $array = new ResourceArray([], 22, 20, 10); 62 | $this->assertNull($array->first()); 63 | } 64 | 65 | public function testLastWhenArrayNotEmpty() 66 | { 67 | $item1 = $this->getMockEntry(); 68 | $item2 = $this->getMockEntry(); 69 | $items = ['item1' => $item1, 'item2' => $item2]; 70 | $total = 22; 71 | $skip = 20; 72 | $limit = 10; 73 | $array = new ResourceArray($items, $total, $skip, $limit); 74 | $this->assertSame($item2, $array->last()); 75 | } 76 | 77 | public function testLastWhenArrayEmptyReturnsNull() 78 | { 79 | $array = new ResourceArray([], 22, 20, 10); 80 | $this->assertNull($array->last()); 81 | } 82 | 83 | private function getMockEntry() 84 | { 85 | return m::mock(EntryInterface::class); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /tests/ResourceEnvelopePoolTest.php: -------------------------------------------------------------------------------- 1 | space = 'i_am_the_space'; 31 | $this->envelope = m::mock(ResourceEnvelopeInterface::class); 32 | $this->pool = new ResourceEnvelopePool(); 33 | $this->pool->registerEnvelopeForSpace($this->envelope, $this->space); 34 | } 35 | 36 | public function testGetEnvelope() 37 | { 38 | $this->assertSame($this->envelope, $this->pool->getEnvelopeForSpace($this->space)); 39 | } 40 | 41 | public function testGetUnknownEnvelope() 42 | { 43 | $this->expectException(\RuntimeException::class); 44 | $this->pool->getEnvelopeForSpace('unknown'); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /tests/WebhookTest.php: -------------------------------------------------------------------------------- 1 | url = 'http://domain.com/hook'; 16 | $this->metadata = m::mock(MetadataInterface::class); 17 | $this->hook = new Webhook($this->url, $this->metadata); 18 | } 19 | 20 | protected function tearDown() 21 | { 22 | m::close(); 23 | } 24 | 25 | public function testIsWebhook() 26 | { 27 | $this->assertInstanceOf(WebhookInterface::class, $this->hook); 28 | } 29 | 30 | public function testGetId() 31 | { 32 | $id = '43'; 33 | $this->metadata 34 | ->shouldReceive('getId') 35 | ->andReturn($id); 36 | $this->assertEquals($id, $this->hook->getId()); 37 | } 38 | 39 | public function testUsesHttpBasic() 40 | { 41 | $this->assertFalse($this->hook->usesHttpBasic()); 42 | $httpBasicHook = new Webhook($this->url, $this->metadata, 'user', 'pass'); 43 | $this->assertTrue($httpBasicHook->usesHttpBasic()); 44 | } 45 | } 46 | --------------------------------------------------------------------------------