From d3fa3d9bcd01dfd867174d7632af0291255521ff Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 2 Jul 2016 20:02:27 +0200 Subject: [PATCH] Language fixes+api routes implemented --- Controller.php | 36 ++++++++++++++++++++++++++++++++++++ Models/Department.php | 36 ++++++++++++++++++++++++++++++++++++ Models/Position.php | 42 ++++++++++++++++++++++++++++++++++++++++-- Models/Unit.php | 42 ++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 152 insertions(+), 4 deletions(-) diff --git a/Controller.php b/Controller.php index 307e39b..5edcfd5 100644 --- a/Controller.php +++ b/Controller.php @@ -15,7 +15,11 @@ */ namespace Modules\Organization; +use Modules\Organization\Models\Department; use Modules\Organization\Models\DepartmentMapper; +use Modules\Organization\Models\Position; +use Modules\Organization\Models\PositionMapper; +use Modules\Organization\Models\Unit; use Modules\Organization\Models\UnitMapper; use phpOMS\Message\RequestAbstract; use phpOMS\Message\ResponseAbstract; @@ -257,4 +261,36 @@ class Controller extends ModuleAbstract implements WebInterface return $view; } + public function apiUnitCreate(RequestAbstract $request, ResponseAbstract $response, $data = null) + { + $unit = new Unit(); + $unit->setName($request->getData('name')); + $unit->setDescription($request->getData('desc')); + + UnitMapper::create($unit); + + $response->set('unit', $unit->jsonSerialize()); + } + + public function apiPositionCreate(RequestAbstract $request, ResponseAbstract $response, $data = null) + { + $position = new Position(); + $position->setName($request->getData('name')); + $position->setDescription($request->getData('desc')); + + PositionMapper::create($position); + + $response->set('unit', $position->jsonSerialize()); + } + + public function apiDepartmentCreate(RequestAbstract $request, ResponseAbstract $response, $data = null) + { + $department = new Department(); + $department->setName($request->getData('name')); + $department->setDescription($request->getData('desc')); + + DepartmentMapper::create($department); + + $response->set('unit', $department->jsonSerialize()); + } } diff --git a/Models/Department.php b/Models/Department.php index 35b0dad..1cdcf2d 100644 --- a/Models/Department.php +++ b/Models/Department.php @@ -71,4 +71,40 @@ class Department { $this->description = $desc; } + + public function toArray() : array + { + return [ + 'id' => $this->id, + 'name' => $this->name, + 'description' => $this->description, + 'unit' => $this->unit, + ]; + } + + /** + * Get string representation. + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __toString() + { + return $this->jsonSerialize(); + } + + /** + * Json serialize. + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function jsonSerialize() + { + return json_encode($this->toArray()); + } } diff --git a/Models/Position.php b/Models/Position.php index 41c7968..a37f649 100644 --- a/Models/Position.php +++ b/Models/Position.php @@ -15,7 +15,9 @@ */ namespace Modules\Organization\Models; -class Position +use phpOMS\Contract\ArrayableInterface; + +class Position implements ArrayableInterface, \JsonSerializable { private $id = 0; @@ -45,7 +47,8 @@ class Position return $this->parent; } - public function setParent(int $parent) { + public function setParent(int $parent) + { $this->parent = $parent; } @@ -58,4 +61,39 @@ class Position { $this->description = $desc; } + + public function toArray() : array + { + return [ + 'id' => $this->id, + 'name' => $this->name, + 'description' => $this->description, + ]; + } + + /** + * Get string representation. + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __toString() + { + return $this->jsonSerialize(); + } + + /** + * Json serialize. + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function jsonSerialize() + { + return json_encode($this->toArray()); + } } diff --git a/Models/Unit.php b/Models/Unit.php index 6ded14b..6fe6f18 100644 --- a/Models/Unit.php +++ b/Models/Unit.php @@ -15,7 +15,9 @@ */ namespace Modules\Organization\Models; -class Unit +use phpOMS\Contract\ArrayableInterface; + +class Unit implements ArrayableInterface, \JsonSerializable { private $id = 0; @@ -45,7 +47,8 @@ class Unit return $this->parent; } - public function setParent(int $parent) { + public function setParent(int $parent) + { $this->parent = $parent; } @@ -58,4 +61,39 @@ class Unit { $this->description = $desc; } + + public function toArray() : array + { + return [ + 'id' => $this->id, + 'name' => $this->name, + 'description' => $this->description, + ]; + } + + /** + * Get string representation. + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function __toString() + { + return $this->jsonSerialize(); + } + + /** + * Json serialize. + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function jsonSerialize() + { + return json_encode($this->toArray()); + } }