black/yo-php

A Yo! client for PHP

Installs: 15

Dependents: 1

Stars: 21

Watchers: 3

Forks: 2

Language: PHP

v0.5.0 2014-08-15 20:30 UTC

README

Yo PHP is a Yo client written in PHP. This library is still a work in progress.

Build Status HHVM Status Scrutinizer Code Quality SensioLabsInsight

Installation

The recomanded way to install Yo PHP is through Composer:

{
    "require": {
        "black/yo-php": "@stable"
    }
}

Protip: You should browse the black/yo-php page to choose a stable version to use, avoid the @stable meta constraint.

Yotip: You want to know when yo-php is updated? Add YOPHPCLIENT \o/!

Usage

yoAll nutshell:

The yoAll method will send a yo to all your friends.

<?php

$yo   = new \Yo\Yo(['token' => 'yourtoken']);
$send = new \Yo\Service\SendYoService($yo->getHttpClient(), $yo->getOptions());
$send->yoAll();
yo nutshell:

The yo method will send a yo to a dedicated username. This username MUST be in uppercase and this is your responsibility.

<?php

$yo   = new \Yo\Yo(['token' => 'yourtoken']);
$send = new \Yo\Service\SendYoService($yo->getHttpClient(), $yo->getOptions());
$send->yo('USERNAME');
subscribers_count nutshell:

The subscribersCount method will retrieve the number of your subscribers. This is just a GET request with a json response.

<?php

$yo     = new \Yo\Yo(['token' => 'yourtoken']);
$status = new \Yo\Service\StatusService($yo->getHttpClient(), $yo->getOptions());

$subscribers = $status->subscribersCount();

If you want to convert the json to an array just replace $status->subscribersCount() by $status->subscribersCount()->json()

Send a link:

It is possible to send a link through Yo since 08/15/2014. Just add a link key to the constructor of new Yo() or use $yo->addLink('url://myurl.com');.

<?php

$yo   = new \Yo\Yo(['token' => 'yourtoken', 'link' => 'http://www.desicomments.com/dc/21/50927/50927.gif']);
$send = new \Yo\Service\SendYoService($yo->getHttpClient(), $yo->getOptions());
$send->yoAll();
<?php

$yo   = new \Yo\Yo(['token' => 'yourtoken');
$yo->addLink('http://www.desicomments.com/dc/21/50927/50927.gif');

$send = new \Yo\Service\SendYoService($yo->getHttpClient(), $yo->getOptions());
$send->yoAll();
Send a location:

It is possible to send your location since 10/07/2014. Just add a location key to the constructor of new Yo() or use this code.

$coordinates = new Geo\Coordinates(latitude, longitude);
$yo->addLocation($coordinates);

Warning 1 It is not possible to receive or send link and location at the same time. When you construct a Yo with link and location, link is always overrided to null.

If you use ->add(Location|Link) function, the class will set the other parameter to null. The code is very simple so take your time and look at the src/spec/Yo/YoSpec.php.

Warning 2 Yo api not using a valid format for coordinates. They use ";" instead of "," so be aware of this and don't forget to explode/convert your values (see example below).

Receive a yo:

During the registration process, Yo will ask to if you want to know when an Yo user Yo you. This pingback send you a GET request with the Yo username and location query parameters.

So... You need to create a dedicated controller. For example:

<?php

namespace Yo\Controller;

class YoController
{
    public function yoAction($username, $location = null)
    {
        $yoUser   = new \Yo\Model\YoUser($username);

        if (null !== $location) {
            $location    = explode(";", $location);
            $coordinates = new Geo\Coordinates($location[0], $location[1]);
            $yoUser->addLocation($location);
        }

        $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
        $dispatcher->addSubscriber(new YourSubscriber());

        $yo = new \Yo\Service\ReceiveYoService($dispatcher);
        $yo->receive($yoUser);
    }
}

As you can see, the ReceiveYoService will dispatch an event named yo.receive and getting his information from a YoUser.

I made the choice of create a true model because you maybe want to persist all your Yo friends in a database or anything you want.

A "default" subscriber is located in Yo/Event directory. This YoSubscriber will add a new line in your Monolog logs. If you want to use it, use this sample code (or see the ./tests/Yo/ReceiveYoServiceTest:

<?php

namespace Yo\Controller;

class YoController
{
    public function yoAction($username)
    {
        $yoUser     = new \Yo\Model\YoUser($username);
        $logger     = new Monolog\Logger();
        $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();

        $dispatcher->addSubscriber(new \Yo\Event\YoSubscriber($logger));

        $yo = new \Yo\Service\ReceiveYoService($dispatcher);
        $yo->receive($yoUser);
    }
}

Running the tests

There is no development key for Yo so the only way to pass the tests suite is to replace the fake token and run the tests.

Contributing

See CONTRIBUTING file.

Credits

This README is heavily inspired by Hateoas library by the great @willdurand. This guy needs your PR for the sake of the REST in PHP.

Alexandre "pocky" Balmes alexandre@lablackroom.com. Send me Flattrs if you love my work, buy me gift or hire me!

License

Yo PHP is released under the MIT License. See the bundled LICENSE file for details.