| 

.NET C# Java Javascript Exception

3
Hallo zusammen,

aktuell arbeite ich an einem Daten Container für mein Framework um die Werte, welche ich bisher in Arrays bearbeitet habe, neu als Objekte nutzen zu können. Dies hat einige Vorteile. Als Beispiel: ein $data->has() Methodenaufruf hilft schnell die Anzahl If (isset(…)) Abfragen zu verringern und so auch den Code lesbarer zu gestalten.

Ich möchte euch nun den unteren Code zur Verfügung stellen. Freue mich aber auch um Verbesserungsvorschläge und Feedback...

<?php
/**
* Data container
*/
class Data implements \Iterator {

protected $container = array();

protected $className;

/**
* Constructor
*
* @param array $values Values
* @throws Exception
*/
public function __construct($values = array()) {

$this->className = get_class($this);

if (!is_array($values)) {
throw new Exception('Values must be an array');
}

foreach ($values as $key => $value) {
$this->set($key, $value);
}
}

/**
* Output the values as a multidimensional array
*
* @return array
*/
public function toArray() {
$data = $this->container;
foreach ($data as $key => $value) {
if ($value instanceof $this->className) {
$data[$key] = $value->toArray();
}
}
return (array) $data;
}

/**
* Check whether the value exists
*
* @param string|integer $name Name/key of value
* @return boolean
*/
public function has($name) {
return isset($this->container[$name]);
}

/**
* Get value
*
* @param string|integer $name Name/key of value
* @return mixed
*/
public function get($name) {
return isset($this->container[$name]) ? $this->container[$name] : null;
}

/**
* Set named value to data
*
* @param string|integer $name Name/key of value
* @param mixed $value Value
* @return DataContainer
* @throws Exception
*/
public function set($name, $value = null) {
if ($name === null) {
throw new Exception('Cannot not set NULL as name of value');
} elseif (!is_string($name) && !is_integer($name)) {
throw new Exception('Value name must be a string or integer');
}

if ($value === null) {
$value = array();
}

if (is_array($value)) {
$value = new $this->className($value);
}

if ($this->has($name) && $value instanceof self && count($value->container) === 0) {
return $this->get($name);
}
$this->container[$name] = $value;
return $this;
}

/**
* Add value to data
*
* @param mixed $value Value
* @return DataContainer
*/
public function add($value) {
if (is_array($value)) {
$value = new $this->className($value);
}
$this->container[] = $value;
return $this;
}


/**
* Search data entry
*
* @param string $name Name of searched value
* @param string $value Search value
* @return DataContainer
*/
public function search($name, $value) {
if (!is_string($name) && !is_integer($name)) {
throw new Exception('Name must be an string or integer');
} elseif (is_object($value)) {
throw new Exception('Search value cannot be an object');
}

$search_result = new DataContainer();
foreach ($this->container as $entry) {

if (!$entry->has($name)) {
throw new Exception($name . ' as name of value does not exists');
}

if (is_string($value)) {
if (strpos($entry->get($name), $value) !== false) {
$search_result->add($entry);
}
} elseif (is_integer($value) || is_bool($value)) {
if ($entry->get($name) === $value) {
$search_result->add($entry);
}
}
}
return $search_result;
}

/**
* Get first data entry
*
* @return mixed
*/
public function first() {
return $this->get(0);
}

/**
* Get last data entry
*
* @return mixed
*/
public function last() {
return $this->count() > 0 ? end($this->container) : null;
}

/**
* Allow deep copies of objects
*/
public function __clone() {
foreach ($this->container as $key => $value) {
if ($value instanceof self) {
$this->container[$key] = clone $value;
}
}
}

/**
* Number of values
*
* @return integer
*/
public function count() {
return count($this->container);
}

/**
* Rewind data
*
* @return mixed
*/
public function rewind() {
return reset($this->container);
}

/**
* Get current value
*
* @return mixed
*/
public function current() {
return current($this->container);
}

/**
* Get name/key of current value
*
* @return string|integer
*/
public function key() {
return key($this->container);
}

/**
* Get next value
*
* @return mixed
*/
public function next() {
return next($this->container);
}

/**
* Check if valid value
*
* @return boolean
*/
public function valid() {
return key($this->container) !== null;
}

}


Beispielnutzung:
$datenArray = array('users' => array(
array('name' => 'Hans'),
array('name' => 'Toni')
));

$daten = new DataContainer($datenArray);

var_dump($daten->get('users'));

$user = $daten->get('users')->get(0);
echo 'Hallo, ich heisse '. $user->get('name');
09.04.2013
rjgamer 31 3
Hab die DateContainer Klasse nochmals überarbeitet...
rjgamer 09.04.2013
1 Antwort
0
Der DataContainer wurde nochmals überarbeitet und funktionell erweitert...
01.05.2013
rjgamer 31 3

Stelle deine Php-Frage jetzt!