diff --git a/.github/actions/cache/action.yml b/.github/actions/cache/action.yml index 39a012ef401..348e879b8bb 100644 --- a/.github/actions/cache/action.yml +++ b/.github/actions/cache/action.yml @@ -367,12 +367,15 @@ runs: if: ${{ inputs.job == 'cts' || inputs.job == 'codegen' }} uses: actions/cache@v2 with: - path: clients/algoliasearch-client-php/lib/Api/SearchApi.php + path: | + clients/algoliasearch-client-php/lib/Api/SearchApi.php + clients/algoliasearch-client-php/lib/Model/Search/** key: | ${{ env.CACHE_VERSION }}-${{ hashFiles( 'clients/algoliasearch-client-php/lib/Api/SearchApi.php', 'clients/algoliasearch-client-php/lib/Configuration/SearchConfig.php', + 'clients/algoliasearch-client-php/lib/Model/Search/**', 'specs/bundled/search.yml', 'templates/php/**', 'generators/src/**' @@ -382,12 +385,15 @@ runs: if: ${{ inputs.job == 'cts' || inputs.job == 'codegen' }} uses: actions/cache@v2 with: - path: clients/algoliasearch-client-php/lib/Api/RecommendApi.php + path: | + clients/algoliasearch-client-php/lib/Api/RecommendApi.php + clients/algoliasearch-client-php/lib/Model/Recommend/** key: | ${{ env.CACHE_VERSION }}-${{ hashFiles( 'clients/algoliasearch-client-php/lib/Api/RecommendApi.php', 'clients/algoliasearch-client-php/lib/Configuration/RecommendConfig.php', + 'clients/algoliasearch-client-php/lib/Model/Recommend/**', 'specs/bundled/recommend.yml', 'templates/php/**', 'generators/src/**' @@ -397,12 +403,15 @@ runs: if: ${{ inputs.job == 'cts' || inputs.job == 'codegen' }} uses: actions/cache@v2 with: - path: clients/algoliasearch-client-php/lib/Api/PersonalizationApi.php + path: | + clients/algoliasearch-client-php/lib/Api/PersonalizationApi.php + clients/algoliasearch-client-php/lib/Model/Personalization/** key: | ${{ env.CACHE_VERSION }}-${{ hashFiles( 'clients/algoliasearch-client-php/lib/Api/PersonalizationApi.php', 'clients/algoliasearch-client-php/lib/Configuration/PersonalizationConfig.php', + 'clients/algoliasearch-client-php/lib/Model/Personalization/**', 'specs/bundled/personalization.yml', 'templates/php/**', 'generators/src/**' @@ -412,12 +421,15 @@ runs: if: ${{ inputs.job == 'cts' || inputs.job == 'codegen' }} uses: actions/cache@v2 with: - path: clients/algoliasearch-client-php/lib/Api/AnalyticsApi.php + path: | + clients/algoliasearch-client-php/lib/Api/AnalyticsApi.php + clients/algoliasearch-client-php/lib/Model/Analytics/** key: | ${{ env.CACHE_VERSION }}-${{ hashFiles( 'clients/algoliasearch-client-php/lib/Api/AnalyticsApi.php', 'clients/algoliasearch-client-php/lib/Configuration/AnalyticsConfig.php', + 'clients/algoliasearch-client-php/lib/Model/Analytics/**', 'specs/bundled/analytics.yml', 'templates/php/**', 'generators/src/**' @@ -427,12 +439,15 @@ runs: if: ${{ inputs.job == 'cts' || inputs.job == 'codegen' }} uses: actions/cache@v2 with: - path: clients/algoliasearch-client-php/lib/Api/InsightsApi.php + path: | + clients/algoliasearch-client-php/lib/Api/InsightsApi.php + clients/algoliasearch-client-php/lib/Model/Insights/** key: | ${{ env.CACHE_VERSION }}-${{ hashFiles( 'clients/algoliasearch-client-php/lib/Api/InsightsApi.php', 'clients/algoliasearch-client-php/lib/Configuration/InsightsConfig.php', + 'clients/algoliasearch-client-php/lib/Model/Insights/**', 'specs/bundled/insights.yml', 'templates/php/**', 'generators/src/**' @@ -442,12 +457,15 @@ runs: if: ${{ inputs.job == 'cts' || inputs.job == 'codegen' }} uses: actions/cache@v2 with: - path: clients/algoliasearch-client-php/lib/Api/AbtestingApi.php + path: | + clients/algoliasearch-client-php/lib/Api/AbtestingApi.php + clients/algoliasearch-client-php/lib/Model/Abtesting/** key: | ${{ env.CACHE_VERSION }}-${{ hashFiles( 'clients/algoliasearch-client-php/lib/Api/AbtestingApi.php', 'clients/algoliasearch-client-php/lib/Configuration/AbtestingConfig.php', + 'clients/algoliasearch-client-php/lib/Model/Abtesting/**', 'specs/bundled/abtesting.yml', 'templates/php/**', 'generators/src/**' @@ -457,12 +475,15 @@ runs: if: ${{ inputs.job == 'cts' || inputs.job == 'codegen' }} uses: actions/cache@v2 with: - path: clients/algoliasearch-client-php/lib/Api/QuerySuggestionsApi.php + path: | + clients/algoliasearch-client-php/lib/Api/QuerySuggestionsApi.php + clients/algoliasearch-client-php/lib/Model/QuerySuggestions/** key: | ${{ env.CACHE_VERSION }}-${{ hashFiles( 'clients/algoliasearch-client-php/lib/Api/QuerySuggestionsApi.php', 'clients/algoliasearch-client-php/lib/Configuration/QuerySuggestionsConfig.php', + 'clients/algoliasearch-client-php/lib/Model/QuerySuggestions/**', 'specs/bundled/query-suggestions.yml', 'templates/php/**', 'generators/src/**' diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index abd8c93c7cd..3fdcf20b6c1 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -302,7 +302,9 @@ jobs: id: cache uses: actions/cache@v2 with: - path: ${{ format('{0}/lib/Api/{1}.php', matrix.client.folder, matrix.client.api) }} + path: | + ${{ format('{0}/lib/Api/{1}.php', matrix.client.folder, matrix.client.api) }} + ${{ format('{0}/lib/Model/{1}/**', matrix.client.folder, matrix.client.capitalizedName) }} key: | ${{ env.CACHE_VERSION }}-${{ hashFiles( diff --git a/clients/algoliasearch-client-php/.openapi-generator-ignore b/clients/algoliasearch-client-php/.openapi-generator-ignore index afab6b370bc..c4bcf230781 100644 --- a/clients/algoliasearch-client-php/.openapi-generator-ignore +++ b/clients/algoliasearch-client-php/.openapi-generator-ignore @@ -6,8 +6,6 @@ docs/** test/** -lib/Model/** -lib/Model/ .travis.yml README.md diff --git a/clients/algoliasearch-client-php/lib/Configuration/AbTestingConfig.php b/clients/algoliasearch-client-php/lib/Configuration/AbtestingConfig.php similarity index 55% rename from clients/algoliasearch-client-php/lib/Configuration/AbTestingConfig.php rename to clients/algoliasearch-client-php/lib/Configuration/AbtestingConfig.php index 2b321336788..4005139a224 100644 --- a/clients/algoliasearch-client-php/lib/Configuration/AbTestingConfig.php +++ b/clients/algoliasearch-client-php/lib/Configuration/AbtestingConfig.php @@ -2,6 +2,6 @@ namespace Algolia\AlgoliaSearch\Configuration; -class AbTestingConfig extends ConfigWithRegion +class AbtestingConfig extends ConfigWithRegion { } diff --git a/clients/algoliasearch-client-php/lib/Configuration/RecommendConfig.php b/clients/algoliasearch-client-php/lib/Configuration/RecommendConfig.php index 14a0987793f..c382d51843f 100644 --- a/clients/algoliasearch-client-php/lib/Configuration/RecommendConfig.php +++ b/clients/algoliasearch-client-php/lib/Configuration/RecommendConfig.php @@ -2,6 +2,6 @@ namespace Algolia\AlgoliaSearch\Configuration; -class RecommendConfig extends ConfigWithRegion +class RecommendConfig extends Configuration { } diff --git a/clients/algoliasearch-client-php/lib/Model/AbstractModel.php b/clients/algoliasearch-client-php/lib/Model/AbstractModel.php new file mode 100644 index 00000000000..5b57ce878f3 --- /dev/null +++ b/clients/algoliasearch-client-php/lib/Model/AbstractModel.php @@ -0,0 +1,50 @@ + = { @@ -70,6 +72,7 @@ async function getClientMatrix({ 'Config', 'Api' ); + matchedGenerator.capitalizedName = createClientName(client, 'php'); } matrix.client.push(matchedGenerator); diff --git a/scripts/generate.ts b/scripts/generate.ts index 753f3553a1a..226866ea00e 100644 --- a/scripts/generate.ts +++ b/scripts/generate.ts @@ -14,6 +14,7 @@ import { getLanguageFolder, getLanguageModelFolder, } from './config'; +import { createClientName } from './cts/utils'; import { formatter } from './formatter'; import { createSpinner } from './oraLog'; import { setHostsOptions } from './pre-gen/setHostsOptions'; @@ -33,6 +34,9 @@ async function removeExistingModel( case 'java': clientModel = client; break; + case 'php': + clientModel = createClientName(client, 'php'); + break; default: break; } diff --git a/clients/algoliasearch-client-php/lib/ModelInterface.php b/templates/php/ModelInterface.mustache similarity index 66% rename from clients/algoliasearch-client-php/lib/ModelInterface.php rename to templates/php/ModelInterface.mustache index a9505a67e5d..65373b70be8 100644 --- a/clients/algoliasearch-client-php/lib/ModelInterface.php +++ b/templates/php/ModelInterface.mustache @@ -1,41 +1,27 @@ arrayParam}} + {{/vars}} + * @see {{{dataType}}} +{{/isModel}} {{/allParams}} * - * @return array + * @return {{#returnType}}{{#responses}}{{#dataType}}{{#-first}}array|{{{dataType}}}{{/-first}}{{/dataType}}{{/responses}}{{/returnType}}{{^returnType}}void{{/returnType}} {{#isDeprecated}} * @deprecated {{/isDeprecated}} @@ -241,7 +247,7 @@ use {{invokerPackage}}\RetryStrategy\ClusterHosts; if (${{paramName}} !== null) { $resourcePath = str_replace( {{=<% %>=}}'{<%baseName%>}'<%={{ }}=%>, - {{#x-is-custom-request}}{{paramName}}{{/x-is-custom-request}}{{^x-is-custom-request}}ObjectSerializer::toPathValue(${{paramName}}){{/x-is-custom-request}}, + {{#x-is-custom-request}}${{paramName}}{{/x-is-custom-request}}{{^x-is-custom-request}}ObjectSerializer::toPathValue(${{paramName}}){{/x-is-custom-request}}, $resourcePath ); } diff --git a/templates/php/arrayParam.mustache b/templates/php/arrayParam.mustache new file mode 100644 index 00000000000..cfd5f935472 --- /dev/null +++ b/templates/php/arrayParam.mustache @@ -0,0 +1 @@ +['{{{baseName}}}'] => ({{#isString}}string{{/isString}}{{#isInteger}}int{{/isInteger}}{{#isBoolean}}bool{{/isBoolean}}{{^isString}}{{^isInteger}}{{^isBoolean}}array{{/isBoolean}}{{/isInteger}}{{/isString}}) {{{description}}}{{#required}} (required){{/required}} \ No newline at end of file diff --git a/templates/php/model.mustache b/templates/php/model.mustache index a6afd1683d6..8a45cc57ab5 100644 --- a/templates/php/model.mustache +++ b/templates/php/model.mustache @@ -6,7 +6,6 @@ namespace {{modelPackage}}; {{^isEnum}} {{^parentSchema}} -use \ArrayAccess; {{/parentSchema}} {{/isEnum}} use \{{invokerPackage}}\ObjectSerializer; @@ -18,12 +17,7 @@ use \{{invokerPackage}}\ObjectSerializer; {{#description}} * @description {{.}} {{/description}} - * @package {{invokerPackage}} -{{^isEnum}} - * @implements \ArrayAccess - * @template TKey int|null - * @template TValue mixed|null -{{/isEnum}} + * @package {{invokerPackage}} */ {{#isEnum}}{{>model_enum}}{{/isEnum}}{{^isEnum}}{{>model_generic}}{{/isEnum}} {{/model}}{{/models}} diff --git a/templates/php/model_generic.mustache b/templates/php/model_generic.mustache index a56a82181e6..4e8cdeb68eb 100644 --- a/templates/php/model_generic.mustache +++ b/templates/php/model_generic.mustache @@ -1,20 +1,11 @@ -class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^parentSchema}}implements ModelInterface, ArrayAccess, \JsonSerializable{{/parentSchema}} +class {{classname}} extends \Algolia\AlgoliaSearch\Model\AbstractModel {{^parentSchema}}implements ModelInterface, \ArrayAccess, \JsonSerializable{{/parentSchema}} { - public const DISCRIMINATOR = {{#discriminator}}'{{discriminatorName}}'{{/discriminator}}{{^discriminator}}null{{/discriminator}}; - - /** - * The original name of the model. - * - * @var string - */ - protected static $openAPIModelName = '{{name}}'; - /** * Array of property to type mappings. Used for (de)serialization * * @var string[] */ - protected static $openAPITypes = [ + protected static $modelTypes = [ {{#vars}}'{{name}}' => '{{{dataType}}}'{{^-last}}, {{/-last}}{{/vars}} ]; @@ -23,10 +14,8 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par * Array of property to format mappings. Used for (de)serialization * * @var string[] - * @phpstan-var array - * @psalm-var array */ - protected static $openAPIFormats = [ + protected static $modelFormats = [ {{#vars}}'{{name}}' => {{#dataFormat}}'{{{.}}}'{{/dataFormat}}{{^dataFormat}}null{{/dataFormat}}{{^-last}}, {{/-last}}{{/vars}} ]; @@ -36,9 +25,9 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par * * @return array */ - public static function openAPITypes() + public static function modelTypes() { - return self::$openAPITypes{{#parentSchema}} + parent::openAPITypes(){{/parentSchema}}; + return self::$modelTypes{{#parentSchema}} + parent::modelTypes(){{/parentSchema}}; } /** @@ -46,22 +35,11 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par * * @return array */ - public static function openAPIFormats() + public static function modelFormats() { - return self::$openAPIFormats{{#parentSchema}} + parent::openAPIFormats(){{/parentSchema}}; + return self::$modelFormats{{#parentSchema}} + parent::modelFormats(){{/parentSchema}}; } - /** - * Array of attributes where the key is the local name, - * and the value is the original name - * - * @var string[] - */ - protected static $attributeMap = [ - {{#vars}}'{{name}}' => '{{baseName}}'{{^-last}}, - {{/-last}}{{/vars}} - ]; - /** * Array of attributes to setter functions (for deserialization of responses) * @@ -82,17 +60,6 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par {{/-last}}{{/vars}} ]; - /** - * Array of attributes where the key is the local name, - * and the value is the original name - * - * @return array - */ - public static function attributeMap() - { - return {{#parentSchema}}parent::attributeMap() + {{/parentSchema}}self::$attributeMap; - } - /** * Array of attributes to setter functions (for deserialization of responses) * @@ -113,16 +80,6 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par return {{#parentSchema}}parent::getters() + {{/parentSchema}}self::$getters; } - /** - * The original name of the model. - * - * @return string - */ - public function getModelName() - { - return self::$openAPIModelName; - } - {{#vars}} {{#isEnum}} {{#allowableValues}} @@ -163,7 +120,6 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par * Constructor * * @param mixed[] $data Associated array of property values - * initializing the model */ public function __construct(array $data = null) { @@ -172,13 +128,10 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par {{/parentSchema}} {{#vars}} - $this->container['{{name}}'] = $data['{{name}}'] ?? {{{defaultValue}}}{{^defaultValue}}null{{/defaultValue}}; + if (isset($data['{{name}}'])) { + $this->container['{{name}}'] = $data['{{name}}']; + } {{/vars}} - {{#discriminator}} - - // Initialize discriminator property with the model name. - $this->container['{{discriminatorName}}'] = static::$openAPIModelName; - {{/discriminator}} } /** @@ -197,14 +150,14 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par {{#vars}} {{#required}} - if ($this->container['{{name}}'] === null) { + if (!isset($this->container['{{name}}']) || $this->container['{{name}}'] === null) { $invalidProperties[] = "'{{name}}' can't be null"; } {{/required}} {{#isEnum}} {{^isContainer}} $allowedValues = $this->{{getter}}AllowableValues(); - if (!is_null($this->container['{{name}}']) && !in_array($this->container['{{name}}'], $allowedValues, true)) { + if (isset($this->container['{{name}}']) && !in_array($this->container['{{name}}'], $allowedValues, true)) { $invalidProperties[] = sprintf( "invalid value '%s' for '{{name}}', must be one of '%s'", $this->container['{{name}}'], @@ -216,43 +169,43 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par {{/isEnum}} {{#hasValidation}} {{#maxLength}} - if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}(mb_strlen($this->container['{{name}}']) > {{maxLength}})) { + if ({{^required}}isset($this->container['{{name}}']) && {{/required}}(mb_strlen($this->container['{{name}}']) > {{maxLength}})) { $invalidProperties[] = "invalid value for '{{name}}', the character length must be smaller than or equal to {{{maxLength}}}."; } {{/maxLength}} {{#minLength}} - if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}(mb_strlen($this->container['{{name}}']) < {{minLength}})) { + if ({{^required}}isset($this->container['{{name}}']) && {{/required}}(mb_strlen($this->container['{{name}}']) < {{minLength}})) { $invalidProperties[] = "invalid value for '{{name}}', the character length must be bigger than or equal to {{{minLength}}}."; } {{/minLength}} {{#maximum}} - if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}($this->container['{{name}}'] >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}})) { + if ({{^required}}isset($this->container['{{name}}']) && {{/required}}($this->container['{{name}}'] >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}})) { $invalidProperties[] = "invalid value for '{{name}}', must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{maximum}}."; } {{/maximum}} {{#minimum}} - if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}($this->container['{{name}}'] <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}})) { + if ({{^required}}isset($this->container['{{name}}']) && {{/required}}($this->container['{{name}}'] <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}})) { $invalidProperties[] = "invalid value for '{{name}}', must be bigger than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{minimum}}."; } {{/minimum}} {{#pattern}} - if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}!preg_match("{{{pattern}}}", $this->container['{{name}}'])) { + if ({{^required}}isset($this->container['{{name}}']) && {{/required}}!preg_match("{{{pattern}}}", $this->container['{{name}}'])) { $invalidProperties[] = "invalid value for '{{name}}', must be conform to the pattern {{{pattern}}}."; } {{/pattern}} {{#maxItems}} - if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}(count($this->container['{{name}}']) > {{maxItems}})) { + if ({{^required}}isset($this->container['{{name}}']) && {{/required}}(count($this->container['{{name}}']) > {{maxItems}})) { $invalidProperties[] = "invalid value for '{{name}}', number of items must be less than or equal to {{{maxItems}}}."; } {{/maxItems}} {{#minItems}} - if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}(count($this->container['{{name}}']) < {{minItems}})) { + if ({{^required}}isset($this->container['{{name}}']) && {{/required}}(count($this->container['{{name}}']) < {{minItems}})) { $invalidProperties[] = "invalid value for '{{name}}', number of items must be greater than or equal to {{{minItems}}}."; } @@ -285,7 +238,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par */ public function {{getter}}() { - return $this->container['{{name}}']; + return $this->container['{{name}}'] ?? null; } /** @@ -416,39 +369,4 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par { unset($this->container[$offset]); } - - /** - * Serializes the object to a value that can be serialized natively by json_encode(). - * @link https://www.php.net/manual/en/jsonserializable.jsonserialize.php - * - * @return mixed Returns data which can be serialized by json_encode(), which is a value - * of any type other than a resource. - */ - public function jsonSerialize() - { - return ObjectSerializer::sanitizeForSerialization($this); - } - - /** - * Gets the string presentation of the object - * - * @return string - */ - public function __toString() - { - return json_encode( - ObjectSerializer::sanitizeForSerialization($this), - JSON_PRETTY_PRINT - ); - } - - /** - * Gets a header-safe presentation of the object - * - * @return string - */ - public function toHeaderValue() - { - return json_encode(ObjectSerializer::sanitizeForSerialization($this)); - } }