GIF89a; Mini Shell

Mini Shell

Direktori : /home/serb/www/api/src/Collections/
Upload File :
Current File : /home/serb/www/api/src/Collections/JsonCollection.php

<?php

namespace Mar\Collections;

class JsonCollection {

	private $_data = [];
	private $_fields = [];
	private $_fieldsToName = [];

	public function __construct($data = array(), $app = null, $fields = array()) {
		if (!is_object($app)) {
			$app = \Slim\Slim::getInstance();
		}

		$this->_app 	= $app;
		$this->_fields 	= $fields;

		$this->parse($data);
	}

	public function parse($data) {
		foreach($data as $index => $values) {
			$this->_data[] = $this->_parseFields($values);
		}
	}

	protected function _parseFields($values) {
		$data = [];
		foreach($values as $field => $value) {
			$fieldName = $this->_fieldName($field);
			if (!isset($data[$fieldName])) {
				$data[$fieldName] = $value;
			} else {
				if (!is_array($data[$fieldName])) {
					$data[$fieldName] = array($this->_fieldsToName[$fieldName] => $data[$fieldName]);
				}

				$data[$fieldName][$field] = $value;
			}

			$this->_fieldsToName[$fieldName] = $field;
		}
		return $data;
	}

	protected function _fieldName($field) {
		$fieldName = isset($this->_fields[$field]) ? $this->_fields[$field] : 'custom_fields';
		if (is_array($fieldName) && isset($fieldName['field'])) {
			$fieldName = $fieldName['field'];
		}
		return $fieldName;
	}

	public function asArray() {
		return $this->_data;
	}

	public function asJson() {
		return json_encode($this->_data);
	}

}

./BlackJoker Mini Shell 1.0