1: <?php
2: /**
3: * @filesource Kotchasan/ObjectTool.php
4: *
5: * @copyright 2016 Goragod.com
6: * @license http://www.kotchasan.com/license/
7: *
8: * @see http://www.kotchasan.com/
9: */
10:
11: namespace Kotchasan;
12:
13: /**
14: * Object tools
15: *
16: * @author Goragod Wiriya <admin@goragod.com>
17: *
18: * @since 1.0
19: */
20: class ObjectTool
21: {
22: /**
23: * คืนค่ารายการที่มีคอลัมน์ตามที่กำหนด.
24: *
25: * @assert (array((object)array('id' => 1, 'name' => 'one'), (object)array('id' => 2, 'name' => 'two'), (object)array('id' => 3, 'name' => 'three')), 'name') [==] (object)array(0 => 'one', 1 => 'two', 2 => 'three')
26: * @assert (array((object)array('id' => 1, 'name' => 'one'), (object)array('id' => 2, 'name' => 'two'), (object)array('id' => 3, 'name' => 'three')), 'name', 'id') [==] (object)array(1 => 'one', 2 => 'two', 3 => 'three')
27: *
28: * @param array $array
29: * @param string $column_key ชื่อคอลัมน์ที่ต้องการ
30: * @param mixed $index_key null คืนค่า index ของ $array, string คืนค่า index จากคอลัมน์ที่กำหนด
31: *
32: * @return array
33: */
34: public static function columns($array, $column_key, $index_key = null)
35: {
36: $result = array();
37: if ($index_key == null) {
38: foreach ($array as $i => $item) {
39: if (isset($item->$column_key)) {
40: $result[$i] = $item->$column_key;
41: }
42: }
43: } else {
44: foreach ($array as $i => $item) {
45: if (isset($item->$column_key) && isset($item->$index_key)) {
46: $result[$item->$index_key] = $item->$column_key;
47: }
48: }
49: }
50:
51: return (object) $result;
52: }
53:
54: /**
55: * ฟังก์ชั่นรวม object แทนที่คีย์เดิม
56: *
57: * @assert ((object)array('one' => 1), array('two' => 2)) [==] (object)array('one' => 1, 'two' => 2)
58: * @assert ((object)array('one' => 1), (object)array('two' => 2)) [==] (object)array('one' => 1, 'two' => 2)
59: *
60: * @param object $a
61: * @param array|object $b
62: *
63: * @return object
64: */
65: public static function replace($a, $b)
66: {
67: foreach ($b as $key => $value) {
68: $a->$key = $value;
69: }
70:
71: return $a;
72: }
73:
74: /**
75: * ค้นหา object จาก property
76: * คืนค่าทุกรายการที่พบ รักษา index ตาม array ของ object ต้นฉบับ และ คืนค่าแอเรย์ว่างถ้าไม่พบ.
77: *
78: * @assert (array((object)array('id' => 1, 'name' => 'one'), (object)array('id' => 2, 'name' => 'two'), (object)array('id' => 3, 'name' => 'one')), 'name', 'one') [==] array(0 => (object)array('id' => 1, 'name' => 'one'), 2 => (object)array('id' => 3, 'name' => 'one'))
79: * @assert (array((object)array('id' => 1, 'name' => 'one'), (object)array('id' => 2, 'name' => 'two'), (object)array('id' => 3, 'name' => 'one')), 'id', 'one') [==] array()
80: *
81: * @param array $input ข้อมูลแอเรย์ ของ object
82: * @param mixed $key property ที่ต้องการค้นหา
83: * @param mixed $search ข้อความค้นหา
84: *
85: * @return array
86: */
87: public static function search($input, $key, $search)
88: {
89: $result = array();
90: foreach ($input as $i => $values) {
91: if (isset($values->$key) && $values->$key == $search) {
92: $result[$i] = $values;
93: }
94: }
95:
96: return $result;
97: }
98: }
99: