1: <?php
2:
3: /**
4: * Deep
5: *
6: * @package rsanchez\Deep
7: * @author Rob Sanchez <info@robsanchez.com>
8: */
9:
10: namespace rsanchez\Deep\Repository;
11:
12: use rsanchez\Deep\Model\AbstractField;
13:
14: /**
15: * Repository of all Fields
16: */
17: abstract class AbstractFieldRepository extends AbstractDeferredRepository
18: {
19: /**
20: * Array of Field keyed by field_name
21: * @var array
22: */
23: protected $fieldsByName = array();
24:
25: /**
26: * Array of Field keyed by field_id
27: * @var array
28: */
29: protected $fieldsById = array();
30:
31: /**
32: * Constructor
33: *
34: * @param \rsanchez\Deep\Model\AbstractField $model
35: */
36: public function __construct(AbstractField $model)
37: {
38: parent::__construct($model);
39: }
40:
41: /**
42: * {@inheritdoc}
43: */
44: protected function boot()
45: {
46: if (is_null($this->collection)) {
47: parent::boot();
48:
49: foreach ($this->collection as $field) {
50: $this->fieldsByName[$field->field_name] = $field;
51: $this->fieldsById[$field->field_id] = $field;
52: }
53: }
54: }
55:
56: /**
57: * Get the collection of Fields
58: *
59: * @return \rsanchez\Deep\Collection\AbstractFieldCollection
60: */
61: public function getFields()
62: {
63: $this->boot();
64:
65: return $this->collection;
66: }
67:
68: /**
69: * Get the field_id for the specified field name
70: *
71: * @param string $field name of the field
72: * @return \rsanchez\Deep\Model\Field
73: */
74: public function getFieldId($field)
75: {
76: $this->boot();
77:
78: return $this->fieldsByName[$field]->field_id;
79: }
80:
81: /**
82: * Get the field_id for the specified field name
83: *
84: * @param int $id id of the field
85: * @return string
86: */
87: public function getFieldName($id)
88: {
89: $this->boot();
90:
91: return $this->fieldsById[$id]->field_name;
92: }
93:
94: /**
95: * Check if this collection has the specified field name
96: *
97: * @param string $field the name of the field
98: * @return boolean
99: */
100: public function hasField($field)
101: {
102: $this->boot();
103:
104: return isset($this->fieldsByName[$field]);
105: }
106:
107: /**
108: * Check if this collection has the specified field id
109: *
110: * @param int $id the id of the field
111: * @return boolean
112: */
113: public function hasFieldId($id)
114: {
115: $this->boot();
116:
117: return isset($this->fieldsById[$id]);
118: }
119:
120: /**
121: * {@inheritdoc}
122: */
123: public function find($id)
124: {
125: $this->boot();
126:
127: return isset($this->fieldsById[$id]) ? $this->fieldsById[$id] : null;
128: }
129: }
130: