Skip to content

Commit

Permalink
freshly generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
gossi committed Jul 21, 2016
1 parent 978b726 commit a3a515c
Show file tree
Hide file tree
Showing 75 changed files with 1,885 additions and 963 deletions.
14 changes: 10 additions & 4 deletions api.json
Original file line number Diff line number Diff line change
Expand Up @@ -10299,6 +10299,12 @@
},
"target_id": {
"type": "integer"
},
"created_at": {
"type": "string"
},
"updated_at": {
"type": "string"
}
}
},
Expand Down Expand Up @@ -10565,16 +10571,16 @@
"type": "integer"
},
"first": {
"type": "integer"
"type": "string"
},
"next": {
"type": "integer"
"type": "string"
},
"previous": {
"type": "integer"
"type": "string"
},
"last": {
"type": "integer"
"type": "string"
}
}
},
Expand Down
12 changes: 0 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,6 @@
"extension-points" : {
"keeko.core.listener" : "schema/listener.json"
},
"extensions" : {
"keeko.core.listener" : [{
"class" : "keeko\\core\\listener\\UserListener",
"method" : "normalizeNames",
"event" : "core.user.pre_save"
}, {
"class" : "keeko\\core\\listener\\UserListener",
"method" : "updateDisplayName",
"event" : "core.user.pre_save"
}
]
},
"actions" : {
"language-paginate" : {
"title" : "Paginates languages",
Expand Down
102 changes: 93 additions & 9 deletions src/domain/UserDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
namespace keeko\core\domain;

use keeko\core\domain\base\UserDomainTrait;
use keeko\core\model\UserQuery;
use keeko\core\model\GroupQuery;
use keeko\core\model\User;
use keeko\core\validator\UserRegistrationValidator;
use keeko\core\validator\UserValidator;
use keeko\framework\foundation\AbstractDomain;
use keeko\framework\preferences\SystemPreferences;
use keeko\framework\validator\ValidatorInterface;

/**
*/
Expand All @@ -13,18 +17,98 @@ class UserDomain extends AbstractDomain {
use UserDomainTrait;

/**
* @param UserQuery $query
* @param mixed $filter
* Returns the validator for users
*
* @param User $user
* @return ValidatorInterface
*/
protected function applyFilter(UserQuery $query, $filter) {
protected function getValidator($user) {
if ($user->isNew()) {
return new UserRegistrationValidator($this->getServiceContainer());
} else {
return new UserValidator($this->getServiceContainer());
}
}

/**
* Returns the validator for users
*
* @return UserValidator
* @param User $user
*/
protected function getValidator() {
return new UserValidator($this->getServiceContainer());
protected function postCreate(User $user) {
$userGroup = GroupQuery::create()->filterByIsDefault(true)->findOne();
if ($userGroup) {
$user->addGroup($userGroup);
$user->save();
}
}

protected function preSave(User $user) {
$this->normalizeNames($user);
$this->updateDisplayName($user);
}

protected function normalizeNames(User $user) {
$prefs = $this->getServiceContainer()->getPreferenceLoader()->getSystemPreferences();

$user->setGivenName($this->normalizeName($user->getGivenName(), $prefs->getUserNormalizeGivenName()));
$user->setFamilyName($this->normalizeName($user->getFamilyName(), $prefs->getUserNormalizeFamilyName()));
}

private function normalizeName($name, $how) {
$name = trim($name);
switch ($how) {
case SystemPreferences::VALUE_NONE:
return $name;

case SystemPreferences::NORMALIZE_TITLECASE:
$words = [];
$ws = [];
preg_match_all('/[^.\s-]+/', $name, $words);
preg_match_all('/[.\s-]+/', $name, $ws);

$len = count($words[0]) - 1;
$result = '';
foreach ($words[0] as $i => $word) {
$result .= ucwords(strtolower($word));

if ($i < $len) {
$result .= $ws[0][$i];
}
}

return $result;

case SystemPreferences::NORMALIZE_UPPERCASE:
return strtoupper($name);

case SystemPreferences::NORMALIZE_LOWERCASE:
return strtolower($name);
}
}

protected function updateDisplayName(User $user) {
$prefs = $this->getServiceContainer()->getPreferenceLoader()->getSystemPreferences();

$choice = $prefs->getUserDisplayName();
if ($prefs->getUserDisplayName() == SystemPreferences::DISPLAY_USERSELECT) {
$choice = $user->getDisplayNameUserSelect();
}

switch ($choice) {
case SystemPreferences::DISPLAY_GIVENFAMILYNAME:
$user->setDisplayName($user->getGivenName() . ' ' . $user->getFamilyName());
break;

case SystemPreferences::DISPLAY_FAMILYGIVENNAME:
$user->setDisplayName($user->getFamilyName() . ' ' . $user->getGivenName());
break;

case SystemPreferences::DISPLAY_NICKNAME:
$user->setDisplayName($user->getNickName());
break;

case SystemPreferences::DISPLAY_USERNAME:
$user->setDisplayName($user->getUserName());
break;
}
}
}
79 changes: 59 additions & 20 deletions src/domain/base/ActionDomainTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use keeko\framework\utils\NameUtils;
use keeko\framework\utils\Parameters;
use phootwork\collection\Map;
use phootwork\lang\Text;

/**
*/
Expand Down Expand Up @@ -110,6 +111,7 @@ public function addGroups($id, $data) {
*/
public function create($data) {
// hydrate
$data = $this->normalize($data);
$serializer = Action::getSerializer();
$model = $serializer->hydrate(new Action(), $data);
$this->hydrateRelationships($model, $data);
Expand All @@ -119,7 +121,7 @@ public function create($data) {
$this->dispatch(ActionEvent::PRE_SAVE, $model, $data);

// validate
$validator = $this->getValidator();
$validator = $this->getValidator($model);
if ($validator !== null && !$validator->validate($model)) {
return new NotValid([
'errors' => $validator->getValidationFailures()
Expand Down Expand Up @@ -160,6 +162,20 @@ public function delete($id) {
return new NotDeleted(['message' => 'Could not delete Action']);
}

/**
* @param array $data
* @return array normalized data
*/
public function normalize(array $data) {
$service = $this->getServiceContainer();
$attribs = isset($data['attributes']) ? $data['attributes'] : [];


$data['attributes'] = $attribs;

return $data;
}

/**
* Returns a paginated result
*
Expand Down Expand Up @@ -188,7 +204,11 @@ public function paginate(Parameters $params) {
}

// paginate
$model = $query->paginate($page, $size);
if ($size == -1) {
$model = $query->findAll();
} else {
$model = $query->paginate($page, $size);
}

// run response
return new Found(['model' => $model]);
Expand Down Expand Up @@ -329,6 +349,7 @@ public function update($id, $data) {
}

// hydrate
$data = $this->normalize($data);
$serializer = Action::getSerializer();
$model = $serializer->hydrate($model, $data);
$this->hydrateRelationships($model, $data);
Expand All @@ -338,7 +359,7 @@ public function update($id, $data) {
$this->dispatch(ActionEvent::PRE_SAVE, $model, $data);

// validate
$validator = $this->getValidator();
$validator = $this->getValidator($model);
if ($validator !== null && !$validator->validate($model)) {
return new NotValid([
'errors' => $validator->getValidationFailures()
Expand Down Expand Up @@ -437,23 +458,41 @@ public function updateGroups($id, $data) {
* @return void
*/
protected function applyFilter($query, $filter) {
foreach ($filter as $column => $value) {
$pos = strpos($column, '.');
if ($pos !== false) {
$rel = NameUtils::toStudlyCase(substr($column, 0, $pos));
$col = substr($column, $pos + 1);
$method = 'use' . $rel . 'Query';
if (method_exists($query, $method)) {
$sub = $query->$method();
$this->applyFilter($sub, [$col => $value]);
$sub->endUse();
}
} else {
$method = 'filterBy' . NameUtils::toStudlyCase($column);
if (method_exists($query, $method)) {
$query->$method($value);
}
}
if (is_array($filter)) {

// filter by fields
if (isset($filter['fields'])) {
foreach ($filter['fields'] as $column => $value) {
$pos = strpos($column, '.');
if ($pos !== false) {
$rel = NameUtils::toStudlyCase(substr($column, 0, $pos));
$col = substr($column, $pos + 1);
$method = 'use' . $rel . 'Query';
if (method_exists($query, $method)) {
$sub = $query->$method();
$this->applyFilter($sub, ['fields' => [$col => $value]]);
$sub->endUse();
}
} else {
$method = 'filterBy' . NameUtils::toStudlyCase($column);
if (method_exists($query, $method)) {
$query->$method($value);
}
}
}
}

// filter by features
if (isset($filter['features'])) {
$features = new Text($filter['features']);
if ($features->contains('random')) {
$query->addAscendingOrderByColumn('RAND()');
}
}
}

if (method_exists($this, 'filter')) {
$this->filter($query, $filter);
}
}

Expand Down
Loading

0 comments on commit a3a515c

Please sign in to comment.