Skip to content

Commit

Permalink
Merge pull request #9 from jupitern/development
Browse files Browse the repository at this point in the history
php8 update
  • Loading branch information
jupitern authored Jun 30, 2022
2 parents 582a740 + 6404bbc commit 184dc65
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 141 deletions.
30 changes: 16 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ soon...

## Requirements

PHP 5.6 or higher.
PHP 8.0 or higher.

## Installation

Include jupitern/table in your project, by adding it to your composer.json file.
```php
{
"require": {
"jupitern/table": "1.*"
"jupitern/table": "3.*"
}
}
```
Expand Down Expand Up @@ -70,7 +70,7 @@ Include jupitern/table in your project, by adding it to your composer.json file.
->value(1)
->add()

// add a new column for (associtive array, PDO or ORM) data
// add a new column for (associative array, PDO or ORM) data
->column()
->title('Age')
->value('age')
Expand All @@ -85,7 +85,7 @@ Include jupitern/table in your project, by adding it to your composer.json file.
})
->add()

// onether closure example for adding a column with edit action with no title on <th>
// another closure example for adding a column with edit action with no title on <th>
// this example assumes data associative array
->column()
->value(function ($row) {
Expand Down Expand Up @@ -169,7 +169,8 @@ $filterData = $db->query("SELECT name as val, name FROM persons limit 10")->fetc
->css('td', 'color', 'red')
->css('td', 'width', '20%')
->add()
->column('Phone')
->column()
->title('Phone')
->filter()
->value('phone')
->css('td', 'color', 'red')
Expand All @@ -187,23 +188,24 @@ $filterData = $db->query("SELECT name as val, name FROM persons limit 10")->fetc
Include Jquery, Datatables and Bootstrap (optional) in your html.

<!-- JQUERY -->
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script
src="https://code.jquery.com/jquery-3.6.0.slim.min.js"
integrity="sha256-u7e5khyithlIdTpu22PHhENmPcRdFiHRjhAuHcs05RI="
crossorigin="anonymous"></script>

<!-- DATATABLES -->
<link href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css" rel="stylesheet">
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<link href="//cdn.datatables.net/1.12.1/css/jquery.dataTables.min.css" rel="stylesheet">
<script src="//cdn.datatables.net/1.12.1/js/jquery.dataTables.min.js"></script>

<!-- Bootstrap and Datatables Bootstrap theme (OPTIONAL) -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" rel="stylesheet">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){
$('#demoTable').DataTable();
});
$(document).ready( function () {
$('#demoTable').DataTable();
});
</script>

```
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"issues": "https://github.com/jupitern/table/issues"
},
"require" :{
"php": ">=5.6",
"php": ">=8.0",
"ext-json": "*"
},
"autoload": {
Expand Down
21 changes: 21 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 20 additions & 14 deletions src/Examples/test1.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

require '../../vendor/autoload.php';
require '../Table/Properties.php';
require '../Table/TableColumn.php';
require '../Table/Table.php';

$table = \Jupitern\Table\Table::instance()
->setData([
Expand All @@ -19,7 +21,7 @@
->attr('tr', 'data-id', function($row) {
return 'row-' . $row['id'];
})
->css('tr', 'background-color', 'red')
->css('tr', 'background-color', '#aaa')
->column()
->title('Name')
->value(function ($row) {
Expand All @@ -36,7 +38,7 @@
->css('th', 'color', 'red')
->css('td', 'width', '20%')
->add()
->column('Phone')
->column()
->value('phone')
->css('td', 'color', 'red')
->css('td', 'width', '20%')
Expand All @@ -53,22 +55,26 @@
<html>
<head>
<!-- JQUERY -->
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script
src="https://code.jquery.com/jquery-3.6.0.slim.min.js"
integrity="sha256-u7e5khyithlIdTpu22PHhENmPcRdFiHRjhAuHcs05RI="
crossorigin="anonymous"></script>

<!-- DATATABLES -->
<link href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css" rel="stylesheet">
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<link href="//cdn.datatables.net/1.12.1/css/jquery.dataTables.min.css" rel="stylesheet">
<script src="//cdn.datatables.net/1.12.1/js/jquery.dataTables.min.js"></script>

<!-- Bootstrap and Datatables Bootstrap theme (OPTIONAL) -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" rel="stylesheet">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<!-- Bootstrap and Datatables Bootstrap theme (OPTIONAL) -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" rel="stylesheet">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<script type="text/javascript">
<script type="text/javascript">

$(document).ready(function(){
$('#demoTable').DataTable();
});
$(document).ready( function () {
$('#demoTable').DataTable();
});

</script>
</head>
Expand Down
5 changes: 4 additions & 1 deletion src/Examples/test2.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

require '../../vendor/autoload.php';
require '../Table/Properties.php';
require '../Table/TableColumn.php';
require '../Table/Table.php';


// grab data from db with PDO or in alternative from your framework ORM
$db = new PDO('mysql:host=HOST_NAME;dbname=DB_NAME;charset=utf8', 'DB_USERNAME', 'DB_PASSWORD',
Expand Down
29 changes: 14 additions & 15 deletions src/Table/Properties.php
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
<?php

namespace Jupitern\Table;

class Properties
{
private $properties;
private array $properties = [];

/**
* @param $property
* @param $value
* @param string $property
* @param mixed $value
* @return $this
*/
public function add($property, $value)
{
public function add(string $property, mixed $value): static
{
$this->properties[$property] = $value;

return $this;
}

/**
* @param $properties
* @param array $properties
* @return $this
*/
public function addAll($properties)
{
if (is_array($properties)) {
$this->properties = array_merge((array)$this->properties, $properties);
}
public function addAll(array $properties): static
{
$this->properties = array_merge((array)$this->properties, $properties);

return $this;
}

/**
* @param $elem
* @param $template
* @param string $template
* @param null $context
* @return string
*/
public function render($template, $context = null)
{
public function render(string $template, $context = null): string
{
$output = '';
foreach ((array)$this->properties as $prop => $value) {
if (is_callable($value)) {
Expand Down
75 changes: 39 additions & 36 deletions src/Table/Table.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
<?php

namespace Jupitern\Table;
use JetBrains\PhpStorm\Pure;

class Table
{

public $columns;
public $hasFilters;
public $titlesMode = null;
public array $columns = [];
public bool $hasFilters;
public string $titlesMode = "";

protected $data;
protected $css = [];
protected $attrs = [];
protected mixed $data;
protected array $css = [];
protected array $attrs = [];


protected function __construct()
#[Pure] protected function __construct()
{
$this->css['table'] = new Properties();
$this->css['tr'] = new Properties();
Expand All @@ -27,8 +29,8 @@ protected function __construct()
*
* @return static
*/
public static function instance()
{
public static function instance(): static
{
return new static();
}

Expand All @@ -38,8 +40,8 @@ public static function instance()
* @param $data
* @return $this
*/
public function setData($data)
{
public function setData(mixed $data): static
{
$this->data = self::isJson($data) ? json_decode($data) : $data;

return $this;
Expand All @@ -48,12 +50,12 @@ public function setData($data)
/**
* set titles auto resolution mode from column name. Options: underscore, camelcase
*
* @param $titleMode
* @param string $titleMode
* @return $this
* @throws \Exception
*/
public function setAutoTitles($titleMode)
{
public function setAutoTitles(string $titleMode): static
{
if (!in_array(strtolower($titleMode), ['camelcase', 'underscore'])) {
throw new \Exception("selected titles mode options not found");
}
Expand All @@ -71,8 +73,8 @@ public function setAutoTitles($titleMode)
* @param $value
* @return $this
*/
public function attr($elem, $attr, $value)
{
public function attr($elem, $attr, $value): static
{
$this->attrs[$elem]->add($attr, $value);

return $this;
Expand All @@ -85,23 +87,23 @@ public function attr($elem, $attr, $value)
* @param $attrs
* @return $this
*/
public function attrs($elem, $attrs)
{
public function attrs($elem, $attrs): static
{
$this->attrs[$elem]->addAll($attrs);

return $this;
}

/**
* add html table style
*
* @param $elem
* @param $attr
* @param $value
* @return $this
*/
public function css($elem, $attr, $value)
{
/**
* add html table style
*
* @param string $elem
* @param string $attr
* @param mixed $value
* @return $this
*/
public function css(string $elem, string $attr, mixed $value): static
{
$this->css[$elem]->add($attr, $value);

return $this;
Expand All @@ -112,9 +114,10 @@ public function css($elem, $attr, $value)
*
* @return TableColumn
*/
public function column()
{
public function column(): TableColumn
{
$column = new TableColumn($this);
$column->title('');
$this->columns[] = $column;

return $column;
Expand All @@ -124,10 +127,10 @@ public function column()
* generate table html
*
* @param bool $returnOutput
* @return mixed
*/
public function render($returnOutput = false)
{
* @return string
*/
public function render(bool $returnOutput = false): string
{
$html = '<table {tableAttrs} {tableCss}><thead><tr>{thead}</tr>{theadFilters}</thead>';
$html .= '<tbody>{tbody}</tbody></table>';

Expand Down Expand Up @@ -170,8 +173,8 @@ public function render($returnOutput = false)
}


public static function isJson($string)
{
public static function isJson(mixed $string): bool
{
if (!is_string($string)) return false;
json_decode($string);

Expand Down
Loading

0 comments on commit 184dc65

Please sign in to comment.