Skip to content

Instantly share code, notes, and snippets.

View christeredvartsen's full-sized avatar

Christer Edvartsen christeredvartsen

View GitHub Profile
@christeredvartsen
christeredvartsen / Dockerfile
Last active November 19, 2016 10:19
Dockerfile to install imbo/behat-api-extension on PHP-7
FROM ubuntu:yakkety
RUN apt-get update && apt-get install -y php7.0 php-mbstring php-dom composer zip
RUN php -v
RUN echo '{"require":{"imbo/behat-api-extension":"^1.0"}}' > composer.json
RUN composer install
@christeredvartsen
christeredvartsen / nested.json
Created October 27, 2016 10:54
Nested markup
{
"value": "foobarbaz",
"markup": [
{
"type": "style:em",
"offset": 0,
"length": 9
},
{
"type": "style:em",
@christeredvartsen
christeredvartsen / FeatureContext.php
Last active September 26, 2016 10:35
BDD-example
<?php
class FeatureContext implements Behat\Behat\Context\ContextInterface {
/**
* @var array
*/
private $articles = [];
/**
* @var array
*/
@christeredvartsen
christeredvartsen / FeatureContext.php
Created June 23, 2016 08:06
Access parameters from behat.yml in the FeatureContext class
<?php
use Behat\Testwork\Hook\Scope\BeforeSuiteScope;
class FeatureContext {
/**
* @BeforeSuite
*/
static public function setup(BeforeSuiteScope $scope) {
var_dump($scope->getSuite()->getSettings()['foo']); // string(3) "bar"
}
}
@christeredvartsen
christeredvartsen / index.php
Created June 3, 2016 09:38
Silex route optional parameters
<?php
require_once __DIR__.'/../vendor/autoload.php';
$app = new Silex\Application();
function handler($a, $b = null, $c = null) {
// ...
}
$app->get('/get/{a}', 'handler');
@christeredvartsen
christeredvartsen / app.php
Created May 25, 2016 06:44
Inject JSON request body as an array into the request object in Silex
<?php
use Silex\Application,
Symfony\Component\HttpFoundation\Request;
$app->before(function(Request $request, Application $app) {
if (($request->isMethod('POST') || $request->isMethod('PUT')) && strpos($request->headers->get('Content-Type'), 'application/json') === 0) {
$request->request->set('data', json_decode($request->getContent(), true) ?: []);
}
});
<?php
use MongoDB\Model\BSONArray,
MongoDB\Model\BSONDocument;
var_dump(
json_encode(['values' => [1, 2]]),
json_encode((new BSONDocument(['values' => new BSONArray([1, 2])]))->getArrayCopy())
);
/*
@christeredvartsen
christeredvartsen / UrlFriendly.php
Created November 9, 2015 20:34
URL friendly strings
<?php
class UrlFriendly {
public function format($string) {
$translate = array(
'Æ' => 'ae', 'Ø' => 'oe', 'Å' => 'aa', 'æ' => 'ae', 'ø' => 'oe',
'å' => 'aa', 'é' => 'e', 'è' => 'e', 'à' => 'a', 'ä' => 'a',
'É' => 'e', 'È' => 'e', 'ö' => 'o', 'Ö' => 'o', 'õ' => 'o',
'Õ' => 'o'
);
@christeredvartsen
christeredvartsen / barcode.php
Last active August 29, 2015 14:11
Render a barcode using Zend\Barcode
<?php
$code = '7090037170053';
$renderer = Zend\Barcode\Barcode::factory(
"EAN-13",
"image",
array(
"text" => $code, // substr($code, 0, -1) seems to work, as the checksum then is added automagically
"withChecksum" => true, // true / false does not seem to matter
), array(
@christeredvartsen
christeredvartsen / update.js
Created February 21, 2014 11:14
Set originalChecksum to the value of the checksum field
db.image.find().forEach(
function (elem) {
db.image.update(
{ _id: elem._id },
{ $set: { originalChecksum: elem.checksum }}
);
}
)