- <?php
-
- /*
-
- Un petit extrait...
-
- */
-
- require_once('class.entry.php');
-
- class ArrayMap extends xRay implements xMap {
-
- /**
- * Crée une instance d'ArrayMap à partir d'un tableau associatif
- * si aucun tableau n'est transmis, la collection sera créée vide
- *
- * @param array $array
- */
- public function __construct($array = '') {
- $this->newRay($array);
- }
-
- protected function newRay($array = '') {
- parent::__construct();
- $this->putAll($array);
- }
-
- /**
- * Ajoute un couple <Clef,Valeur> à la collection
- *
- * @param object $k
- * @param object $v
- * @return boolean
- */
- public function put($k, $v) {
- // on ne remplace en aucun cas la valeur éxistante
- if (!$this->containsKey($k)) {
- $this->list[$this->size()] = new Entry($k, $v);
- return true;
- }
- return false;
- }
-
- /**
- * Ajoute un tableau associatif d'éléments à la collection
- *
- * @param array $array
- * @return boolean
- */
- public function putAll($array) {
- if (is_array($array) && count($array) > 0)
- foreach ($array as $k => $v)
- $this->put($k, $v);
- return (is_array($array) && count($array) > 0);
- }
-
- /**
- * Récupère un élément de la collection associé à la clef transmise
- *
- * @param object $k
- * @return object
- */
- public function get($k) {
- foreach ($this->list as $entry)
- if ($entry->getKey() == $k)
- return $entry->getValue();
- return null;
- }
-
- private function indexOf($k) {
- foreach ($this->list as $id => $entry)
- if ($entry->getKey() == $k)
- return $id;
- return -1;
- }
-
- /**
- * Supprime définitivement un élément de la collection associé à la clef transmise
- *
- * @param int $k
- * @return boolean
- */
- public function remove($k) {
- $i = $this->indexOf($k);
- if ($i > -1) {
- for ($i; $i<($this->size()-1); $i++)
- $this->list[$i] = $this->list[($i+1)];
- unset($this->list[($this->size()-1)]);
- return true;
- }
- return false;
- }
-
- /**
- * Trie la collection de manière croissante ou décroissante
- *
- * @param int $what
- * @param int $order
- */
- public function sort($what = Key, $order = Asc) {
- list($keys, $values) = $this->toArray();
- $map = array_combine($keys, $values);
- switch ($what) {
- case Key:
- ($order == Asc) ? ksort($map) : krsort($map);
- break;
- case Value:
- ($order == Asc) ? asort($map) : arsort($map);
- break;
- }
- $this->clear();
- $this->putAll($map);
- }
-
- /**
- * Vérifie si la clef existe bien dans la collection
- *
- * @param object $key
- * @return boolean
- */
- public function containsKey($k) {
- if ($this->get($k) != null)
- return true;
- return false;
- }
-
- /**
- * Vérifie si la valeur existe bien dans la collection
- *
- * @param object $value
- * @return boolean
- */
- public function containsValue($v) {
- foreach ($this->list as $entry)
- if ($entry->getValue() == $v)
- return true;
- return false;
- }
-
- /**
- * Retourne la collection sous forme de tableau d'Entry
- *
- * @return array<Entry>
- */
- public function entrySet() {
- return $this->list;
- }
-
- /**
- * Retourne la collection sous forme de tableau indéxé mutlidimensionnel
- * array[ $keys ] => $values
- *
- * @return array
- */
- public function toArray() {
- $keys = array();
- $values = array();
- foreach ($this->list as $i => $entry) {
- $keys[] = $entry->getKey();
- $values[] = $entry->getValue();
- }
- return array_combine($keys, $values);
- }
-
- /**
- * Retourne un iterateur sur la collection
- *
- * @return MapIterator
- */
- public function iterator() {
- return new MapIterator($this);
- }
-
- public function containsAll(Array $objects) {
- return (count(array_intersect($objects, $this->toArray())) == count($objects));
- }
-
- }
-
- ?>
<?php
/*
Un petit extrait...
*/
require_once('class.entry.php');
class ArrayMap extends xRay implements xMap {
/**
* Crée une instance d'ArrayMap à partir d'un tableau associatif
* si aucun tableau n'est transmis, la collection sera créée vide
*
* @param array $array
*/
public function __construct($array = '') {
$this->newRay($array);
}
protected function newRay($array = '') {
parent::__construct();
$this->putAll($array);
}
/**
* Ajoute un couple <Clef,Valeur> à la collection
*
* @param object $k
* @param object $v
* @return boolean
*/
public function put($k, $v) {
// on ne remplace en aucun cas la valeur éxistante
if (!$this->containsKey($k)) {
$this->list[$this->size()] = new Entry($k, $v);
return true;
}
return false;
}
/**
* Ajoute un tableau associatif d'éléments à la collection
*
* @param array $array
* @return boolean
*/
public function putAll($array) {
if (is_array($array) && count($array) > 0)
foreach ($array as $k => $v)
$this->put($k, $v);
return (is_array($array) && count($array) > 0);
}
/**
* Récupère un élément de la collection associé à la clef transmise
*
* @param object $k
* @return object
*/
public function get($k) {
foreach ($this->list as $entry)
if ($entry->getKey() == $k)
return $entry->getValue();
return null;
}
private function indexOf($k) {
foreach ($this->list as $id => $entry)
if ($entry->getKey() == $k)
return $id;
return -1;
}
/**
* Supprime définitivement un élément de la collection associé à la clef transmise
*
* @param int $k
* @return boolean
*/
public function remove($k) {
$i = $this->indexOf($k);
if ($i > -1) {
for ($i; $i<($this->size()-1); $i++)
$this->list[$i] = $this->list[($i+1)];
unset($this->list[($this->size()-1)]);
return true;
}
return false;
}
/**
* Trie la collection de manière croissante ou décroissante
*
* @param int $what
* @param int $order
*/
public function sort($what = Key, $order = Asc) {
list($keys, $values) = $this->toArray();
$map = array_combine($keys, $values);
switch ($what) {
case Key:
($order == Asc) ? ksort($map) : krsort($map);
break;
case Value:
($order == Asc) ? asort($map) : arsort($map);
break;
}
$this->clear();
$this->putAll($map);
}
/**
* Vérifie si la clef existe bien dans la collection
*
* @param object $key
* @return boolean
*/
public function containsKey($k) {
if ($this->get($k) != null)
return true;
return false;
}
/**
* Vérifie si la valeur existe bien dans la collection
*
* @param object $value
* @return boolean
*/
public function containsValue($v) {
foreach ($this->list as $entry)
if ($entry->getValue() == $v)
return true;
return false;
}
/**
* Retourne la collection sous forme de tableau d'Entry
*
* @return array<Entry>
*/
public function entrySet() {
return $this->list;
}
/**
* Retourne la collection sous forme de tableau indéxé mutlidimensionnel
* array[ $keys ] => $values
*
* @return array
*/
public function toArray() {
$keys = array();
$values = array();
foreach ($this->list as $i => $entry) {
$keys[] = $entry->getKey();
$values[] = $entry->getValue();
}
return array_combine($keys, $values);
}
/**
* Retourne un iterateur sur la collection
*
* @return MapIterator
*/
public function iterator() {
return new MapIterator($this);
}
public function containsAll(Array $objects) {
return (count(array_intersect($objects, $this->toArray())) == count($objects));
}
}
?>