@@ 1298-1318 (lines=21) @@ | ||
1295 | * @return Assert |
|
1296 | * @throws AssertionFailedException |
|
1297 | */ |
|
1298 | public function keysExist($keys, $message = null, $propertyPath = null) |
|
1299 | { |
|
1300 | if ( $this->doAllOrNullOr(__FUNCTION__, func_get_args()) ) |
|
1301 | { |
|
1302 | return $this; |
|
1303 | } |
|
1304 | $this->isArray($message, $propertyPath); |
|
1305 | foreach ( $keys as $key ) |
|
1306 | { |
|
1307 | if ( !array_key_exists($key, $this->value) ) |
|
1308 | { |
|
1309 | $message = $message |
|
1310 | ?: sprintf( |
|
1311 | 'Array does not contain an element with key "%s"', |
|
1312 | $this->stringify($key) |
|
1313 | ); |
|
1314 | throw $this->createException($message, self::INVALID_KEYS_EXIST, $propertyPath, ['key' => $key]); |
|
1315 | } |
|
1316 | } |
|
1317 | return $this; |
|
1318 | } |
|
1319 | ||
1320 | /** |
|
1321 | * Assert that property exists in array |
|
@@ 1329-1346 (lines=18) @@ | ||
1326 | * @return Assert |
|
1327 | * @throws AssertionFailedException |
|
1328 | */ |
|
1329 | public function propertyExists($key, $message = null, $propertyPath = null) |
|
1330 | { |
|
1331 | if ( $this->doAllOrNullOr(__FUNCTION__, func_get_args()) ) |
|
1332 | { |
|
1333 | return $this; |
|
1334 | } |
|
1335 | $this->isObject($message, $propertyPath); |
|
1336 | if ( !property_exists($this->value, $key) && !isset( $this->value->{$key} ) ) |
|
1337 | { |
|
1338 | $message = $message |
|
1339 | ?: sprintf( |
|
1340 | 'Object does not contain a property with key "%s"', |
|
1341 | $this->stringify($key) |
|
1342 | ); |
|
1343 | throw $this->createException($message, self::INVALID_PROPERTY_EXISTS, $propertyPath, ['key' => $key]); |
|
1344 | } |
|
1345 | return $this; |
|
1346 | } |
|
1347 | ||
1348 | /** |
|
1349 | * Assert that properties exists in array |
|
@@ 1357-1378 (lines=22) @@ | ||
1354 | * @return Assert |
|
1355 | * @throws AssertionFailedException |
|
1356 | */ |
|
1357 | public function propertiesExist(array $keys, $message = null, $propertyPath = null) |
|
1358 | { |
|
1359 | if ( $this->doAllOrNullOr(__FUNCTION__, func_get_args()) ) |
|
1360 | { |
|
1361 | return $this; |
|
1362 | } |
|
1363 | $this->isObject($message, $propertyPath); |
|
1364 | foreach ( $keys as $key ) |
|
1365 | { |
|
1366 | // Using isset to allow resolution of magically defined properties |
|
1367 | if ( !property_exists($this->value, $key) && !isset( $this->value->{$key} ) ) |
|
1368 | { |
|
1369 | $message = $message |
|
1370 | ?: sprintf( |
|
1371 | 'Object does not contain a property with key "%s"', |
|
1372 | $this->stringify($key) |
|
1373 | ); |
|
1374 | throw $this->createException($message, self::INVALID_PROPERTIES_EXIST, $propertyPath, ['key' => $key]); |
|
1375 | } |
|
1376 | } |
|
1377 | return $this; |
|
1378 | } |
|
1379 | ||
1380 | /** |
|
1381 | * Assert that string is valid utf8 |