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);
     }, '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'];
 }
Example #2
0
 /**
  * Has And Belongs To Many association
  *
  * @param string $className
  * @param string $tableName
  * @param string $foreignKey
  * @param string $foreignKeyRelated
  * @return HasAndBelongsToMany
  */
 protected function hasAndBelongsToMany($className, $tableName = null, $foreignKey = null, $foreignKeyRelated = null)
 {
     if (is_null($tableName)) {
         $table1 = \Inflector\Inflector::tableize(get_class($this));
         $table2 = \Inflector\Inflector::tableize($className);
         $tables = [$table1, $table2];
         asort($tables);
         $tableName = implode('_', $tables);
     }
     return new HasAndBelongsToMany($this, $className, $tableName, $foreignKey, $foreignKeyRelated);
 }
Example #3
0
 /**
  * Return humanized error messages
  */
 public function fullMessages()
 {
     $errors = [];
     foreach ($this->container as $attribute => $errs) {
         foreach ($errs as $er) {
             if ($attribute == 'base') {
                 $errors[] = $er;
             } else {
                 $errors[] = \Inflector\Inflector::humanize($attribute) . ' ' . $er;
             }
         }
     }
     return $errors;
 }
Example #4
0
<?php

use 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
 /**
  * Get foreign key name
  *
  * @return string
  */
 public static function getForeignKey()
 {
     return Inflector::underscore(Inflector::singularize(static::className())) . '_' . static::getPrimaryKey();
 }
Example #6
0
<?php

use 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
    /**
     * Get Pivot model
     *
     * @return \ORM\Model
     */
    function pivot()
    {
        list($t1, $t2) = explode('_', $this->tableName);
        $t1 = Inflector::singularize($t1);
        $t2 = Inflector::singularize($t2);
        $className = Inflector::classify(implode('_', [$t1, $t2]));
        $fk1 = $this->getForeignKey();
        $fk2 = $this->getForeignKeyRelated();
        if (!class_exists($className, false)) {
            $code = <<<PIVOT
                class {$className} extends \\ORM\\Model
                {
                    static function getTable()
                    {
                        return '{$this->tableName}';
                    }

                    function validate()
                    {
                        if(!is_numeric(\$this->{$fk1})) {
                            \$this->addError('{$fk1} must be numeric');
                        }

                        if(!is_numeric(\$this->{$fk2})) {
                            \$this->addError('{$fk2} must be numeric');
                        }
                    }
                }
PIVOT;
            eval($code);
        }
        return new $className();
    }
Example #8
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');
            });
        });
    });
});