Skip to content

Commit 19412d7

Browse files
committed
[DEV-3393] Add MissingDocumentFieldException and change "meta" to
"fields" to avoid confusion.
1 parent 280a643 commit 19412d7

3 files changed

Lines changed: 94 additions & 24 deletions

File tree

src/Document/Document.php

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ class Document
4747
private $_bucket = null;
4848

4949
/**
50-
* Document metadata
50+
* Document fields
5151
*
5252
* @var array
5353
*/
54-
private $_meta = [];
54+
private $_fields = [];
5555

5656

5757
/**
@@ -61,13 +61,13 @@ class Document
6161
*
6262
* @param string $doc_id document identifier
6363
* @param Bucket $bucket document key/value store
64-
* @param array $meta document metadata
64+
* @param array $fields document fields
6565
*/
66-
public function __construct( $doc_id, Bucket $bucket, array $meta = [] )
66+
public function __construct( $doc_id, Bucket $bucket, array $fields = [] )
6767
{
6868
$this->_id = (string)$doc_id;
6969
$this->_bucket = $bucket;
70-
$this->_meta = $meta;
70+
$this->_fields = $fields;
7171
}
7272

7373

@@ -89,7 +89,16 @@ public function getId()
8989
*/
9090
public function getProgramId()
9191
{
92-
return $this->_getMetaByName( "programId" );
92+
// This existed before MissingDocumentFieldException and
93+
// was expected to not throw an exception when it does not exist.
94+
try
95+
{
96+
return (string)$this->_getFieldByName( "programId" );
97+
}
98+
catch ( MissingDocumentFieldException $e )
99+
{
100+
return "";
101+
}
93102
}
94103

95104

@@ -100,7 +109,7 @@ public function getProgramId()
100109
*/
101110
public function getAgentId()
102111
{
103-
return $this->_getMetaByName( "agentId" );
112+
return $this->_getFieldByName( "agentId" );
104113
}
105114

106115

@@ -111,7 +120,7 @@ public function getAgentId()
111120
*/
112121
public function getAgentEntityId()
113122
{
114-
return $this->_getMetaByName( "agentEntityId" );
123+
return $this->_getFieldByName( "agentEntityId" );
115124
}
116125

117126

@@ -122,7 +131,7 @@ public function getAgentEntityId()
122131
*/
123132
public function getInitialRatedDate()
124133
{
125-
return $this->_getMetaByName( "initialRatedDate" );
134+
return $this->_getFieldByName( "initialRatedDate" );
126135
}
127136

128137

@@ -133,7 +142,7 @@ public function getInitialRatedDate()
133142
*/
134143
public function getStartDate()
135144
{
136-
return $this->_getMetaByName( "startDate" );
145+
return $this->_getFieldByName( "startDate" );
137146
}
138147

139148

@@ -144,7 +153,7 @@ public function getStartDate()
144153
*/
145154
public function getAgentName()
146155
{
147-
return $this->_getMetaByName( "agentName" );
156+
return $this->_getFieldByName( "agentName" );
148157
}
149158

150159

@@ -160,15 +169,19 @@ public function getBucket()
160169

161170

162171
/**
163-
* Get metadata by key name
172+
* Get fields by key name
164173
*
165-
* @param String The name of the key holding the data.
166-
*
167-
* @return string metadata or empty string if unknown
174+
* @param String The name of the key holding the data
175+
* @return string field or empty string if unknown
176+
* @throws MissingDocumentFieldException when the field does not exist
168177
*/
169-
private function _getMetaByName( $name )
178+
private function _getFieldByName( $name )
170179
{
171-
$data = &$this->_meta[ $name ];
172-
return (string)$data;
180+
if ( array_key_exists( $name, $this->_fields ) )
181+
{
182+
return $this->_fields[ $name ];
183+
}
184+
185+
throw new MissingDocumentFieldException( "Missing field data" );
173186
}
174187
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Missin document field data exception
4+
*
5+
* Copyright (C) 2016 LoVullo Associates, Inc.
6+
*
7+
* This file is part of libliza-php.
8+
*
9+
* libliza-php is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*/
22+
23+
24+
namespace Lovullo\Liza\Document;
25+
26+
27+
/**
28+
* Invalid or incomplete document field data were provided
29+
*
30+
* N.B: This exception should not provide the bad data, as that would leak
31+
* implementation details (and potentially very sensitive data); this is a
32+
* public-facing exception!
33+
*/
34+
class MissingDocumentFieldException
35+
extends \Exception
36+
{
37+
}

test/Document/DocumentTest.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
use \Lovullo\Liza\Document\Document as Sut;
2626

27-
2827
class DocumentTest
2928
extends \PHPUnit_Framework_TestCase
3029
{
@@ -116,11 +115,6 @@ public function emptyGettersDataProvider()
116115
{
117116
return [
118117
[ "getProgramId" ],
119-
[ "getAgentId" ],
120-
[ "getAgentName" ],
121-
[ "getAgentEntityId" ],
122-
[ "getInitialRatedDate" ],
123-
[ "getStartDate" ],
124118
];
125119
}
126120

@@ -139,4 +133,30 @@ public function testEmptyGetters( $methodName )
139133
)->$methodName()
140134
);
141135
}
136+
137+
138+
public function exceptionGettersDataProvider()
139+
{
140+
return [
141+
[ "getAgentId" ],
142+
[ "getAgentName" ],
143+
[ "getAgentEntityId" ],
144+
[ "getInitialRatedDate" ],
145+
[ "getStartDate" ],
146+
];
147+
}
148+
149+
150+
/**
151+
* @dataProvider exceptionGettersDataProvider
152+
* @expectedException \Lovullo\Liza\Document\MissingDocumentFieldException
153+
*/
154+
public function testExceptionGetters( $methodName )
155+
{
156+
$this->createSut(
157+
1234,
158+
$this->getDummyBucket(),
159+
[]
160+
)->$methodName();
161+
}
142162
}

0 commit comments

Comments
 (0)