diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a0caea92..457d82de9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - JSON update requests give precedence to `Stringable` over `JsonSerializable` for object set as field value to keep behaviour consistent across request formats +### Removed +- Support for config objects, you have to convert them to an array before passing to a constructor or `setOptions()` + ## [6.3.5] ### Added diff --git a/README.md b/README.md index c118347e6..819535f13 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ composer require solarium/solarium When upgrading from an earlier version, you should be aware of a number of pitfalls. +* [Pitfall when upgrading to 6.3.6](https://solarium.readthedocs.io/en/stable/getting-started/#pitfall-when-upgrading-to-636) * [Pitfall when upgrading to 6.3.2](https://solarium.readthedocs.io/en/stable/getting-started/#pitfall-when-upgrading-to-632) * [Pitfall when upgrading to 6.3](https://solarium.readthedocs.io/en/stable/getting-started/#pitfall-when-upgrading-to-63) * [Pitfalls when upgrading from 3.x or 4.x or 5.x](https://solarium.readthedocs.io/en/stable/getting-started/#pitfalls-when-upgrading-from-3x-or-4x-or-5x) diff --git a/docs/getting-started.md b/docs/getting-started.md index d04979c4d..719181b03 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -90,6 +90,11 @@ Upgrading When upgrading from an earlier version, you should be aware of a number of pitfalls. +### Pitfall when upgrading to 6.3.6 + +Using a config object is no longer supported. You have to convert it to an array before passing +it to a constructor or the `setOptions()` method. + ### Pitfall when upgrading to 6.3.2 Support for PHP 7 was removed in Solarium 6.3.2. Upgrade to PHP 8 first to use the latest Solarium version. diff --git a/docs/queries/select-query/building-a-select-query/building-a-select-query.md b/docs/queries/select-query/building-a-select-query/building-a-select-query.md index 1251e3525..877500f0e 100644 --- a/docs/queries/select-query/building-a-select-query/building-a-select-query.md +++ b/docs/queries/select-query/building-a-select-query/building-a-select-query.md @@ -92,10 +92,8 @@ An example using the select query in config mode: (filterqueries and components require_once(__DIR__.'/init.php'); htmlHeader(); - // In this case an array is used for configuration to keep the example simple. -// For an easier to use config file you are probably better of with another format, like Zend_Config_Ini -// See the documentation for more info about this. +// You can also call setters on the query instance for each option. $select = array( 'query' => '*:*', 'start' => 2, diff --git a/docs/solarium-concepts.md b/docs/solarium-concepts.md index 5fd81421d..63a2bb3d7 100644 --- a/docs/solarium-concepts.md +++ b/docs/solarium-concepts.md @@ -11,9 +11,9 @@ Solarium allows for three main modes of usage of the library: programmatically, Currently only the programmatic and extending modes support all features, the configuration mode doesn't support some complex cases. This might be improved over time but there will always be limits to what is possible with configuration only, without creating very complex configurations. -The configuration mode supports an array as input or an object that implements the `toArray()` method (this is also compatible with the Zend Framework `Zend_Config` component). +The configuration mode supports an associative array as input. -The three modes apply to all Solarium classes that extend `Solarium\Core\Configurable`. This includes all Solarium classes that are intended for direct usage, e.g. the query classes, filterqueries, components etcetera. You can check to API for a class to see if it supports config mode. +The three modes apply to all Solarium classes that extend `Solarium\Core\Configurable`. This includes all Solarium classes that are intended for direct usage, e.g. the query classes, filterqueries, components etcetera. You can check the API for a class to see if it supports config mode. As an example the three modes are demonstrated, all creating an identical Solr client instance: @@ -80,10 +80,6 @@ htmlFooter(); require_once(__DIR__.'/init.php'); htmlHeader(); - -// In this case an array is used for configuration to keep the example simple. -// For an easier to use config file you are probably better of with another format, like Zend_Config_Ini -// See the documentation for more info about this. $select = array( 'query' => '*:*', 'start' => 2, @@ -150,7 +146,7 @@ use Solarium\QueryType\Select\Query\Query as Select; htmlHeader(); // In most cases using the API or config is advisable, however in some cases it can make sense to extend classes. -// This makes it possible to create 'query inheritance' like in this example +// This makes it possible to create 'query inheritance' like in this example. class ProductQuery extends Select { protected function init() diff --git a/examples/4.2-configuration-usage.php b/examples/4.2-configuration-usage.php index 9c2bb4685..eeb108bc5 100644 --- a/examples/4.2-configuration-usage.php +++ b/examples/4.2-configuration-usage.php @@ -3,10 +3,6 @@ require_once(__DIR__.'/init.php'); htmlHeader(); - -// In this case an array is used for configuration to keep the example simple. -// For an easier to use config file you are probably better of with another format, like Zend_Config_Ini -// See the documentation for more info about this. $select = array( 'query' => '*:*', 'start' => 2, diff --git a/examples/4.3-extending-usage.php b/examples/4.3-extending-usage.php index 277944e38..cd8e17672 100644 --- a/examples/4.3-extending-usage.php +++ b/examples/4.3-extending-usage.php @@ -7,7 +7,7 @@ htmlHeader(); // In most cases using the API or config is advisable, however in some cases it can make sense to extend classes. -// This makes it possible to create 'query inheritance' like in this example +// This makes it possible to create 'query inheritance' like in this example. class ProductQuery extends Select { protected function init() diff --git a/src/Component/FacetSet.php b/src/Component/FacetSet.php index 1828944db..023fc6a89 100644 --- a/src/Component/FacetSet.php +++ b/src/Component/FacetSet.php @@ -115,12 +115,12 @@ public function getExtractFromResponse(): ?bool * * @see FacetSetTrait::createFacet for more information on $options * - * @param array|object|string|null $options - * @param bool $add + * @param string|array|null $options + * @param bool $add * * @return \Solarium\Component\Facet\Field|FacetInterface */ - public function createFacetField($options = null, bool $add = true): FacetInterface + public function createFacetField(string|array|null $options = null, bool $add = true): FacetInterface { return $this->createFacet(FacetSetInterface::FACET_FIELD, $options, $add); } @@ -130,12 +130,12 @@ public function createFacetField($options = null, bool $add = true): FacetInterf * * @see FacetSetTrait::createFacet for more information on $options * - * @param array|object|string|null $options - * @param bool $add + * @param string|array|null $options + * @param bool $add * * @return \Solarium\Component\Facet\Query */ - public function createFacetQuery($options = null, bool $add = true): FacetInterface + public function createFacetQuery(string|array|null $options = null, bool $add = true): FacetInterface { return $this->createFacet(FacetSetInterface::FACET_QUERY, $options, $add); } @@ -145,12 +145,12 @@ public function createFacetQuery($options = null, bool $add = true): FacetInterf * * @see FacetSetTrait::createFacet for more information on $options * - * @param array|object|string|null $options - * @param bool $add + * @param string|array|null $options + * @param bool $add * * @return \Solarium\Component\Facet\MultiQuery */ - public function createFacetMultiQuery($options = null, bool $add = true): FacetInterface + public function createFacetMultiQuery(string|array|null $options = null, bool $add = true): FacetInterface { return $this->createFacet(FacetSetInterface::FACET_MULTIQUERY, $options, $add); } @@ -160,12 +160,12 @@ public function createFacetMultiQuery($options = null, bool $add = true): FacetI * * @see FacetSetTrait::createFacet for more information on $options * - * @param array|object|string|null $options - * @param bool $add + * @param string|array|null $options + * @param bool $add * * @return \Solarium\Component\Facet\Range */ - public function createFacetRange($options = null, bool $add = true): FacetInterface + public function createFacetRange(string|array|null $options = null, bool $add = true): FacetInterface { return $this->createFacet(FacetSetInterface::FACET_RANGE, $options, $add); } @@ -175,12 +175,12 @@ public function createFacetRange($options = null, bool $add = true): FacetInterf * * @see FacetSetTrait::createFacet for more information on $options * - * @param array|object|string|null $options - * @param bool $add + * @param string|array|null $options + * @param bool $add * * @return \Solarium\Component\Facet\Pivot */ - public function createFacetPivot($options = null, bool $add = true): FacetInterface + public function createFacetPivot(string|array|null $options = null, bool $add = true): FacetInterface { return $this->createFacet(FacetSetInterface::FACET_PIVOT, $options, $add); } @@ -190,12 +190,12 @@ public function createFacetPivot($options = null, bool $add = true): FacetInterf * * @see FacetSetTrait::createFacet for more information on $options * - * @param array|object|string|null $options - * @param bool $add + * @param string|array|null $options + * @param bool $add * * @return \Solarium\Component\Facet\Interval */ - public function createFacetInterval($options = null, bool $add = true): FacetInterface + public function createFacetInterval(string|array|null $options = null, bool $add = true): FacetInterface { return $this->createFacet(FacetSetInterface::FACET_INTERVAL, $options, $add); } @@ -205,12 +205,12 @@ public function createFacetInterval($options = null, bool $add = true): FacetInt * * @see FacetSetTrait::createFacet for more information on $options * - * @param array|object|string|null $options - * @param bool $add + * @param string|array|null $options + * @param bool $add * * @return \Solarium\Component\Facet\JsonAggregation */ - public function createJsonFacetAggregation($options = null, bool $add = true): FacetInterface + public function createJsonFacetAggregation(string|array|null $options = null, bool $add = true): FacetInterface { return $this->createFacet(FacetSetInterface::JSON_FACET_AGGREGATION, $options, $add); } @@ -220,12 +220,12 @@ public function createJsonFacetAggregation($options = null, bool $add = true): F * * @see FacetSetTrait::createFacet for more information on $options * - * @param array|object|string|null $options - * @param bool $add + * @param string|array|null $options + * @param bool $add * * @return \Solarium\Component\Facet\JsonTerms */ - public function createJsonFacetTerms($options = null, bool $add = true): FacetInterface + public function createJsonFacetTerms(string|array|null $options = null, bool $add = true): FacetInterface { return $this->createFacet(FacetSetInterface::JSON_FACET_TERMS, $options, $add); } @@ -235,12 +235,12 @@ public function createJsonFacetTerms($options = null, bool $add = true): FacetIn * * @see FacetSetTrait::createFacet for more information on $options * - * @param array|object|string|null $options - * @param bool $add + * @param string|array|null $options + * @param bool $add * * @return \Solarium\Component\Facet\JsonQuery */ - public function createJsonFacetQuery($options = null, bool $add = true): FacetInterface + public function createJsonFacetQuery(string|array|null $options = null, bool $add = true): FacetInterface { return $this->createFacet(FacetSetInterface::JSON_FACET_QUERY, $options, $add); } @@ -250,12 +250,12 @@ public function createJsonFacetQuery($options = null, bool $add = true): FacetIn * * @see FacetSetTrait::createFacet for more information on $options * - * @param array|object|string|null $options - * @param bool $add + * @param string|array|null $options + * @param bool $add * * @return \Solarium\Component\Facet\JsonRange */ - public function createJsonFacetRange($options = null, bool $add = true): FacetInterface + public function createJsonFacetRange(string|array|null $options = null, bool $add = true): FacetInterface { return $this->createFacet(FacetSetInterface::JSON_FACET_RANGE, $options, $add); } diff --git a/src/Component/FacetSetInterface.php b/src/Component/FacetSetInterface.php index 2c764f823..87cd2efaa 100644 --- a/src/Component/FacetSetInterface.php +++ b/src/Component/FacetSetInterface.php @@ -138,18 +138,18 @@ public function setFacets(array $facets): self; * * If you supply a string as the first arguments ($options) it will be used as the key for the facet * and it will be added to this query. - * If you supply an options array/object that contains a key the facet will also be added to the query. + * If you supply an options array that contains a key the facet will also be added to the query. * * When no key is supplied the facet cannot be added, in that case you will need to add it manually * after setting the key, by using the addFacet method. * * @param string $type - * @param array|object|null $options + * @param string|array|null $options * @param bool $add * * @throws OutOfBoundsException * * @return FacetInterface */ - public function createFacet(string $type, $options = null, bool $add = true): FacetInterface; + public function createFacet(string $type, string|array|null $options = null, bool $add = true): FacetInterface; } diff --git a/src/Component/FacetSetTrait.php b/src/Component/FacetSetTrait.php index ee1f348a7..23637223d 100644 --- a/src/Component/FacetSetTrait.php +++ b/src/Component/FacetSetTrait.php @@ -155,20 +155,20 @@ public function setFacets(array $facets): self * * If you supply a string as the first arguments ($options) it will be used as the key for the facet * and it will be added to this query. - * If you supply an options array/object that contains a key the facet will also be added to the query. + * If you supply an options array that contains a key the facet will also be added to the query. * * When no key is supplied the facet cannot be added, in that case you will need to add it manually * after setting the key, by using the addFacet method. * - * @param string $type - * @param array|object|string|null $options - * @param bool $add + * @param string $type + * @param string|array|null $options + * @param bool $add * * @throws OutOfBoundsException * * @return \Solarium\Component\Facet\FacetInterface */ - public function createFacet(string $type, $options = null, bool $add = true): FacetInterface + public function createFacet(string $type, string|array|null $options = null, bool $add = true): FacetInterface { $type = strtolower($type); diff --git a/src/Component/Stats/Stats.php b/src/Component/Stats/Stats.php index 33c6d7f0a..f4bec2ff0 100644 --- a/src/Component/Stats/Stats.php +++ b/src/Component/Stats/Stats.php @@ -68,16 +68,16 @@ public function getResponseParser(): ?ComponentParserInterface * * If you supply a string as the first arguments ($options) it will be used as the key for the field * and it will be added to this query component. - * If you supply an options array/object that contains a key the field will also be added to the component. + * If you supply an options array that contains a key the field will also be added to the component. * * When no key is supplied the field cannot be added, in that case you will need to add it manually * after setting the key, by using the addField method. * - * @param mixed $options + * @param string|array $options * * @return Field */ - public function createField($options = null): Field + public function createField(string|array $options = null): Field { if (\is_string($options)) { $fq = new Field(); diff --git a/src/Core/Client/Client.php b/src/Core/Client/Client.php index b982f186f..19cd7a712 100755 --- a/src/Core/Client/Client.php +++ b/src/Core/Client/Client.php @@ -298,7 +298,7 @@ class Client extends Configurable implements ClientInterface * * @param AdapterInterface $adapter * @param EventDispatcherInterface $eventDispatcher - * @param array|null $options + * @param array $options */ public function __construct(AdapterInterface $adapter, EventDispatcherInterface $eventDispatcher, array $options = null) { @@ -313,17 +313,17 @@ public function __construct(AdapterInterface $adapter, EventDispatcherInterface * * If you supply a string as the first arguments ($options) it will be used as the key for the endpoint * and it will be registered. - * If you supply an options array/object that contains a key the endpoint will also be registered. + * If you supply an options array that contains a key the endpoint will also be registered. * * When no key is supplied the endpoint cannot be registered, in that case you will need to do this manually * after setting the key, by using the addEndpoint method. * - * @param mixed $options - * @param bool $setAsDefault + * @param string|array|null $options + * @param bool $setAsDefault * * @return Endpoint */ - public function createEndpoint($options = null, bool $setAsDefault = false): Endpoint + public function createEndpoint(string|array|null $options = null, bool $setAsDefault = false): Endpoint { if (\is_string($options)) { $endpoint = new Endpoint(); @@ -1147,7 +1147,7 @@ public function createQuery(string $type, array $options = null): QueryInterface /** * Create a select query instance. * - * @param mixed $options + * @param array $options * * @return \Solarium\Core\Query\AbstractQuery|\Solarium\QueryType\Select\Query\Query */ @@ -1159,7 +1159,7 @@ public function createSelect(array $options = null): SelectQuery /** * Create a MoreLikeThis query instance. * - * @param mixed $options + * @param array $options * * @return \Solarium\Core\Query\AbstractQuery|\Solarium\QueryType\MoreLikeThis\Query */ @@ -1171,7 +1171,7 @@ public function createMoreLikeThis(array $options = null): MoreLikeThisQuery /** * Create an update query instance. * - * @param mixed $options + * @param array $options * * @return \Solarium\Core\Query\AbstractQuery|\Solarium\QueryType\Update\Query\Query */ @@ -1183,7 +1183,7 @@ public function createUpdate(array $options = null): UpdateQuery /** * Create a ping query instance. * - * @param mixed $options + * @param array $options * * @return \Solarium\Core\Query\AbstractQuery|\Solarium\QueryType\Ping\Query */ @@ -1195,7 +1195,7 @@ public function createPing(array $options = null): PingQuery /** * Create an analysis field query instance. * - * @param mixed $options + * @param array $options * * @return \Solarium\Core\Query\AbstractQuery|\Solarium\QueryType\Analysis\Query\Field */ @@ -1207,7 +1207,7 @@ public function createAnalysisField(array $options = null): AnalysisQueryField /** * Create an analysis document query instance. * - * @param mixed $options + * @param array $options * * @return \Solarium\Core\Query\AbstractQuery|\Solarium\QueryType\Analysis\Query\Document */ @@ -1219,7 +1219,7 @@ public function createAnalysisDocument(array $options = null): AnalysisQueryDocu /** * Create a terms query instance. * - * @param mixed $options + * @param array $options * * @return \Solarium\Core\Query\AbstractQuery|\Solarium\QueryType\Terms\Query */ @@ -1231,7 +1231,7 @@ public function createTerms(array $options = null): TermsQuery /** * Create a specllcheck query instance. * - * @param mixed $options + * @param array $options * * @return \Solarium\Core\Query\AbstractQuery|\Solarium\QueryType\Spellcheck\Query */ @@ -1243,7 +1243,7 @@ public function createSpellcheck(array $options = null): SpellcheckQuery /** * Create a suggester query instance. * - * @param mixed $options + * @param array $options * * @return \Solarium\Core\Query\AbstractQuery|\Solarium\QueryType\Suggester\Query */ @@ -1255,7 +1255,7 @@ public function createSuggester(array $options = null): SuggesterQuery /** * Create an extract query instance. * - * @param mixed $options + * @param array $options * * @return \Solarium\Core\Query\AbstractQuery|\Solarium\QueryType\Extract\Query */ @@ -1267,7 +1267,7 @@ public function createExtract(array $options = null): ExtractQuery /** * Create a stream query instance. * - * @param mixed $options + * @param array $options * * @return \Solarium\Core\Query\AbstractQuery|\Solarium\QueryType\Stream\Query */ @@ -1283,7 +1283,7 @@ public function createStream(array $options = null): StreamQuery /** * Create a graph query instance. * - * @param mixed $options + * @param array $options * * @return \Solarium\Core\Query\AbstractQuery|\Solarium\QueryType\Graph\Query */ @@ -1299,7 +1299,7 @@ public function createGraph(array $options = null): GraphQuery /** * Create a RealtimeGet query instance. * - * @param mixed $options + * @param array $options * * @return \Solarium\Core\Query\AbstractQuery|\Solarium\QueryType\RealtimeGet\Query */ @@ -1311,7 +1311,7 @@ public function createRealtimeGet(array $options = null): RealtimeGetQuery /** * Create a Luke query instance. * - * @param mixed $options + * @param array $options * * @return \Solarium\Core\Query\AbstractQuery|\Solarium\QueryType\Luke\Query */ @@ -1323,7 +1323,7 @@ public function createLuke(array $options = null): LukeQuery /** * Create a CoreAdmin query instance. * - * @param mixed $options + * @param array $options * * @return \Solarium\Core\Query\AbstractQuery|\Solarium\QueryType\Server\CoreAdmin\Query\Query */ @@ -1335,7 +1335,7 @@ public function createCoreAdmin(array $options = null): CoreAdminQuery /** * Create a Collections API query instance. * - * @param mixed $options + * @param array $options * * @return \Solarium\Core\Query\AbstractQuery|\Solarium\QueryType\Server\Collections\Query\Query */ @@ -1347,7 +1347,7 @@ public function createCollections(array $options = null): CollectionsQuery /** * Create a Configsets API query instance. * - * @param mixed $options + * @param array $options * * @return \Solarium\Core\Query\AbstractQuery|\Solarium\QueryType\Server\Configsets\Query\Query */ @@ -1359,7 +1359,7 @@ public function createConfigsets(array $options = null): ConfigsetsQuery /** * Create an API query instance. * - * @param mixed $options + * @param array $options * * @return \Solarium\Core\Query\AbstractQuery|\Solarium\QueryType\Server\Api\Query */ @@ -1371,7 +1371,7 @@ public function createApi(array $options = null): ApiQuery /** * Create a managed resources query instance. * - * @param mixed $options + * @param array $options * * @return \Solarium\Core\Query\AbstractQuery|\Solarium\QueryType\ManagedResources\Query\Resources */ @@ -1383,7 +1383,7 @@ public function createManagedResources(array $options = null): ManagedResourcesQ /** * Create a managed stopwords query instance. * - * @param mixed $options + * @param array $options * * @return \Solarium\Core\Query\AbstractQuery|\Solarium\QueryType\ManagedResources\Query\Stopwords */ @@ -1395,7 +1395,7 @@ public function createManagedStopwords(array $options = null): ManagedStopwordsQ /** * Create a managed synonyms query instance. * - * @param mixed $options + * @param array $options * * @return \Solarium\Core\Query\AbstractQuery|\Solarium\QueryType\ManagedResources\Query\Synonyms */ diff --git a/src/Core/Client/ClientInterface.php b/src/Core/Client/ClientInterface.php index d5b4d28c3..85971ab99 100644 --- a/src/Core/Client/ClientInterface.php +++ b/src/Core/Client/ClientInterface.php @@ -73,17 +73,17 @@ interface ClientInterface * * If you supply a string as the first arguments ($options) it will be used as the key for the endpoint * and it will be registered. - * If you supply an options array/object that contains a key the endpoint will also be registered. + * If you supply an options array that contains a key the endpoint will also be registered. * * When no key is supplied the endpoint cannot be registered, in that case you will need to do this manually * after setting the key, by using the addEndpoint method. * - * @param mixed $options - * @param bool $setAsDefault + * @param string|array|null $options + * @param bool $setAsDefault * * @return Endpoint */ - public function createEndpoint($options = null, bool $setAsDefault = false): Endpoint; + public function createEndpoint(string|array|null $options = null, bool $setAsDefault = false): Endpoint; /** * Add an endpoint. diff --git a/src/Core/Configurable.php b/src/Core/Configurable.php index a05771041..f5ba13103 100644 --- a/src/Core/Configurable.php +++ b/src/Core/Configurable.php @@ -9,8 +9,6 @@ namespace Solarium\Core; -use Solarium\Exception\InvalidArgumentException; - /** * Base class for configurable classes. * @@ -35,11 +33,9 @@ class Configurable implements ConfigurableInterface * * After handling the options the {@link _init()} method is called. * - * @param array|\Zend_Config $options - * - * @throws InvalidArgumentException + * @param array $options */ - public function __construct($options = null) + public function __construct(array $options = null) { if (null !== $options) { $this->setOptions($options); @@ -51,44 +47,25 @@ public function __construct($options = null) /** * Set options. * - * If $options is an object, it will be converted into an array by calling - * its toArray method. This is compatible with the Zend_Config classes in - * Zend Framework, but can also easily be implemented in any other object. - * If $options does not have the toArray method, the internal method will - * be used instead. - * - * @param array|\Zend_Config $options - * @param bool $overwrite True for overwriting existing options, false - * for merging (new values overwrite old ones if needed) - * - * @throws InvalidArgumentException + * @param array $options + * @param bool $overwrite True for overwriting existing options, false for + * merging (new values overwrite old ones if needed) * * @return self Provides fluent interface */ - public function setOptions($options, bool $overwrite = false): self + public function setOptions(array $options, bool $overwrite = false): self { - if (null !== $options) { - // first convert to array if needed - if (!\is_array($options)) { - if (\is_object($options)) { - $options = (!method_exists($options, 'toArray') ? $this->toArray($options) : $options->toArray()); - } else { - throw new InvalidArgumentException('Options value given to the setOptions() method must be an array or a Zend_Config object'); - } - } - - if (true === $overwrite) { - $this->options = $options; - } else { - $this->options = array_merge($this->options, $options); - } + if (true === $overwrite) { + $this->options = $options; + } else { + $this->options = array_merge($this->options, $options); + } - if (true === \is_callable([$this, 'initLocalParameters'])) { - $this->initLocalParameters(); - } - // re-init for new options - $this->init(); + if (true === \is_callable([$this, 'initLocalParameters'])) { + $this->initLocalParameters(); } + // re-init for new options + $this->init(); return $this; } @@ -100,9 +77,9 @@ public function setOptions($options, bool $overwrite = false): self * * @param string $name * - * @return mixed|null + * @return mixed */ - public function getOption(string $name) + public function getOption(string $name): mixed { return $this->options[$name] ?? null; } @@ -143,31 +120,10 @@ protected function init() * * @return self Provides fluent interface */ - protected function setOption(string $name, $value): self + protected function setOption(string $name, mixed $value): self { $this->options[$name] = $value; return $this; } - - /** - * Turns an object array into an associative multidimensional array. - * - * @param $object object|object[] - * - * @return array - */ - protected function toArray($object): array - { - if (\is_object($object)) { - return (array) $object; - } - - /* - * Return array converted to object - * Using __METHOD__ (Magic constant) - * for recursive call - */ - return array_map(__METHOD__, $object); - } } diff --git a/src/Core/ConfigurableInterface.php b/src/Core/ConfigurableInterface.php index e3e4a2b13..4d0994261 100644 --- a/src/Core/ConfigurableInterface.php +++ b/src/Core/ConfigurableInterface.php @@ -9,8 +9,6 @@ namespace Solarium\Core; -use Solarium\Exception\InvalidArgumentException; - /** * Interface for configurable classes. * @@ -23,19 +21,13 @@ interface ConfigurableInterface /** * Set options. * - * If $options is an object it will be converted into an array by calling - * its toArray method. This is compatible with the Zend_Config classes in - * Zend Framework, but can also easily be implemented in any other object. - * - * @param array|\Zend_Config $options - * @param bool $overwrite True for overwriting existing options, false - * for merging (new values overwrite old ones if needed) - * - * @throws InvalidArgumentException + * @param array $options + * @param bool $overwrite True for overwriting existing options, false for + * merging (new values overwrite old ones if needed) * * @return self Provides fluent interface */ - public function setOptions($options, bool $overwrite = false): self; + public function setOptions(array $options, bool $overwrite = false): self; /** * Get an option value by name. @@ -46,7 +38,7 @@ public function setOptions($options, bool $overwrite = false): self; * * @return mixed */ - public function getOption(string $name); + public function getOption(string $name): mixed; /** * Get all options. diff --git a/src/Plugin/CustomizeRequest/CustomizeRequest.php b/src/Plugin/CustomizeRequest/CustomizeRequest.php index 5b8f6e9fe..aafb88ccf 100644 --- a/src/Plugin/CustomizeRequest/CustomizeRequest.php +++ b/src/Plugin/CustomizeRequest/CustomizeRequest.php @@ -35,16 +35,16 @@ class CustomizeRequest extends AbstractPlugin * * If you supply a string as the first arguments ($options) it will be used as the key for the Customization * and it will be added to this plugin. - * If you supply an options array/object that contains a key the Customization will also be added to the plugin. + * If you supply an options array that contains a key the Customization will also be added to the plugin. * * When no key is supplied the Customization cannot be added, in that case you will need to add it manually * after setting the key, by using the addCustomization method. * - * @param mixed $options + * @param string|array $options * * @return Customization */ - public function createCustomization($options = null): Customization + public function createCustomization(string|array $options = null): Customization { if (\is_string($options)) { $fq = new Customization(); diff --git a/src/QueryType/ManagedResources/Query/AbstractQuery.php b/src/QueryType/ManagedResources/Query/AbstractQuery.php index cc7748227..fac38c34b 100644 --- a/src/QueryType/ManagedResources/Query/AbstractQuery.php +++ b/src/QueryType/ManagedResources/Query/AbstractQuery.php @@ -175,13 +175,13 @@ public function removeTerm(): self * Create a command instance. * * @param string $type - * @param mixed $options + * @param array $options * * @throws \Solarium\Exception\InvalidArgumentException * * @return \Solarium\QueryType\ManagedResources\Query\AbstractCommand */ - public function createCommand($type, $options = null): AbstractCommand + public function createCommand(string $type, array $options = null): AbstractCommand { $type = strtolower($type); diff --git a/src/QueryType/Select/Query/Query.php b/src/QueryType/Select/Query/Query.php index 261ab3d6c..ac6ad8b35 100644 --- a/src/QueryType/Select/Query/Query.php +++ b/src/QueryType/Select/Query/Query.php @@ -515,16 +515,16 @@ public function setSorts(array $sorts): self * * If you supply a string as the first arguments ($options) it will be used as the key for the filterquery * and it will be added to this query. - * If you supply an options array/object that contains a key the filterquery will also be added to the query. + * If you supply an options array that contains a key the filterquery will also be added to the query. * * When no key is supplied the filterquery cannot be added, in that case you will need to add it manually * after setting the key, by using the addFilterQuery method. * - * @param mixed $options + * @param string|array $options * * @return FilterQuery */ - public function createFilterQuery($options = null): FilterQuery + public function createFilterQuery(string|array $options = null): FilterQuery { if (\is_string($options)) { $fq = new FilterQuery(); diff --git a/src/QueryType/Server/AbstractServerQuery.php b/src/QueryType/Server/AbstractServerQuery.php index 41e5ae430..37b47c86f 100644 --- a/src/QueryType/Server/AbstractServerQuery.php +++ b/src/QueryType/Server/AbstractServerQuery.php @@ -36,13 +36,13 @@ abstract class AbstractServerQuery extends AbstractQuery implements ActionInterf * Create a command instance. * * @param string $type - * @param mixed $options + * @param array $options * * @throws InvalidArgumentException * * @return ActionInterface */ - public function createAction($type, $options = null): ActionInterface + public function createAction(string $type, array $options = null): ActionInterface { if (!isset($this->actionTypes[$type])) { throw new InvalidArgumentException(sprintf('Action unknown: %s', $type)); diff --git a/tests/Core/ConfigurableTest.php b/tests/Core/ConfigurableTest.php index ba061453a..140ec090e 100644 --- a/tests/Core/ConfigurableTest.php +++ b/tests/Core/ConfigurableTest.php @@ -4,7 +4,6 @@ use PHPUnit\Framework\TestCase; use Solarium\Core\Configurable; -use Solarium\Exception\InvalidArgumentException; use Solarium\Exception\RuntimeException; class ConfigurableTest extends TestCase @@ -20,49 +19,7 @@ public function testConstructorNoConfig() $this->assertSame($configTest->getOptions(), $defaultOptions); } - public function testConstructorWithObject() - { - $configTest = new ConfigTest(new MyConfigObject()); - - // the default options should be merged with the constructor values, - // overwriting any default values. - $expectedOptions = [ - 'option1' => 1, - 'option2' => 'newvalue2', - 'option3' => 3, - ]; - - $this->assertSame($expectedOptions, $configTest->getOptions()); - } - - public function testConstructorWithObjectWithoutToArray() - { - $config = new \stdClass(); - $config->option2 = 'newvalue2'; - $config->option3 = 3; - $config->option4 = [ - 'option5' => 5, - 'option6' => 'newvalue6', - ]; - - $configTest = new ConfigTest($config); - - // the default options should be merged with the constructor values, - // overwriting any default values. - $expectedOptions = [ - 'option1' => 1, - 'option2' => 'newvalue2', - 'option3' => 3, - 'option4' => [ - 'option5' => 5, - 'option6' => 'newvalue6', - ], - ]; - - $this->assertSame($expectedOptions, $configTest->getOptions()); - } - - public function testConstructorWithArrayConfig() + public function testConstructorWithConfig() { $configTest = new ConfigTest( ['option2' => 'newvalue2', 'option3' => 3] @@ -85,7 +42,7 @@ public function testGetOption() $this->assertSame(1, $configTest->getOption('option1')); } - public function testGetOptionWIthInvalidName() + public function testGetOptionWithInvalidName() { $configTest = new ConfigTest(); $this->assertNull($configTest->getOption('invalidoptionname')); @@ -94,6 +51,7 @@ public function testGetOptionWIthInvalidName() public function testInitialisation() { $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('test init'); new ConfigTestInit(); } @@ -108,6 +66,12 @@ public function testSetOptions() ); } + public function testSetOptionsReturnsSelf() + { + $configTest = new ConfigTest(); + $this->assertSame($configTest, $configTest->setOptions([])); + } + public function testSetOptionsWithOverride() { $configTest = new ConfigTest(); @@ -119,13 +83,27 @@ public function testSetOptionsWithOverride() ); } - public function testSetOptionsWithInvalidArgument() + public function testSetOptionsCallsInitLocalParameters() + { + $configTest = new ConfigTestInitLocalParameters(); + + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('test initLocalParameters'); + $configTest->setOptions([]); + } + + public function testSetOption() { $configTest = new ConfigTest(); + $configTest->setOption('option2', 'newvalue'); - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Options value given to the setOptions() method must be an array or a Zend_Config object'); - $configTest->setOptions('option2=2&option3=3'); + $this->assertSame('newvalue', $configTest->getOption('option2')); + } + + public function testSetOptionReturnsSelf() + { + $configTest = new ConfigTest(); + $this->assertSame($configTest, $configTest->setOption('option', 'value')); } } @@ -135,6 +113,14 @@ class ConfigTest extends Configurable 'option1' => 1, 'option2' => 'value 2', ]; + + /** + * Override visibility. + */ + public function setOption(string $name, mixed $value): self + { + return parent::setOption($name, $value); + } } class ConfigTestInit extends ConfigTest @@ -145,10 +131,10 @@ protected function init() } } -class MyConfigObject +class ConfigTestInitLocalParameters extends ConfigTest { - public function toArray() + protected function initLocalParameters() { - return ['option2' => 'newvalue2', 'option3' => 3]; + throw new RuntimeException('test initLocalParameters'); } }