Skip to content

Commit

Permalink
Merge pull request #1 from tigron/feature/typehints
Browse files Browse the repository at this point in the history
Add type hints, improve quality
  • Loading branch information
LionelLaffineur authored Sep 2, 2024
2 parents 41d0d3c + e829b4d commit 67d9b5e
Showing 1 changed file with 23 additions and 29 deletions.
52 changes: 23 additions & 29 deletions lib/Skeleton/Package/Crud/Web/Module/Crud.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* Module Login
*
Expand All @@ -9,36 +12,32 @@

namespace Skeleton\Package\Crud\Web\Module;

use \Skeleton\Pager\Web\Pager;
use \Skeleton\Application\Web\Module;
use \Skeleton\Application\Web\Template;
use \Skeleton\Core\Http\Session;
use Skeleton\Application\Web\Module;
use Skeleton\Application\Web\Template;
use Skeleton\Core\Http\Session;

abstract class Crud extends Module {

/**
* Login required ?
* Default = yes
*
* @access protected
* @var bool $login_required
*/
protected $login_required = false;
protected bool $login_required = false;

/**
* Template to use
*
* @access protected
* @var string $template
*/
protected $template = '@skeleton-package-crud\content.twig';
protected ?string $template = '@skeleton-package-crud\content.twig';

/**
* Display method
*
* @access public
*/
public function display() {
public function display(): void {
/**
* Initialize the template object
*/
Expand Down Expand Up @@ -85,7 +84,7 @@ public function display() {
$fields = $classname::get_object_fields();

foreach ($fields as $key => $definition) {
if (substr($definition['Field'], -3) == '_id') {
if (substr($definition['Field'], -3) === '_id') {
unset($fields[$key]);
}
}
Expand All @@ -103,7 +102,7 @@ public function display() {
*
* @access public
*/
public function display_edit() {
public function display_edit(): void {
$template = Template::get();

/**
Expand All @@ -117,18 +116,18 @@ public function display_edit() {

$fields = $classname::get_object_fields();
foreach ($fields as $key => $definition) {
if (substr($definition['Field'], -3) == '_id') {
if (substr($definition['Field'], -3) === '_id') {
unset($fields[$key]);
}
if ($definition['Field'] == 'id') {
if ($definition['Field'] === 'id') {
unset($fields[$key]);
}
}
$template->assign('default_fields', $fields);

if (isset($_POST['object'])) {
$object->load_array($_POST['object']);
if (is_callable( [$object, 'validate'] )) {
if (is_callable([$object, 'validate'])) {
$object->validate($errors);
} else {
$errors = [];
Expand All @@ -149,7 +148,7 @@ public function display_edit() {
*
* @access public
*/
public function display_create() {
public function display_create(): void {
if (!$this->is_creatable()) {
throw new \Exception('Cannot create object. Not allowed');
}
Expand All @@ -165,7 +164,7 @@ public function display_create() {
if (isset($_POST['object'])) {
$object = new $classname();
$object->load_array($_POST['object']);
if (is_callable( [$object, 'validate'] )) {
if (is_callable([$object, 'validate'])) {
$object->validate($errors);
} else {
$errors = [];
Expand All @@ -186,10 +185,10 @@ public function display_create() {

$fields = $classname::get_object_fields();
foreach ($fields as $key => $definition) {
if (substr($definition['Field'], -3) == '_id') {
if (substr($definition['Field'], -3) === '_id') {
unset($fields[$key]);
}
if ($definition['Field'] == 'id') {
if ($definition['Field'] === 'id') {
unset($fields[$key]);
}
}
Expand All @@ -200,21 +199,19 @@ public function display_create() {
* Is deletable
*
* @access public
* @param Object $object
* @return bool $deletable
*/
public function is_deletable($object) {
public function is_deletable(object $object): bool {
return true;
}

/**
* Is editable
*
* @access public
* @param Object $object
* @return bool $editable
*/
public function is_editable($object) {
public function is_editable(object $object): bool {
return true;
}

Expand All @@ -224,7 +221,7 @@ public function is_editable($object) {
* @access public
* @return bool $creatable
*/
public function is_creatable() {
public function is_creatable(): bool {
return true;
}

Expand All @@ -233,7 +230,7 @@ public function is_creatable() {
*
* @access public
*/
public function display_delete() {
public function display_delete(): void {
/**
* Get the pager
*/
Expand All @@ -249,9 +246,6 @@ public function display_delete() {
* Get pager
*
* @access public
* @return Skeleton\Pager\Web\Pager $pager
*/
abstract public function get_pager();


abstract public function get_pager(): \Skeleton\Pager\Web\Pager;
}

0 comments on commit 67d9b5e

Please sign in to comment.