Overview

Namespaces

  • InfoContact
    • IcFwk
      • Library
      • Router
  • PHP

Classes

  • Application
  • ApplicationComponent
  • Collection
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  • Download
  1: <?php
  2: 
  3: namespace InfoContact\IcFwk\Library;
  4: 
  5: /**
  6:  * Classe Collection
  7:  *
  8:  * Permet la gestion de tableaux en tant qu'objets pour en simplifier l'utilisation.
  9:  */
 10: class Collection implements \IteratorAggregate, \ArrayAccess {
 11: 
 12:     /** @var array $items Tableau de données */
 13:     private $items;
 14: 
 15:     /**
 16:      * Permet la création de la collection
 17:      * @param array $items Tableau de données
 18:      */
 19:     public function __construct(array $items) {
 20:         $this->items = $items;
 21:     }
 22:     
 23:     /**
 24:      * Récupère la valeur pour une clé $key
 25:      * <p>
 26:      * La méthode est chaînable, on peut accèder aux sous clé par chainage ou en une seule fois.
 27:      * Ex :<br>
 28:      * <pre>
 29:      * $maCollection->get("key")->("subkey");
 30:      * $maCollection->get("key.subkey");
 31:      * </pre>
 32:      * </p>
 33:      * @param string $key Clé dont on veut récupérer la valeur
 34:      * @return mixed Valeur de la clé (null si n'existe pas)
 35:      */
 36:     public function get($key) {
 37:         $index = explode(".", $key);
 38:         return $this->getValue($index, $this->items);
 39:     }
 40: 
 41:     /**
 42:      * Permet de récupérer la valeur de manière récursive via chainabilité
 43:      * @param array $indexes Tableau des clés successives
 44:      * @param array $value Tableau de recherche
 45:      * @return mixed Valeur de la clé (null si n'existe pas)
 46:      */
 47:     private function getValue(array $indexes, $value) {
 48:         $key = array_shift($indexes);
 49:         if(empty($indexes)) {
 50:             if(!array_key_exists($key, $value)) {
 51:                 return NULL;
 52:             }
 53:             $result = $value[$key];
 54:             if(is_array($result)) {
 55:                 return new \InfoContact\IcFwk\Library\Collection($result);
 56:             }
 57:             return $result;
 58:         }
 59:         return $this->getValue($indexes, $value[$key]);
 60:     }
 61: 
 62:     /**
 63:      * 
 64:      * @param string $key
 65:      * @param mixed $value
 66:      */
 67:     public function set($key, $value) {
 68:         $this->items[$key] = $value;
 69:     }
 70: 
 71:     /**
 72:      * 
 73:      * @param string $key
 74:      * @return bool 
 75:      */
 76:     public function has($key) {
 77:         return array_key_exists($key, $this->items);
 78:     }
 79: 
 80:     /**
 81:      * 
 82:      * @param string $key
 83:      * @param string $value
 84:      * @return \InfoContact\IcFwk\Library\Collection
 85:      */
 86:     public function lists($key, $value) {
 87:         $results = [];
 88:         foreach ($this->items as $item) {
 89:             $results[$item[$key]] = $item[$value];
 90:         }
 91:         return new \InfoContact\IcFwk\Library\Collection($results);
 92:     }
 93: 
 94:     /**
 95:      * 
 96:      * @param string $key
 97:      * @return \InfoContact\IcFwk\Library\Collection
 98:      */
 99:     public function extract($key) {
100:         $results = [];
101:         foreach ($this->items as $item) {
102:             $results[] = $item[$key];
103:         }
104:         return new \InfoContact\IcFwk\Library\Collection($results);
105:     }
106: 
107:     /**
108:      * 
109:      * @param string $glue
110:      * @return string
111:      */
112:     public function join($glue) {
113:         return implode($glue, $this->items);
114:     }
115: 
116:     /**
117:      * 
118:      * @param false|string $key
119:      * @return int
120:      */
121:     public function max($key = false) {
122:         if($key) {
123:             return $this->extract($key)->max();
124:         }
125:         return max($this->items);
126:     }
127: 
128:     /**
129:      * 
130:      * @param type $offset
131:      * @return type
132:      */
133:     public function offsetExists($offset) {
134:         return $this->has($offset);
135:     }
136: 
137:     /**
138:      * 
139:      * @param type $offset
140:      * @return type
141:      */
142:     public function offsetGet($offset) {
143:         return $this->get($offset);
144:     }
145: 
146:     /**
147:      * 
148:      * @param type $offset
149:      * @param type $value
150:      */
151:     public function offsetSet($offset, $value) {
152:         $this->set($offset, $value);
153:     }
154: 
155:     /**
156:      * 
157:      * @param type $offset
158:      */
159:     public function offsetUnset($offset) {
160:         if($this->has($offset)) {
161:             unset($this->items[$offset]);
162:         }
163:     }
164:     
165:     /**
166:      * 
167:      * @return \ArrayIterator
168:      */
169:     public function getIterator() {
170:         return new \ArrayIterator($this->items);
171:     }
172: 
173: }
174: 
IcFwk API API documentation generated by ApiGen