Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
199 lars 1
<?php namespace Clockwork\Request;
2
 
3
// Data structure representing custom user data
4
class UserData
5
{
6
	// Data items
7
	protected $data = [];
8
 
9
	// Data title
10
	protected $title;
11
 
12
	// Add generic user data
13
	public function data(array $data, $key = null)
14
	{
15
		if ($key !== null) {
16
			return $this->data[$key] = new UserDataItem($data);
17
		}
18
 
19
		return $this->data[] = new UserDataItem($data);
20
	}
21
 
22
	// Add user data shown as counters
23
	public function counters(array $data)
24
	{
25
		return $this->data($data)
26
			->showAs('counters');
27
	}
28
 
29
	// Add user data shown as table
30
	public function table($title, array $data)
31
	{
32
		return $this->data($data)
33
			->showAs('table')
34
			->title($title);
35
	}
36
 
37
	// Set data title
38
	public function title($title)
39
	{
40
		$this->title = $title;
41
		return $this;
42
	}
43
 
44
	// Transform data and all contents to a serializable array with metadata
45
	public function toArray()
46
	{
47
		return array_merge(
48
			array_map(function ($data) { return $data->toArray(); }, $this->data),
49
			[ '__meta' => array_filter([ 'title' => $this->title ]) ]
50
		);
51
	}
52
}