/** * Deserializes a Json into readable datas * @param string $string * * @return ArrayCollection */ public static function deserialize($string) { if ($string == "") { throw new \Exception('File is empty.'); } $collection = new ArrayCollection(); $array = json_decode($string, true); foreach ($array as $groupAssoc) { if (!empty($groupAssoc["roles"]) && !empty($groupAssoc["name"])) { $group = new Group(); $group->setName($groupAssoc['name']); foreach ($groupAssoc["roles"] as $role) { $role = Kernel::getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\Role')->findOneByName($role['name']); $group->addRole($role); } $collection[] = $group; } } return $collection; }
/** * @param array $data * @param Group $group * * @return RZ\Roadiz\Core\Entities\Group * @throws \RuntimeException */ protected function editGroup(array $data, Group $group) { if (isset($data['name'])) { $existing = $this->getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\Group')->findOneBy(['name' => $data['name']]); if ($existing !== null && $existing->getId() != $group->getId()) { throw new EntityAlreadyExistsException($this->getTranslator()->trans("group.name.already.exists"), 1); } $group->setName($data['name']); $this->getService('em')->flush(); return $group; } else { throw new \RuntimeException("Group name is not defined", 1); } return null; }