/** * 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); }, 'primaryKey' => function () { return 'id'; }, 'foreignKey' => function ($class) { $pos = strrpos($class, '\\'); $basename = substr($class, $pos !== false ? $pos + 1 : 0); return Inflector::underscore(Inflector::singularize($basename)) . '_id'; }, 'fieldName' => function ($class) { $pos = strrpos($class, '\\'); $basename = substr($class, $pos !== false ? $pos + 1 : 0); return Inflector::underscore(Inflector::singularize($basename)); }, 'usingName' => function ($name) { return Inflector::singularize($name); }, 'getter' => function ($name) { return 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $name))); }, 'setter' => function ($name) { return 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $name))); }]]; $config = Set::merge($defaults, $config); $this->_conventions = $config['conventions']; }
/** * Get foreign key name * * @return string */ public static function getForeignKey() { return Inflector::underscore(Inflector::singularize(static::className())) . '_' . static::getPrimaryKey(); }
}); }); describe("::parameterize()", function () { it("parameterizes a string", function () { $result = Inflector::parameterize('Foo:Bar & Cie'); expect($result)->toBe('foo-bar-cie'); $result = Inflector::parameterize('Foo:Bar & Cie', '_'); expect($result)->toBe('foo_bar_cie'); }); }); describe("::underscore()", function () { it("underscores a string", function () { expect(Inflector::underscore('ClassName'))->toBe('class_name'); expect(Inflector::underscore('TestField'))->toBe('test_field'); expect(Inflector::underscore('MyName\\Space'))->toBe('my_name\\space'); expect(Inflector::underscore('dashed-version'))->toBe('dashed_version'); }); }); describe("::dasherize()", function () { it("dasherizes a string", function () { expect(Inflector::dasherize('class_name'))->toBe('class-name'); expect(Inflector::dasherize('test_field'))->toBe('test-field'); }); }); describe("::camelize()", function () { it("camelizes a string", function () { expect(Inflector::camelize('test-field'))->toBe('TestField'); expect(Inflector::camelize('test_field'))->toBe('TestField'); expect(Inflector::camelize('TEST_FIELD'))->toBe('TestField'); expect(Inflector::camelize('my_name\\space'))->toBe('MyName\\Space'); });