Giter Club home page Giter Club logo

common's Introduction

AdadgioCommonBundle

Installation

Install with composer

composer require adadgio/common

Table of contents

  1. Curl
  2. Url
  3. ParamResolver
  4. JsonResponse

Curl

To do Curl requests (get or post) and return results

use Adadgio\Common\Http\Curl;


// GET request
$curl = new Curl();
$response = $curl
    ->get('http://example.com', array('query_param_1' => 'query param value'));


// or POST request with params
$curl = new Curl();
$response = $curl
    ->setContentType(Curl::JSON) // other options are JSON|XML|TEXT|FORL_URLENCODED|FORM_MULTIPART
    ->post('http://example.com', array('post_field_1' => 'psot field value'));

// POST request with more options
$curl = new Curl();
$response = $curl
    ->setContentType(Curl::JSON) // other options are JSON|XML|TEXT|FORL_URLENCODED|FORM_MULTIPART
    ->setAuthorizationBasic('Token', 'user', 'pass') // or ->setAuthorization('Basic', base64_encode('user:pass'))
    // or ->setAuthorization('Token', yO3my4To3en)
    ->setCookies(true)
    // ->addHeader('X-Custom-Header', 'CUst0mApiK3y')
    ->verifyHost(false) // or ->addOption(CURLOPT_SSL_VERIFYHOST, false)
    ->verifyPeer(true, "/home/my/server/cacert.pem") // second param is empty but you should set it in php.ini
    ->post('http://example.com', array('post_field_1' => 'psot field value'));

UrlHelper

Help to check if an URL is relative or aboslute, normalise an URL, get protocol, check protocol less...

All methods are commented in UrlHelper.php

ParamResolver

Two methods let to know if value is a valid array or valid integer (>0). If it's not the case, return default value parameter.

use Adadgio\Common\ParamResolver;

$arrayToTest = array(
    'one'    => true,
    'barbar' => 29393,
    'first'  => 'foo',
);

$defaultArray = array(
    'one'     => false,
    'second'  => 'yolo',
);

$array = ParamResolver::toArray($arrayToTest, $defaultArray);
use Adadgio\Common\ParamResolver;

$integer = ParamResolver::toInt(430, 0);

JsonResponse

A custom easy response object handler to respond with JSON data

use Adadgio\Common\JsonResponse;

// $data is an object or an array
$data = array('yes' => 'no', 'bar' => 'foo');

// default response code is 200
$code = 200;

// specific headers, default: array()
$headers = array();

$array = JsonResponse::fire($data, $code, $headers);

Since

This tool return an human date to display, like few some seconds... Default referential is the current date and time, a custom referential can be set in second parameter.

use Adadgio\Common\Moment\Since;
// Examples with referential date: "2016-07-29 12:00:00"

Since::format(new \DateTime('2016-06-29 09:00:00'));
// 09:00

Since::format(new \DateTime('2016-07-28 22:00:00'));
// Yesterday

Since::format(new \DateTime('2016-07-29 12:00:00'));
// Now

Since::format(new \DateTime('2016-07-29 11:59:55'));
// 5 seconds

common's People

Contributors

tibeoh avatar adadgio avatar

Watchers

 avatar Esteban avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.