1: <?php
2:
3: namespace Himedia\Padocc\Numbering;
4:
5: /**
6: * Gestion d'une numérotation hiérarchique (1.1, 1.2, ...).
7: *
8: * Un appel à addCounterDivision() suivi d'un appel à removeCounterDivision() est sans effet.
9: * L'inverse est également vrai si l'on n'est pas au niveau le plus haut.
10: * Par exemple : 1.3.7 => 1.3 => 1.3.7
11: *
12: * @author Geoffroy AUBRY <gaubry@hi-media.com>
13: */
14: class Adapter implements NumberingInterface
15: {
16:
17: /**
18: * Compteur hiérarchique.
19: * Mémorise pour la plus haute valeur d'un niveau hiérarchique donné
20: * la plus haute valeur atteinte du sous-niveau.
21: * @var array
22: */
23: private $aCounter;
24:
25: /**
26: * Chaîne intercalée entre chaque niveau hiérarchique.
27: * @var string
28: * @see getNextCounterValue()
29: */
30: private $sSeparator;
31:
32: /**
33: * Niveau hierarchique courant.
34: * @var int
35: */
36: private $iCurrentDivision;
37:
38: /**
39: * Constructeur.
40: *
41: * @param string $sSeparator chaîne intercalée entre chaque niveau hiérarchique
42: * @codeCoverageIgnore
43: */
44: public function __construct ($sSeparator = '.')
45: {
46: $this->sSeparator = $sSeparator;
47: $this->aCounter = array(0);
48: $this->iCurrentDivision = 0;
49: }
50:
51: /**
52: * Retourne la prochaine valeur du compteur hiérarchique en incrémentant le plus bas niveau.
53: * Exemple : 1.1 => 1.2
54: *
55: * @return string prochaine valeur du compteur hiérarchique en intercalant le séparateur entre chaque niveau
56: */
57: public function getNextCounterValue ()
58: {
59: $this->aCounter[$this->iCurrentDivision]++;
60: if (count($this->aCounter) > $this->iCurrentDivision+1) {
61: $this->aCounter = array_slice($this->aCounter, 0, $this->iCurrentDivision+1);
62: }
63: return implode($this->sSeparator, array_slice($this->aCounter, 0, $this->iCurrentDivision+1));
64: }
65:
66: /**
67: * Ajoute une nouvelle division hiérarchique et l'initialise à 0.
68: * Par exemple : 1.1 => 1.1.0
69: *
70: * @return NumberingInterface $this
71: */
72: public function addCounterDivision ()
73: {
74: $this->iCurrentDivision++;
75: if ($this->iCurrentDivision >= count($this->aCounter)) {
76: $this->aCounter[] = 0;
77: }
78: return $this;
79: }
80:
81: /**
82: * Remonte d'un niveau hiérarchique.
83: *
84: * @return NumberingInterface $this
85: */
86: public function removeCounterDivision ()
87: {
88: if ($this->iCurrentDivision > 0) {
89: $this->iCurrentDivision--;
90: }
91: return $this;
92: }
93: }
94: