1: <?php
2:
3: /**
4: * Copyright 2015, Eduardo Trujillo
5: *
6: * For the full copyright and license information, please view the LICENSE
7: * file that was distributed with this source code.
8: *
9: * This file is part of the Nucleus package
10: */
11:
12: namespace Chromabits\Nucleus\Meditation;
13:
14: use Chromabits\Nucleus\Exceptions\CoreException;
15: use Chromabits\Nucleus\Exceptions\LackOfCoffeeException;
16: use Chromabits\Nucleus\Foundation\BaseObject;
17:
18: /**
19: * Class SpecGraphNodeFactory.
20: *
21: * A fluent factory for describing and building nodes for a SpecGraph.
22: *
23: * @author Eduardo Trujillo <ed@chromabits.com>
24: * @package Chromabits\Nucleus\Meditation
25: */
26: class SpecGraphNodeFactory extends BaseObject
27: {
28: /**
29: * @var string|null
30: */
31: protected $name;
32:
33: /**
34: * @var array<string>
35: */
36: protected $dependencies;
37:
38: /**
39: * @var Spec|null
40: */
41: protected $spec;
42:
43: /**
44: * Construct a new instance of SpecGraphNodeFactory.
45: *
46: * @param string|null $name
47: */
48: public function __construct($name = null)
49: {
50: parent::__construct();
51:
52: $this->name = $name;
53: $this->dependencies = [];
54: $this->spec = null;
55: }
56:
57: /**
58: * Return whether or not the definition is valid.
59: *
60: * @return bool
61: */
62: public function isValid()
63: {
64: return ($this->name !== null && $this->spec !== null);
65: }
66:
67: /**
68: * Add a dependencies or list of dependencies to this node.
69: *
70: * @param string[]|string $dependencies
71: *
72: * @throws LackOfCoffeeException
73: * @return $this
74: */
75: public function dependsOn($dependencies)
76: {
77: if (is_array($dependencies)) {
78: $this->dependencies = array_merge(
79: $dependencies,
80: $this->dependencies
81: );
82: } elseif (is_string($dependencies)) {
83: $this->dependencies[] = $dependencies;
84: } else {
85: throw new LackOfCoffeeException(
86: 'Dependencies can either be an array of strings or a string'
87: );
88: }
89:
90: return $this;
91: }
92:
93: /**
94: * Specify the Spec to use.
95: *
96: * @param Spec $spec
97: *
98: * @return $this
99: */
100: public function withSpec(Spec $spec)
101: {
102: $this->spec = $spec;
103:
104: return $this;
105: }
106:
107: /**
108: * Get the node name.
109: *
110: * @return string
111: * @throws CoreException
112: */
113: public function getName()
114: {
115: if ($this->name === null) {
116: throw new CoreException('Incomplete definition');
117: }
118:
119: return $this->name;
120: }
121:
122: /**
123: * Get the dependencies for this node.
124: *
125: * @return string[]
126: */
127: public function getDependencies()
128: {
129: return $this->dependencies;
130: }
131:
132: /**
133: * Get the spec for this node.
134: *
135: * @return Spec
136: * @throws CoreException
137: */
138: public function getSpec()
139: {
140: if ($this->spec === null) {
141: throw new CoreException('Incomplete definition');
142: }
143:
144: return $this->spec;
145: }
146: }
147: