1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace DoctrineORMModule\Yuml; |
21
|
|
|
|
22
|
|
|
use Zend\Mvc\Controller\AbstractActionController; |
23
|
|
|
use Zend\Http\Client; |
24
|
|
|
use Zend\Http\Request; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Utility to generate Yuml compatible strings from metadata graphs |
28
|
|
|
* |
29
|
|
|
* @license MIT |
30
|
|
|
* @link http://www.doctrine-project.org/ |
31
|
|
|
* @author Marco Pivetta <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class YumlController extends AbstractActionController |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var Client |
37
|
|
|
*/ |
38
|
|
|
protected $httpClient; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param Client $httpClient |
42
|
|
|
*/ |
43
|
1 |
|
public function __construct(Client $httpClient) |
44
|
|
|
{ |
45
|
1 |
|
$this->httpClient = $httpClient; |
46
|
1 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Redirects the user to a YUML graph drawn with the provided `dsl_text` |
50
|
|
|
* |
51
|
|
|
* @return \Zend\Http\Response |
52
|
|
|
* |
53
|
|
|
* @throws \UnexpectedValueException if the YUML service answered incorrectly |
54
|
|
|
*/ |
55
|
2 |
|
public function indexAction() |
56
|
|
|
{ |
57
|
|
|
/* @var $request \Zend\Http\Request */ |
58
|
2 |
|
$request = $this->getRequest(); |
59
|
2 |
|
$this->httpClient->setMethod(Request::METHOD_POST); |
60
|
2 |
|
$this->httpClient->setParameterPost(array('dsl_text' => $request->getPost('dsl_text'))); |
61
|
2 |
|
$response = $this->httpClient->send(); |
62
|
|
|
|
63
|
2 |
|
if (!$response->isSuccess()) { |
64
|
1 |
|
throw new \UnexpectedValueException('HTTP Request failed'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/* @var $redirect \Zend\Mvc\Controller\Plugin\Redirect */ |
68
|
1 |
|
$redirect = $this->plugin('redirect'); |
69
|
|
|
|
70
|
1 |
|
return $redirect->toUrl('http://yuml.me/' . $response->getBody()); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.