Skip to content

Instantly share code, notes, and snippets.

@real34
Last active October 10, 2015 09:57
Show Gist options
  • Save real34/3672234 to your computer and use it in GitHub Desktop.
Save real34/3672234 to your computer and use it in GitHub Desktop.
Illustration basique de tri d'un tableau de périodes et de fusion des périodes successives

Current errors when trying to include this Gist as a micropackage as follow:

[...]
 "repositories": [
        {"url": "https://gist.github.com/3672234.git", "type": "vcs"}
    ],
    "require": {
        "real34/agist": "dev-master"
    }
[...]

Error :

composer update -vvv                                    
Reading ./composer.json
Executing command (CWD): git describe --exact-match --tags
Executing command (CWD): git branch --no-color --no-abbrev -v
Executing command (CWD): hg branch
Loading composer repositories with package information
Executing command (CWD): git clone --mirror 'https://gist.github.com/3672234.git' '/home/pierre/.composer/cache/vcs/https---gist.github.com-3672234.git/'
Executing command (/home/pierre/.composer/cache/vcs/https---gist.github.com-3672234.git/): git show-ref --tags
Executing command (/home/pierre/.composer/cache/vcs/https---gist.github.com-3672234.git/): git branch --no-color --no-abbrev -v
Executing command (/home/pierre/.composer/cache/vcs/https---gist.github.com-3672234.git/): git branch --no-color
Executing command (/home/pierre/.composer/cache/vcs/https---gist.github.com-3672234.git/): git show 'master':composer.json
Executing command (/home/pierre/.composer/cache/vcs/https---gist.github.com-3672234.git/): git log -1 --format=%at 'master'


                                                                                                                                   
  [Composer\Repository\InvalidRepositoryException]                                                                                 
  No valid composer.json was found in any branch or tag of https://gist.github.com/3672234.git, could not load a package from it. 
{
"name": "real34/agist",
"description": "Sample composer file to proving hosting micropackages on Gists works",
"license": "MIT",
"autoload": {
"classmap": ["."]
}
}
<?php
$periodes = array(
array('Period' => array(
'start_date' => '2012-09-04',
'end_date' => '2012-09-06',
'type_day' => 'unavailable',
)),
array('Period' => array(
'start_date' => '2012-09-07',
'end_date' => '2012-09-10',
'type_day' => 'unavailable',
)),
array('Period' => array(
'start_date' => '2012-09-13',
'end_date' => '2012-09-16',
'type_day' => 'booked',
)),
// Pour illustrer le tri du tableau
array('Period' => array(
'start_date' => '2012-09-10',
'end_date' => '2012-09-13',
'type_day' => 'unavailable',
)),
array('Period' => array(
'start_date' => '2012-09-16',
'end_date' => '2012-09-21',
'type_day' => 'booked',
)),
array('Period' => array(
'start_date' => '2012-10-02',
'end_date' => '2012-10-10',
'type_day' => 'unavailable',
)),
array('Period' => array(
'start_date' => '2012-10-10',
'end_date' => '2012-10-14',
'type_day' => 'unavailable',
)),
);
$expected = array(
array('Period' => array(
'start_date' => '2012-09-04',
'end_date' => '2012-09-06',
'type_day' => 'unavailable',
)),
array('Period' => array(
'start_date' => '2012-09-07',
'end_date' => '2012-09-21',
'type_day' => 'booked',
)),
array('Period' => array(
'start_date' => '2012-10-02',
'end_date' => '2012-10-14',
'type_day' => 'unavailable',
)),
);
// Fonction de comparaison des périodes
function cmp($periodA, $periodB) {
$a = strtotime($periodA['Period']['start_date']);
$b = strtotime($periodB['Period']['start_date']);
return $a - $b;
}
// 1 trier le tableau par ordre de dates 'start_date' (si besoin)
uasort($periodes, 'cmp');
// 2 parcourir le tableau $periodes et merger les périodes dans le nouveau tableau $result
$mergedPeriods = array_reduce($periodes, function($result, $item) {
$latestPeriod = array_pop($result);
if ($item['Period']['start_date'] == $latestPeriod['Period']['end_date']) {
$item['Period']['start_date'] = $latestPeriod['Period']['start_date'];
} elseif (!empty($latestPeriod)) {
array_push($result, $latestPeriod);
}
array_push($result, $item);
return $result;
}, array());
if ($mergedPeriods == $expected) {
echo 'OK';
} else {
echo 'KO';
var_dump($mergedPeriods);
var_dump($expected);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment