Skip to content

Commit

Permalink
Merge pull request #3 from leroy-merlin-br/non-static-methods
Browse files Browse the repository at this point in the history
Non static methods
  • Loading branch information
Randson Oliveira committed Mar 28, 2015
2 parents 5df8612 + 5a0dbbc commit 4a0538a
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 10 deletions.
28 changes: 21 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,30 @@ Examples of usage of the Inflect(i.e portuguese).
```php
<?php

use Inflect\Inflect;
use Inflect\Inflect

Inflect::pluralize('pão'); // pães
Inflect::pluralize('carro'); // carros
$inflector = new Inflect;

Inflect::singularize('carros'); // carro
Inflect::singularize('pães'); // pão
$inflector->pluralize('pão'); // pães
$inflector->pluralize('carro'); // carros

Inflect::camelize('tablename'); // TableName
Inflect::camelize('tablename', true); // tableName
$inflector->singularize('carros'); // carro
$inflector->singularize('pães'); // pão

$inflector->camelize('tablename'); // TableName
$inflector->camelize('tablename', true); // tableName
```

You can also use it without instantiating the object trought the `Facade` class

```php
<?php

use Inflect\Facade as Inflect; // Use facade to be able to do static calls

Inflect::pluralize('carro'); // carros
Inflect::singularize('carros'); // carro
Inflect::camelize('tablename'); // TableName
```

## Installation
Expand Down
56 changes: 56 additions & 0 deletions src/Inflect/Facade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Inflect;

/**
* This is a helper class that allows the usag of the Inflect\Inflect trought
* static calls
*
* @example
* // Calling Inflect::plurarize as if it was static
* Inflect\Facade::pluralize('pão')
*
* @package Inflect
*/
class Facade
{
static $inflector;

/**
* Returns an instance of Inflect\Inflect
*
* @return Inflect
*/
protected static function getInflector()
{
if (!self::$inflector) {
self::$inflector = new Inflect;
}

return self::$inflector;
}

/**
* Handle dynamic, static calls to the actual inflector instance
*
* @param string $method
* @param array $args
* @return mixed
*/
public static function __callStatic($method, $args)
{
$instance = self::getInflector();

switch (count($args))
{
case 0:
return $instance->$method();

case 1:
return $instance->$method($args[0]);

default:
return call_user_func_array(array($instance, $method), $args);
}
}
}
11 changes: 8 additions & 3 deletions src/Inflect/Inflect.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace Inflect;

/**
* Handle words in Portuguese
*
* @package Inflect
*/
class Inflect
{

Expand Down Expand Up @@ -86,7 +91,7 @@ class Inflect
* @param string
* @return string
**/
public static function pluralize($string)
public function pluralize($string)
{
if (in_array(strtolower($string), self::$uncountable)) {
return $string;
Expand Down Expand Up @@ -115,7 +120,7 @@ public static function pluralize($string)
* @param string
* @return string
**/
public static function singularize($string)
public function singularize($string)
{
if (in_array(strtolower($string), self::$uncountable)) {
return $string;
Expand Down Expand Up @@ -144,7 +149,7 @@ public static function singularize($string)
* @param string
* @return string
**/
public static function camelize($string, $uppercase_first_letter = false)
public function camelize($string, $uppercase_first_letter = false)
{
if ($uppercase_first_letter) {
return str_replace(' ', '', ucwords(strtr($string, '_-', ' ')));
Expand Down

0 comments on commit 4a0538a

Please sign in to comment.