Skip to content

Commit

Permalink
Merge pull request #22 from GeometriaLab/feature/userRole-37093795
Browse files Browse the repository at this point in the history
Add Singularize filter.
  • Loading branch information
shumkov committed Dec 5, 2012
2 parents 62b76e9 + 32490c2 commit 739bd4c
Show file tree
Hide file tree
Showing 19 changed files with 454 additions and 47 deletions.
101 changes: 101 additions & 0 deletions library/GeometriaLab/Filter/Singularize.php
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;
}
}
22 changes: 11 additions & 11 deletions library/GeometriaLab/Permissions/Assertion/Assertion.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ class Assertion
/** /**
* Resource tree * Resource tree
* *
* @var ResourceInterface[] * @var Resource\ResourceInterface[]
*/ */
protected $resources = array(); protected $resources = array();


/** /**
* Get all resources * Get all resources
* *
* @return ResourceInterface[] * @return Resource\ResourceInterface[]
*/ */
public function getResources() public function getResources()
{ {
Expand All @@ -26,11 +26,11 @@ public function getResources()
/** /**
* Adds a Resource having an identifier unique to the Assertion * Adds a Resource having an identifier unique to the Assertion
* *
* @param ResourceInterface $resource * @param Resource\ResourceInterface $resource
* @throws Exception\InvalidArgumentException * @throws Exception\InvalidArgumentException
* @return Assertion * @return Assertion
*/ */
public function addResource(ResourceInterface $resource) public function addResource(Resource\ResourceInterface $resource)
{ {
$resourceName = $resource->getName(); $resourceName = $resource->getName();


Expand All @@ -48,7 +48,7 @@ public function addResource(ResourceInterface $resource)
* *
* The $resource parameter can either be a Resource or a Resource identifier. * The $resource parameter can either be a Resource or a Resource identifier.
* *
* @param ResourceInterface|string $resource * @param Resource\ResourceInterface|string $resource
* @return boolean * @return boolean
*/ */
public function hasResource($resource) public function hasResource($resource)
Expand All @@ -63,9 +63,9 @@ public function hasResource($resource)
* *
* The $resource parameter can either be a Resource or a Resource identifier. * The $resource parameter can either be a Resource or a Resource identifier.
* *
* @param ResourceInterface|string $resource * @param Resource\ResourceInterface|string $resource
* @throws Exception\InvalidArgumentException * @throws Exception\InvalidArgumentException
* @return ResourceInterface * @return Resource\ResourceInterface
*/ */
public function getResource($resource) public function getResource($resource)
{ {
Expand All @@ -83,7 +83,7 @@ public function getResource($resource)
* *
* The $resource parameter can either be a Resource or a Resource identifier. * The $resource parameter can either be a Resource or a Resource identifier.
* *
* @param ResourceInterface|string $resource * @param Resource\ResourceInterface|string $resource
* @throws Exception\InvalidArgumentException * @throws Exception\InvalidArgumentException
* @return Assertion * @return Assertion
*/ */
Expand All @@ -103,7 +103,7 @@ public function removeResource($resource)
/** /**
* Returns false if and only if the Resource has deny to the $privilege * Returns false if and only if the Resource has deny to the $privilege
* *
* @param ResourceInterface|string $resource * @param Resource\ResourceInterface|string $resource
* @param string $privilege * @param string $privilege
* @param mixed $arg1 [optional] * @param mixed $arg1 [optional]
* @param mixed $arg2 [optional] * @param mixed $arg2 [optional]
Expand Down Expand Up @@ -133,12 +133,12 @@ public function assert($resource, $privilege, $arg1 = null, $arg2 = null, $argN
* Get Resource name * Get Resource name
* The $resource parameter can either be a Resource or a Resource identifier. * The $resource parameter can either be a Resource or a Resource identifier.
* *
* @param ResourceInterface|string $resource * @param Resource\ResourceInterface|string $resource
* @return string * @return string
*/ */
protected static function getResourceName($resource) protected static function getResourceName($resource)
{ {
if ($resource instanceof ResourceInterface) { if ($resource instanceof Resource\ResourceInterface) {
return $resource->getName(); return $resource->getName();
} }


Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php <?php


namespace GeometriaLab\Permissions\Assertion; namespace GeometriaLab\Permissions\Assertion\Resource;


abstract class AbstractResource implements ResourceInterface abstract class AbstractResource implements ResourceInterface
{ {
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php <?php


namespace GeometriaLab\Permissions\Assertion; namespace GeometriaLab\Permissions\Assertion\Resource;


interface ResourceInterface interface ResourceInterface
{ {
Expand Down
2 changes: 1 addition & 1 deletion library/GeometriaLab/Permissions/Assertion/Service.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private function addResources()
$pathPattern = $this->getResourcesPath() . '*'; $pathPattern = $this->getResourcesPath() . '*';


foreach (ZendGlob::glob($pathPattern, ZendGlob::GLOB_BRACE) as $file) { foreach (ZendGlob::glob($pathPattern, ZendGlob::GLOB_BRACE) as $file) {
/* @var \GeometriaLab\Permissions\Assertion\ResourceInterface $resource */ /* @var \GeometriaLab\Permissions\Assertion\Resource\ResourceInterface $resource */
$resourceName = ucfirst(pathinfo($file, PATHINFO_FILENAME)); $resourceName = ucfirst(pathinfo($file, PATHINFO_FILENAME));
$resourceClassName = $namespace . '\\' . $resourceName; $resourceClassName = $namespace . '\\' . $resourceName;


Expand Down
119 changes: 119 additions & 0 deletions library/GeometriaLab/Permissions/Roles/AbstractRoles.php
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;
}
}
15 changes: 15 additions & 0 deletions library/GeometriaLab/Permissions/Roles/ResourceRoles.php
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
{

}
Loading

0 comments on commit 739bd4c

Please sign in to comment.