Scalr\Model\Entity\Role::getBehaviorsConflicts PHP Méthode

getBehaviorsConflicts() public static méthode

Checks given set of behaviors on compatibility
public static getBehaviorsConflicts ( array $behaviors ) : array
$behaviors array Array of a Role behaviors
Résultat array Returns an array of behaviors that cannot be used together
    public static function getBehaviorsConflicts(array $behaviors)
    {
        static $overlaps = null;
        if (empty($overlaps)) {
            $overlaps = static::$behaviorsOverlaps;
            foreach (static::$dbBehaviors as $dbBehavior) {
                $overlaps[$dbBehavior] = array_diff(static::$dbBehaviors, (array) $dbBehavior);
            }
        }
        $conflicts = [];
        foreach ($behaviors as $behavior) {
            if (isset($overlaps[$behavior])) {
                $intersect = array_intersect($behaviors, $overlaps[$behavior]);
                if (!empty($intersect)) {
                    $conflicts = array_merge($conflicts, (array) $behavior, $intersect);
                }
            }
        }
        return array_unique($conflicts);
    }

Usage Example

Exemple #1
0
 protected function _builtinAutomation($from, $to, $action)
 {
     switch ($action) {
         case static::ACT_CONVERT_TO_OBJECT:
             /* @var $from Role */
             if ($from->isScalarized) {
                 $to->builtinAutomation = static::behaviorsToData($from->getBehaviors());
             }
             break;
         case static::ACT_CONVERT_TO_ENTITY:
             /* @var $to Role */
             if (!is_array($from->builtinAutomation)) {
                 throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, "Property builtinAutomation must be an array");
             }
             $behaviors = array_unique($from->builtinAutomation);
             if (count($behaviors) != count($from->builtinAutomation)) {
                 throw new ApiErrorException(409, ErrorMessage::ERR_UNICITY_VIOLATION, "Builtin automation list contains duplicates");
             }
             $behaviors = static::behaviorsToEntity($behaviors);
             $conflicts = Role::getBehaviorsConflicts($behaviors);
             if (!empty($conflicts)) {
                 throw new ApiErrorException(409, ErrorMessage::ERR_UNICITY_VIOLATION, sprintf("The following behaviors can not be combined: [%s]", implode(', ', static::behaviorsToData($conflicts))));
             }
             $to->setBehaviors($behaviors);
             break;
         case static::ACT_GET_FILTER_CRITERIA:
             return [['behaviors' => ['$regex' => implode('|', static::behaviorsToEntity((array) $from->builtinAutomation, false))]]];
     }
 }