@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.js CHANGED
@@ -19,7 +19,7 @@ import {
19
19
  schemaToBlueprint,
20
20
  toColumnName,
21
21
  toTableName
22
- } from "./chunk-H37M25AK.js";
22
+ } from "./chunk-ILSNN7UV.js";
23
23
  export {
24
24
  formatColumnMethod,
25
25
  formatForeignKey,
package/dist/plugin.cjs CHANGED
@@ -1097,6 +1097,9 @@ function generateMigrationsFromChanges(changes, options = {}) {
1097
1097
  return migrations;
1098
1098
  }
1099
1099
 
1100
+ // src/model/generator.ts
1101
+ var import_omnify_types = require("@famgia/omnify-types");
1102
+
1100
1103
  // src/utils.ts
1101
1104
  function toSnakeCase(str) {
1102
1105
  return str.replace(/([A-Z])/g, "_$1").replace(/^_/, "").toLowerCase();
@@ -1126,6 +1129,45 @@ var DEFAULT_OPTIONS = {
1126
1129
  baseModelPath: "app/Models/OmnifyBase",
1127
1130
  modelPath: "app/Models"
1128
1131
  };
1132
+ function generateLocalizedDisplayNames(displayName, indent = " ") {
1133
+ if (displayName === void 0) {
1134
+ return "";
1135
+ }
1136
+ if (typeof displayName === "string") {
1137
+ return `${indent}'en' => '${escapePhpString(displayName)}',`;
1138
+ }
1139
+ if ((0, import_omnify_types.isLocaleMap)(displayName)) {
1140
+ const entries = Object.entries(displayName).map(([locale, value]) => `${indent}'${locale}' => '${escapePhpString(value)}',`).join("\n");
1141
+ return entries;
1142
+ }
1143
+ return "";
1144
+ }
1145
+ function generatePropertyLocalizedDisplayNames(schema, indent = " ") {
1146
+ const properties = schema.properties ?? {};
1147
+ const entries = [];
1148
+ for (const [propName, propDef] of Object.entries(properties)) {
1149
+ const snakeName = toSnakeCase(propName);
1150
+ const displayName = propDef.displayName;
1151
+ if (displayName === void 0) {
1152
+ continue;
1153
+ }
1154
+ const innerIndent = indent + " ";
1155
+ if (typeof displayName === "string") {
1156
+ entries.push(`${indent}'${snakeName}' => [
1157
+ ${innerIndent}'en' => '${escapePhpString(displayName)}',
1158
+ ${indent}],`);
1159
+ } else if ((0, import_omnify_types.isLocaleMap)(displayName)) {
1160
+ const localeEntries = Object.entries(displayName).map(([locale, value]) => `${innerIndent}'${locale}' => '${escapePhpString(value)}',`).join("\n");
1161
+ entries.push(`${indent}'${snakeName}' => [
1162
+ ${localeEntries}
1163
+ ${indent}],`);
1164
+ }
1165
+ }
1166
+ return entries.join("\n");
1167
+ }
1168
+ function escapePhpString(str) {
1169
+ return str.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
1170
+ }
1129
1171
  function resolveOptions(options) {
1130
1172
  return {
1131
1173
  baseModelNamespace: options?.baseModelNamespace ?? DEFAULT_OPTIONS.baseModelNamespace,
@@ -1508,11 +1550,14 @@ use Illuminate\\Database\\Eloquent\\Relations\\MorphOne;
1508
1550
  use Illuminate\\Database\\Eloquent\\Relations\\MorphMany;
1509
1551
  use Illuminate\\Database\\Eloquent\\Relations\\MorphToMany;
1510
1552
  use Illuminate\\Database\\Eloquent\\Collection as EloquentCollection;
1553
+ use {{BASE_MODEL_NAMESPACE}}\\Traits\\HasLocalizedDisplayName;
1554
+ use {{BASE_MODEL_NAMESPACE}}\\Locales\\{{CLASS_NAME}}Locales;
1511
1555
  {{IMPORTS}}
1512
1556
 
1513
1557
  {{DOC_COMMENT}}
1514
1558
  class {{CLASS_NAME}}BaseModel extends {{BASE_MODEL_CLASS}}
1515
1559
  {
1560
+ use HasLocalizedDisplayName;
1516
1561
  {{TRAITS}}
1517
1562
  /**
1518
1563
  * The table associated with the model.
@@ -1531,6 +1576,20 @@ class {{CLASS_NAME}}BaseModel extends {{BASE_MODEL_CLASS}}
1531
1576
  */
1532
1577
  public $timestamps = {{TIMESTAMPS}};
1533
1578
 
1579
+ /**
1580
+ * Localized display names for this model.
1581
+ *
1582
+ * @var array<string, string>
1583
+ */
1584
+ protected static array $localizedDisplayNames = {{CLASS_NAME}}Locales::DISPLAY_NAMES;
1585
+
1586
+ /**
1587
+ * Localized display names for properties.
1588
+ *
1589
+ * @var array<string, array<string, string>>
1590
+ */
1591
+ protected static array $localizedPropertyDisplayNames = {{CLASS_NAME}}Locales::PROPERTY_DISPLAY_NAMES;
1592
+
1534
1593
  /**
1535
1594
  * The attributes that are mass assignable.
1536
1595
  */
@@ -1587,12 +1646,15 @@ use Illuminate\\Database\\Eloquent\\Relations\\MorphMany;
1587
1646
  use Illuminate\\Database\\Eloquent\\Relations\\MorphToMany;
1588
1647
  use Illuminate\\Database\\Eloquent\\Collection as EloquentCollection;
1589
1648
  use Illuminate\\Notifications\\Notifiable;
1649
+ use {{BASE_MODEL_NAMESPACE}}\\Traits\\HasLocalizedDisplayName;
1650
+ use {{BASE_MODEL_NAMESPACE}}\\Locales\\{{CLASS_NAME}}Locales;
1590
1651
  {{IMPORTS}}
1591
1652
 
1592
1653
  {{DOC_COMMENT}}
1593
1654
  class {{CLASS_NAME}}BaseModel extends Authenticatable
1594
1655
  {
1595
1656
  use Notifiable;
1657
+ use HasLocalizedDisplayName;
1596
1658
  {{TRAITS}}
1597
1659
  /**
1598
1660
  * The table associated with the model.
@@ -1611,6 +1673,20 @@ class {{CLASS_NAME}}BaseModel extends Authenticatable
1611
1673
  */
1612
1674
  public $timestamps = {{TIMESTAMPS}};
1613
1675
 
1676
+ /**
1677
+ * Localized display names for this model.
1678
+ *
1679
+ * @var array<string, string>
1680
+ */
1681
+ protected static array $localizedDisplayNames = {{CLASS_NAME}}Locales::DISPLAY_NAMES;
1682
+
1683
+ /**
1684
+ * Localized display names for properties.
1685
+ *
1686
+ * @var array<string, array<string, string>>
1687
+ */
1688
+ protected static array $localizedPropertyDisplayNames = {{CLASS_NAME}}Locales::PROPERTY_DISPLAY_NAMES;
1689
+
1614
1690
  /**
1615
1691
  * The attributes that are mass assignable.
1616
1692
  */
@@ -1715,6 +1791,132 @@ class OmnifyServiceProvider extends ServiceProvider
1715
1791
  ]);
1716
1792
  }
1717
1793
  }
1794
+ `,
1795
+ "has-localized-display-name": `<?php
1796
+
1797
+ namespace {{BASE_MODEL_NAMESPACE}}\\Traits;
1798
+
1799
+ /**
1800
+ * Trait for localized display names.
1801
+ * Uses Laravel's app()->getLocale() for locale resolution.
1802
+ *
1803
+ * DO NOT EDIT - This file is auto-generated by Omnify.
1804
+ * Any changes will be overwritten on next generation.
1805
+ *
1806
+ * @generated by @famgia/omnify-laravel
1807
+ */
1808
+ trait HasLocalizedDisplayName
1809
+ {
1810
+ /**
1811
+ * Get the localized display name for this model.
1812
+ *
1813
+ * @param string|null $locale Locale code (defaults to app locale)
1814
+ * @return string
1815
+ */
1816
+ public static function displayName(?string $locale = null): string
1817
+ {
1818
+ $locale = $locale ?? app()->getLocale();
1819
+ $displayNames = static::$localizedDisplayNames ?? [];
1820
+
1821
+ return $displayNames[$locale]
1822
+ ?? $displayNames[config('app.fallback_locale', 'en')]
1823
+ ?? $displayNames[array_key_first($displayNames) ?? 'en']
1824
+ ?? class_basename(static::class);
1825
+ }
1826
+
1827
+ /**
1828
+ * Get all localized display names for this model.
1829
+ *
1830
+ * @return array<string, string>
1831
+ */
1832
+ public static function allDisplayNames(): array
1833
+ {
1834
+ return static::$localizedDisplayNames ?? [];
1835
+ }
1836
+
1837
+ /**
1838
+ * Get the localized display name for a property.
1839
+ *
1840
+ * @param string $property Property name
1841
+ * @param string|null $locale Locale code (defaults to app locale)
1842
+ * @return string
1843
+ */
1844
+ public static function propertyDisplayName(string $property, ?string $locale = null): string
1845
+ {
1846
+ $locale = $locale ?? app()->getLocale();
1847
+ $displayNames = static::$localizedPropertyDisplayNames[$property] ?? [];
1848
+
1849
+ return $displayNames[$locale]
1850
+ ?? $displayNames[config('app.fallback_locale', 'en')]
1851
+ ?? $displayNames[array_key_first($displayNames) ?? 'en']
1852
+ ?? $property;
1853
+ }
1854
+
1855
+ /**
1856
+ * Get all localized display names for a property.
1857
+ *
1858
+ * @param string $property Property name
1859
+ * @return array<string, string>
1860
+ */
1861
+ public static function allPropertyDisplayNames(string $property): array
1862
+ {
1863
+ return static::$localizedPropertyDisplayNames[$property] ?? [];
1864
+ }
1865
+
1866
+ /**
1867
+ * Get all property display names for a given locale.
1868
+ *
1869
+ * @param string|null $locale Locale code (defaults to app locale)
1870
+ * @return array<string, string>
1871
+ */
1872
+ public static function allPropertyDisplayNamesForLocale(?string $locale = null): array
1873
+ {
1874
+ $locale = $locale ?? app()->getLocale();
1875
+ $result = [];
1876
+
1877
+ foreach (static::$localizedPropertyDisplayNames ?? [] as $property => $displayNames) {
1878
+ $result[$property] = $displayNames[$locale]
1879
+ ?? $displayNames[config('app.fallback_locale', 'en')]
1880
+ ?? $displayNames[array_key_first($displayNames) ?? 'en']
1881
+ ?? $property;
1882
+ }
1883
+
1884
+ return $result;
1885
+ }
1886
+ }
1887
+ `,
1888
+ "locales": `<?php
1889
+
1890
+ namespace {{BASE_MODEL_NAMESPACE}}\\Locales;
1891
+
1892
+ /**
1893
+ * Localized display names for {{CLASS_NAME}}.
1894
+ *
1895
+ * DO NOT EDIT - This file is auto-generated by Omnify.
1896
+ * Any changes will be overwritten on next generation.
1897
+ *
1898
+ * @generated by @famgia/omnify-laravel
1899
+ */
1900
+ class {{CLASS_NAME}}Locales
1901
+ {
1902
+ /**
1903
+ * Localized display names for the model.
1904
+ *
1905
+ * @var array<string, string>
1906
+ */
1907
+ public const DISPLAY_NAMES = [
1908
+ {{LOCALIZED_DISPLAY_NAMES}}
1909
+ ];
1910
+
1911
+ /**
1912
+ * Localized display names for properties.
1913
+ *
1914
+ * @var array<string, array<string, string>>
1915
+ */
1916
+ public const PROPERTY_DISPLAY_NAMES = [
1917
+ {{LOCALIZED_PROPERTY_DISPLAY_NAMES}}
1918
+ ];
1919
+ }
1718
1920
  `
1719
1921
  };
1720
1922
  return stubs[stubName] ?? "";
@@ -1734,15 +1936,42 @@ function generateServiceProvider(schemas, options, stubContent) {
1734
1936
  schemaName: "__service_provider__"
1735
1937
  };
1736
1938
  }
1939
+ function generateLocalizedDisplayNameTrait(options, stubContent) {
1940
+ const content = stubContent.replace(/\{\{BASE_MODEL_NAMESPACE\}\}/g, options.baseModelNamespace);
1941
+ return {
1942
+ path: `${options.baseModelPath}/Traits/HasLocalizedDisplayName.php`,
1943
+ content,
1944
+ type: "trait",
1945
+ overwrite: true,
1946
+ // Always overwrite trait
1947
+ schemaName: "__trait__"
1948
+ };
1949
+ }
1950
+ function generateLocalesClass(schema, options, stubContent) {
1951
+ const className = toPascalCase(schema.name);
1952
+ const localizedDisplayNames = generateLocalizedDisplayNames(schema.displayName);
1953
+ const localizedPropertyDisplayNames = generatePropertyLocalizedDisplayNames(schema);
1954
+ 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);
1955
+ return {
1956
+ path: `${options.baseModelPath}/Locales/${className}Locales.php`,
1957
+ content,
1958
+ type: "locales",
1959
+ overwrite: true,
1960
+ // Always overwrite locales
1961
+ schemaName: schema.name
1962
+ };
1963
+ }
1737
1964
  function generateModels(schemas, options) {
1738
1965
  const resolved = resolveOptions(options);
1739
1966
  const models = [];
1740
1967
  models.push(generateBaseModel(schemas, resolved, getStubContent("base-model")));
1968
+ models.push(generateLocalizedDisplayNameTrait(resolved, getStubContent("has-localized-display-name")));
1741
1969
  models.push(generateServiceProvider(schemas, resolved, getStubContent("service-provider")));
1742
1970
  for (const schema of Object.values(schemas)) {
1743
1971
  if (schema.kind === "enum") {
1744
1972
  continue;
1745
1973
  }
1974
+ models.push(generateLocalesClass(schema, resolved, getStubContent("locales")));
1746
1975
  models.push(generateEntityBaseModel(
1747
1976
  schema,
1748
1977
  schemas,