Skip to content

Commit

Permalink
Greatly simplify Object classes
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed May 11, 2017
1 parent ec4d451 commit 77c2e47
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 901 deletions.
185 changes: 0 additions & 185 deletions system/src/Grav/Framework/Object/AbstractObject.php

This file was deleted.

35 changes: 0 additions & 35 deletions system/src/Grav/Framework/Object/AbstractObjectCollection.php

This file was deleted.

58 changes: 58 additions & 0 deletions system/src/Grav/Framework/Object/Object.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* @package Grav\Framework\Object
*
* @copyright Copyright (C) 2014 - 2017 RocketTheme, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/

namespace Grav\Framework\Object;

use RocketTheme\Toolbox\ArrayTraits\ArrayAccessWithGetters;
use RocketTheme\Toolbox\ArrayTraits\Export;

/**
* Object class.
*
* @package Grav\Framework\Object
*/
class Object implements ObjectInterface
{
use ObjectTrait, ArrayAccessWithGetters, Export;

/**
* Properties of the object.
* @var array
*/
protected $items;

/**
* @var string
*/
private $key;

/**
* @param array $elements
* @param string $key
*/
public function __construct(array $elements = [], $key = null)
{

$this->items = $elements;
$this->key = $key !== null ? $key : $this->getKey();

if ($this->key === null) {
throw new \InvalidArgumentException('Object cannot be created without assigning a key');
}
}

/**
* Implements JsonSerializable interface.
*
* @return array
*/
public function jsonSerialize()
{
return ['key' => $this->getKey(), 'object' => $this->toArray()];
}
}
50 changes: 50 additions & 0 deletions system/src/Grav/Framework/Object/ObjectCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* @package Grav\Framework\Object
*
* @copyright Copyright (C) 2014 - 2017 RocketTheme, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/

namespace Grav\Framework\Object;

use Grav\Framework\Collection\ArrayCollection;

/**
* Object Collection
* @package Grav\Framework\Object
*/
class ObjectCollection extends ArrayCollection implements ObjectCollectionInterface
{
use ObjectCollectionTrait;

/**
* @var string
*/
private $key;

/**
* @param array $elements
* @param string $key
*/
public function __construct(array $elements = [], $key = null)
{
parent::__construct($elements);

$this->key = $key !== null ? $key : $this->getKey();

if ($this->key === null) {
throw new \InvalidArgumentException('Object cannot be created without assigning a key');
}
}

/**
* Implements JsonSerializable interface.
*
* @return array
*/
public function jsonSerialize()
{
return ['key' => $this->getKey(), 'objects' => $this->toArray()];
}
}
15 changes: 14 additions & 1 deletion system/src/Grav/Framework/Object/ObjectCollectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* ObjectCollection Interface
* @package Grav\Framework\Collection
*/
interface ObjectCollectionInterface extends CollectionInterface
interface ObjectCollectionInterface extends CollectionInterface, ObjectInterface
{
/**
* Create a copy from this collection by cloning all objects in the collection.
Expand All @@ -23,6 +23,11 @@ interface ObjectCollectionInterface extends CollectionInterface
*/
public function copy();

/**
* @return array
*/
public function getObjectKeys();

/**
* @param string $property Object property to be fetched.
* @return array Values of the property.
Expand All @@ -42,4 +47,12 @@ public function setProperty($property, $value);
* @return array Return values.
*/
public function call($name, array $arguments);

/**
* Group items in the collection by a field.
*
* @param string $property
* @return array
*/
public function group($property);
}
Loading

0 comments on commit 77c2e47

Please sign in to comment.