@famgia/omnify-laravel 0.0.18 → 0.0.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1167,6 +1167,9 @@ function generateMigrationsFromChanges(changes, options = {}) {
1167
1167
  var import_node_fs = require("fs");
1168
1168
  var import_node_path = require("path");
1169
1169
 
1170
+ // src/model/generator.ts
1171
+ var import_omnify_types = require("@famgia/omnify-types");
1172
+
1170
1173
  // src/utils.ts
1171
1174
  function toSnakeCase(str) {
1172
1175
  return str.replace(/([A-Z])/g, "_$1").replace(/^_/, "").toLowerCase();
@@ -1196,6 +1199,45 @@ var DEFAULT_OPTIONS = {
1196
1199
  baseModelPath: "app/Models/OmnifyBase",
1197
1200
  modelPath: "app/Models"
1198
1201
  };
1202
+ function generateLocalizedDisplayNames(displayName, indent = " ") {
1203
+ if (displayName === void 0) {
1204
+ return "";
1205
+ }
1206
+ if (typeof displayName === "string") {
1207
+ return `${indent}'en' => '${escapePhpString(displayName)}',`;
1208
+ }
1209
+ if ((0, import_omnify_types.isLocaleMap)(displayName)) {
1210
+ const entries = Object.entries(displayName).map(([locale, value]) => `${indent}'${locale}' => '${escapePhpString(value)}',`).join("\n");
1211
+ return entries;
1212
+ }
1213
+ return "";
1214
+ }
1215
+ function generatePropertyLocalizedDisplayNames(schema, indent = " ") {
1216
+ const properties = schema.properties ?? {};
1217
+ const entries = [];
1218
+ for (const [propName, propDef] of Object.entries(properties)) {
1219
+ const snakeName = toSnakeCase(propName);
1220
+ const displayName = propDef.displayName;
1221
+ if (displayName === void 0) {
1222
+ continue;
1223
+ }
1224
+ const innerIndent = indent + " ";
1225
+ if (typeof displayName === "string") {
1226
+ entries.push(`${indent}'${snakeName}' => [
1227
+ ${innerIndent}'en' => '${escapePhpString(displayName)}',
1228
+ ${indent}],`);
1229
+ } else if ((0, import_omnify_types.isLocaleMap)(displayName)) {
1230
+ const localeEntries = Object.entries(displayName).map(([locale, value]) => `${innerIndent}'${locale}' => '${escapePhpString(value)}',`).join("\n");
1231
+ entries.push(`${indent}'${snakeName}' => [
1232
+ ${localeEntries}
1233
+ ${indent}],`);
1234
+ }
1235
+ }
1236
+ return entries.join("\n");
1237
+ }
1238
+ function escapePhpString(str) {
1239
+ return str.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
1240
+ }
1199
1241
  function resolveOptions(options) {
1200
1242
  return {
1201
1243
  baseModelNamespace: options?.baseModelNamespace ?? DEFAULT_OPTIONS.baseModelNamespace,
@@ -1578,11 +1620,14 @@ use Illuminate\\Database\\Eloquent\\Relations\\MorphOne;
1578
1620
  use Illuminate\\Database\\Eloquent\\Relations\\MorphMany;
1579
1621
  use Illuminate\\Database\\Eloquent\\Relations\\MorphToMany;
1580
1622
  use Illuminate\\Database\\Eloquent\\Collection as EloquentCollection;
1623
+ use {{BASE_MODEL_NAMESPACE}}\\Traits\\HasLocalizedDisplayName;
1624
+ use {{BASE_MODEL_NAMESPACE}}\\Locales\\{{CLASS_NAME}}Locales;
1581
1625
  {{IMPORTS}}
1582
1626
 
1583
1627
  {{DOC_COMMENT}}
1584
1628
  class {{CLASS_NAME}}BaseModel extends {{BASE_MODEL_CLASS}}
1585
1629
  {
1630
+ use HasLocalizedDisplayName;
1586
1631
  {{TRAITS}}
1587
1632
  /**
1588
1633
  * The table associated with the model.
@@ -1601,6 +1646,20 @@ class {{CLASS_NAME}}BaseModel extends {{BASE_MODEL_CLASS}}
1601
1646
  */
1602
1647
  public $timestamps = {{TIMESTAMPS}};
1603
1648
 
1649
+ /**
1650
+ * Localized display names for this model.
1651
+ *
1652
+ * @var array<string, string>
1653
+ */
1654
+ protected static array $localizedDisplayNames = {{CLASS_NAME}}Locales::DISPLAY_NAMES;
1655
+
1656
+ /**
1657
+ * Localized display names for properties.
1658
+ *
1659
+ * @var array<string, array<string, string>>
1660
+ */
1661
+ protected static array $localizedPropertyDisplayNames = {{CLASS_NAME}}Locales::PROPERTY_DISPLAY_NAMES;
1662
+
1604
1663
  /**
1605
1664
  * The attributes that are mass assignable.
1606
1665
  */
@@ -1657,12 +1716,15 @@ use Illuminate\\Database\\Eloquent\\Relations\\MorphMany;
1657
1716
  use Illuminate\\Database\\Eloquent\\Relations\\MorphToMany;
1658
1717
  use Illuminate\\Database\\Eloquent\\Collection as EloquentCollection;
1659
1718
  use Illuminate\\Notifications\\Notifiable;
1719
+ use {{BASE_MODEL_NAMESPACE}}\\Traits\\HasLocalizedDisplayName;
1720
+ use {{BASE_MODEL_NAMESPACE}}\\Locales\\{{CLASS_NAME}}Locales;
1660
1721
  {{IMPORTS}}
1661
1722
 
1662
1723
  {{DOC_COMMENT}}
1663
1724
  class {{CLASS_NAME}}BaseModel extends Authenticatable
1664
1725
  {
1665
1726
  use Notifiable;
1727
+ use HasLocalizedDisplayName;
1666
1728
  {{TRAITS}}
1667
1729
  /**
1668
1730
  * The table associated with the model.
@@ -1681,6 +1743,20 @@ class {{CLASS_NAME}}BaseModel extends Authenticatable
1681
1743
  */
1682
1744
  public $timestamps = {{TIMESTAMPS}};
1683
1745
 
1746
+ /**
1747
+ * Localized display names for this model.
1748
+ *
1749
+ * @var array<string, string>
1750
+ */
1751
+ protected static array $localizedDisplayNames = {{CLASS_NAME}}Locales::DISPLAY_NAMES;
1752
+
1753
+ /**
1754
+ * Localized display names for properties.
1755
+ *
1756
+ * @var array<string, array<string, string>>
1757
+ */
1758
+ protected static array $localizedPropertyDisplayNames = {{CLASS_NAME}}Locales::PROPERTY_DISPLAY_NAMES;
1759
+
1684
1760
  /**
1685
1761
  * The attributes that are mass assignable.
1686
1762
  */
@@ -1785,6 +1861,132 @@ class OmnifyServiceProvider extends ServiceProvider
1785
1861
  ]);
1786
1862
  }
1787
1863
  }
1864
+ `,
1865
+ "has-localized-display-name": `<?php
1866
+
1867
+ namespace {{BASE_MODEL_NAMESPACE}}\\Traits;
1868
+
1869
+ /**
1870
+ * Trait for localized display names.
1871
+ * Uses Laravel's app()->getLocale() for locale resolution.
1872
+ *
1873
+ * DO NOT EDIT - This file is auto-generated by Omnify.
1874
+ * Any changes will be overwritten on next generation.
1875
+ *
1876
+ * @generated by @famgia/omnify-laravel
1877
+ */
1878
+ trait HasLocalizedDisplayName
1879
+ {
1880
+ /**
1881
+ * Get the localized display name for this model.
1882
+ *
1883
+ * @param string|null $locale Locale code (defaults to app locale)
1884
+ * @return string
1885
+ */
1886
+ public static function displayName(?string $locale = null): string
1887
+ {
1888
+ $locale = $locale ?? app()->getLocale();
1889
+ $displayNames = static::$localizedDisplayNames ?? [];
1890
+
1891
+ return $displayNames[$locale]
1892
+ ?? $displayNames[config('app.fallback_locale', 'en')]
1893
+ ?? $displayNames[array_key_first($displayNames) ?? 'en']
1894
+ ?? class_basename(static::class);
1895
+ }
1896
+
1897
+ /**
1898
+ * Get all localized display names for this model.
1899
+ *
1900
+ * @return array<string, string>
1901
+ */
1902
+ public static function allDisplayNames(): array
1903
+ {
1904
+ return static::$localizedDisplayNames ?? [];
1905
+ }
1906
+
1907
+ /**
1908
+ * Get the localized display name for a property.
1909
+ *
1910
+ * @param string $property Property name
1911
+ * @param string|null $locale Locale code (defaults to app locale)
1912
+ * @return string
1913
+ */
1914
+ public static function propertyDisplayName(string $property, ?string $locale = null): string
1915
+ {
1916
+ $locale = $locale ?? app()->getLocale();
1917
+ $displayNames = static::$localizedPropertyDisplayNames[$property] ?? [];
1918
+
1919
+ return $displayNames[$locale]
1920
+ ?? $displayNames[config('app.fallback_locale', 'en')]
1921
+ ?? $displayNames[array_key_first($displayNames) ?? 'en']
1922
+ ?? $property;
1923
+ }
1924
+
1925
+ /**
1926
+ * Get all localized display names for a property.
1927
+ *
1928
+ * @param string $property Property name
1929
+ * @return array<string, string>
1930
+ */
1931
+ public static function allPropertyDisplayNames(string $property): array
1932
+ {
1933
+ return static::$localizedPropertyDisplayNames[$property] ?? [];
1934
+ }
1935
+
1936
+ /**
1937
+ * Get all property display names for a given locale.
1938
+ *
1939
+ * @param string|null $locale Locale code (defaults to app locale)
1940
+ * @return array<string, string>
1941
+ */
1942
+ public static function allPropertyDisplayNamesForLocale(?string $locale = null): array
1943
+ {
1944
+ $locale = $locale ?? app()->getLocale();
1945
+ $result = [];
1946
+
1947
+ foreach (static::$localizedPropertyDisplayNames ?? [] as $property => $displayNames) {
1948
+ $result[$property] = $displayNames[$locale]
1949
+ ?? $displayNames[config('app.fallback_locale', 'en')]
1950
+ ?? $displayNames[array_key_first($displayNames) ?? 'en']
1951
+ ?? $property;
1952
+ }
1953
+
1954
+ return $result;
1955
+ }
1956
+ }
1957
+ `,
1958
+ "locales": `<?php
1959
+
1960
+ namespace {{BASE_MODEL_NAMESPACE}}\\Locales;
1961
+
1962
+ /**
1963
+ * Localized display names for {{CLASS_NAME}}.
1964
+ *
1965
+ * DO NOT EDIT - This file is auto-generated by Omnify.
1966
+ * Any changes will be overwritten on next generation.
1967
+ *
1968
+ * @generated by @famgia/omnify-laravel
1969
+ */
1970
+ class {{CLASS_NAME}}Locales
1971
+ {
1972
+ /**
1973
+ * Localized display names for the model.
1974
+ *
1975
+ * @var array<string, string>
1976
+ */
1977
+ public const DISPLAY_NAMES = [
1978
+ {{LOCALIZED_DISPLAY_NAMES}}
1979
+ ];
1980
+
1981
+ /**
1982
+ * Localized display names for properties.
1983
+ *
1984
+ * @var array<string, array<string, string>>
1985
+ */
1986
+ public const PROPERTY_DISPLAY_NAMES = [
1987
+ {{LOCALIZED_PROPERTY_DISPLAY_NAMES}}
1988
+ ];
1989
+ }
1788
1990
  `
1789
1991
  };
1790
1992
  return stubs[stubName] ?? "";
@@ -1804,15 +2006,42 @@ function generateServiceProvider(schemas, options, stubContent) {
1804
2006
  schemaName: "__service_provider__"
1805
2007
  };
1806
2008
  }
2009
+ function generateLocalizedDisplayNameTrait(options, stubContent) {
2010
+ const content = stubContent.replace(/\{\{BASE_MODEL_NAMESPACE\}\}/g, options.baseModelNamespace);
2011
+ return {
2012
+ path: `${options.baseModelPath}/Traits/HasLocalizedDisplayName.php`,
2013
+ content,
2014
+ type: "trait",
2015
+ overwrite: true,
2016
+ // Always overwrite trait
2017
+ schemaName: "__trait__"
2018
+ };
2019
+ }
2020
+ function generateLocalesClass(schema, options, stubContent) {
2021
+ const className = toPascalCase(schema.name);
2022
+ const localizedDisplayNames = generateLocalizedDisplayNames(schema.displayName);
2023
+ const localizedPropertyDisplayNames = generatePropertyLocalizedDisplayNames(schema);
2024
+ const content = stubContent.replace(/\{\{BASE_MODEL_NAMESPACE\}\}/g, options.baseModelNamespace).replace(/\{\{CLASS_NAME\}\}/g, className).replace(/\{\{LOCALIZED_DISPLAY_NAMES\}\}/g, localizedDisplayNames).replace(/\{\{LOCALIZED_PROPERTY_DISPLAY_NAMES\}\}/g, localizedPropertyDisplayNames);
2025
+ return {
2026
+ path: `${options.baseModelPath}/Locales/${className}Locales.php`,
2027
+ content,
2028
+ type: "locales",
2029
+ overwrite: true,
2030
+ // Always overwrite locales
2031
+ schemaName: schema.name
2032
+ };
2033
+ }
1807
2034
  function generateModels(schemas, options) {
1808
2035
  const resolved = resolveOptions(options);
1809
2036
  const models = [];
1810
2037
  models.push(generateBaseModel(schemas, resolved, getStubContent("base-model")));
2038
+ models.push(generateLocalizedDisplayNameTrait(resolved, getStubContent("has-localized-display-name")));
1811
2039
  models.push(generateServiceProvider(schemas, resolved, getStubContent("service-provider")));
1812
2040
  for (const schema of Object.values(schemas)) {
1813
2041
  if (schema.kind === "enum") {
1814
2042
  continue;
1815
2043
  }
2044
+ models.push(generateLocalesClass(schema, resolved, getStubContent("locales")));
1816
2045
  models.push(generateEntityBaseModel(
1817
2046
  schema,
1818
2047
  schemas,