/** * 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']; }
/** * The routing strategy, it creates all necessary routes to match RESTFul URLs for the * provided resource name. * * @param object $router The router instance. * @param string $resource The resource name. * @param array $options The options array. */ public function __invoke($router, $resource, $options = []) { $options += ['name' => $resource, 'key' => $this->_key, 'format' => $this->_format, 'rkey' => $this->_rkey, 'rformat' => $this->_format, 'action' => ':{action}']; $slug = Inflector::dasherize(Inflector::underscore($resource)); $path = '{resource:' . $slug . '}'; $placeholder = '{id:' . $options['format'] . '}'; $rplaceholder = '{rid:' . $options['rformat'] . '}'; $pattern = '[{relation}/' . $rplaceholder . '/]' . $path . '[/' . $placeholder . ']' . '[/' . $options['action'] . ']'; $options['params'] = ['resource' => $slug]; return $router->bind($pattern, $options, function ($route) use($router, $resource, $options) { return $this->dispatch($resource, $route, $router, $options); }); }
}); }); 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('TestField'))->toBe('TestField'); expect(Inflector::camelize('my_name\\space'))->toBe('MyName\\Space');