function getFeedLink(Craur $value) { if ($value->get('@rel', '') === 'feed') { return $value->get('@href'); } throw new Exception('This is no feed link!'); }
/** * Only write the direct descendants into a new row. * * @example * $craur = new Craur( * array( * 'name' => 'My Book', * 'year' => '2012', * 'categories' => array( // Will be ignored * 'comedy', * 'fantasy' * ), * 'authors' => array( // Will be ignored * array('name' => 'Paul'), * array('name' => 'Erwin') * ), * 'pages' => '212' * ) * ); * * $expected_data = array( * 0 => 'My Book', * 1 => '2012', * 4 => '212' * ); * * $result_data = CraurCsvWriter::extractDirectDescendants($craur, array( * 'name', * 'year', * 'categories[]', * 'authors[].name', * 'pages', * ),''); * * assert(json_encode($expected_data) == json_encode($result_data)); * @return array */ static function extractDirectDescendants(Craur $craur, $field_mappings, $prefix = '') { $row = array(); foreach ($field_mappings as $pos => $field_mapping_with_prefix) { if (substr($field_mapping_with_prefix, 0, strlen($prefix)) == $prefix) { /* * Get rid of the prefix */ $field_mapping = substr($field_mapping_with_prefix, strlen($prefix)); if (strpos($field_mapping, '.') === false) { /* * Something like: name, age or categories * * So the $field_mappings[$pos] is something like: book[].name, book[].author[].age or book[].categories[] * which indicates, if we want one or multiple values */ if (substr($field_mapping, -2, 2) !== '[]') { /* * We want one value! */ $row[$pos] = $craur->get($field_mapping, ''); } } } } return $row; }
function youngerThenThreeYears(Craur $value) { if ($value->get('@age') < 3) { return $value; } throw new Exception('Is not younger then three years!'); }
function isACheapBook(Craur $value) { if ($value->get('price') > 20) { throw new Exception('Is no cheap book!'); } return $value; }