Example #1
0
 /**
  * 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'];
 }
Example #2
0
 /**
  * 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);
     });
 }
Example #3
0
 public function __construct($options = [])
 {
     if (isset($options['table'])) {
         $this->table = $options['table'];
         $this->alias = $options['table'];
     }
     if (isset($options['alias']) && $options['alias']) {
         $this->alias = $options['alias'];
     }
     $this->pk .= ucfirst(Inflector::singularize($this->table));
     if (isset($options['pk']) && $options['pk']) {
         $this->pk = $options['pk'];
     }
 }
Example #4
0
<?php

use Lead\Inflector\Inflector;
Inflector::singular('/s$/i', '', 'fr');
Inflector::plural('/([^s])$/i', '\\1s', 'fr');
Inflector::plural('/(eu|eau)$/i', '\\1x', 'fr');
Inflector::singular('/(eu|eau)x$/i', '\\1', 'fr');
Inflector::plural('/(x|z)$/i', '\\1', 'fr');
Inflector::irregular('monsieur', 'messieurs', 'fr');
Inflector::irregular('madame', 'mesdames', 'fr');
Inflector::irregular('mademoiselle', 'mesdemoiselles', 'fr');
/**
 * Warning, using an "exhastive" list of rules will slow
 * down all singularizations/pluralizations generation.
 * So it's preferable to only add the ones you are actually using.
 */
Example #5
0
 /**
  * Helper for `Payload::push()`.
  *
  * @param  object  $entity       The Chaos entity to push in the payload.
  * @param  boolean $relationship Indicates whether the entity is some related data or not.
  * @return array                 The pushed data
  */
 public function _push($entity, $related = false)
 {
     if (!$entity instanceof Model) {
         $this->errors([['status' => 500, 'code' => 500, 'title' => "The JSON-API serializer only supports Chaos entities."]]);
         return;
     }
     $definition = $entity::definition();
     $data = $this->_data($entity);
     if (($link = $this->_link) && $entity->exists()) {
         $data['links']['self'] = $link(Inflector::camelize($data['type']), ['id' => $entity->id()], ['absolute' => true]);
     }
     if ($related) {
         $this->_store($data);
         unset($data['attributes']);
         unset($data['links']);
     } elseif ($relations = $definition->relations()) {
         $this->_populateRelationships($entity, $relations, $data);
     }
     return $data;
 }
Example #6
0
<?php

use Lead\Inflector\Inflector;
Inflector::singular('/s$/i', '', 'es');
Inflector::plural('/$/i', 's', 'es');
Inflector::singular('/es$/i', '', 'es');
Inflector::plural('/([^aeéiou])$/i', '\\1es', 'es');
Inflector::singular('/ces$/i', 'z', 'es');
Inflector::plural('/z$/i', 'ces', 'es');
Inflector::singular('/iones$/i', 'ión', 'es');
Inflector::plural('/ión$/i', 'iones', 'es');
Inflector::irregular('carácter', 'caracteres', 'es');
/**
 * Warning, using an "exhastive" list of rules will slow
 * down all singularizations/pluralizations generation.
 * So it's preferable to only add the ones you are actually using.
 */
Example #7
0
        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');
            });
        });
    });
});