Skip to content

Commit 1148aa5

Browse files
author
Steein Inc
committed
"count()" - Added a method of calculating elements
1 parent e7dcd91 commit 1148aa5

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

build/index.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77

88

99

10-
$json = new JSON();
10+
$foo = new JSON;
1111

12-
$json->test->one->two = 's';
1312

14-
echo $json->getObject('test')->getObject('one')->getObject('two');
13+
14+
15+
echo '<pre>';
16+
print_r($foo->count());
17+
echo '<pre>';

src/SteeinJSON/JSON.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace SteeinJSON;
33

4+
use Countable;
45
use JsonSchema\Constraints\Factory;
56
use JsonSchema\SchemaStorage;
67
use JsonSchema\Validator;
@@ -23,7 +24,7 @@
2324
* @copyright 2017 - Steein Inc
2425
* @version PHP 7 >=
2526
*/
26-
class JSON extends JSONConfig
27+
class JSON extends JSONConfig implements Countable
2728
{
2829
/***
2930
* The array to store all the data
@@ -365,4 +366,18 @@ protected function formattedJson($json = false)
365366

366367
return $result;
367368
}
369+
370+
/**
371+
* Count elements of an object
372+
*
373+
* @return int
374+
* @throws \ErrorException
375+
*/
376+
public function count()
377+
{
378+
if(!isset($this->storage))
379+
throw new \ErrorException("The store did not match");
380+
else
381+
return \count((array)$this->storage);
382+
}
368383
}

0 commit comments

Comments
 (0)