PHP code example of tobento / app-card

1. Go to this page and download the library: Download tobento/app-card library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tobento / app-card example snippets


use Tobento\App\AppFactory;
use Tobento\App\Card\CardsInterface;
use Tobento\App\Card\FilterInputInterface;

// Create the app
$app = (new AppFactory())->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');

// Adding boots:
$app->boot(\Tobento\App\Card\Boot\Card::class);
$app->booting();

// Implemented interfaces:
$cards = $app->get(CardsInterface::class);
$filterInput = $app->get(FilterInputInterface::class);

// Run the app
$app->run();

use Tobento\App\Card\Card;
use Tobento\App\Card\CardInterface;
use Tobento\App\Card\CardsInterface;
use Tobento\App\Card\Factory;
use Tobento\Service\View\ViewInterface;

$app->on(
    CardsInterface::class,
    static function(CardsInterface $cards): void {
        // Using a card factory:
        $cards->add(name: 'foo', card: new Factory\Table(
            rows: [['foo']],
        ));
        
        // Using a callable being autowired:
        $cards->add(
            name: 'foo',
            card: static function (string $name, ViewInterface $view): CardInterface {
                return new Card\Table(
                    view: $view,
                    rows: [['foo']],
                );
            }
        );
        
        // Using a card instance:
        $cards->add(name: 'foo', card: new SomeCard());
        
        // Using a card class string being autowired:
        $cards->add(name: 'foo', card: SomeCard::class);
    }
);

use Tobento\App\Card\CardInterface;

// Returns a new instance with the filtered cards.
$cards = $cards->filter(fn(CardInterface $c): bool => $c->priority() > 1);

// Returns a new instance with the specified group filtered.
$cards = $cards->group(name: 'name');

// Returns a new instance with only the card(s) specified.
$cards = $cards->only('foo', 'bar');

// Returns a new instance except the specified card(s).
$cards = $cards->except('foo', 'bar');

// Returns true if card exists, otherwise false.
$cards->has(name: 'foo');

// Returns a card by name or null if not exists.
$card = $cards->get(name: 'foo');

// Returns all card names.
$cardNames = $cards->names();

// Returns the number of cards (int).
$numberOfCards = $cards->count();

// Returns all cards.
$cards = $cards->all(); // array<string, CardInterface>

// Iterating cards.
foreach($cards as $card) {}

<!DOCTYPE html>
<html lang="<?= $view->esc($view->get('htmlLang', 'en')) 

use Tobento\App\Card\Cards;

class DashboardCards extends Cards
{
    //
}

use Tobento\App\Card\Factory;

$app->on(
    DashboardCards::class,
    static function(DashboardCards $cards): void {
        // Adding cards:
        $cards->add(name: 'foo', card: new Factory\Table(
            rows: ['foo', 'bar'],
        ));
    }
);

use Psr\Container\ContainerInterface;
use Tobento\App\Card\Card;
use Tobento\App\Card\CardFactoryInterface;
use Tobento\App\Card\CardInterface;
use Tobento\Service\View\ViewInterface;

$card = new Card\Group(
    container: $container, // ContainerInterface
    view: $view, // ViewInterface
    
    cards: [], // array<array-key, CardInterface|CardFactoryInterface>
    
    // You may set a title:
    title: 'A title',
    
    // You may set a group to be later filtered:
    group: 'main',
    
    // You may set a priority:
    priority: 100,
);

use Tobento\App\Card\Card;
use Tobento\Service\View\ViewInterface;

$card = new Card\Html(
    view: $view, // ViewInterface
    
    // The html Must be escaped.
    html: 'html', // string|\Stringable
    
    // You may set a title:
    title: 'A title',
    
    // You may set a group to be later filtered:
    group: 'main',
    
    // You may set a priority:
    priority: 100,
);

use Tobento\App\Card\Card;
use Tobento\Service\View\ViewInterface;

$card = new Card\KeyedList(
    view: $view, // ViewInterface
    
    items: [
        'key' => 'value',
    ],
    
    // You may set a title:
    title: 'A title',
    
    // You may set a group to be later filtered:
    group: 'main',
    
    // You may set a priority:
    priority: 100,
);

use Tobento\App\Card\Card;
use Tobento\App\Card\Renderable\Link;
use Tobento\Service\View\ViewInterface;

$card = new Card\Table(
    view: $view, // ViewInterface
    
    // You may add table headers:
    headers: ['Description', 'Action'],
    
    // Add table rows:
    rows: [
        ['Desc', new Link(url: 'Url', label: 'Label', attributes: ['class' => 'button'])],
    ],
    
    // You may set a title:
    title: 'A title',
    
    // You may set a group to be later filtered:
    group: 'main',
    
    // You may set a priority:
    priority: 100,
);

use Tobento\App\Card\CardFactoryInterface;
use Tobento\App\Card\CardInterface;
use Tobento\App\Card\Factory;

$factory = new Factory\Group(
    cards: [], // array<array-key, CardInterface|CardFactoryInterface>
    
    // You may set a title:
    title: 'A title',
    
    // You may set a group to be later filtered:
    group: 'main',
    
    // You may set a priority:
    priority: 100,
);

use Tobento\App\Card\Factory;

$factory = new Factory\Html(
    // The html Must be escaped.
    html: 'html', // string|\Stringable
    
    // You may set a title:
    title: 'A title',
    
    // You may set a group to be later filtered:
    group: 'main',
    
    // You may set a priority:
    priority: 100,
);

use Tobento\App\Card\Factory;

$factory = new Factory\KeyedList(
    items: [
        'key' => 'value',
    ],
    
    // You may set a title:
    title: 'A title',
    
    // You may set a group to be later filtered:
    group: 'main',
    
    // You may set a priority:
    priority: 100,
);

use Tobento\App\Card\Factory;
use Tobento\App\Card\Renderable\Link;

$factory = new Factory\Table(
    // You may add table headers:
    headers: ['Description', 'Action'],
    
    // Add table rows:
    rows: [
        ['Desc', new Link(url: 'Url', label: 'Label', attributes: ['class' => 'button'])],
    ],
    
    // You may set a title:
    title: 'A title',
    
    // You may set a group to be later filtered:
    group: 'main',
    
    // You may set a priority:
    priority: 100,
);

use Tobento\App\Card\CardInterface;
use Tobento\App\Card\FilterInputInterface;
use Tobento\Service\View\ViewInterface;

final class OrdersCard implements CardInterface
{
    public function __construct(
        private ViewInterface $view,
        private OrderRepository $orderRepository,
        private FilterInputInterface $input,
        private string $title = '',
        private string $group = '',
        private int $priority = 0,
    ) {}

    public function group(): string
    {
        return $this->group;
    }

    public function priority(): int
    {
        return $this->priority;
    }
    
    public function render(): string
    {
        return $this->view->render('card/orders', ['card' => $this]);
    }
    
    public function statuses(): array
    {
        return ['paid' => 'Paid', 'unpaid' => 'Unpaid'];
    }
    
    public function activeStatus(): string
    {
        $value = $this->input->get('order.status', '');
        
        // validate input as data may come from user input:
        if (in_array($value, array_keys($this->statuses()))) {
            return $value;
        }
        
        return 'paid';
    }
    
    public function getOrders(): array
    {
        return $this->orderRepository->findAll(
            where: [
                'status' => $this->activeStatus(),
            ],
            limit: 15,
        );
    }
    
    public function title(): string
    {
        return $this->title;
    }
}

<div class="card">
     if ($card->title()) { 
app/config/card.php