├── .gitignore ├── .travis.yml ├── LICENSE ├── Makefile ├── README.md ├── composer.json ├── composer.lock ├── phpunit.xml ├── src └── Hook │ ├── Client.php │ ├── Collection.php │ └── Keys.php └── tests ├── bootstrap.php └── unit ├── CollectionTest.php └── KeysTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | php: 3 | - 5.4 4 | - 5.5 5 | - 5.6 6 | 7 | before_script: 8 | # install hook server 9 | - git clone --depth=1 https://github.com/doubleleft/hook.git 10 | - travis_retry composer self-update 11 | - travis_retry composer install --no-dev --prefer-dist -d hook 12 | - cd ./hook && sh ./.travis/install_server.sh && cd - 13 | 14 | # create default app for testing 15 | - curl -XPOST http://hook.dev/index.php/apps --data '{"app":{"name":"hook-php"}}' > tests/app.json 16 | 17 | # install javascript dependencies and compile dist files 18 | - composer install 19 | 20 | script: 21 | - ./vendor/bin/phpunit 22 | 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Doubleleft 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | APIGEN_PATH = ~/Downloads/apigen 2 | 3 | docs: 4 | mkdir -p ../hook-php-docs 5 | rm -rf ../hook-php-docs/.git 6 | php -d memory_limit=512M ${APIGEN_PATH}/apigen.php --destination ../hook-php-docs --debug \ 7 | --exclude */tests/* \ 8 | --source ./src/ 9 | git init ../hook-php-docs 10 | cd ../hook-php-docs && git remote add origin git@github.com:doubleleft/hook-php.git && git checkout -b gh-pages && git add . && git commit -m "update public documentation" && git push origin gh-pages -f 11 | open ../hook-php-docs/index.html 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | hook-php [![Build status](https://travis-ci.org/doubleleft/hook-php.svg?branch=master)](https://travis-ci.org/doubleleft/hook-php/) 2 | === 3 | 4 | PHP client for [hook](https://github.com/doubleleft/hook). 5 | 6 | Different from JavaScript client, there is no callback. Every request is 7 | synchronous. 8 | 9 | How to use ([API Reference](http://doubleleft.github.io/hook-php)) 10 | --- 11 | 12 | ```php 13 | 1, 16 | 'key' => '006f04b4f723c9920e259a746f9318be', 17 | 'endpoint' => 'http://hook.dev/index.php/' 18 | )); 19 | 20 | $hook->collection('scores')->create(array( 21 | 'name' => 'Endel', 22 | 'score' => 7 23 | )); 24 | ``` 25 | 26 | License 27 | --- 28 | 29 | MIT 30 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "doubleleft/hook-php", 3 | "description": "Doubleleft backend API", 4 | "keywords": ["hook", "client", "php"], 5 | "license": "MIT", 6 | "authors": [{ 7 | "name": "Endel Dreyer", 8 | "email": "edreyer@doubleleft.com" 9 | }], 10 | "autoload": { 11 | "classmap": [ 12 | ], 13 | "psr-0": { 14 | "Hook\\" : "src/" 15 | } 16 | }, 17 | "require": { 18 | "guzzlehttp/guzzle": "4.*" 19 | }, 20 | "require-dev": { 21 | "phpunit/phpunit": "3.7.*" 22 | }, 23 | "scripts": { }, 24 | "minimum-stability": "dev" 25 | } 26 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" 5 | ], 6 | "hash": "b5fc90c4c31af5b1f164a3fda4b1a33d", 7 | "packages": [ 8 | { 9 | "name": "guzzlehttp/guzzle", 10 | "version": "dev-master", 11 | "source": { 12 | "type": "git", 13 | "url": "https://github.com/guzzle/guzzle.git", 14 | "reference": "c57f300af7b288306a3665c89b930ea9b70ba214" 15 | }, 16 | "dist": { 17 | "type": "zip", 18 | "url": "https://api.github.com/repos/guzzle/guzzle/zipball/c57f300af7b288306a3665c89b930ea9b70ba214", 19 | "reference": "c57f300af7b288306a3665c89b930ea9b70ba214", 20 | "shasum": "" 21 | }, 22 | "require": { 23 | "ext-json": "*", 24 | "guzzlehttp/streams": "~1.0", 25 | "php": ">=5.4.0" 26 | }, 27 | "require-dev": { 28 | "ext-curl": "*", 29 | "phpunit/phpunit": "~4.0", 30 | "psr/log": "~1.0" 31 | }, 32 | "suggest": { 33 | "ext-curl": "Guzzle will use specific adapters if cURL is present" 34 | }, 35 | "type": "library", 36 | "extra": { 37 | "branch-alias": { 38 | "dev-master": "4.1.x-dev" 39 | } 40 | }, 41 | "autoload": { 42 | "psr-4": { 43 | "GuzzleHttp\\": "src/" 44 | }, 45 | "files": [ 46 | "src/functions.php" 47 | ] 48 | }, 49 | "notification-url": "https://packagist.org/downloads/", 50 | "license": [ 51 | "MIT" 52 | ], 53 | "authors": [ 54 | { 55 | "name": "Michael Dowling", 56 | "email": "mtdowling@gmail.com", 57 | "homepage": "https://github.com/mtdowling" 58 | } 59 | ], 60 | "description": "Guzzle is a PHP HTTP client library and framework for building RESTful web service clients", 61 | "homepage": "http://guzzlephp.org/", 62 | "keywords": [ 63 | "client", 64 | "curl", 65 | "framework", 66 | "http", 67 | "http client", 68 | "rest", 69 | "web service" 70 | ], 71 | "time": "2014-06-10 17:18:37" 72 | }, 73 | { 74 | "name": "guzzlehttp/streams", 75 | "version": "1.1.0", 76 | "source": { 77 | "type": "git", 78 | "url": "https://github.com/guzzle/streams.git", 79 | "reference": "cf0c8c33ca95cc147efba4c714f630ee44767180" 80 | }, 81 | "dist": { 82 | "type": "zip", 83 | "url": "https://api.github.com/repos/guzzle/streams/zipball/cf0c8c33ca95cc147efba4c714f630ee44767180", 84 | "reference": "cf0c8c33ca95cc147efba4c714f630ee44767180", 85 | "shasum": "" 86 | }, 87 | "require": { 88 | "php": ">=5.4.0" 89 | }, 90 | "require-dev": { 91 | "phpunit/phpunit": "~4.0" 92 | }, 93 | "type": "library", 94 | "extra": { 95 | "branch-alias": { 96 | "dev-master": "1.0.x-dev" 97 | } 98 | }, 99 | "autoload": { 100 | "psr-4": { 101 | "GuzzleHttp\\Stream\\": "src/" 102 | }, 103 | "files": [ 104 | "src/functions.php" 105 | ] 106 | }, 107 | "notification-url": "https://packagist.org/downloads/", 108 | "license": [ 109 | "MIT" 110 | ], 111 | "authors": [ 112 | { 113 | "name": "Michael Dowling", 114 | "email": "mtdowling@gmail.com", 115 | "homepage": "https://github.com/mtdowling" 116 | } 117 | ], 118 | "description": "Provides a simple abstraction over streams of data (Guzzle 4+)", 119 | "homepage": "http://guzzlephp.org/", 120 | "keywords": [ 121 | "Guzzle", 122 | "stream" 123 | ], 124 | "time": "2014-04-03 04:48:24" 125 | } 126 | ], 127 | "packages-dev": [ 128 | { 129 | "name": "phpunit/php-code-coverage", 130 | "version": "1.2.x-dev", 131 | "source": { 132 | "type": "git", 133 | "url": "https://github.com/sebastianbergmann/php-code-coverage.git", 134 | "reference": "61fed79803b6c75d461b909dfac05bbef5cd4f46" 135 | }, 136 | "dist": { 137 | "type": "zip", 138 | "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/61fed79803b6c75d461b909dfac05bbef5cd4f46", 139 | "reference": "61fed79803b6c75d461b909dfac05bbef5cd4f46", 140 | "shasum": "" 141 | }, 142 | "require": { 143 | "php": ">=5.3.3", 144 | "phpunit/php-file-iterator": ">=1.3.0@stable", 145 | "phpunit/php-text-template": ">=1.2.0@stable", 146 | "phpunit/php-token-stream": ">=1.1.3@stable" 147 | }, 148 | "require-dev": { 149 | "phpunit/phpunit": "3.7.*@dev" 150 | }, 151 | "suggest": { 152 | "ext-dom": "*", 153 | "ext-xdebug": ">=2.0.5" 154 | }, 155 | "type": "library", 156 | "extra": { 157 | "branch-alias": { 158 | "dev-master": "1.2.x-dev" 159 | } 160 | }, 161 | "autoload": { 162 | "classmap": [ 163 | "PHP/" 164 | ] 165 | }, 166 | "notification-url": "https://packagist.org/downloads/", 167 | "include-path": [ 168 | "" 169 | ], 170 | "license": [ 171 | "BSD-3-Clause" 172 | ], 173 | "authors": [ 174 | { 175 | "name": "Sebastian Bergmann", 176 | "email": "sb@sebastian-bergmann.de", 177 | "role": "lead" 178 | } 179 | ], 180 | "description": "Library that provides collection, processing, and rendering functionality for PHP code coverage information.", 181 | "homepage": "https://github.com/sebastianbergmann/php-code-coverage", 182 | "keywords": [ 183 | "coverage", 184 | "testing", 185 | "xunit" 186 | ], 187 | "time": "2014-05-19 01:58:57" 188 | }, 189 | { 190 | "name": "phpunit/php-file-iterator", 191 | "version": "1.3.4", 192 | "source": { 193 | "type": "git", 194 | "url": "https://github.com/sebastianbergmann/php-file-iterator.git", 195 | "reference": "acd690379117b042d1c8af1fafd61bde001bf6bb" 196 | }, 197 | "dist": { 198 | "type": "zip", 199 | "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/acd690379117b042d1c8af1fafd61bde001bf6bb", 200 | "reference": "acd690379117b042d1c8af1fafd61bde001bf6bb", 201 | "shasum": "" 202 | }, 203 | "require": { 204 | "php": ">=5.3.3" 205 | }, 206 | "type": "library", 207 | "autoload": { 208 | "classmap": [ 209 | "File/" 210 | ] 211 | }, 212 | "notification-url": "https://packagist.org/downloads/", 213 | "include-path": [ 214 | "" 215 | ], 216 | "license": [ 217 | "BSD-3-Clause" 218 | ], 219 | "authors": [ 220 | { 221 | "name": "Sebastian Bergmann", 222 | "email": "sb@sebastian-bergmann.de", 223 | "role": "lead" 224 | } 225 | ], 226 | "description": "FilterIterator implementation that filters files based on a list of suffixes.", 227 | "homepage": "https://github.com/sebastianbergmann/php-file-iterator/", 228 | "keywords": [ 229 | "filesystem", 230 | "iterator" 231 | ], 232 | "time": "2013-10-10 15:34:57" 233 | }, 234 | { 235 | "name": "phpunit/php-text-template", 236 | "version": "1.2.0", 237 | "source": { 238 | "type": "git", 239 | "url": "https://github.com/sebastianbergmann/php-text-template.git", 240 | "reference": "206dfefc0ffe9cebf65c413e3d0e809c82fbf00a" 241 | }, 242 | "dist": { 243 | "type": "zip", 244 | "url": "https://api.github.com/repos/sebastianbergmann/php-text-template/zipball/206dfefc0ffe9cebf65c413e3d0e809c82fbf00a", 245 | "reference": "206dfefc0ffe9cebf65c413e3d0e809c82fbf00a", 246 | "shasum": "" 247 | }, 248 | "require": { 249 | "php": ">=5.3.3" 250 | }, 251 | "type": "library", 252 | "autoload": { 253 | "classmap": [ 254 | "Text/" 255 | ] 256 | }, 257 | "notification-url": "https://packagist.org/downloads/", 258 | "include-path": [ 259 | "" 260 | ], 261 | "license": [ 262 | "BSD-3-Clause" 263 | ], 264 | "authors": [ 265 | { 266 | "name": "Sebastian Bergmann", 267 | "email": "sb@sebastian-bergmann.de", 268 | "role": "lead" 269 | } 270 | ], 271 | "description": "Simple template engine.", 272 | "homepage": "https://github.com/sebastianbergmann/php-text-template/", 273 | "keywords": [ 274 | "template" 275 | ], 276 | "time": "2014-01-30 17:20:04" 277 | }, 278 | { 279 | "name": "phpunit/php-timer", 280 | "version": "1.0.5", 281 | "source": { 282 | "type": "git", 283 | "url": "https://github.com/sebastianbergmann/php-timer.git", 284 | "reference": "19689d4354b295ee3d8c54b4f42c3efb69cbc17c" 285 | }, 286 | "dist": { 287 | "type": "zip", 288 | "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/19689d4354b295ee3d8c54b4f42c3efb69cbc17c", 289 | "reference": "19689d4354b295ee3d8c54b4f42c3efb69cbc17c", 290 | "shasum": "" 291 | }, 292 | "require": { 293 | "php": ">=5.3.3" 294 | }, 295 | "type": "library", 296 | "autoload": { 297 | "classmap": [ 298 | "PHP/" 299 | ] 300 | }, 301 | "notification-url": "https://packagist.org/downloads/", 302 | "include-path": [ 303 | "" 304 | ], 305 | "license": [ 306 | "BSD-3-Clause" 307 | ], 308 | "authors": [ 309 | { 310 | "name": "Sebastian Bergmann", 311 | "email": "sb@sebastian-bergmann.de", 312 | "role": "lead" 313 | } 314 | ], 315 | "description": "Utility class for timing", 316 | "homepage": "https://github.com/sebastianbergmann/php-timer/", 317 | "keywords": [ 318 | "timer" 319 | ], 320 | "time": "2013-08-02 07:42:54" 321 | }, 322 | { 323 | "name": "phpunit/php-token-stream", 324 | "version": "dev-master", 325 | "source": { 326 | "type": "git", 327 | "url": "https://github.com/sebastianbergmann/php-token-stream.git", 328 | "reference": "955c24b708f8bfd6a05f303217a8dac3a443d531" 329 | }, 330 | "dist": { 331 | "type": "zip", 332 | "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/955c24b708f8bfd6a05f303217a8dac3a443d531", 333 | "reference": "955c24b708f8bfd6a05f303217a8dac3a443d531", 334 | "shasum": "" 335 | }, 336 | "require": { 337 | "ext-tokenizer": "*", 338 | "php": ">=5.3.3" 339 | }, 340 | "type": "library", 341 | "extra": { 342 | "branch-alias": { 343 | "dev-master": "1.2-dev" 344 | } 345 | }, 346 | "autoload": { 347 | "classmap": [ 348 | "PHP/" 349 | ] 350 | }, 351 | "notification-url": "https://packagist.org/downloads/", 352 | "include-path": [ 353 | "" 354 | ], 355 | "license": [ 356 | "BSD-3-Clause" 357 | ], 358 | "authors": [ 359 | { 360 | "name": "Sebastian Bergmann", 361 | "email": "sb@sebastian-bergmann.de", 362 | "role": "lead" 363 | } 364 | ], 365 | "description": "Wrapper around PHP's tokenizer extension.", 366 | "homepage": "https://github.com/sebastianbergmann/php-token-stream/", 367 | "keywords": [ 368 | "tokenizer" 369 | ], 370 | "time": "2014-05-12 05:34:42" 371 | }, 372 | { 373 | "name": "phpunit/phpunit", 374 | "version": "3.7.x-dev", 375 | "source": { 376 | "type": "git", 377 | "url": "https://github.com/sebastianbergmann/phpunit.git", 378 | "reference": "ae6cefd7cc84586a5ef27e04bae11ee940ec63dc" 379 | }, 380 | "dist": { 381 | "type": "zip", 382 | "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/ae6cefd7cc84586a5ef27e04bae11ee940ec63dc", 383 | "reference": "ae6cefd7cc84586a5ef27e04bae11ee940ec63dc", 384 | "shasum": "" 385 | }, 386 | "require": { 387 | "ext-ctype": "*", 388 | "ext-dom": "*", 389 | "ext-json": "*", 390 | "ext-pcre": "*", 391 | "ext-reflection": "*", 392 | "ext-spl": "*", 393 | "php": ">=5.3.3", 394 | "phpunit/php-code-coverage": "~1.2", 395 | "phpunit/php-file-iterator": "~1.3", 396 | "phpunit/php-text-template": "~1.1", 397 | "phpunit/php-timer": "~1.0", 398 | "phpunit/phpunit-mock-objects": "~1.2", 399 | "symfony/yaml": "~2.0" 400 | }, 401 | "require-dev": { 402 | "pear-pear.php.net/pear": "1.9.4" 403 | }, 404 | "suggest": { 405 | "phpunit/php-invoker": "~1.1" 406 | }, 407 | "bin": [ 408 | "composer/bin/phpunit" 409 | ], 410 | "type": "library", 411 | "extra": { 412 | "branch-alias": { 413 | "dev-master": "3.7.x-dev" 414 | } 415 | }, 416 | "autoload": { 417 | "classmap": [ 418 | "PHPUnit/" 419 | ] 420 | }, 421 | "notification-url": "https://packagist.org/downloads/", 422 | "include-path": [ 423 | "", 424 | "../../symfony/yaml/" 425 | ], 426 | "license": [ 427 | "BSD-3-Clause" 428 | ], 429 | "authors": [ 430 | { 431 | "name": "Sebastian Bergmann", 432 | "email": "sebastian@phpunit.de", 433 | "role": "lead" 434 | } 435 | ], 436 | "description": "The PHP Unit Testing framework.", 437 | "homepage": "http://www.phpunit.de/", 438 | "keywords": [ 439 | "phpunit", 440 | "testing", 441 | "xunit" 442 | ], 443 | "time": "2014-04-30 12:24:19" 444 | }, 445 | { 446 | "name": "phpunit/phpunit-mock-objects", 447 | "version": "1.2.x-dev", 448 | "source": { 449 | "type": "git", 450 | "url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git", 451 | "reference": "c39c4511c3b007539eb170c32cbc2af49a07351a" 452 | }, 453 | "dist": { 454 | "type": "zip", 455 | "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/c39c4511c3b007539eb170c32cbc2af49a07351a", 456 | "reference": "c39c4511c3b007539eb170c32cbc2af49a07351a", 457 | "shasum": "" 458 | }, 459 | "require": { 460 | "php": ">=5.3.3", 461 | "phpunit/php-text-template": ">=1.1.1@stable" 462 | }, 463 | "require-dev": { 464 | "phpunit/phpunit": "3.7.*@dev" 465 | }, 466 | "suggest": { 467 | "ext-soap": "*" 468 | }, 469 | "type": "library", 470 | "extra": { 471 | "branch-alias": { 472 | "dev-master": "1.2.x-dev" 473 | } 474 | }, 475 | "autoload": { 476 | "classmap": [ 477 | "PHPUnit/" 478 | ] 479 | }, 480 | "notification-url": "https://packagist.org/downloads/", 481 | "include-path": [ 482 | "" 483 | ], 484 | "license": [ 485 | "BSD-3-Clause" 486 | ], 487 | "authors": [ 488 | { 489 | "name": "Sebastian Bergmann", 490 | "email": "sb@sebastian-bergmann.de", 491 | "role": "lead" 492 | } 493 | ], 494 | "description": "Mock Object library for PHPUnit", 495 | "homepage": "https://github.com/sebastianbergmann/phpunit-mock-objects/", 496 | "keywords": [ 497 | "mock", 498 | "xunit" 499 | ], 500 | "time": "2014-02-16 12:43:56" 501 | }, 502 | { 503 | "name": "symfony/yaml", 504 | "version": "dev-master", 505 | "target-dir": "Symfony/Component/Yaml", 506 | "source": { 507 | "type": "git", 508 | "url": "https://github.com/symfony/Yaml.git", 509 | "reference": "b01d366060f33e18fd98b0008c41a01b0d25c95c" 510 | }, 511 | "dist": { 512 | "type": "zip", 513 | "url": "https://api.github.com/repos/symfony/Yaml/zipball/b01d366060f33e18fd98b0008c41a01b0d25c95c", 514 | "reference": "b01d366060f33e18fd98b0008c41a01b0d25c95c", 515 | "shasum": "" 516 | }, 517 | "require": { 518 | "php": ">=5.3.3" 519 | }, 520 | "type": "library", 521 | "extra": { 522 | "branch-alias": { 523 | "dev-master": "2.6-dev" 524 | } 525 | }, 526 | "autoload": { 527 | "psr-0": { 528 | "Symfony\\Component\\Yaml\\": "" 529 | } 530 | }, 531 | "notification-url": "https://packagist.org/downloads/", 532 | "license": [ 533 | "MIT" 534 | ], 535 | "authors": [ 536 | { 537 | "name": "Fabien Potencier", 538 | "email": "fabien@symfony.com", 539 | "homepage": "http://fabien.potencier.org", 540 | "role": "Lead Developer" 541 | }, 542 | { 543 | "name": "Symfony Community", 544 | "homepage": "http://symfony.com/contributors" 545 | } 546 | ], 547 | "description": "Symfony Yaml Component", 548 | "homepage": "http://symfony.com", 549 | "time": "2014-06-04 06:43:44" 550 | } 551 | ], 552 | "aliases": [ 553 | 554 | ], 555 | "minimum-stability": "dev", 556 | "stability-flags": [ 557 | 558 | ], 559 | "platform": [ 560 | 561 | ], 562 | "platform-dev": [ 563 | 564 | ] 565 | } 566 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | ./tests/unit/ 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/Hook/Client.php: -------------------------------------------------------------------------------- 1 | credentials = $credentials; 40 | $this->keys = new Keys($this); 41 | } 42 | 43 | /** 44 | * collection 45 | * @param string $name 46 | */ 47 | public function collection($name) { 48 | return new Collection(array( 49 | 'name' => $name, 50 | 'client' => $this 51 | )); 52 | } 53 | 54 | /** 55 | * get 56 | * @param mixed $segments 57 | * @param array $headers 58 | */ 59 | public function get($segments, $params = array(), $headers = array()) { 60 | return $this->request('get', $segments, $params, $headers); 61 | } 62 | 63 | /** 64 | * remove 65 | * @param mixed $segments 66 | * @param array $headers 67 | */ 68 | public function remove($segments, $headers = array()) { 69 | return $this->request('delete', $segments, array(), $headers); 70 | } 71 | 72 | /** 73 | * put 74 | * @param mixed $segments 75 | * @param array $data 76 | * @param array $headers 77 | */ 78 | public function put($segments, $data = array(), $headers = array()) { 79 | return $this->request('put', $segments, $data, $headers); 80 | } 81 | 82 | /** 83 | * post 84 | * @param mixed $segments 85 | * @param array $data 86 | * @param array $headers 87 | */ 88 | public function post($segments, $data = array(), $headers = array()) { 89 | return $this->request('post', $segments, $data, $headers); 90 | } 91 | 92 | protected function request($method, $segments, $data = array(), $headers = array()) { 93 | $client = new \GuzzleHttp\Client(); 94 | $method = strtoupper($method); 95 | $body = null; 96 | 97 | if ($method === "GET" && !empty($data)) { 98 | $segments .= '?' . urlencode(json_encode($data)); 99 | 100 | } elseif ($method !== "GET" && !empty($data)) { 101 | $body = json_encode($data); 102 | } 103 | 104 | return $client->{$method}($this->credentials['endpoint'] . $segments, array( 105 | 'headers' => $this->getHeaders(), 106 | 'body' => $body, 107 | 'exceptions' => false 108 | ))->json(); 109 | } 110 | 111 | protected function getHeaders($concat = array()) { 112 | return array_merge(array( 113 | 'Content-Type' => 'application/json', 114 | 'X-App-Id' => $this->credentials['app_id'], 115 | 'X-App-Key' => $this->credentials['key'], 116 | 'User-Agent' => 'hook-php' 117 | ), $concat); 118 | 119 | } 120 | 121 | } 122 | -------------------------------------------------------------------------------- /src/Hook/Collection.php: -------------------------------------------------------------------------------- 1 | name = $options['name']; 62 | $this->client = isset($options['client']) ? $options['client'] : Client::getInstance(); 63 | $this->segments = 'collection/' . $this->name; 64 | $this->reset(); 65 | } 66 | 67 | protected function reset() { 68 | $this->wheres = array(); 69 | $this->options = array(); 70 | $this->ordering = array(); 71 | $this->group = array(); 72 | $this->limit = null; 73 | $this->offset = null; 74 | return $this; 75 | } 76 | 77 | public function create(array $data) { 78 | return $this->client->post($this->segments, $data); 79 | } 80 | 81 | public function where($field, $_operation = null, $_value = null, $operation = 'and') { 82 | $operation = (is_null($_value)) ? "=" : $_operation; 83 | $value = (is_null($_value)) ? $_operation : $_value; 84 | 85 | if (is_array($field)) { 86 | foreach($field as $field => $value) { 87 | if (is_array($value)) { 88 | $operation = $value[0]; 89 | $value = $value[1]; 90 | } 91 | $this->addWhere($field, $operation, $value, $operation); 92 | } 93 | } else { 94 | $this->addWhere($field, $operation, $value, $operation); 95 | } 96 | 97 | return $this; 98 | } 99 | 100 | public function orWhere($field, $_operation = null, $_value = null) { 101 | return $this->where($field, $_operation, $_value, 'or'); 102 | } 103 | 104 | public function get() { 105 | return $this->client->get($this->segments, $this->buildQuery()); 106 | } 107 | 108 | public function find($_id) { 109 | return $this->client->get($this->segments . '/' . $_id, $this->buildQuery()); 110 | } 111 | 112 | public function select() { 113 | $this->options['select'] = func_get_args(); 114 | return $this; 115 | } 116 | 117 | public function with() { 118 | $this->options['with'] = func_get_args(); 119 | return $this; 120 | } 121 | 122 | public function group() { 123 | $this->group = func_get_args(); 124 | return $this; 125 | } 126 | 127 | public function count($field = '*') { 128 | $this->options['aggregation'] = array('method' => 'count', 'field' => $field); 129 | return $this->get(); 130 | } 131 | 132 | public function max($field) { 133 | $this->options['aggregation'] = array('method' => 'max', 'field' => $field); 134 | return $this->get(); 135 | } 136 | 137 | public function min($field) { 138 | $this->options['aggregation'] = array('method' => 'min', 'field' => $field); 139 | return $this->get(); 140 | } 141 | 142 | public function avg($field) { 143 | $this->options['aggregation'] = array('method' => 'avg', 'field' => $field); 144 | return $this->get(); 145 | } 146 | 147 | public function sum($field) { 148 | $this->options['aggregation'] = array('method' => 'sum', 'field' => $field); 149 | return $this->get(); 150 | } 151 | 152 | public function first() { 153 | $this->options['first'] = 1; 154 | return $this->get(); 155 | } 156 | 157 | public function firstOrCreate($data) { 158 | $this->options['first'] = 1; 159 | $this->options['data'] = $data; 160 | return $this->client->post($this->segments, $this->buildQuery()); 161 | } 162 | 163 | public function sort($field, $direction = null) { 164 | if (is_null($direction)) { 165 | $direction = 'asc'; 166 | } else if (is_int($direction)) { 167 | $direction = (intval($direction) === -1) ? 'desc' : 'asc'; 168 | } 169 | $this->ordering[] = array($field, $direction); 170 | return $this; 171 | } 172 | 173 | public function limit($int) { 174 | $this->limit = $int; 175 | return $this; 176 | } 177 | 178 | public function offset($int) { 179 | $this->offset = $int; 180 | return $this; 181 | } 182 | 183 | public function channel($options) { 184 | throw Exception("Not implemented."); 185 | } 186 | 187 | public function remove($_id = null) { 188 | $path = $this->segments; 189 | if (!is_null($_id)) { 190 | $path .= '/' . $_id; 191 | } 192 | return $this->client->remove($path, $this->buildQuery()); 193 | } 194 | 195 | public function update($_id, array $data = null) { 196 | return $this->client->post($this->segments . '/' . $_id, $data); 197 | } 198 | 199 | public function increment($field, $value) { 200 | $this->options['operation'] = array('method' => 'increment', 'field' => $field, 'value' => $value); 201 | return $this->client->put($this->segments, $this->buildQuery()); 202 | } 203 | 204 | public function decrement($field, $value) { 205 | $this->options['operation'] = array('method' => 'decrement', 'field' => $field, 'value' => $value); 206 | return $this->client->put($this->segments, $this->buildQuery()); 207 | } 208 | 209 | public function updateAll(array $data) { 210 | $this->options['data'] = $data; 211 | return $this->client->put($this->segments, $this->buildQuery()); 212 | } 213 | 214 | protected function addWhere($field, $operation, $value) { 215 | $this->wheres[] = array($field, strtolower($operation), $value); 216 | return $this; 217 | } 218 | 219 | protected function buildQuery() { 220 | $query = array(); 221 | 222 | // apply limit / offset 223 | if ($this->limit !== null) { $query['limit'] = $this->limit; } 224 | if ($this->offset !== null) { $query['offset'] = $this->offset; } 225 | 226 | // apply wheres 227 | if (count($this->wheres) > 0) { 228 | $query['q'] = $this->wheres; 229 | } 230 | 231 | // apply ordering 232 | if (count($this->ordering) > 0) { 233 | $query['s'] = $this->ordering; 234 | } 235 | 236 | // apply group 237 | if (count($this->group) > 0) { 238 | $query['g'] = $this->group; 239 | } 240 | 241 | $shortnames = array( 242 | 'paginate' => 'p', 243 | 'first' => 'f', 244 | 'aggregation' => 'aggr', 245 | 'operation' => 'op', 246 | 'data' => 'data', 247 | 'with' => 'with', 248 | 'select' => 'select' 249 | ); 250 | 251 | foreach($shortnames as $field => $shortname) { 252 | if (isset($this->options[$field])) { 253 | $query[$shortnames[$field]] = $this->options[$field]; 254 | } 255 | } 256 | 257 | // clear wheres/ordering for future calls 258 | $this->reset(); 259 | 260 | return $query; 261 | } 262 | } 263 | -------------------------------------------------------------------------------- /src/Hook/Keys.php: -------------------------------------------------------------------------------- 1 | client = $client; 9 | } 10 | 11 | public function get($key) { 12 | return $this->client->get('key/' . $key); 13 | } 14 | 15 | public function set($key, $value) { 16 | return $this->client->post('key/' . $key, array('value' => $value)); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 | client = Hook\Client::configure(array( 14 | 'app_id' => $app_key['app_id'], 15 | 'key' => $app_key['key'], 16 | 'endpoint' => 'http://hook.dev/index.php/' 17 | )); 18 | parent::setUp(); 19 | } 20 | 21 | public function tearDown() { 22 | parent::tearDown(); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /tests/unit/CollectionTest.php: -------------------------------------------------------------------------------- 1 | client->collection('scores'); } catch(\Exception $e) {} 9 | } 10 | 11 | public function testGeneralMethods() { 12 | 13 | // create 14 | $one = $this->client->collection('scores')->create(array( 15 | 'name' => "One", 16 | 'score' => 100 17 | )); 18 | $this->assertTrue($one['name'] == "One"); 19 | $this->assertTrue($one['score'] == 100); 20 | 21 | $two = $this->client->collection('scores')->create(array('name' => "Two", 'score' => 200)); 22 | $three = $this->client->collection('scores')->create(array('name' => "Three", 'score' => 50)); 23 | $four = $this->client->collection('scores')->create(array('name' => "Four", 'score' => 25)); 24 | $five = $this->client->collection('scores')->create(array('name' => "Five", 'score' => 125)); 25 | $six = $this->client->collection('scores')->create(array('name' => "Six", 'score' => 75)); 26 | 27 | // filters 28 | $filtering = $this->client->collection('scores')->sort('name', 1)->where('name', 'Two')->get(); 29 | $this->assertTrue(count($filtering) == 1); 30 | $this->assertTrue($filtering[0]['name'] == 'Two'); 31 | 32 | $count = $this->client->collection('scores')->where('score', '<', '100')->count(); 33 | $this->assertTrue($count == 3); 34 | 35 | $count = $this->client->collection('scores')->where('score', '<=', '100')->count(); 36 | $this->assertTrue($count == 4); 37 | 38 | $this->client->collection('scores')->sort('score', -1)->updateAll(array('name' => "Top")); 39 | $updated = $this->client->collection('scores')->update($six['_id'], array('name' => "Six updated")); 40 | $this->assertTrue($updated['name'] == "Six updated"); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /tests/unit/KeysTest.php: -------------------------------------------------------------------------------- 1 | client->keys->get('doesnt_exists'); 8 | $this->assertTrue($doesnt_exists === null); 9 | 10 | // get/set 11 | $this->client->keys->set('float', 5.5); 12 | $this->assertTrue($this->client->keys->get('float') === 5.5); 13 | $this->client->keys->set('integer', 500); 14 | $this->assertTrue($this->client->keys->get('integer') === 500); 15 | $this->client->keys->set('string', "Hello world!"); 16 | $this->assertTrue($this->client->keys->get('string') === 'Hello world!'); 17 | } 18 | 19 | } 20 | 21 | --------------------------------------------------------------------------------