/** * Configures the schema class. * * @param array $config Possible options are: * - `'conventions'` _array_: Allow to override the default convention rules for generating * primary or foreign key as well as for table/collection names * from an entity class name. */ public function config($config = []) { $defaults = ['conventions' => ['source' => function ($class) { $basename = substr(strrchr($class, '\\'), 1); return Inflector::underscore($basename); }, 'key' => function () { return 'id'; }, 'reference' => function ($class) { $pos = strrpos($class, '\\'); $basename = substr($class, $pos !== false ? $pos + 1 : 0); return Inflector::underscore(Inflector::singularize($basename)) . '_id'; }, 'references' => function ($class) { $pos = strrpos($class, '\\'); $basename = substr($class, $pos !== false ? $pos + 1 : 0); return Inflector::underscore(Inflector::singularize($basename)) . '_ids'; }, 'field' => function ($class) { $pos = strrpos($class, '\\'); $basename = substr($class, $pos !== false ? $pos + 1 : 0); return Inflector::underscore(Inflector::singularize($basename)); }, 'multiple' => function ($name) { return Inflector::pluralize($name); }, 'single' => function ($name) { return Inflector::singularize($name); }]]; $config = Set::merge($defaults, $config); $this->_conventions = $config['conventions']; }
it("reset the inflector rules", function () { Inflector::reset(); expect(Inflector::singularize('posts'))->toBe('post'); expect(Inflector::pluralize('post'))->toBe('posts'); }); it("clears all the inflector rules", function () { Inflector::reset(true); expect(Inflector::singularize('posts'))->toBe('posts'); expect(Inflector::pluralize('post'))->toBe('post'); }); }); context("using a custom language", function () { describe("::singularize/pluralize()", function () { it("can manages several languages", function () { Inflector::reset(); require 'spec/Fixture/fr.php'; require 'spec/Fixture/es.php'; expect(Inflector::singularize('taxes'))->toBe('tax'); expect(Inflector::pluralize('tax'))->toBe('taxes'); expect(Inflector::singularize('bateaux', 'fr'))->toBe('bateau'); expect(Inflector::pluralize('bateau', 'fr'))->toBe('bateaux'); expect(Inflector::singularize('ediciones', 'es'))->toBe('edición'); expect(Inflector::pluralize('edición', 'es'))->toBe('ediciones'); Inflector::singular('/x$/i', '', 'zz'); Inflector::plural('/([^x])$/i', '\\1x', 'zz'); expect(Inflector::singularize('abcdefx', 'zz'))->toBe('abcdef'); expect(Inflector::pluralize('abcdef', 'zz'))->toBe('abcdefx'); }); }); }); });