2017 © Pedro Peláez
 

library rt-extends

ZF2 useful tools

image

remithomas/rt-extends

ZF2 useful tools

  • Wednesday, August 27, 2014
  • by remithomas
  • Repository
  • 1 Watchers
  • 15 Stars
  • 2,683 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 4 Forks
  • 1 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

rt-extends Build Status

A list of ZF2 useful tools. To provide some utilities to generate list of languages, Sql query (on duplicate key update), flashmessenger, countries states and more, (*1)


Features / Goals

  • Sql : Db\Sql\DuplicateInsert ON DUPLICATE KEY UPDATE option
  • Validators : Date is later, is Earlier
  • Uri
    • Thumbnail : Get Thumbnail from URI
    • Type : Get the type of the media
    • Meta : Get metadata of the media
  • Useful
    • Location : List of countries, states list, zipcode search
    • I18n : continents list, languages list, Timezones list
    • Data : Fake data Lorem Ipsum generator
    • File : create Zip Archive, unzip archive, get Favicon
    • PHP : sprintf with dynamic variables
  • View\Helper : extended Flash messenger (sub-message and messages are translated)
    • Date : CountDown
    • BodyClasses : manage your body CSS classes from controller
  • Snippets : create basic CSRF quickly

Ask for contributions

Some ideas to implement to this useful code ? Or ups some errors appear, (*2)

Requirements

# Installation

How to install ?

Using composer.json

{
    "name": "zendframework/skeleton-application",
    "description": "Skeleton Application for ZF2",
    "license": "BSD-3-Clause",
    "keywords": [
        "framework",
        "zf2"
    ],
    "minimum-stability": "dev",
    "homepage": "http://framework.zend.com/",
    "require": {
        "php": ">=5.3.3",
        "zendframework/zendframework": "dev-master",
        "remithomas/rt-extends": "dev-master"
    }
}

Activate the module :

application.config.php, (*3)

<?php
return array(
    'modules' => array(
        'Application',
        'RtExtends',
    )
);
?>

# Examples

Db\Sql

Sql class including DuplicateInsert.php, (*4)

$value = array(
    'user_id' => 2,
    'value' => 'myvalue'
);

$sql = new \RtExtends\Db\Sql($adapter, 'mytable');

$DuplicateInsert = $sql->duplicateInsert();
$DuplicateInsert->values($values);

$sqlString = $sql->getSqlStringForSqlObject($DuplicateInsert);
$adapter->query($sqlString, Adapter::QUERY_MODE_EXECUTE);

Db\Sql\DuplicateInsert

 2,
    'value' => 'myvalue'
);
            
$DuplicateInsert = new RtExtends\Db\Sql\DuplicateInsert("user");
$DuplicateInsert->values($value);

$statment = $this->dbAdapter->createStatement(); 
$DuplicateInsert->prepareStatement($this->dbAdapter, $statment); 
$statment->execute(); 
?>

the above code generates this query, (*5)

INSERT INTO `user` (`user_id`, `value`) 
VALUES (2, 'myvalue')
  ON DUPLICATE KEY UPDATE `user_id`=VALUES(`user_id`), `value`=VALUES(`value`);

Validator\Date\IsLater

public function getInputFilterSpecification()
{
    return array(
        'datestart' => array(
            'required' => true,
            'validators' => array(
                array(
                    'name' => "RtExtends\Validator\Date\IsLater",
                    'options' => array(
                        'min' => date ("d F Y - H:i", mktime()),
                        'format' => 'd F Y - H:i',
                        'timezone' => 'Europe/London'
                    )
                )
            ),
        ),
    );
}

Validator\Date\IsEarlier

public function getInputFilterSpecification()
{
    return array(
        'dateend' => array(
            'required' => true,
            'validators' => array(
                array(
                    'name' => "RtExtends\Validator\Date\IsEarlier",
                    'options' => array(
                        'max' => date ("d F Y - H:i", mktime()),
                        'format' => 'd F Y - H:i',
                        'timezone' => 'Europe/London'
                    )
                )
            ),
        ),
    );
}

Uri

Uri\Thumb

Get list of thumbnail of an URI, (*6)

$uri = "http://www.dailymotion.com/video/x9003r_pac-man-remi-gaillard_fun";
$uri = "http://www.youtube.com/watch?v=1VVkIOxRcX0"; // youtube
$thumbUri = new \RtExtends\Uri\Thumb();
var_dump($thumbUri->getThumbs($uri));

// limit the number of thumbnails
var_dump($thumbUri->getThumbs($uri, 2));

Get one thumbnail of an URI, (*7)

$uri = "http://framework.zend.com/blog/"; 
$thumbUri = new \RtExtends\Uri\Thumb();
var_dump($thumbUri->getThumb($uri));

Uri\Type

Get the type of the media, (*8)

$typeUri = new \RtExtends\Uri\Type();
$typeUri->getType("http://www.youtube.com/watch?v=1VVkIOxRcX0"); // return youtube

Uri\MetaData

Get the meta of the media. All URI gets title and description, (*9)

$metadataUri = new \RtExtends\Uri\MetaData();
$metadataUri->getMetaData("http://www.youtube.com/watch?v=1VVkIOxRcX0"); 
/*
return array(
    'title' => 'media title',
    'description' => 'media description',
    // ...
)
*/

Useful\I18n\Languages

List of languages, (*10)

var_dump(RtExtends\Useful\I18n\Languages::getSimpleCodeLanguages());
// array("fr"=>"Français","en"=>"English",'pt'=>'Português',....)
var_dump(RtExtends\Useful\I18n\Languages::getLanguages());
// array('lv_LV'=>'Latvija - Latviešu','en_LB'=>'Lebanon - English','lt_LT'=>'Lietuva - Lietuvių','fr_LU'=>'Luxembourg - Français',,....)

Useful\Location

List of countries (array returned), (*11)

\RtExtends\Useful\Location\Countries::getCountries();

Zipcode, (*12)

\RtExtends\Useful\Location\Ziptastic::dataLocation("US", "33330");
/*
Return a stdClass object
object(stdClass)#748 (3) {
  ["city"] => string(15) "Fort Lauderdale"
  ["state"] => string(7) "Florida"
  ["country"] => string(2) "US"
}
*/

States of countries (DE,FR,US are available), (*13)

\RtExtends\Useful\Location\Country\Us::states();
\RtExtends\Useful\Location\Country\Us::statesFIPS(); // FIPS codes
\RtExtends\Useful\Location\Country\Fr::states();
\RtExtends\Useful\Location\Country\De::states();

Counties of countries (FR,US are available), (*14)

\RtExtends\Useful\Location\Country\Fr::counties();
\RtExtends\Useful\Location\Country\Fr::countiesStructured(); // by states
\RtExtends\Useful\Location\Country\Us::counties();
\RtExtends\Useful\Location\Country\Us::countiesStructured(); // by states

Useful\File\Zip

Create zip file, (*15)

<?php
$files=array('file1.jpg', 'file2.jpg', 'file3.gif');
/**
 *
 * @param array $files
 * @param string $destination
 * @param bool $overwrite
 * @return boolean 
 */
RtExtends\Useful\File\Zip::createZip($files, 'path/destination/myzipfile.zip', true); 
?> 

Unzip archive, (*16)

<?php
RtExtends\Useful\File\Zip::unzip('path/to/archive', 'path/destination'); 
?> 

Useful\Data\Fake

Lorem Ipsum : Paragraph

Generate paragraphs, (*17)

) with class 'lead'
echo \RtExtends\Useful\Data\Fake::getParagraphLoremIpsum(3,"p",array("class"=>"lead")); 

// generate 2 divs (element 
) with class 'alert alert-info' echo \RtExtends\Useful\Data\Fake::getParagraphLoremIpsum(2,"div",array("class"=>"alert alert-info")); ?>

If you don't need to get a list of random paragraphs, just do like that, (*18)

echo \RtExtends\Useful\Data\Fake::getParagraphLoremIpsum(2,"div",array("class"=>"alert alert-info"),false); 
?>

Lorem Ipsum : Words

// simple use : 10 words
echo \RtExtends\Useful\Data\Fake::getWordLoremIpsum(10);

// include in tag (ex: <p class='lead>>)
echo \RtExtends\Useful\Data\Fake::getWordLoremIpsum(10,"p",array("class"=>"lead"),"!");

// no random option
echo \RtExtends\Useful\Data\Fake::getWordLoremIpsum(10,"p",array("class"=>"lead"),"!", false);

// no random option but a special line
echo \RtExtends\Useful\Data\Fake::getWordLoremIpsum(10,"p",array("class"=>"lead"),"!", 3);

Useful\Php\String

$data = array("this", "cool");
echo RtExtends\Useful\Php\String::sprintfArray("%s is %s", $data);

$data = array(
    "otherway" => "Or",
    "second" => "like that"
);
echo RtExtends\Useful\Php\String::sprintfArray("%(otherway)s maybe %(second)s !", $data);

View\Helper\ExtendedFlashMessenger

In your layout

echo $this->extendedFlashMessenger(true); // true is to get also current messages

In your controller

// Success !
$flashMessage = new FlashMessage();
$flashMessage->setTitle("bravo");
$flashMessage->setMessages("Yes you did");

$this->flashmessenger()->addSuccessMessage($flashMessage);

// Error !
$flashMessage = new FlashMessage();
$flashMessage->setTitle("Ups");
$flashMessage->setMessages("This is wrong");

$this->flashmessenger()->addErrorMessage($flashMessage);

With variable, (*19)


// Ups error ! (with sub messages $subMessage = new FlashMessageSub(); $subMessage->setMessage("Ups %(name)s"); // first message $subMessage->setVariables(array( 'name' => "John Doe" )); $subMessageSecond = new FlashMessageSub(); $subMessageSecond->setMessage("Please check here %(url)s"); // first message $subMessageSecond->setVariables(array( 'url' => "http://php.net" )); $flashMessage = new FlashMessage(); $flashMessage->setTitle("Sorry"); $flashMessage->setMessages(array($subMessage,$subMessageSecond)); $this->flashmessenger()->addErrorMessage($flashMessage);

Date view helper : countdown

date_default_timezone_set('America/Montreal');
$date = new \DateTime('NOW');

// countdown result in ARRAY
$countDown = $this->rtCountDown($date->getTimestamp()+167890, $date->getTimestamp(), true);

// string (is default param)
$countDown = $this->rtCountDown($date->getTimestamp()+167890, $date->getTimestamp(), false);
<<<<<<< HEAD

BodyClasses view helper

Inside your controller, (*20)

$this->BodyClasses()->addClass("my-css-class");
$this->BodyClasses()->addClass(array("my-css-class", "blue-bck"));

Inside your view script, (*21)

?><body class="<?php echo $this->rtBodyClasses()->render(); ?>">
=======
>>>>>>> FETCH_HEAD

Snippets

Some quick snippets, (*22)

Snippets\Form\Element

$form = new Form('my-form');
$form->add(\RtExtends\Snippets\Form\Element\Csrf::getCreateElementArray("crsf", 60*60));
/* generate
$form->add(array(
    'type' => 'Zend\Form\Element\Csrf',
    'name' => 'crsf',
    'options' => array(
        'csrf_options' => array(
            'timeout' => '3600'
        )
    )
));*/

Snippets\Js

Jquery, (*23)


Jquery UI, (*24)


# Thanks

# Todo

  • many other validators
  • Fake data (more tool)
  • some good helpers
  • Currency
  • More countries

The Versions

27/08 2014

dev-master

9999999-dev https://github.com/remithomas/rt-extends

ZF2 useful tools

  Sources   Download

The Requires

 

validator zf2 date data snippets fake insert duplicate

22/05 2013

1.0.1

1.0.1.0 https://github.com/remithomas/rt-extends

ZF2 useful tools

  Sources   Download

The Requires

 

validator zf2 date data snippets fake insert duplicate