-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from GeometriaLab/feature/userRole-37093795
Add Singularize filter.
- Loading branch information
Showing
19 changed files
with
454 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,101 @@ | |||
<?php | |||
|
|||
namespace GeometriaLab\Filter; | |||
|
|||
use \Zend\Filter\FilterInterface as ZendFilterInterface; | |||
|
|||
class Singularize implements ZendFilterInterface | |||
{ | |||
/** | |||
* @var array | |||
*/ | |||
protected $singular = array ( | |||
'/(quiz)zes$/i' => '\1', | |||
'/(matr)ices$/i' => '\1ix', | |||
'/(vert|ind)ices$/i' => '\1ex', | |||
'/^(ox)en/i' => '\1', | |||
'/(alias|status)es$/i' => '\1', | |||
'/([octop|vir])i$/i' => '\1us', | |||
'/(cris|ax|test)es$/i' => '\1is', | |||
'/(shoe)s$/i' => '\1', | |||
'/(o)es$/i' => '\1', | |||
'/(bus)es$/i' => '\1', | |||
'/([m|l])ice$/i' => '\1ouse', | |||
'/(x|ch|ss|sh)es$/i' => '\1', | |||
'/(m)ovies$/i' => '\1ovie', | |||
'/(s)eries$/i' => '\1eries', | |||
'/([^aeiouy]|qu)ies$/i' => '\1y', | |||
'/([lr])ves$/i' => '\1f', | |||
'/(tive)s$/i' => '\1', | |||
'/(hive)s$/i' => '\1', | |||
'/([^f])ves$/i' => '\1fe', | |||
'/(^analy)ses$/i' => '\1sis', | |||
'/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis', | |||
'/([ti])a$/i' => '\1um', | |||
'/(n)ews$/i' => '\1ews', | |||
'/status/i' => 'status', | |||
'/s$/i' => '', | |||
); | |||
/** | |||
* @var array | |||
*/ | |||
protected $uncountable = array( | |||
'equipment', | |||
'information', | |||
'rice', | |||
'money', | |||
'species', | |||
'series', | |||
'fish', | |||
'sheep' | |||
); | |||
/** | |||
* @var array | |||
*/ | |||
protected $irregular = array( | |||
'person' => 'people', | |||
'man' => 'men', | |||
'child' => 'children', | |||
'sex' => 'sexes', | |||
'move' => 'moves' | |||
); | |||
/** | |||
* @var array | |||
*/ | |||
protected static $cachedWords = array(); | |||
|
|||
/** | |||
* Returns the result of filtering $value | |||
* | |||
* @param string $word | |||
* @return string | |||
*/ | |||
function filter($word) | |||
{ | |||
if (isset(static::$cachedWords[$word])) { | |||
return static::$cachedWords[$word]; | |||
} | |||
|
|||
$lowercaseWord = strtolower($word); | |||
foreach ($this->uncountable as $uncountable){ | |||
if (substr($lowercaseWord,(-1 * strlen($uncountable))) == $uncountable) { | |||
return static::$cachedWords[$word] = $word; | |||
} | |||
} | |||
|
|||
foreach ($this->irregular as $plural=> $singular) { | |||
$arr = array(); | |||
if (preg_match('/(' . $singular . ')$/i', $word, $arr)) { | |||
return static::$cachedWords[$word] = preg_replace('/(' . $singular . ')$/i', substr($arr[0], 0, 1) . substr($plural, 1), $word); | |||
} | |||
} | |||
|
|||
foreach ($this->singular as $rule => $replacement) { | |||
if (preg_match($rule, $word)) { | |||
return static::$cachedWords[$word] = preg_replace($rule, $replacement, $word); | |||
} | |||
} | |||
|
|||
return static::$cachedWords[$word] = $word; | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ermissions/Assertion/AbstractResource.php → ...s/Assertion/Resource/AbstractResource.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...rmissions/Assertion/ResourceInterface.php → .../Assertion/Resource/ResourceInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
library/GeometriaLab/Permissions/Roles/AbstractRoles.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,119 @@ | |||
<?php | |||
|
|||
namespace GeometriaLab\Permissions\Roles; | |||
|
|||
use GeometriaLab\Model\AbstractModel, | |||
GeometriaLab\Model\Persistent\AbstractModel as PersistentAbstractModel, | |||
GeometriaLab\Permissions\Assertion\Resource\ResourceInterface; | |||
|
|||
/** | |||
* @property \GeometriaLab\Permissions\Roles\ResourceRoles[] $resourceRoles | |||
*/ | |||
abstract class AbstractRoles extends PersistentAbstractModel | |||
{ | |||
/** | |||
* Resource roles map | |||
* | |||
* @var array | |||
*/ | |||
protected $resourceRolesMap; | |||
|
|||
/** | |||
* Has role for Model | |||
* | |||
* @param string $role | |||
* @param AbstractModel $model | |||
* @return bool | |||
* @throws \RuntimeException | |||
*/ | |||
public function hasRole($role, AbstractModel $model) | |||
{ | |||
if (!isset($model->id)) { | |||
$modelClassName = get_class($model); | |||
throw new \RuntimeException("Need 'id' property in model '{$modelClassName}'"); | |||
} | |||
|
|||
$parts = explode('\\', get_class($model)); | |||
$resourceName = array_pop($parts); | |||
return $this->hasRoleForProperty('resourcesRoles', $role, $resourceName, $model->id); | |||
} | |||
|
|||
/** | |||
* Has role for Resource in City | |||
* | |||
* @param string $role | |||
* @param string $cityId | |||
* @param ResourceInterface|string $resource | |||
* @return bool | |||
*/ | |||
public function hasRoleInCity($role, $cityId, $resource) | |||
{ | |||
return $this->hasRoleForProperty('citiesRoles', $role, $resource, $cityId); | |||
} | |||
|
|||
/** | |||
* Has Role for resource in property | |||
* | |||
* @param string $property | |||
* @param string $role | |||
* @param string $resourceName | |||
* @param string $objectId | |||
* @return bool | |||
* @throws \InvalidArgumentException | |||
*/ | |||
protected function hasRoleForProperty($property, $role, $resourceName, $objectId) | |||
{ | |||
$permission = $this->getPermissionByResourceName($resourceName); | |||
|
|||
if ($permission === null) { | |||
return false; | |||
} | |||
|
|||
if (!isset($permission->{$property})) { | |||
throw new \InvalidArgumentException("Property '{$property}' doesn't exist"); | |||
} | |||
|
|||
// @TODO Hack for super manager | |||
if (isset($permission->{$property}[0]) && $permission->{$property}[0] === $role) { | |||
return true; | |||
} | |||
|
|||
if (!isset($permission->{$property}[$objectId])) { | |||
return false; | |||
} | |||
|
|||
return $permission->{$property}[$objectId] === $role; | |||
} | |||
|
|||
/** | |||
* Get Role by name | |||
* | |||
* @param string $resourceName | |||
* @return ResourceRoles|null | |||
* @throws \RuntimeException | |||
*/ | |||
protected function getPermissionByResourceName($resourceName) | |||
{ | |||
if ($this->resourceRoles === null) { | |||
return null; | |||
} | |||
|
|||
if ($this->resourceRolesMap === null) { | |||
// @TODO Add feature getting Model from array by id | |||
foreach ($this->resourceRoles as $index => $resourceRole) { | |||
$this->resourceRolesMap[$resourceRole->resourceName] = $index; | |||
} | |||
} | |||
|
|||
if (isset($this->resourceRolesMap[$resourceName])) { | |||
$index = $this->resourceRolesMap[$resourceName]; | |||
if (!isset($this->resourceRoles[$index])) { | |||
throw new \RuntimeException("Can't find '{$resourceName}' resourceRoles. ResourceRolesMap is broken."); | |||
} | |||
|
|||
return $this->resourceRoles[$index]; | |||
} | |||
|
|||
return null; | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,15 @@ | |||
<?php | |||
|
|||
namespace GeometriaLab\Permissions\Roles; | |||
|
|||
use GeometriaLab\Model\AbstractModel; | |||
|
|||
/** | |||
* @property string $resourceName | |||
* @property array $resourcesRoles | |||
* @property array $citiesRoles | |||
*/ | |||
class ResourceRoles extends AbstractModel | |||
{ | |||
|
|||
} |
Oops, something went wrong.