galactium/space

Galactium Space

v0.4.5 2019-11-12 19:43 UTC

This package is auto-updated.

Last update: 2024-04-04 06:05:35 UTC


README

Codacy Badge

Galactium Space is a light library to provide extended functionality for Galactium projects.

Requirements

  • PHP 7.1 or above
  • Phalcon 3.3

Installation

composer require galactium/space

Components

Translation

New Translation Adapter: NestedArray:

  • implements \JsonSerializable interface:
use Galactium\Space\Translation\Adapter\NestedArray;

$messages = [
    'title' => [
        'message' => 'Welcome'
    ],
];

$adapter = new NestedArray([
    'content' => $messages,
]);

json_encode($adapter->toArray());
  • supports "dot"-navigation:
use Galactium\Space\Translation\Adapter\NestedArray;

$messages = [
    'title' => [
        'message' => 'Welcome'
    ],
];

$adapter = new NestedArray([
    'content' => $messages,
]);

 echo $adapter->_('title.message'); // prints Welocme

Translation Manager

  • Supports fallback language;
  • Supports Loader to load translations
use Galactium\Space\Translation\Loader\File;
use Galactium\Space\Translation\Manager;

$mainLanguage = 'en';
$fallbackLanguage = 'ru';

$translationManager = new Manager(new File('/path/to/messages/dir/'), $mainLanguage, $fallbackLanguage);
  • Supports hierarchically messages directories:
/resources
    /messages
        /en
            /Module
                Common.php
                Edit.php
$mainLanguage = 'en';
$fallbackLanguage = 'ru';

$translationManager = new Manager(new File('/recourses/messages/'), $mainLanguage, $fallbackLanguage);

$translationManager->loadTranslation('Module::Common'); // to load only /recourses/messages/en/Module/Common.php

If you don't need multiple language files in Module directory, you can put to them translation file with the same name:

/resources
    /messages
        /en
            /Module
                Module.php
  • All lodaded translations are returned in single NestedArray adapter:
$translationManager->getLoadedTranslations() // return Galactium\Space\Translation\Adapter\NestedArray

Please note: Translation Manager works only with NestedArray Adapter.

Mail

Mailer wrapper of swiftmailer/swiftmailer.

use Galactium\Space\Mail\Manager;
use Phalcon\Config;

$transport = (new \Swift_SmtpTransport('smtp.service.com', 465, false))
    ->setUsername('user_name')
    ->setPassword('user_password');

$mailer = new \Swift_Mailer($transport);

$manager = new Manager($mailer, $transport, new Config([
    'views_dir' => '/path/to/views/dir/',
    'from' => [ // define a global 'from'
        'email' => 'example@test.com',
        'name' => 'My Name'
    ]
]));
// create your first message: 
$mailManager->message()
            ->to('test@test.com')
            ->subject('Subject')
    	    ->body('Text')
    	    ->send();
// create using a volt template:
$mailManager->message()
            ->view('volt-template', [
                'user' => Users::findFIrst()
            ])
            ->to('test@test.com')
            ->subject('Subject')
    	    ->send();
// you can pass a callback to manipulate the view instance:
$mailManager->message()
            ->view('volt-template', [
                'user' => Users::findFIrst()
            ], function (ViewBaseInterface $view) {
			// code here ...
            })
            ->to('test@test.com')
            ->subject('Subject')
    	    ->send();

You can use {{ _message }} to get access to Message instance in your template. For example, it can be used to insert an image as embed content:

<img src="{{ _message.embed('/path/to/image.png') }}" alt="my image">

Identifier

The identifier has been developed to easily convert a model's key params (like module, namespace, table name, identity field, and it's value) to string. Identifier Manager can generate GUID (Galactium Unique Identifer) in format: module::namespace.class.dotted.params for a model which implements Galactium\Space\Identifier\IdentifiableInterface interface.

class Model implements IdentifiableInterface {
    public const MODULE_NAME = 'Module'
    public $id;
}

class Guids implements IntercatorInterface {
    public $guid;
}

$record = Model::findFirst(1);

$idetifier = (string) $record->identify(); // return 'module::namespace.model.id.1'

$guid = Guids::findFirst(1);

$interactedRecord = $guid->interacte() // same that Model::findFirst(1);

By default Identifer supports only Galactium's class structure.

Errors Manager

Errors Manager provide a simple error handling for an app. It is used to catch all exceptions generated by an app and converting them to beautiful response.

Seo

Seo Component includes:

  • Breadcrumbs

    use Galactium\Space\Seo\Manager;
    
    $seoManager = new Manager();
    
    $seoManager->breadcrumbs()->push('title','/link');
    
    $seoManager->get(); // returns Galactium\Space\Seo\Breadcrumbs\Breadcrumb[]
  • Meta information

    use Galactium\Space\Seo\Manager;
    
    $seoManager = new Manager();
    
    $seoManager->metas()
                ->add(new Titile('Title'))
                ->add(new Description('Description'))
                ->add(new Keywords('Keywords'))
                ->add(new Canonical('/canonical'));
    
    foreach($seoManager->metas() as $meta){
        echo $meta->render();
    }
  • OpenGraph

    use Galactium\Space\Seo\Manager;
    
    $seoManager = new Manager(); 
    
    $seoManager->openGraph()
               ->setType('opengrpah.type')
               ->setTitle('title')
               ->setDescription('description')
               ->if($item->hasImage(), function ($openGraph) use ($item) {
                   $openGraph->setImage($item->getImage()->getSrc());
               })
               ->setUrl('/href');
  • SchemaOrg (Factory for Spatie\SchemaOrg)

You can get access to all components listed above using Galactium\Space\Seo\Manager.

Other improvements

  • \Galactium\Space\Mvc\Model has a special field $append You can easily overwrite or add any attribute to json.
class Model {
    protected $append = [
        'full_name'
    ];
    public $id;
    public $first_name;
    public $last_name;
    
    public function getFullName()
    {
        return "{$this->first_name} {$this->last_name}"; 
    }
    
}
json_encode(Model::findFirst()); //returns all attrubutes and result of getFullName() method.

Please note: For some reasons $append doesn't work in some cases.

License

This library is licensed under the Apache 2.0 License - see the LICENSE.md file for details.