@@ 1285-1305 (lines=21) @@ | ||
1282 | * @return Assert |
|
1283 | * @throws AssertionFailedException |
|
1284 | */ |
|
1285 | public function keysExist($keys, $message = null, $propertyPath = null) |
|
1286 | { |
|
1287 | if ( $this->doAllOrNullOr(__FUNCTION__, func_get_args()) ) |
|
1288 | { |
|
1289 | return $this; |
|
1290 | } |
|
1291 | $this->isArray($message, $propertyPath); |
|
1292 | foreach ( $keys as $key ) |
|
1293 | { |
|
1294 | if ( !array_key_exists($key, $this->value) ) |
|
1295 | { |
|
1296 | $message = $message |
|
1297 | ?: sprintf( |
|
1298 | 'Array does not contain an element with key "%s"', |
|
1299 | $this->stringify($key) |
|
1300 | ); |
|
1301 | throw $this->createException($message, self::INVALID_KEYS_EXIST, $propertyPath, ['key' => $key]); |
|
1302 | } |
|
1303 | } |
|
1304 | return $this; |
|
1305 | } |
|
1306 | ||
1307 | /** |
|
1308 | * Assert that property exists in array |
|
@@ 1316-1333 (lines=18) @@ | ||
1313 | * @return Assert |
|
1314 | * @throws AssertionFailedException |
|
1315 | */ |
|
1316 | public function propertyExists($key, $message = null, $propertyPath = null) |
|
1317 | { |
|
1318 | if ( $this->doAllOrNullOr(__FUNCTION__, func_get_args()) ) |
|
1319 | { |
|
1320 | return $this; |
|
1321 | } |
|
1322 | $this->isObject($message, $propertyPath); |
|
1323 | if ( !property_exists($this->value, $key) && !isset( $this->value->{$key} ) ) |
|
1324 | { |
|
1325 | $message = $message |
|
1326 | ?: sprintf( |
|
1327 | 'Object does not contain a property with key "%s"', |
|
1328 | $this->stringify($key) |
|
1329 | ); |
|
1330 | throw $this->createException($message, self::INVALID_PROPERTY_EXISTS, $propertyPath, ['key' => $key]); |
|
1331 | } |
|
1332 | return $this; |
|
1333 | } |
|
1334 | ||
1335 | /** |
|
1336 | * Assert that properties exists in array |
|
@@ 1344-1365 (lines=22) @@ | ||
1341 | * @return Assert |
|
1342 | * @throws AssertionFailedException |
|
1343 | */ |
|
1344 | public function propertiesExist(array $keys, $message = null, $propertyPath = null) |
|
1345 | { |
|
1346 | if ( $this->doAllOrNullOr(__FUNCTION__, func_get_args()) ) |
|
1347 | { |
|
1348 | return $this; |
|
1349 | } |
|
1350 | $this->isObject($message, $propertyPath); |
|
1351 | foreach ( $keys as $key ) |
|
1352 | { |
|
1353 | // Using isset to allow resolution of magically defined properties |
|
1354 | if ( !property_exists($this->value, $key) && !isset( $this->value->{$key} ) ) |
|
1355 | { |
|
1356 | $message = $message |
|
1357 | ?: sprintf( |
|
1358 | 'Object does not contain a property with key "%s"', |
|
1359 | $this->stringify($key) |
|
1360 | ); |
|
1361 | throw $this->createException($message, self::INVALID_PROPERTIES_EXIST, $propertyPath, ['key' => $key]); |
|
1362 | } |
|
1363 | } |
|
1364 | return $this; |
|
1365 | } |
|
1366 | ||
1367 | /** |
|
1368 | * Assert that string is valid utf8 |